|
@@ -1,4 +1,7 @@
|
|
|
from typing import Any, Optional
|
|
|
+import requests
|
|
|
+import uuid
|
|
|
+import json
|
|
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, File, UploadFile, Form
|
|
|
from fastapi.encoders import jsonable_encoder
|
|
@@ -10,7 +13,11 @@ from app.api import deps
|
|
|
from uuid import uuid4
|
|
|
|
|
|
router = APIRouter()
|
|
|
-
|
|
|
+baseUrl = "https://nft-api-staging.joyso.io/api/v1/"
|
|
|
+headers = {
|
|
|
+ 'Authorization': 'Basic bmZ0OmMxOTEzOWMzYjM3YjdjZWU3ZmY3OTFiZGU3NzdjZWNl',
|
|
|
+ 'Content-Type': 'application/json'
|
|
|
+}
|
|
|
|
|
|
@router.get("/", response_model=list[schemas.NftPrint])
|
|
|
# @router.get("/")
|
|
@@ -38,17 +45,35 @@ async def create_item(
|
|
|
db: Session = Depends(deps.get_db),
|
|
|
# item_in: schemas.NftCreate,
|
|
|
hash: Optional[str] = Form(None),
|
|
|
- userid: Optional[str] = Form(None),
|
|
|
- title: Optional[str] = Form(None),
|
|
|
+ title: str = Form(...),
|
|
|
context: Optional[str] = Form(None),
|
|
|
is_active: Optional[bool] = Form(True),
|
|
|
catagory: Optional[str] = Form(None),
|
|
|
image: UploadFile = File(...),
|
|
|
+ uid: int = Form(...),
|
|
|
+ address: str = Form(...),
|
|
|
+ amount: int = Form(...),
|
|
|
current_user: models.users = Depends(deps.get_current_active_user),
|
|
|
) -> Any:
|
|
|
"""
|
|
|
Create new item.
|
|
|
"""
|
|
|
+ path = "erc1155"
|
|
|
+ txid = str(uuid.uuid4())
|
|
|
+
|
|
|
+ payload = json.dumps({
|
|
|
+ "txid": txid,
|
|
|
+ "to": address,
|
|
|
+ "uid": uid,
|
|
|
+ "amount": amount
|
|
|
+ })
|
|
|
+ r = requests.post(
|
|
|
+ baseUrl + path,
|
|
|
+ headers=headers,
|
|
|
+ data=payload
|
|
|
+ )
|
|
|
+ if r.status_code != 200:
|
|
|
+ raise HTTPException(status_code=400, detail="not correct mint informaiton")
|
|
|
if image.content_type.split('/')[0] != 'image':
|
|
|
raise HTTPException(status_code=415,
|
|
|
detail='content type error! Please upload valid image type')
|
|
@@ -56,6 +81,7 @@ async def create_item(
|
|
|
with open(settings.IMG_PATH + filename, 'wb+') as f:
|
|
|
f.write(image.file.read())
|
|
|
f.close()
|
|
|
+ userid = crud.user.get_by_address(db, address = address)
|
|
|
item_in = schemas.NftCreate
|
|
|
item_in.hash = hash
|
|
|
item_in.userid = userid
|
|
@@ -64,6 +90,7 @@ async def create_item(
|
|
|
item_in.is_active = is_active
|
|
|
item_in.category = catagory
|
|
|
item_in.imgurl = settings.IMG_HOST + filename
|
|
|
+ item_in.uid = uid
|
|
|
nft = crud.nft.create_with_owner(
|
|
|
db=db, obj_in=item_in, owner_id=item_in.userid)
|
|
|
return nft
|
|
@@ -74,7 +101,7 @@ def update_item(
|
|
|
*,
|
|
|
db: Session = Depends(deps.get_db),
|
|
|
title: str,
|
|
|
- title_new: Optional[str] = Form(None),
|
|
|
+ id: int,
|
|
|
context: Optional[str] = Form(None),
|
|
|
is_active: Optional[bool] = Form(True),
|
|
|
catagory: Optional[str] = Form(None),
|
|
@@ -82,15 +109,15 @@ def update_item(
|
|
|
current_user: models.users = Depends(deps.get_current_active_user),
|
|
|
) -> Any:
|
|
|
"""
|
|
|
- Update bulk items.
|
|
|
+ Update items.
|
|
|
"""
|
|
|
- nft = crud.nft.get_by_title(db=db, title=title)
|
|
|
+ nft = crud.nft.get(db=db, id = id)
|
|
|
if not nft:
|
|
|
raise HTTPException(status_code=404, detail="Item not found")
|
|
|
# if not crud.user.is_superuser(current_user) and (nft.userid != current_user.userid):
|
|
|
if not crud.user.is_superuser(current_user):
|
|
|
raise HTTPException(status_code=400, detail="Not enough permissions")
|
|
|
- item_in = schemas.NftBulkUpdate(**jsonable_encoder(nft[0]))
|
|
|
+ item_in = schemas.NftBulkUpdate(**jsonable_encoder(nft))
|
|
|
if image:
|
|
|
if image.content_type.split('/')[0] != 'image':
|
|
|
raise HTTPException(status_code=415,
|
|
@@ -100,14 +127,15 @@ def update_item(
|
|
|
f.write(image.file.read())
|
|
|
f.close()
|
|
|
item_in.imgurl = settings.IMG_HOST + filename
|
|
|
- if title_new:
|
|
|
- item_in.title = title_new
|
|
|
+ if title:
|
|
|
+ item_in.title = title
|
|
|
if context:
|
|
|
item_in.context = context
|
|
|
if is_active:
|
|
|
item_in.is_active = is_active
|
|
|
if catagory:
|
|
|
item_in.category = catagory
|
|
|
+
|
|
|
|
|
|
nft = crud.nft.update_bulk_title(db=db, db_obj_list=nft, obj_in=item_in)
|
|
|
return nft
|
|
@@ -117,8 +145,12 @@ def update_item(
|
|
|
def update_item(
|
|
|
*,
|
|
|
db: Session = Depends(deps.get_db),
|
|
|
+ title: Optional[str] = Form(None),
|
|
|
id: int,
|
|
|
- item_in: schemas.NftUpdate,
|
|
|
+ context: Optional[str] = Form(None),
|
|
|
+ is_active: Optional[bool] = Form(True),
|
|
|
+ catagory: Optional[str] = Form(None),
|
|
|
+ image: Optional[UploadFile] = File(None),
|
|
|
current_user: models.users = Depends(deps.get_current_active_user),
|
|
|
) -> Any:
|
|
|
"""
|
|
@@ -129,6 +161,24 @@ def update_item(
|
|
|
raise HTTPException(status_code=404, detail="Item not found")
|
|
|
if not crud.user.is_superuser(current_user) and (nft.userid != current_user.userid):
|
|
|
raise HTTPException(status_code=400, detail="Not enough permissions")
|
|
|
+ item_in = schemas.NftUpdate(**jsonable_encoder(nft))
|
|
|
+ if image:
|
|
|
+ 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:
|
|
|
+ f.write(image.file.read())
|
|
|
+ f.close()
|
|
|
+ item_in.imgurl = settings.IMG_HOST + filename
|
|
|
+ if title:
|
|
|
+ item_in.title = title
|
|
|
+ if context:
|
|
|
+ item_in.context = context
|
|
|
+ if is_active:
|
|
|
+ item_in.is_active = is_active
|
|
|
+ if catagory:
|
|
|
+ item_in.category = catagory
|
|
|
nft = crud.nft.update(db=db, db_obj=nft, obj_in=item_in)
|
|
|
return nft
|
|
|
|