-
Notifications
You must be signed in to change notification settings - Fork 0
[Feat] 훈련 달력 백엔드 구현 (Plans API) #90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1a647c5
feat: add SavedPlan model for training calendar (#89)
zweadfx c667b69
feat: add plan schemas for training calendar (#89)
zweadfx 5c8b36c
feat: add plans CRUD endpoints for training calendar (#89)
zweadfx b2ada00
feat: register plans router (#89)
zweadfx 744cb62
fix: add year/month query validation to plans endpoint (#89)
zweadfx 1c2adf6
fix: validate day_number bounds in complete endpoint (#89)
zweadfx fd66a1e
fix: add title and total_days validation to SavePlanRequest (#89)
zweadfx c805b92
fix: add day_number validation to CompleteDayRequest (#89)
zweadfx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| """Training plan (calendar) endpoints — member only.""" | ||
|
|
||
| from fastapi import APIRouter, Depends, HTTPException, Query | ||
| from sqlalchemy.orm import Session | ||
|
|
||
| from src.api.v1.endpoints.auth import get_current_user | ||
| from src.db.database import get_db | ||
| from src.db.models import SavedPlan, User | ||
| from src.models.plan_schema import CompleteDayRequest, SavedPlanResponse, SavePlanRequest | ||
| from src.models.response_schema import SuccessResponse | ||
|
|
||
| router = APIRouter() | ||
|
|
||
|
|
||
| @router.post("/", response_model=SuccessResponse[SavedPlanResponse]) | ||
| def save_plan( | ||
| req: SavePlanRequest, | ||
| current_user: User = Depends(get_current_user), | ||
| db: Session = Depends(get_db), | ||
| ) -> SuccessResponse[SavedPlanResponse]: | ||
| plan = SavedPlan( | ||
| user_id=current_user.id, | ||
| plan_type=req.plan_type, | ||
| title=req.title, | ||
| data=req.data, | ||
| start_date=req.start_date, | ||
| total_days=req.total_days, | ||
| completed_days=[], | ||
| ) | ||
| db.add(plan) | ||
| db.commit() | ||
| db.refresh(plan) | ||
| return SuccessResponse(data=SavedPlanResponse.model_validate(plan)) | ||
|
|
||
|
|
||
| @router.get("/", response_model=SuccessResponse[list[SavedPlanResponse]]) | ||
| def get_plans( | ||
| year: int = Query(..., ge=1, le=9999), | ||
| month: int = Query(..., ge=1, le=12), | ||
| current_user: User = Depends(get_current_user), | ||
| db: Session = Depends(get_db), | ||
| ) -> SuccessResponse[list[SavedPlanResponse]]: | ||
| from calendar import monthrange | ||
| from datetime import date | ||
|
|
||
| first_day = date(year, month, 1) | ||
| last_day = date(year, month, monthrange(year, month)[1]) | ||
|
|
||
| plans = ( | ||
| db.query(SavedPlan) | ||
| .filter( | ||
| SavedPlan.user_id == current_user.id, | ||
| SavedPlan.start_date <= last_day, | ||
| ) | ||
| .order_by(SavedPlan.start_date) | ||
| .all() | ||
| ) | ||
|
|
||
| # Filter: plans whose date range overlaps with the requested month | ||
| result = [] | ||
| for plan in plans: | ||
| from datetime import timedelta | ||
|
|
||
| plan_end = plan.start_date + timedelta(days=plan.total_days - 1) | ||
| if plan_end >= first_day: | ||
| result.append(SavedPlanResponse.model_validate(plan)) | ||
|
|
||
| return SuccessResponse(data=result) | ||
|
|
||
|
|
||
| @router.patch("/{plan_id}/complete", response_model=SuccessResponse[SavedPlanResponse]) | ||
| def complete_plan_day( | ||
| plan_id: int, | ||
| req: CompleteDayRequest, | ||
| current_user: User = Depends(get_current_user), | ||
| db: Session = Depends(get_db), | ||
| ) -> SuccessResponse[SavedPlanResponse]: | ||
| plan = db.query(SavedPlan).filter( | ||
| SavedPlan.id == plan_id, | ||
| SavedPlan.user_id == current_user.id, | ||
| ).first() | ||
| if not plan: | ||
| raise HTTPException(status_code=404, detail="Plan not found") | ||
|
|
||
| if req.day_number < 1 or req.day_number > plan.total_days: | ||
| raise HTTPException(status_code=422, detail="day_number out of range") | ||
|
|
||
| completed: list[int] = list(plan.completed_days or []) | ||
| if req.completed: | ||
| if req.day_number not in completed: | ||
| completed.append(req.day_number) | ||
| else: | ||
| completed = [d for d in completed if d != req.day_number] | ||
|
|
||
| plan.completed_days = completed | ||
| db.commit() | ||
| db.refresh(plan) | ||
| return SuccessResponse(data=SavedPlanResponse.model_validate(plan)) | ||
|
zweadfx marked this conversation as resolved.
|
||
|
|
||
|
|
||
| @router.delete("/{plan_id}", response_model=SuccessResponse[None]) | ||
| def delete_plan( | ||
| plan_id: int, | ||
| current_user: User = Depends(get_current_user), | ||
| db: Session = Depends(get_db), | ||
| ) -> SuccessResponse[None]: | ||
| plan = db.query(SavedPlan).filter( | ||
| SavedPlan.id == plan_id, | ||
| SavedPlan.user_id == current_user.id, | ||
| ).first() | ||
| if not plan: | ||
| raise HTTPException(status_code=404, detail="Plan not found") | ||
|
|
||
| db.delete(plan) | ||
| db.commit() | ||
| return SuccessResponse(message="Plan deleted successfully.") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,11 @@ | ||
| from fastapi import APIRouter | ||
|
|
||
| from src.api.v1.endpoints import auth, gear, skill, whistle | ||
| from src.api.v1.endpoints import auth, gear, plans, skill, whistle | ||
|
|
||
| api_router = APIRouter() | ||
|
|
||
| api_router.include_router(auth.router, prefix="/auth", tags=["Auth"]) | ||
| api_router.include_router(skill.router, prefix="/skill", tags=["Skill Lab"]) | ||
| api_router.include_router(gear.router, prefix="/gear", tags=["Gear Advisor"]) | ||
| api_router.include_router(whistle.router, prefix="/whistle", tags=["The Whistle"]) | ||
| api_router.include_router(plans.router, prefix="/plans", tags=["Plans"]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| """Pydantic schemas for training plan (calendar) endpoints.""" | ||
|
|
||
| from datetime import date, datetime | ||
| from typing import Any, Literal | ||
|
|
||
| from pydantic import BaseModel, Field | ||
|
|
||
|
|
||
| class SavePlanRequest(BaseModel): | ||
| plan_type: Literal["weekly", "skill"] | ||
| title: str = Field(..., min_length=1, max_length=200) | ||
| data: dict[str, Any] | ||
| start_date: date | ||
| total_days: int = Field(..., ge=1) | ||
|
|
||
|
|
||
| class SavedPlanResponse(BaseModel): | ||
| id: int | ||
| plan_type: str | ||
| title: str | ||
| data: dict[str, Any] | ||
| start_date: date | ||
| total_days: int | ||
| completed_days: list[int] | ||
| created_at: datetime | ||
|
|
||
| model_config = {"from_attributes": True} | ||
|
|
||
|
|
||
| class CompleteDayRequest(BaseModel): | ||
| day_number: int = Field(..., ge=1) | ||
| completed: bool | ||
|
zweadfx marked this conversation as resolved.
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.