|
| 1 | +from app.schemas.base import ResponseBase |
| 2 | +from app.schemas.memo import MemoCreate, MemoResponse, MemoUpdate |
| 3 | +from app.services.memo import ( |
| 4 | + select_today_memos_service, |
| 5 | + select_month_memos_service, |
| 6 | + select_month_memos_by_category_id_service, |
| 7 | + add_memo_service, |
| 8 | + edit_memo_service, |
| 9 | + delete_memo_service, |
| 10 | +) |
| 11 | +from app.db.session import get_db |
| 12 | +from fastapi import APIRouter, Depends, HTTPException, status |
| 13 | +from sqlalchemy.ext.asyncio import AsyncSession |
| 14 | +from typing import List |
| 15 | +from uuid import UUID |
| 16 | + |
| 17 | +router = APIRouter() |
| 18 | + |
| 19 | + |
| 20 | +@router.get("/", response_model=ResponseBase[List[MemoResponse]]) |
| 21 | +async def get_today_memos( |
| 22 | + user_id: UUID, db: AsyncSession = Depends(get_db) |
| 23 | +) -> ResponseBase[List[MemoResponse]]: |
| 24 | + try: |
| 25 | + memos = await select_today_memos_service(db, user_id) |
| 26 | + return ResponseBase(status_code=status.HTTP_200_OK, data=memos) |
| 27 | + except HTTPException as e: |
| 28 | + return ResponseBase(status_code=e.status_code, error=e.detail) |
| 29 | + except Exception as e: |
| 30 | + return ResponseBase( |
| 31 | + status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, error=str(e) |
| 32 | + ) |
| 33 | + |
| 34 | + |
| 35 | +@router.get("/", response_model=ResponseBase[List[MemoResponse]]) |
| 36 | +async def get_month_memos( |
| 37 | + user_id: UUID, year: int, month: int, db: AsyncSession = Depends(get_db) |
| 38 | +) -> ResponseBase[List[MemoResponse]]: |
| 39 | + try: |
| 40 | + memos = await select_month_memos_service(db, user_id, year, month) |
| 41 | + return ResponseBase(status_code=status.HTTP_200_OK, data=memos) |
| 42 | + except HTTPException as e: |
| 43 | + return ResponseBase(status_code=e.status_code, error=e.detail) |
| 44 | + except Exception as e: |
| 45 | + return ResponseBase( |
| 46 | + status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, error=str(e) |
| 47 | + ) |
| 48 | + |
| 49 | + |
| 50 | +@router.get("/", response_model=ResponseBase[List[MemoResponse]]) |
| 51 | +async def get_month_memos_by_category( |
| 52 | + user_id: UUID, |
| 53 | + category_id: UUID, |
| 54 | + year: int, |
| 55 | + month: int, |
| 56 | + db: AsyncSession = Depends(get_db), |
| 57 | +) -> ResponseBase[List[MemoResponse]]: |
| 58 | + try: |
| 59 | + memos = await select_month_memos_by_category_id_service( |
| 60 | + db, user_id, category_id, year, month |
| 61 | + ) |
| 62 | + return ResponseBase(status_code=status.HTTP_200_OK, data=memos) |
| 63 | + except HTTPException as e: |
| 64 | + return ResponseBase(status_code=e.status_code, error=e.detail) |
| 65 | + except Exception as e: |
| 66 | + return ResponseBase( |
| 67 | + status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, error=str(e) |
| 68 | + ) |
| 69 | + |
| 70 | + |
| 71 | +@router.post("/", response_model=ResponseBase[MemoResponse]) |
| 72 | +async def post_memo( |
| 73 | + memo: MemoCreate, |
| 74 | + user_id: UUID, |
| 75 | + category_id: UUID, |
| 76 | + db: AsyncSession = Depends(get_db), |
| 77 | +) -> ResponseBase[MemoResponse]: |
| 78 | + try: |
| 79 | + memo = await add_memo_service(db, user_id, category_id, memo) |
| 80 | + return ResponseBase(status_code=status.HTTP_201_CREATED, data=memo) |
| 81 | + except HTTPException as e: |
| 82 | + return ResponseBase(status_code=e.status_code, error=e.detail) |
| 83 | + except Exception as e: |
| 84 | + return ResponseBase( |
| 85 | + status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, error=str(e) |
| 86 | + ) |
| 87 | + |
| 88 | + |
| 89 | +@router.put("/{memo_id}", response_model=ResponseBase[MemoResponse]) |
| 90 | +async def update_memo( |
| 91 | + user_id: UUID, |
| 92 | + memo_id: UUID, |
| 93 | + memo: MemoUpdate, |
| 94 | + db: AsyncSession = Depends(get_db), |
| 95 | +) -> ResponseBase[MemoResponse]: |
| 96 | + try: |
| 97 | + memo = await edit_memo_service(db, user_id, memo_id, memo) |
| 98 | + return ResponseBase(status_code=status.HTTP_200_OK, data=memo) |
| 99 | + except HTTPException as e: |
| 100 | + return ResponseBase(status_code=e.status_code, error=e.detail) |
| 101 | + except Exception as e: |
| 102 | + return ResponseBase( |
| 103 | + status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, error=str(e) |
| 104 | + ) |
| 105 | + |
| 106 | + |
| 107 | +@router.delete("/{memo_id}", response_model=ResponseBase[bool]) |
| 108 | +async def delete_memo( |
| 109 | + user_id: UUID, |
| 110 | + memo_id: UUID, |
| 111 | + db: AsyncSession = Depends(get_db), |
| 112 | +) -> ResponseBase[bool]: |
| 113 | + try: |
| 114 | + result = await delete_memo_service(db, user_id, memo_id) |
| 115 | + return ResponseBase(status_code=status.HTTP_200_OK, data=result) |
| 116 | + except HTTPException as e: |
| 117 | + return ResponseBase(status_code=e.status_code, error=e.detail) |
| 118 | + except Exception as e: |
| 119 | + return ResponseBase( |
| 120 | + status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, error=str(e) |
| 121 | + ) |
0 commit comments