API Reference
User goals module for managing user fitness goals.
This module provides CRUD operations and data models for user goal tracking including activity type goals, intervals, and progress calculation.
Exports
- CRUD: get_user_goals_by_user_id, get_user_goal_by_user_and_goal_id, create_user_goal, update_user_goal, delete_user_goal
- Schemas: UsersGoalBase, UsersGoalCreate, UsersGoalUpdate, UsersGoalRead, UsersGoalProgress
- Enums: Interval, ActivityType, GoalType
- Utils: calculate_user_goals
ActivityType
Bases: StrEnum
Supported activity types for user goals.
Attributes:
| Name | Type | Description |
|---|---|---|
RUN |
Running activities. |
|
BIKE |
Cycling activities. |
|
SWIM |
Swimming activities. |
|
WALK |
Walking or hiking activities. |
|
STRENGTH |
Strength or resistance training. |
|
CARDIO |
Cardiovascular training. |
Source code in backend/app/users/users_goals/schema.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | |
GoalType
Bases: StrEnum
Types of measurable user goals.
Attributes:
| Name | Type | Description |
|---|---|---|
CALORIES |
Target calories burned. |
|
ACTIVITIES |
Target count of completed activities. |
|
DISTANCE |
Target distance traveled. |
|
ELEVATION |
Target elevation gain. |
|
DURATION |
Target total duration. |
Source code in backend/app/users/users_goals/schema.py
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | |
Interval
Bases: StrEnum
Recurrence intervals for user goals.
Attributes:
| Name | Type | Description |
|---|---|---|
DAILY |
Daily recurrence interval. |
|
WEEKLY |
Weekly recurrence interval. |
|
MONTHLY |
Monthly recurrence interval. |
|
YEARLY |
Yearly recurrence interval. |
Source code in backend/app/users/users_goals/schema.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | |
UsersGoalBase
Bases: BaseModel
Base schema for users fitness goals.
Attributes:
| Name | Type | Description |
|---|---|---|
interval |
Interval
|
Goal time interval (daily, weekly, monthly, yearly). |
activity_type |
ActivityType
|
Type of activity for the goal. |
goal_type |
GoalType
|
Type of goal metric being tracked. |
goal_calories |
StrictInt | None
|
Target calories in kcal. |
goal_activities_number |
StrictInt | None
|
Target number of activities. |
goal_distance |
StrictInt | None
|
Target distance in meters. |
goal_elevation |
StrictInt | None
|
Target elevation gain in meters. |
goal_duration |
StrictInt | None
|
Target duration in seconds. |
Source code in backend/app/users/users_goals/schema.py
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | |
ensure_correct_goal_field
ensure_correct_goal_field()
Validate exactly one goal field matches goal_type.
Returns:
| Type | Description |
|---|---|
UsersGoalBase
|
Self with validated goal field. |
Raises:
| Type | Description |
|---|---|
PydanticCustomError
|
If required field missing or extra fields set. |
Source code in backend/app/users/users_goals/schema.py
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | |
UsersGoalCreate
Bases: UsersGoalBase
Schema for creating a new users goal.
Inherits all validation from UsersGoalBase.
Source code in backend/app/users/users_goals/schema.py
156 157 158 159 160 161 | |
UsersGoalProgress
Bases: BaseModel
Schema for user goal progress tracking.
Attributes:
| Name | Type | Description |
|---|---|---|
goal_id |
StrictInt
|
Goal identifier. |
interval |
Interval
|
Goal time interval. |
activity_type |
ActivityType
|
Activity type for this goal. |
goal_type |
GoalType
|
Type of goal metric. |
start_date |
StrictStr
|
Period start date. |
end_date |
StrictStr
|
Period end date. |
percentage_completed |
StrictInt | None
|
Completion percentage. |
total_calories |
StrictInt | None
|
Total calories achieved. |
total_activities_number |
StrictInt | None
|
Total activities completed. |
total_distance |
StrictInt | None
|
Total distance achieved. |
total_elevation |
StrictInt | None
|
Total elevation gained. |
total_duration |
StrictInt | None
|
Total duration achieved. |
goal_calories |
StrictInt | None
|
Target calories. |
goal_activities_number |
StrictInt | None
|
Target activities count. |
goal_distance |
StrictInt | None
|
Target distance. |
goal_elevation |
StrictInt | None
|
Target elevation. |
goal_duration |
StrictInt | None
|
Target duration. |
Source code in backend/app/users/users_goals/schema.py
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 | |
UsersGoalRead
Bases: UsersGoalBase
Schema for reading a user goal.
Attributes:
| Name | Type | Description |
|---|---|---|
id |
StrictInt
|
Unique identifier for the goal. |
user_id |
StrictInt
|
User who owns this goal. |
Source code in backend/app/users/users_goals/schema.py
164 165 166 167 168 169 170 171 172 173 174 | |
UsersGoalUpdate
Bases: UsersGoalRead
Schema for updating an existing users goal.
Supports partial updates. Omitted fields remain unchanged.
Source code in backend/app/users/users_goals/schema.py
177 178 179 180 181 182 | |
calculate_user_goals
calculate_user_goals(user_id, date, db)
Calculate progress for all user goals on a specified date.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
int
|
The ID of the user. |
required |
date
|
str | None
|
Date in YYYY-MM-DD format. If None, uses current date. |
required |
db
|
Session
|
SQLAlchemy database session. |
required |
Returns:
| Type | Description |
|---|---|
list[UsersGoalProgress] | None
|
List of UsersGoalProgress objects, or None if no goals found. |
Raises:
| Type | Description |
|---|---|
HTTPException
|
If database error occurs. |
Source code in backend/app/users/users_goals/utils.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | |
create_user_goal
create_user_goal(user_id, user_goal, db)
Create a new user goal.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
int
|
The ID of the user. |
required |
user_goal
|
UsersGoalCreate
|
Goal data to create. |
required |
db
|
Session
|
SQLAlchemy database session. |
required |
Returns:
| Type | Description |
|---|---|
UsersGoalRead
|
The created UsersGoal schema. |
Raises:
| Type | Description |
|---|---|
HTTPException
|
If goal already exists (409) or database error occurs (500). |
Source code in backend/app/users/users_goals/crud.py
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | |
delete_user_goal
delete_user_goal(user_id, goal_id, db)
Delete a user's goal from the database.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
int
|
ID of the user who owns the goal. |
required |
goal_id
|
int
|
ID of the goal to delete. |
required |
db
|
Session
|
SQLAlchemy database session. |
required |
Raises:
| Type | Description |
|---|---|
HTTPException
|
If goal not found (404) or database error occurs (500). |
Source code in backend/app/users/users_goals/crud.py
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 | |
get_user_goal_by_user_and_goal_id
get_user_goal_by_user_and_goal_id(user_id, goal_id, db)
Retrieve a specific goal by user ID and goal ID.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
int
|
The ID of the user. |
required |
goal_id
|
int
|
The ID of the goal. |
required |
db
|
Session
|
SQLAlchemy database session. |
required |
Returns:
| Type | Description |
|---|---|
UsersGoalRead | None
|
UsersGoal schema if found, None otherwise. |
Raises:
| Type | Description |
|---|---|
HTTPException
|
If database error occurs. |
Source code in backend/app/users/users_goals/crud.py
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | |
get_user_goals_by_user_id
get_user_goals_by_user_id(user_id, db, interval=None, activity_type=None, goal_type=None)
Retrieve goals for a specific user with optional filters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
int
|
The ID of the user. |
required |
db
|
Session
|
SQLAlchemy database session. |
required |
interval
|
Interval | None
|
Optional filter by goal interval. |
None
|
activity_type
|
ActivityType | None
|
Optional filter by activity type. |
None
|
goal_type
|
GoalType | None
|
Optional filter by goal type. |
None
|
Returns:
| Type | Description |
|---|---|
list[UsersGoalRead]
|
List of UsersGoal schemas matching the filters. |
Raises:
| Type | Description |
|---|---|
HTTPException
|
If database error occurs. |
Source code in backend/app/users/users_goals/crud.py
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | |
update_user_goal
update_user_goal(user_id, user_goal, db)
Update a user's goal.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
user_id
|
int
|
ID of the user. |
required |
user_goal
|
UsersGoalUpdate
|
Schema with fields to update. |
required |
db
|
Session
|
SQLAlchemy database session. |
required |
Returns:
| Type | Description |
|---|---|
UsersGoalRead
|
The updated UsersGoal schema. |
Raises:
| Type | Description |
|---|---|
HTTPException
|
If goal not found (404) or database error occurs (500). |
Source code in backend/app/users/users_goals/crud.py
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 | |