149 lines
3.8 KiB
Python
149 lines
3.8 KiB
Python
from typing import Any
|
|
|
|
from fastapi import APIRouter, HTTPException, status
|
|
from sqlmodel import func, select
|
|
|
|
from app.api.deps import CurrentUser, SessionDep
|
|
from app.models.base import (
|
|
ApiTags,
|
|
Message,
|
|
RowId,
|
|
)
|
|
from app.models.place import (
|
|
Place,
|
|
PlaceCreate,
|
|
PlaceUpdate,
|
|
PlacePublic,
|
|
PlacesPublic,
|
|
)
|
|
from app.models.user import (
|
|
PermissionModule,
|
|
PermissionPart,
|
|
PermissionRight,
|
|
)
|
|
|
|
router = APIRouter(prefix="/places", tags=[ApiTags.PLACES])
|
|
|
|
|
|
# region # Places ########################################################
|
|
|
|
|
|
@router.get("/", response_model=PlacesPublic)
|
|
def read_places(
|
|
session: SessionDep, current_user: CurrentUser, skip: int = 0, limit: int = 100
|
|
) -> Any:
|
|
"""
|
|
Retrieve all places.
|
|
"""
|
|
|
|
if current_user.has_permissions(
|
|
module=PermissionModule.PLACE,
|
|
part=PermissionPart.ADMIN,
|
|
rights=PermissionRight.READ,
|
|
):
|
|
count_statement = select(func.count()).select_from(Place)
|
|
count = session.exec(count_statement).one()
|
|
statement = select(Place).offset(skip).limit(limit)
|
|
places = session.exec(statement).all()
|
|
return PlacesPublic(data=places, count=count)
|
|
|
|
return PlacesPublic(data=[], count=0)
|
|
|
|
|
|
@router.get("/{id}", response_model=PlacePublic)
|
|
def read_place(session: SessionDep, current_user: CurrentUser, id: RowId) -> Any:
|
|
"""
|
|
Get place by ID.
|
|
"""
|
|
place = session.get(Place, id)
|
|
if not place:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND, detail="Place not found"
|
|
)
|
|
|
|
if not current_user.has_permissions(
|
|
module=PermissionModule.PLACE,
|
|
part=PermissionPart.ADMIN,
|
|
rights=PermissionRight.READ,
|
|
):
|
|
raise HTTPException(
|
|
status_code=status.HTTP_403_FORBIDDEN, detail="Not enough permissions"
|
|
)
|
|
|
|
return place
|
|
|
|
|
|
@router.post("/", response_model=PlacePublic)
|
|
def create_place(
|
|
*, session: SessionDep, current_user: CurrentUser, place_in: PlaceCreate
|
|
) -> Any:
|
|
"""
|
|
Create new place.
|
|
"""
|
|
|
|
if not current_user.has_permissions(
|
|
module=PermissionModule.PLACE,
|
|
part=PermissionPart.ADMIN,
|
|
rights=PermissionRight.CREATE,
|
|
):
|
|
raise HTTPException(
|
|
status_code=status.HTTP_403_FORBIDDEN, detail="Not enough permissions"
|
|
)
|
|
|
|
place = Place.create(create_obj=place_in, session=session)
|
|
return place
|
|
|
|
|
|
@router.put("/{id}", response_model=PlacePublic)
|
|
def update_place(
|
|
*, session: SessionDep, current_user: CurrentUser, id: RowId, place_in: PlaceUpdate
|
|
) -> Any:
|
|
"""
|
|
Update a place.
|
|
"""
|
|
place = session.get(Place, id)
|
|
if not place:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND, detail="Place not found"
|
|
)
|
|
|
|
if not current_user.has_permissions(
|
|
module=PermissionModule.PLACE,
|
|
part=PermissionPart.ADMIN,
|
|
rights=PermissionRight.UPDATE,
|
|
):
|
|
raise HTTPException(
|
|
status_code=status.HTTP_403_FORBIDDEN, detail="Not enough permissions"
|
|
)
|
|
|
|
place = Place.update(db_obj=place, in_obj=place_in, session=session)
|
|
return place
|
|
|
|
|
|
@router.delete("/{id}")
|
|
def delete_place(session: SessionDep, current_user: CurrentUser, id: RowId) -> Message:
|
|
"""
|
|
Delete a place.
|
|
"""
|
|
place = session.get(Place, id)
|
|
if not place:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND, detail="Place not found"
|
|
)
|
|
|
|
if not current_user.has_permissions(
|
|
module=PermissionModule.PLACE,
|
|
part=PermissionPart.ADMIN,
|
|
rights=PermissionRight.DELETE,
|
|
):
|
|
raise HTTPException(
|
|
status_code=status.HTTP_403_FORBIDDEN, detail="Not enough permissions"
|
|
)
|
|
|
|
session.delete(place)
|
|
session.commit()
|
|
return Message(message="Place deleted successfully")
|
|
|
|
|
|
# endregion
|