from typing import List from fastapi import APIRouter, HTTPException, status from sqlalchemy import select from src.core.log_conf import logger from src.core.security import CurrentUser from kontor_model.db.models.admin import Profile from src.db.repository.user import create_new_profile from src.db.session import SessionDep from kontor_model.schema.user.profile import ProfileResponse, ProfileModel, to_response router = APIRouter() @router.get("/profile", response_model=ProfileModel) async def read_profile(current_user: CurrentUser): return current_user @router.get("/profiles", response_model=List[ProfileResponse]) def get_all_profiles(db: SessionDep) -> List[ProfileResponse]: results: List[ProfileResponse] = [] profiles = db.scalars(select(Profile)).all() for profile in profiles: response = to_response(profile) results.append(response) return results @router.get("/profiles/{profile_id}", response_model=ProfileResponse) def get_profile(profile_id: str, db: SessionDep) -> ProfileResponse: profile = db.get(Profile, profile_id) if not profile: raise HTTPException(status_code=404, detail="Profile could not be found") response = to_response(profile) return response @router.delete("/profiles/{profile_id}", status_code=status.HTTP_204_NO_CONTENT) def delete_profile(profile_id: str, db: SessionDep): # type: ignore profile = db.get(Profile, profile_id) if not profile: raise HTTPException(status_code=404, detail="Profile could not be found") logger.info(f"delete Profile: {profile_id}") delete_profile(profile_id=profile_id, db=db) @router.post("/profiles", status_code=status.HTTP_201_CREATED) def add_profile(new_profile: ProfileModel, db: SessionDep) -> ProfileResponse: logger.info(f"add profile {new_profile.username}") try: profile: Profile = create_new_profile(new_profile, db) except: raise HTTPException(status_code=409, detail="Profile duplicate") response = to_response(profile) return response