backend_and_cms/backend/app/api/routes/sechedule.py

61 lines
1.7 KiB
Python
Raw Normal View History

2024-09-17 12:11:39 +08:00
import uuid
from typing import Any, Annotated, Optional
from app.utils import validate_file_size_type, save_picture, del_picture
from fastapi import APIRouter, HTTPException, UploadFile, File, Form
from sqlmodel import func, select
from app.api.deps import CurrentUser, SessionDep
from app.models import (
ScheduleCreate,
Schedule,
ScheduleUpdate,
Course,
CoursePublicWithImages,
)
router = APIRouter()
@router.post("/", response_model=CoursePublicWithImages)
async def create_schedule(
*, session: SessionDep, current_user: CurrentUser, schedule_in: ScheduleCreate
) -> Any:
schedule = Schedule.model_validate(schedule_in)
session.add(schedule)
session.commit()
session.refresh(schedule)
course = session.get(Course, schedule.course_id)
return course
@router.put("/{id}", response_model=CoursePublicWithImages)
async def edit_schedule(
*,
session: SessionDep,
current_user: CurrentUser,
schedule_in: ScheduleUpdate,
id: uuid.UUID,
) -> Any:
schedule = session.get(Schedule, id)
update_dict = schedule_in.model_dump(exclude_unset=True)
schedule.sqlmodel_update(update_dict)
session.add(schedule)
session.commit()
session.refresh(schedule)
course = session.get(Course, schedule.course_id)
return course
@router.delete("/{id}", response_model=CoursePublicWithImages)
async def delete_schedule(
*, session: SessionDep, current_user: CurrentUser, id: uuid.UUID
) -> Any:
schedule = session.get(Schedule, id)
if not schedule:
raise HTTPException(status_code=404, detail="schedule not found")
session.delete(schedule)
session.commit()
course = session.get(Course, schedule.course_id)
return course