Importing RJSF lib with first application
This commit is contained in:
28
api/rpk-api/hub/firm/__init__.py
Normal file
28
api/rpk-api/hub/firm/__init__.py
Normal file
@@ -0,0 +1,28 @@
|
||||
from beanie import PydanticObjectId
|
||||
from pydantic import Field, BaseModel
|
||||
from pymongo import IndexModel
|
||||
|
||||
from hub import CrudDocument
|
||||
|
||||
class Firm(CrudDocument):
|
||||
name: str = Field()
|
||||
instance: str = Field()
|
||||
owner: PydanticObjectId = Field()
|
||||
|
||||
def compute_label(self) -> str:
|
||||
return self.name
|
||||
|
||||
class Settings:
|
||||
indexes = [
|
||||
IndexModel(["name", "instance"], unique=True),
|
||||
]
|
||||
|
||||
class FirmRead(BaseModel):
|
||||
instance: str = Field()
|
||||
name: str = Field()
|
||||
|
||||
class FirmCreate(FirmRead):
|
||||
pass
|
||||
|
||||
class FirmUpdate(FirmRead):
|
||||
pass
|
||||
72
api/rpk-api/hub/firm/routes.py
Normal file
72
api/rpk-api/hub/firm/routes.py
Normal file
@@ -0,0 +1,72 @@
|
||||
from beanie import PydanticObjectId
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
|
||||
from hub.auth import get_current_user
|
||||
from hub.firm import Firm, FirmRead, FirmCreate, FirmUpdate
|
||||
|
||||
model = Firm
|
||||
model_read = FirmRead
|
||||
model_create = FirmCreate
|
||||
model_update = FirmUpdate
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@router.post("/", response_description="{} added to the database".format(model.__name__))
|
||||
async def create(item: model_create, user=Depends(get_current_user)) -> dict:
|
||||
exists = await Firm.find_one({"name": item.name, "instance": item.instance})
|
||||
if exists:
|
||||
raise HTTPException(status_code=400, detail="Firm already exists")
|
||||
|
||||
item.created_by = user.id
|
||||
item.updated_by = user.id
|
||||
item.owner = user.id
|
||||
o = await model(**item.model_dump()).create()
|
||||
return model_read(**o.model_dump())
|
||||
|
||||
@router.get("/{id}", response_description="{} record retrieved".format(model.__name__))
|
||||
async def read_id(id: PydanticObjectId, user=Depends(get_current_user)) -> model_read:
|
||||
item = await model.get(id)
|
||||
return model_read(**item.model_dump())
|
||||
|
||||
|
||||
@router.put("/{id}", response_description="{} record updated".format(model.__name__))
|
||||
async def update(id: PydanticObjectId, req: model_update, user=Depends(get_current_user)) -> model_read:
|
||||
item = await model.get(id)
|
||||
if not item:
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
detail="{} record not found!".format(model.__name__)
|
||||
)
|
||||
|
||||
if item.owner != user.id:
|
||||
raise HTTPException(
|
||||
status_code=403,
|
||||
detail="Insufficient credentials to modify {} record".format(model.__name__)
|
||||
)
|
||||
|
||||
req = {k: v for k, v in req.model_dump().items() if v is not None}
|
||||
update_query = {"$set": {
|
||||
field: value for field, value in req.items()
|
||||
}}
|
||||
|
||||
await item.update(update_query)
|
||||
return model_read(**item.dict())
|
||||
|
||||
@router.delete("/{id}", response_description="{} record deleted from the database".format(model.__name__))
|
||||
async def delete(id: PydanticObjectId, user=Depends(get_current_user)) -> dict:
|
||||
item = await model.get(id)
|
||||
if not item:
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
detail="{} record not found!".format(model.__name__)
|
||||
)
|
||||
if item.owner != user.id:
|
||||
raise HTTPException(
|
||||
status_code=403,
|
||||
detail="Insufficient credentials delete {} record".format(model.__name__)
|
||||
)
|
||||
|
||||
await item.delete()
|
||||
return {
|
||||
"message": "{} deleted successfully".format(model.__name__)
|
||||
}
|
||||
Reference in New Issue
Block a user