Skip to content

Commit 358dcbf

Browse files
docs(projects): Documentation of code
Adding documentation of code for projects routes.
1 parent c3530fb commit 358dcbf

File tree

4 files changed

+146
-7
lines changed

4 files changed

+146
-7
lines changed

src/models/project_model.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,28 @@
1-
from sqlalchemy import Column, Integer, String, Text, ForeignKey, TIMESTAMP, func
21
from sqlalchemy.orm import relationship
32
from src.modules.database_conection import Base
3+
from sqlalchemy import Column, Integer, String, Text, ForeignKey, TIMESTAMP, func
4+
45

56
class Project(Base):
7+
"""
8+
Represents a project entity in the to-do list application.
9+
10+
Attributes:
11+
id (int): Primary key identifier for the project.
12+
user_id (int): Foreign key referencing the user who owns the project.
13+
title (str): Title of the project.
14+
description (str): Detailed description of the project.
15+
created_at (datetime): Timestamp when the project was created.
16+
updated_at (datetime): Timestamp when the project was last updated.
17+
user (User): Relationship to the User model, representing the owner of the project.
18+
"""
19+
620
__tablename__ = "projects"
721

822
id = Column(Integer, primary_key=True, index=True)
9-
user_id = Column(Integer, ForeignKey("users.id", ondelete="CASCADE"), nullable=False)
23+
user_id = Column(
24+
Integer, ForeignKey("users.id", ondelete="CASCADE"), nullable=False
25+
)
1026
title = Column(String(255), nullable=False)
1127
description = Column(Text)
1228
created_at = Column(TIMESTAMP, server_default=func.now())

src/models/user_model.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@ class User(Base):
2727
created_at = Column(TIMESTAMP, server_default=func.now(), nullable=False)
2828
updated_at = Column(TIMESTAMP, server_default=func.now(), onupdate=func.now(), nullable=False)
2929

30+
# Relationship to projects
3031
projects = relationship("Project", back_populates="user", cascade="all, delete-orphan")

src/routes/project_routes.py

Lines changed: 105 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
from fastapi import APIRouter, Depends, HTTPException, status
2-
from sqlalchemy.orm import Session
31
from typing import List
42

5-
from src.utils.security import get_current_user
3+
from sqlalchemy.orm import Session
4+
from fastapi import APIRouter, Depends, HTTPException, status
5+
6+
from src.utils.utils import get_db
67
from src.models.user_model import User
78
from src.models.project_model import Project
8-
from src.utils.utils import get_db
9+
from src.utils.security import get_current_user
910
from src.schemas.projects.projects_schema import ProjectCreate, ProjectResponse
1011

