/
2madeira
/
nami
Обзор
Документация
Войти
/
2madeira
/
nami
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
develop
src/api/api.py
67 строк
2 KB
Andrey Zhunev
feat(api): improve API endpoints typing and validation
07 фев 2026, 23:13
07 фев 2026, 23:13
bbd81c6
Код
Авторство
О чём код?
from fastapi import APIRouter from sqlalchemy import desc from sqlalchemy.orm import query from src.services import check_in_habit, create_habit, delete_habit, get_habits, update_habit from src.dependencies import SessionDep from src.schemas import HabitCreateSchema, HabitResponse, CheckInResponse, HabitUpdateSchema router = APIRouter() @router.post("/habits", status_code=201) async def create_habit_endpoint( session: SessionDep, habit: HabitCreateSchema, ) -> HabitResponse: # указать типы здесь, а не в response_model тела декоратора """Create habit""" return await create_habit( session, habit.name, habit.user_id, habit.frequency_type, habit.day_part, habit.description ) @router.get("/habits", status_code=201) async def get_habits_endpoint( session: SessionDep, user_id: int # ID пользователя для запроса списка его привычек ) -> list[HabitResponse]: """Get user habits""" return await get_habits(session, user_id) @router.delete("/habits/{habit_id}", status_code=204) async def delete_habit_endpoint( session: SessionDep, habit_id: int ) -> None: """Delete habit""" return await delete_habit(session, habit_id) @router.patch("/habits/{habit_id}", status_code=201) async def update_habit_endpoint( session: SessionDep, habit_id: int, habit: HabitUpdateSchema ) -> HabitResponse: """Update habit fields name and description if exist""" updates = habit.model_dump(exclude_unset=True) return await update_habit( session, habit_id, updates ) @router.post("/habits/{habit_id}/check_in", status_code=201) async def check_in_endpoint( session: SessionDep, habit_id: int, # передаем id нужной привычки ) -> CheckInResponse: """Check-in habit for complete""" return await check_in_habit(session, habit_id)