refactor: restructure project into layered architecture
This commit is contained in:
64
backend/api/admin.py
Normal file
64
backend/api/admin.py
Normal file
@@ -0,0 +1,64 @@
|
||||
from fastapi import APIRouter
|
||||
from fastapi import Depends
|
||||
from fastapi import HTTPException
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
import schemas
|
||||
from services import crud
|
||||
|
||||
from app.db.session import SessionLocal
|
||||
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
def get_db():
|
||||
|
||||
db = SessionLocal()
|
||||
|
||||
try:
|
||||
yield db
|
||||
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
# CREATE ITEM
|
||||
@router.post(
|
||||
"/items",
|
||||
response_model=schemas.ItemResponse
|
||||
)
|
||||
def create_item(
|
||||
item: schemas.ItemCreate,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
|
||||
return crud.create_item(
|
||||
db,
|
||||
item.title
|
||||
)
|
||||
|
||||
|
||||
# DELETE ITEM
|
||||
@router.delete("/items/{item_id}")
|
||||
def delete_item(
|
||||
item_id: int,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
|
||||
item = crud.delete_item(
|
||||
db,
|
||||
item_id
|
||||
)
|
||||
|
||||
if not item:
|
||||
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
detail="Item not found"
|
||||
)
|
||||
|
||||
return {
|
||||
"message": "Item deleted"
|
||||
}
|
||||
Reference in New Issue
Block a user