add ApiReturnModel ー>return value 統一
This commit is contained in:
@@ -9,6 +9,7 @@ from app.db.schemas import *
|
||||
from typing import List, Optional
|
||||
from app.core.auth import get_current_active_user,get_current_user
|
||||
from app.core.apiexception import APIException
|
||||
from app.core.common import ApiReturnModel
|
||||
|
||||
import httpx
|
||||
import app.core.config as config
|
||||
@@ -255,7 +256,7 @@ async def flow_delete(
|
||||
|
||||
@r.get(
|
||||
"/domains",
|
||||
response_model=List[Domain],
|
||||
response_model=ApiReturnModel[List[Domain]],
|
||||
response_model_exclude_none=True,
|
||||
)
|
||||
async def domain_details(
|
||||
@@ -268,11 +269,11 @@ async def domain_details(
|
||||
domains =get_alldomains(db)
|
||||
else:
|
||||
domains = get_domains(db,user.id)
|
||||
return domains
|
||||
return ApiReturnModel(data = domains)
|
||||
except Exception as 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(
|
||||
request: Request,
|
||||
domain: DomainIn,
|
||||
@@ -280,13 +281,13 @@ async def domain_create(
|
||||
db=Depends(get_db),
|
||||
):
|
||||
try:
|
||||
return create_domain(db, domain,user.id)
|
||||
return ApiReturnModel(data = create_domain(db, domain,user.id))
|
||||
except Exception as e:
|
||||
raise APIException('platform:domain',request.url._url,f"Error occurred while create domain:",e)
|
||||
|
||||
|
||||
@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(
|
||||
request: Request,
|
||||
@@ -295,13 +296,13 @@ async def domain_edit(
|
||||
db=Depends(get_db),
|
||||
):
|
||||
try:
|
||||
return edit_domain(db, domain,user.id)
|
||||
return ApiReturnModel(data = edit_domain(db, domain,user.id))
|
||||
except Exception as e:
|
||||
raise APIException('platform:domain',request.url._url,f"Error occurred while edit domain:",e)
|
||||
|
||||
|
||||
@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(
|
||||
request: Request,
|
||||
@@ -309,7 +310,8 @@ async def domain_delete(
|
||||
db=Depends(get_db),
|
||||
):
|
||||
try:
|
||||
return delete_domain(db,id)
|
||||
if delete_domain(db,id):
|
||||
return ApiReturnModel(data=None)
|
||||
except Exception as 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(
|
||||
"/domain/{userid}",
|
||||
response_model=ApiReturnModel[Domain],
|
||||
response_model_exclude_none=True,
|
||||
)
|
||||
async def create_userdomain(
|
||||
request: Request,
|
||||
userid: int,
|
||||
domainids:List[int] ,
|
||||
domainid:int ,
|
||||
user=Depends(get_current_active_user),
|
||||
db=Depends(get_db),
|
||||
):
|
||||
try:
|
||||
domain = add_userdomain(db, userid,domainids)
|
||||
return domain
|
||||
return ApiReturnModel(data=add_userdomain(db, userid,domainid))
|
||||
except Exception as 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,
|
||||
domainid:int,
|
||||
userid: int,
|
||||
response_model=ApiReturnModel[Domain],
|
||||
db=Depends(get_db),
|
||||
):
|
||||
try:
|
||||
return delete_userdomain(db, userid,domainid)
|
||||
if delete_userdomain(db, userid,domainid):
|
||||
return ApiReturnModel(data=None)
|
||||
except Exception as e:
|
||||
raise APIException('platform:delete',request.url._url,f"Error occurred while delete user({userid}) domain:",e)
|
||||
|
||||
|
||||
@r.get(
|
||||
"/activedomain",
|
||||
response_model=Domain,
|
||||
response_model=ApiReturnModel[Domain],
|
||||
response_model_exclude_none=True,
|
||||
)
|
||||
async def get_useractivedomain(
|
||||
@@ -375,14 +380,15 @@ async def get_useractivedomain(
|
||||
try:
|
||||
# domain = get_activedomain(db, user.id)
|
||||
domain = get_activedomain(db, userId if userId is not None else user.id)
|
||||
if domain is None:
|
||||
return JSONResponse(content=None,status_code=HTTPStatus.OK)
|
||||
return domain
|
||||
# if domain is None:
|
||||
# return JSONResponse(content=None,status_code=HTTPStatus.OK)
|
||||
return ApiReturnModel(data =domain)
|
||||
except Exception as e:
|
||||
raise APIException('platform:activedomain',request.url._url,f"Error occurred while get user({user.id}) activedomain:",e)
|
||||
|
||||
@r.put(
|
||||
"/activedomain/{domainid}",
|
||||
response_model=ApiReturnModel[Domain],
|
||||
response_model_exclude_none=True,
|
||||
)
|
||||
async def update_activeuserdomain(
|
||||
@@ -394,7 +400,7 @@ async def update_activeuserdomain(
|
||||
):
|
||||
try:
|
||||
domain = active_userdomain(db, userId if userId is not None else user.id,domainid)
|
||||
return domain
|
||||
return ApiReturnModel(data= domain)
|
||||
except Exception as 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
|
||||
)
|
||||
db.add(db_domain)
|
||||
db.flush()
|
||||
add_userdomain(db,userid,db_domain.id)
|
||||
#add_userdomain(db,userid,db_domain.id)
|
||||
db.commit()
|
||||
db.refresh(db_domain)
|
||||
return db_domain
|
||||
|
||||
def delete_domain(db: Session,id: int):
|
||||
db_domain = db.query(models.Domain).get(id)
|
||||
if not db_domain:
|
||||
raise HTTPException(status.HTTP_404_NOT_FOUND, detail="Domain not found")
|
||||
db.delete(db_domain)
|
||||
db.commit()
|
||||
return db_domain
|
||||
#if not db_domain:
|
||||
# raise HTTPException(status.HTTP_404_NOT_FOUND, detail="Domain not found")
|
||||
if db_domain:
|
||||
db.delete(db_domain)
|
||||
db.commit()
|
||||
return True
|
||||
|
||||
|
||||
def edit_domain(
|
||||
@@ -339,6 +339,8 @@ def edit_domain(
|
||||
def add_userdomain(db: Session, userid:int,domainid:int):
|
||||
user_domain = models.UserDomain(userid = userid, domainid = domainid )
|
||||
db.add(user_domain)
|
||||
db.commit()
|
||||
db.refresh(user_domain)
|
||||
return user_domain
|
||||
|
||||
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):
|
||||
db_domain = db.query(models.UserDomain).filter(and_(models.UserDomain.userid == userid,models.UserDomain.domainid == domainid)).first()
|
||||
if not db_domain:
|
||||
raise HTTPException(status.HTTP_404_NOT_FOUND, detail="Domain not found")
|
||||
db.delete(db_domain)
|
||||
db.commit()
|
||||
return db_domain
|
||||
#if not db_domain:
|
||||
# raise HTTPException(status.HTTP_404_NOT_FOUND, detail="Domain not found")
|
||||
if db_domain:
|
||||
db.delete(db_domain)
|
||||
db.commit()
|
||||
return True
|
||||
|
||||
def active_userdomain(db: Session, userid: int,domainid: int):
|
||||
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
|
||||
if len(user_domains)==1:
|
||||
db_domain = user_domains[0][0];
|
||||
else:
|
||||
db_domain = next((domain for domain,active in user_domains if active),None)
|
||||
# else:
|
||||
# db_domain = next((domain for domain,active in user_domains if active),None)
|
||||
# raise HTTPException(status.HTTP_404_NOT_FOUND, detail="Domain not found")
|
||||
return db_domain
|
||||
|
||||
|
||||
Reference in New Issue
Block a user