|
@@ -1,10 +1,13 @@
|
|
|
-from typing import Any, List
|
|
|
+from typing import Any, List, Optional
|
|
|
|
|
|
-from fastapi import APIRouter, Depends, HTTPException
|
|
|
+from fastapi import APIRouter, Depends, HTTPException, File, UploadFile, Form
|
|
|
+from pydantic.errors import NotNoneError
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
|
|
from app import crud, models, schemas
|
|
|
+from app.core.config import settings
|
|
|
from app.api import deps
|
|
|
+from uuid import uuid4
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
@@ -30,15 +33,36 @@ def read_items(
|
|
|
|
|
|
|
|
|
@router.post("/", response_model=schemas.NftBase)
|
|
|
-def create_item(
|
|
|
+async def create_item(
|
|
|
*,
|
|
|
db: Session = Depends(deps.get_db),
|
|
|
- item_in: schemas.NftCreate,
|
|
|
+ # item_in: schemas.NftCreate,
|
|
|
+ hash: Optional[str] = Form(None),
|
|
|
+ userid: Optional[str] = Form(None),
|
|
|
+ title: Optional[str] = Form(None),
|
|
|
+ context: Optional[str] = Form(None),
|
|
|
+ is_active: Optional[bool] = Form(True),
|
|
|
+ catagory: Optional[str] = Form(None),
|
|
|
+ image: UploadFile = File(...),
|
|
|
current_user: models.users = Depends(deps.get_current_active_user),
|
|
|
) -> Any:
|
|
|
"""
|
|
|
Create new item.
|
|
|
"""
|
|
|
+ if image.content_type.split('/')[0] != 'image':
|
|
|
+ raise HTTPException(status_code=415, detail='content type error! Please upload valid image type')
|
|
|
+ filename = str(uuid4()) + '.' + image.content_type.split('/')[1]
|
|
|
+ with open(settings.IMG_PATH + filename, 'wb+') as f:
|
|
|
+ await f.write(image.file.read())
|
|
|
+ f.close()
|
|
|
+ item_in = schemas.NftCreate
|
|
|
+ item_in.hash = hash
|
|
|
+ item_in.userid = userid
|
|
|
+ item_in.title = title
|
|
|
+ item_in.context = context
|
|
|
+ item_in.is_active = is_active
|
|
|
+ item_in.category = catagory
|
|
|
+ item_in.imgurl = settings.IMG_HOST + filename
|
|
|
nft = crud.nft.create_with_owner(
|
|
|
db=db, obj_in=item_in, owner_id=current_user.userid)
|
|
|
return nft
|