add ApiReturnModel ー>return value 統一
This commit is contained in:
@@ -9,6 +9,7 @@ from app.db.schemas import *
|
|||||||
from typing import List, Optional
|
from typing import List, Optional
|
||||||
from app.core.auth import get_current_active_user,get_current_user
|
from app.core.auth import get_current_active_user,get_current_user
|
||||||
from app.core.apiexception import APIException
|
from app.core.apiexception import APIException
|
||||||
|
from app.core.common import ApiReturnModel
|
||||||
|
|
||||||
import httpx
|
import httpx
|
||||||
import app.core.config as config
|
import app.core.config as config
|
||||||
@@ -255,7 +256,7 @@ async def flow_delete(
|
|||||||
|
|
||||||
@r.get(
|
@r.get(
|
||||||
"/domains",
|
"/domains",
|
||||||
response_model=List[Domain],
|
response_model=ApiReturnModel[List[Domain]],
|
||||||
response_model_exclude_none=True,
|
response_model_exclude_none=True,
|
||||||
)
|
)
|
||||||
async def domain_details(
|
async def domain_details(
|
||||||
@@ -268,11 +269,11 @@ async def domain_details(
|
|||||||
domains =get_alldomains(db)
|
domains =get_alldomains(db)
|
||||||
else:
|
else:
|
||||||
domains = get_domains(db,user.id)
|
domains = get_domains(db,user.id)
|
||||||
return domains
|
return ApiReturnModel(data = domains)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise APIException('platform:domains',request.url._url,f"Error occurred while get domains:",e)
|
raise APIException('platform:domains',request.url._url,f"Error occurred while get domains:",e)
|
||||||
|
|
||||||
@r.post("/domain", response_model=Domain, response_model_exclude_none=True)
|
@r.post("/domain", response_model=ApiReturnModel[Domain], response_model_exclude_none=True)
|
||||||
async def domain_create(
|
async def domain_create(
|
||||||
request: Request,
|
request: Request,
|
||||||
domain: DomainIn,
|
domain: DomainIn,
|
||||||
@@ -280,13 +281,13 @@ async def domain_create(
|
|||||||
db=Depends(get_db),
|
db=Depends(get_db),
|
||||||
):
|
):
|
||||||
try:
|
try:
|
||||||
return create_domain(db, domain,user.id)
|
return ApiReturnModel(data = create_domain(db, domain,user.id))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise APIException('platform:domain',request.url._url,f"Error occurred while create domain:",e)
|
raise APIException('platform:domain',request.url._url,f"Error occurred while create domain:",e)
|
||||||
|
|
||||||
|
|
||||||
@r.put(
|
@r.put(
|
||||||
"/domain", response_model=Domain, response_model_exclude_none=True
|
"/domain", response_model=ApiReturnModel[Domain], response_model_exclude_none=True
|
||||||
)
|
)
|
||||||
async def domain_edit(
|
async def domain_edit(
|
||||||
request: Request,
|
request: Request,
|
||||||
@@ -295,13 +296,13 @@ async def domain_edit(
|
|||||||
db=Depends(get_db),
|
db=Depends(get_db),
|
||||||
):
|
):
|
||||||
try:
|
try:
|
||||||
return edit_domain(db, domain,user.id)
|
return ApiReturnModel(data = edit_domain(db, domain,user.id))
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise APIException('platform:domain',request.url._url,f"Error occurred while edit domain:",e)
|
raise APIException('platform:domain',request.url._url,f"Error occurred while edit domain:",e)
|
||||||
|
|
||||||
|
|
||||||
@r.delete(
|
@r.delete(
|
||||||
"/domain/{id}", response_model=Domain, response_model_exclude_none=True
|
"/domain/{id}", response_model=ApiReturnModel[Domain], response_model_exclude_none=True
|
||||||
)
|
)
|
||||||
async def domain_delete(
|
async def domain_delete(
|
||||||
request: Request,
|
request: Request,
|
||||||
@@ -309,7 +310,8 @@ async def domain_delete(
|
|||||||
db=Depends(get_db),
|
db=Depends(get_db),
|
||||||
):
|
):
|
||||||
try:
|
try:
|
||||||
return delete_domain(db,id)
|
if delete_domain(db,id):
|
||||||
|
return ApiReturnModel(data=None)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise APIException('platform:domain',request.url._url,f"Error occurred while delete domain:",e)
|
raise APIException('platform:domain',request.url._url,f"Error occurred while delete domain:",e)
|
||||||
|
|
||||||
@@ -332,17 +334,18 @@ async def userdomain_details(
|
|||||||
|
|
||||||
@r.post(
|
@r.post(
|
||||||
"/domain/{userid}",
|
"/domain/{userid}",
|
||||||
|
response_model=ApiReturnModel[Domain],
|
||||||
response_model_exclude_none=True,
|
response_model_exclude_none=True,
|
||||||
)
|
)
|
||||||
async def create_userdomain(
|
async def create_userdomain(
|
||||||
request: Request,
|
request: Request,
|
||||||
userid: int,
|
userid: int,
|
||||||
domainids:List[int] ,
|
domainid:int ,
|
||||||
|
user=Depends(get_current_active_user),
|
||||||
db=Depends(get_db),
|
db=Depends(get_db),
|
||||||
):
|
):
|
||||||
try:
|
try:
|
||||||
domain = add_userdomain(db, userid,domainids)
|
return ApiReturnModel(data=add_userdomain(db, userid,domainid))
|
||||||
return domain
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise APIException('platform:domain',request.url._url,f"Error occurred while add user({userid}) domain:",e)
|
raise APIException('platform:domain',request.url._url,f"Error occurred while add user({userid}) domain:",e)
|
||||||
|
|
||||||
@@ -353,17 +356,19 @@ async def userdomain_delete(
|
|||||||
request: Request,
|
request: Request,
|
||||||
domainid:int,
|
domainid:int,
|
||||||
userid: int,
|
userid: int,
|
||||||
|
response_model=ApiReturnModel[Domain],
|
||||||
db=Depends(get_db),
|
db=Depends(get_db),
|
||||||
):
|
):
|
||||||
try:
|
try:
|
||||||
return delete_userdomain(db, userid,domainid)
|
if delete_userdomain(db, userid,domainid):
|
||||||
|
return ApiReturnModel(data=None)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise APIException('platform:delete',request.url._url,f"Error occurred while delete user({userid}) domain:",e)
|
raise APIException('platform:delete',request.url._url,f"Error occurred while delete user({userid}) domain:",e)
|
||||||
|
|
||||||
|
|
||||||
@r.get(
|
@r.get(
|
||||||
"/activedomain",
|
"/activedomain",
|
||||||
response_model=Domain,
|
response_model=ApiReturnModel[Domain],
|
||||||
response_model_exclude_none=True,
|
response_model_exclude_none=True,
|
||||||
)
|
)
|
||||||
async def get_useractivedomain(
|
async def get_useractivedomain(
|
||||||
@@ -375,14 +380,15 @@ async def get_useractivedomain(
|
|||||||
try:
|
try:
|
||||||
# domain = get_activedomain(db, user.id)
|
# domain = get_activedomain(db, user.id)
|
||||||
domain = get_activedomain(db, userId if userId is not None else user.id)
|
domain = get_activedomain(db, userId if userId is not None else user.id)
|
||||||
if domain is None:
|
# if domain is None:
|
||||||
return JSONResponse(content=None,status_code=HTTPStatus.OK)
|
# return JSONResponse(content=None,status_code=HTTPStatus.OK)
|
||||||
return domain
|
return ApiReturnModel(data =domain)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise APIException('platform:activedomain',request.url._url,f"Error occurred while get user({user.id}) activedomain:",e)
|
raise APIException('platform:activedomain',request.url._url,f"Error occurred while get user({user.id}) activedomain:",e)
|
||||||
|
|
||||||
@r.put(
|
@r.put(
|
||||||
"/activedomain/{domainid}",
|
"/activedomain/{domainid}",
|
||||||
|
response_model=ApiReturnModel[Domain],
|
||||||
response_model_exclude_none=True,
|
response_model_exclude_none=True,
|
||||||
)
|
)
|
||||||
async def update_activeuserdomain(
|
async def update_activeuserdomain(
|
||||||
@@ -394,7 +400,7 @@ async def update_activeuserdomain(
|
|||||||
):
|
):
|
||||||
try:
|
try:
|
||||||
domain = active_userdomain(db, userId if userId is not None else user.id,domainid)
|
domain = active_userdomain(db, userId if userId is not None else user.id,domainid)
|
||||||
return domain
|
return ApiReturnModel(data= domain)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise APIException('platform:activedomain',request.url._url,f"Error occurred while update user({user.id}) activedomain:",e)
|
raise APIException('platform:activedomain',request.url._url,f"Error occurred while update user({user.id}) activedomain:",e)
|
||||||
|
|
||||||
|
|||||||
12
backend/app/core/common.py
Normal file
12
backend/app/core/common.py
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
|
||||||
|
from pydantic import BaseModel,Field
|
||||||
|
from pydantic.generics import GenericModel
|
||||||
|
from typing import Generic,TypeVar,Optional
|
||||||
|
|
||||||
|
T = TypeVar('T')
|
||||||
|
|
||||||
|
class ApiReturnModel(GenericModel,Generic[T]):
|
||||||
|
code:int = 0
|
||||||
|
msg:str ="OK"
|
||||||
|
data:T
|
||||||
|
|
||||||
@@ -293,19 +293,19 @@ def create_domain(db: Session, domain: schemas.DomainIn,userid:int):
|
|||||||
ownerid = domain.ownerid
|
ownerid = domain.ownerid
|
||||||
)
|
)
|
||||||
db.add(db_domain)
|
db.add(db_domain)
|
||||||
db.flush()
|
#add_userdomain(db,userid,db_domain.id)
|
||||||
add_userdomain(db,userid,db_domain.id)
|
|
||||||
db.commit()
|
db.commit()
|
||||||
db.refresh(db_domain)
|
db.refresh(db_domain)
|
||||||
return db_domain
|
return db_domain
|
||||||
|
|
||||||
def delete_domain(db: Session,id: int):
|
def delete_domain(db: Session,id: int):
|
||||||
db_domain = db.query(models.Domain).get(id)
|
db_domain = db.query(models.Domain).get(id)
|
||||||
if not db_domain:
|
#if not db_domain:
|
||||||
raise HTTPException(status.HTTP_404_NOT_FOUND, detail="Domain not found")
|
# raise HTTPException(status.HTTP_404_NOT_FOUND, detail="Domain not found")
|
||||||
db.delete(db_domain)
|
if db_domain:
|
||||||
db.commit()
|
db.delete(db_domain)
|
||||||
return db_domain
|
db.commit()
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
def edit_domain(
|
def edit_domain(
|
||||||
@@ -339,6 +339,8 @@ def edit_domain(
|
|||||||
def add_userdomain(db: Session, userid:int,domainid:int):
|
def add_userdomain(db: Session, userid:int,domainid:int):
|
||||||
user_domain = models.UserDomain(userid = userid, domainid = domainid )
|
user_domain = models.UserDomain(userid = userid, domainid = domainid )
|
||||||
db.add(user_domain)
|
db.add(user_domain)
|
||||||
|
db.commit()
|
||||||
|
db.refresh(user_domain)
|
||||||
return user_domain
|
return user_domain
|
||||||
|
|
||||||
def add_userdomains(db: Session, userid:int,domainids:list[str]):
|
def add_userdomains(db: Session, userid:int,domainids:list[str]):
|
||||||
@@ -349,11 +351,12 @@ def add_userdomains(db: Session, userid:int,domainids:list[str]):
|
|||||||
|
|
||||||
def delete_userdomain(db: Session, userid: int,domainid: int):
|
def delete_userdomain(db: Session, userid: int,domainid: int):
|
||||||
db_domain = db.query(models.UserDomain).filter(and_(models.UserDomain.userid == userid,models.UserDomain.domainid == domainid)).first()
|
db_domain = db.query(models.UserDomain).filter(and_(models.UserDomain.userid == userid,models.UserDomain.domainid == domainid)).first()
|
||||||
if not db_domain:
|
#if not db_domain:
|
||||||
raise HTTPException(status.HTTP_404_NOT_FOUND, detail="Domain not found")
|
# raise HTTPException(status.HTTP_404_NOT_FOUND, detail="Domain not found")
|
||||||
db.delete(db_domain)
|
if db_domain:
|
||||||
db.commit()
|
db.delete(db_domain)
|
||||||
return db_domain
|
db.commit()
|
||||||
|
return True
|
||||||
|
|
||||||
def active_userdomain(db: Session, userid: int,domainid: int):
|
def active_userdomain(db: Session, userid: int,domainid: int):
|
||||||
db_userdomains = db.query(models.UserDomain).filter(models.UserDomain.userid == userid).all()
|
db_userdomains = db.query(models.UserDomain).filter(models.UserDomain.userid == userid).all()
|
||||||
@@ -376,8 +379,8 @@ def get_activedomain(db: Session, userid: int)-> t.Optional[models.Domain]:
|
|||||||
db_domain=None
|
db_domain=None
|
||||||
if len(user_domains)==1:
|
if len(user_domains)==1:
|
||||||
db_domain = user_domains[0][0];
|
db_domain = user_domains[0][0];
|
||||||
else:
|
# else:
|
||||||
db_domain = next((domain for domain,active in user_domains if active),None)
|
# db_domain = next((domain for domain,active in user_domains if active),None)
|
||||||
# raise HTTPException(status.HTTP_404_NOT_FOUND, detail="Domain not found")
|
# raise HTTPException(status.HTTP_404_NOT_FOUND, detail="Domain not found")
|
||||||
return db_domain
|
return db_domain
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user