This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
from typing import List
|
||||
from fastapi import APIRouter, HTTPException, status
|
||||
from sqlalchemy import select
|
||||
from src.core.log_conf import logger
|
||||
|
||||
from src.db.models.admin import Profile
|
||||
from src.db.repository.user import create_new_profile, get_profile_details
|
||||
from src.db.session import SessionDep
|
||||
from src.schema.user.profile import ProfileResponse, ProfileModel
|
||||
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
@router.get("/profiles", response_model=List[ProfileResponse])
|
||||
def get_all_profiles(db: SessionDep) -> List[ProfileResponse]: # type: ignore
|
||||
results: List[ProfileResponse] = []
|
||||
profiles = db.scalars(select(Profile)).all()
|
||||
for profile in profiles:
|
||||
response = get_profile_details(profile)
|
||||
results.append(response)
|
||||
return results
|
||||
|
||||
@router.get("/profiles/{profile_id}", response_model=ProfileResponse)
|
||||
def get_profile(profile_id: str, db: SessionDep) -> ProfileResponse: # type: ignore
|
||||
profile = db.get(Profile, profile_id)
|
||||
if not profile:
|
||||
raise HTTPException(status_code=404, detail="MediaActor could not be found")
|
||||
response = get_profile_details(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: # type: ignore
|
||||
logger.info(f"add profile {new_profile.user_name}")
|
||||
try:
|
||||
profile: Profile = create_new_profile(new_profile, db)
|
||||
except:
|
||||
raise HTTPException(status_code=409, detail="Profile duplicate")
|
||||
response = get_profile_details(profile)
|
||||
return response
|
||||
Reference in New Issue
Block a user