52 lines
1.6 KiB
Python
52 lines
1.6 KiB
Python
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from fastapi.responses import JSONResponse
|
|
from sqlalchemy.orm import Session
|
|
from typing import List, Dict, Any
|
|
|
|
from app.core.deps import get_db
|
|
from app.io8ref.Azzu.services import Service
|
|
|
|
# Create two routers or one with route aliases
|
|
router = APIRouter(tags=["Azzu Entity CRUD"])
|
|
|
|
# add data
|
|
@router.post("/Azzu")
|
|
@router.post("/azzu")
|
|
def Savedata(data: Dict[str, Any], db: Session = Depends(get_db)):
|
|
saved = Service.Savedata(db, data)
|
|
return saved
|
|
|
|
# get all
|
|
@router.get("/Azzu")
|
|
@router.get("/azzu")
|
|
def getdetails(db: Session = Depends(get_db)):
|
|
items = Service.getdetails(db)
|
|
return items
|
|
|
|
# getby id
|
|
@router.get("/Azzu/{id}")
|
|
@router.get("/azzu/{id}")
|
|
def getdetailsbyId(id: int, db: Session = Depends(get_db)):
|
|
item = Service.getdetailsbyId(db, id)
|
|
if not item:
|
|
raise HTTPException(status_code=404, detail="Item not found")
|
|
return item
|
|
|
|
# update by id
|
|
@router.put("/Azzu/{id}")
|
|
@router.put("/azzu/{id}")
|
|
def update(data: Dict[str, Any], id: int, db: Session = Depends(get_db)):
|
|
updated = Service.update(db, data, id)
|
|
if not updated:
|
|
raise HTTPException(status_code=404, detail="Item not found")
|
|
return updated
|
|
|
|
# delete by id
|
|
@router.delete("/Azzu/{id}")
|
|
@router.delete("/azzu/{id}")
|
|
def delete_by_id(id: int, db: Session = Depends(get_db)):
|
|
success = Service.delete_by_id(db, id)
|
|
if not success:
|
|
raise HTTPException(status_code=404, detail="Item not found")
|
|
return JSONResponse(status_code=status.HTTP_200_OK, content={"message": "Deleted successfully"})
|