1112
project_router = APIRouter(prefix="/projects", tags=["projects"])
@@ -19,6 +20,27 @@ def create_project(
1920
db: Session = Depends(get_db),
2021
current_user: User = Depends(get_current_user),
2122
):
23+
"""
24+
Create a new project for the authenticated user.
25+
26+
This endpoint allows the current authenticated user to create a new project
27+
by providing the required project data. The project will be associated with
28+
the user who is currently authenticated.
29+
30+
Args:
31+
project (ProjectCreate): The project data to be created, including title and description.
32+
db (Session): SQLAlchemy database session dependency.
33+
current_user (User): The currently authenticated user, injected by dependency.
34+
35+
Returns:
36+
ProjectResponse: The newly created project data.
37+
38+
Raises:
39+
HTTPException: If the user is not authenticated or if there is a database error.
40+
41+
Status Codes:
42+
201 CREATED: Project was successfully created.
43+
"""
2244
new_project = Project(
2345
user_id=current_user.id, title=project.title, description=project.description
2446
)
@@ -32,6 +54,25 @@ def create_project(
3254
def list_projects(
3355
db: Session = Depends(get_db), current_user: User = Depends(get_current_user)
3456
):
57+
"""
58+
List all projects for the authenticated user.
59+
60+
This endpoint allows the current authenticated user to retrieve a list of all
61+
projects associated with their account.
62+
63+
Args:
64+
db (Session): SQLAlchemy database session dependency.
65+
current_user (User): The currently authenticated user, injected by dependency.
66+
67+
Returns:
68+
List[ProjectResponse]: A list of projects associated with the authenticated user.
69+
70+
Raises:
71+
HTTPException: If the user is not authenticated.
72+
73+
Status Codes:
74+
200 OK: Projects were successfully retrieved.
75+
"""
3576
return db.query(Project).filter(Project.user_id == current_user.id).all()
3677

3778

@@ -41,6 +82,27 @@ def get_project(
4182
db: Session = Depends(get_db),
4283
current_user: User = Depends(get_current_user),
4384
):
85+
"""
86+
Get a specific project for the authenticated user.
87+
88+
This endpoint allows the current authenticated user to retrieve a specific
89+
project associated with their account by providing the project ID.
90+
91+
Args:
92+
project_id (int): The ID of the project to retrieve.
93+
db (Session): SQLAlchemy database session dependency.
94+
current_user (User): The currently authenticated user, injected by dependency.
95+
96+
Returns:
97+
ProjectResponse: The project data for the specified project ID.
98+
99+
Raises:
100+
HTTPException: If the user is not authenticated or if the project is not found.
101+
102+
Status Codes:
103+
200 OK: Project was successfully retrieved.
104+
404 NOT FOUND: Project was not found.
105+
"""
44106
project = (
45107
db.query(Project)
46108
.filter(Project.id == project_id, Project.user_id == current_user.id)
@@ -58,6 +120,29 @@ def update_project(
58120
db: Session = Depends(get_db),
59121
current_user: User = Depends(get_current_user),
60122
):
123+
"""
124+
Update a specific project for the authenticated user.
125+
126+
This endpoint allows the current authenticated user to update a specific
127+
project associated with their account by providing the project ID and the
128+
updated project data.
129+
130+
Args:
131+
project_id (int): The ID of the project to update.
132+
updated (ProjectCreate): The updated project data, including title and description.
133+
db (Session): SQLAlchemy database session dependency.
134+
current_user (User): The currently authenticated user, injected by dependency.
135+
136+
Returns:
137+
ProjectResponse: The updated project data.
138+
139+
Raises:
140+
HTTPException: If the user is not authenticated or if the project is not found.
141+
142+
Status Codes:
143+
200 OK: Project was successfully updated.
144+
404 NOT FOUND: Project was not found.
145+
"""
61146
project = (
62147
db.query(Project)
63148
.filter(Project.id == project_id, Project.user_id == current_user.id)
@@ -78,6 +163,22 @@ def delete_project(
78163
db: Session = Depends(get_db),
79164
current_user: User = Depends(get_current_user),
80165
):
166+
"""
167+
Delete a specific project for the authenticated user.
168+
This endpoint allows the current authenticated user to delete a specific
169+
project associated with their account by providing the project ID.
170+
171+
Args:
172+
project_id (int): The ID of the project to delete.
173+
db (Session): SQLAlchemy database session dependency.
174+
current_user (User): The currently authenticated user, injected by dependency.
175+
176+
Raises:
177+
HTTPException: If the user is not authenticated or if the project is not found.
178+
179+
Status Codes:
180+
204 NO CONTENT: Project was successfully deleted.
181+
"""
81182
project = (
82183
db.query(Project)
83184
.filter(Project.id == project_id, Project.user_id == current_user.id)

src/schemas/projects/projects_schema.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,33 @@
1-
from pydantic import BaseModel
21
from typing import Optional
32
from datetime import datetime
3+
from pydantic import BaseModel
44

55
class ProjectCreate(BaseModel):
6+
"""
7+
ProjectCreate schema for creating new projects.
8+
9+
Attributes:
10+
title (str): Title of the project.
11+
description (Optional[str]): Description of the project.
12+
"""
613
title: str
714
description: Optional[str] = None
815

916
class ProjectResponse(ProjectCreate):
17+
"""
18+
ProjectResponse schema for API responses.
19+
20+
Inherits from ProjectCreate and adds additional fields for the project ID,
21+
creation timestamp, and last update timestamp.
22+
23+
Attributes:
24+
id (int): Unique identifier for the project.
25+
created_at (datetime): Timestamp when the project was created.
26+
updated_at (datetime): Timestamp when the project was last updated.
27+
28+
Config:
29+
from_attributes (bool): Enables population of the model from ORM objects.
30+
"""
1031
id: int
1132
created_at: datetime
1233
updated_at: datetime

0 commit comments

Comments
 (0)