77 lines
2.7 KiB
Python
77 lines
2.7 KiB
Python
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.core.database import get_db
|
|
from app.crud import manufacturer as manufacturer_crud
|
|
from app.deps import require_permission
|
|
from app.models.user import User
|
|
from app.schemas.manufacturer import ManufacturerCreate, ManufacturerRead, ManufacturerUpdate
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("", response_model=list[ManufacturerRead])
|
|
async def list_manufacturers(
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
db: AsyncSession = Depends(get_db),
|
|
_: User = Depends(require_permission("manufacturers:read")),
|
|
):
|
|
return await manufacturer_crud.list_all(db, skip=skip, limit=limit)
|
|
|
|
|
|
@router.post("", response_model=ManufacturerRead, status_code=status.HTTP_201_CREATED)
|
|
async def create_manufacturer(
|
|
payload: ManufacturerCreate,
|
|
db: AsyncSession = Depends(get_db),
|
|
_: User = Depends(require_permission("manufacturers:write")),
|
|
):
|
|
return await manufacturer_crud.create(
|
|
db,
|
|
name=payload.name,
|
|
url=str(payload.url),
|
|
logo_url=str(payload.logo_url) if payload.logo_url else None,
|
|
)
|
|
|
|
|
|
@router.get("/{manufacturer_id}", response_model=ManufacturerRead)
|
|
async def get_manufacturer(
|
|
manufacturer_id: int,
|
|
db: AsyncSession = Depends(get_db),
|
|
_: User = Depends(require_permission("manufacturers:read")),
|
|
):
|
|
m = await manufacturer_crud.get(db, manufacturer_id)
|
|
if m is None:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Manufacturer not found")
|
|
return m
|
|
|
|
|
|
@router.patch("/{manufacturer_id}", response_model=ManufacturerRead)
|
|
async def update_manufacturer(
|
|
manufacturer_id: int,
|
|
payload: ManufacturerUpdate,
|
|
db: AsyncSession = Depends(get_db),
|
|
_: User = Depends(require_permission("manufacturers:write")),
|
|
):
|
|
m = await manufacturer_crud.get(db, manufacturer_id)
|
|
if m is None:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Manufacturer not found")
|
|
data = payload.model_dump(exclude_unset=True)
|
|
if "url" in data and data["url"] is not None:
|
|
data["url"] = str(data["url"])
|
|
if "logo_url" in data and data["logo_url"] is not None:
|
|
data["logo_url"] = str(data["logo_url"])
|
|
return await manufacturer_crud.update(db, m, data)
|
|
|
|
|
|
@router.delete("/{manufacturer_id}", status_code=status.HTTP_204_NO_CONTENT)
|
|
async def delete_manufacturer(
|
|
manufacturer_id: int,
|
|
db: AsyncSession = Depends(get_db),
|
|
_: User = Depends(require_permission("manufacturers:delete")),
|
|
):
|
|
m = await manufacturer_crud.get(db, manufacturer_id)
|
|
if m is None:
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Manufacturer not found")
|
|
await manufacturer_crud.delete(db, m)
|