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
- Models: UsersGoal (ORM model)
- Enums: Interval, ActivityType, GoalType
- Utils: calculate_user_goals
ActivityType
Bases: str, Enum
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: str, Enum
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: str, Enum
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 | |
UserGoalModel
Bases: Base
User fitness goal tracking model.
Attributes:
| Name | Type | Description |
|---|---|---|
id |
Mapped[int]
|
Primary key. |
user_id |
Mapped[int]
|
Foreign key to users table. |
interval |
Mapped[str]
|
Goal time interval (daily, weekly, monthly, yearly). |
activity_type |
Mapped[str]
|
Type of activity for the goal. |
goal_type |
Mapped[str]
|
Type of goal metric. |
goal_calories |
Mapped[int | None]
|
Target calories in kcal. |
goal_activities_number |
Mapped[int | None]
|
Target number of activities. |
goal_distance |
Mapped[int | None]
|
Target distance in meters. |
goal_elevation |
Mapped[int | None]
|
Target elevation gain in meters. |
goal_duration |
Mapped[int | None]
|
Target duration in seconds. |
user |
Mapped[int | None]
|
Relationship to Users model. |
Source code in backend/app/users/users_goals/models.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 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 | |
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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | |
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
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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | |
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
170 171 172 173 174 175 | |
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
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 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 | |
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
178 179 180 181 182 183 184 185 186 187 188 | |
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
191 192 193 194 195 196 | |
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
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 78 79 80 | |
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 |
|---|---|
UsersGoal
|
The created UsersGoal model. |
Raises:
| Type | Description |
|---|---|
HTTPException
|
If goal already exists (409) or database error occurs (500). |
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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | |
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
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 | |
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 |
|---|---|
UsersGoal | None
|
UsersGoal model if found, None otherwise. |
Raises:
| Type | Description |
|---|---|
HTTPException
|
If database error occurs. |
Source code in backend/app/users/users_goals/crud.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | |
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[UsersGoal]
|
List of UsersGoal models matching the filters. |
Raises:
| Type | Description |
|---|---|
HTTPException
|
If database error occurs. |
Source code in backend/app/users/users_goals/crud.py
13 14 15 16 17 18 19 20 21 22 23 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 | |
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 |
|---|---|
UsersGoal
|
The updated UsersGoal model. |
Raises:
| Type | Description |
|---|---|
HTTPException
|
If goal not found (404) or database error occurs (500). |
Source code in backend/app/users/users_goals/crud.py
137 138 139 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 | |