Skip to content

Commit 38c482d

Browse files
feat(stick-wall-routes): Routes of stick wall
Routes dedicated to deal with stick wall created. All CRUD operations.
1 parent 420a9aa commit 38c482d

File tree

6 files changed

+249
-3
lines changed

6 files changed

+249
-3
lines changed

src/models/stickwall_model.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from sqlalchemy.orm import relationship
2+
from sqlalchemy import Column, Integer, String, Text, ForeignKey, TIMESTAMP, func
3+
4+
from src.modules.database_conection import Base
5+
6+
7+
class StickWall(Base):
8+
"""
9+
Represents a StickWall note entity in the to-do list application.
10+
11+
Attributes:
12+
id (int): Primary key identifier for the note.
13+
user_id (int): Foreign key referencing the user who owns the note.
14+
name (str): Title of the note.
15+
text (str): Content of the note.
16+
color (str): Hex color associated with the note (e.g. "#FFF176").
17+
created_at (datetime): Timestamp when the note was created.
18+
updated_at (datetime): Timestamp when the note was last updated.
19+
user (User): Relationship to the User model, representing the owner.
20+
"""
21+
22+
__tablename__ = "stick_wall"
23+
24+
id = Column(Integer, primary_key=True, index=True)
25+
user_id = Column(
26+
Integer, ForeignKey("users.id", ondelete="CASCADE"), nullable=False
27+
)
28+
name = Column(String(255), nullable=False)
29+
text = Column(Text, nullable=False)
30+
color = Column(String(7), nullable=False, server_default="#FFFF88")
31+
created_at = Column(TIMESTAMP, server_default=func.now())
32+
updated_at = Column(TIMESTAMP, server_default=func.now(), onupdate=func.now())
33+
34+
user = relationship("User", back_populates="stick_notes")

src/models/user_model.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from src.modules.database_conection import Base
44
from sqlalchemy import Column, Integer, String, Date, TIMESTAMP
55

6+
67
class User(Base):
78
"""
89
Represents a user in the system.
@@ -16,6 +17,7 @@ class User(Base):
1617
created_at (datetime): Timestamp when the user was created.
1718
updated_at (datetime): Timestamp when the user was last updated.
1819
"""
20+
1921
__tablename__ = "users"
2022

2123
id = Column(Integer, primary_key=True, index=True)
@@ -25,9 +27,15 @@ class User(Base):
2527
birth_date = Column(Date, nullable=True)
2628
password_hash = Column(String(255), nullable=False)
2729
created_at = Column(TIMESTAMP, server_default=func.now(), nullable=False)
28-
updated_at = Column(TIMESTAMP, server_default=func.now(), onupdate=func.now(), nullable=False)
30+
updated_at = Column(
31+
TIMESTAMP, server_default=func.now(), onupdate=func.now(), nullable=False
32+
)
2933

3034
# Relationship to projects
31-
projects = relationship("Project", back_populates="user", cascade="all, delete-orphan")
32-
35+
projects = relationship(
36+
"Project", back_populates="user", cascade="all, delete-orphan"
37+
)
3338
tasks = relationship("Task", back_populates="user", cascade="all, delete-orphan")
39+
stick_notes = relationship(
40+
"StickWall", back_populates="user", cascade="all, delete-orphan"
41+
)

src/modules/modules_api.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from src.routes.user_routes import user_router
44
from src.routes.task_routes import task_router
55
from src.routes.project_routes import project_router
6+
from src.routes.stickwall_routes import stickwall_router
67
from src.routes.filter_routes import router as filter_router
78
from src.routes.authentication_routes import authentication_router
89

@@ -13,4 +14,5 @@
1314
router.include_router(user_router)
1415
router.include_router(project_router)
1516
router.include_router(task_router)
17+
router.include_router(stickwall_router)
1618
router.include_router(filter_router)

src/routes/stickwall_routes.py

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
from typing import List, Optional
2+
3+
from sqlalchemy.orm import Session
4+
from fastapi import APIRouter, Depends, HTTPException, status
5+
6+
from src.utils.utils import get_db
7+
from src.utils.security import get_current_user
8+
from src.models.user_model import User
9+
from src.models.stickwall_model import StickWall
10+
from src.schemas.stickwall.stickwall_schema import (
11+
StickWallCreate,
12+
StickWallResponse,
13+
)
14+
15+
stickwall_router = APIRouter(prefix="/stickwall", tags=["stickwall"])
16+
17+
18+
@stickwall_router.get("/", response_model=List[StickWallResponse])
19+
def list_notes(
20+
color: Optional[str] = None,
21+
db: Session = Depends(get_db),
22+
current_user: User = Depends(get_current_user),
23+
):
24+
"""
25+
List all StickWall notes for the authenticated user.
26+
27+
Args:
28+
color (str, optional): Hex color to filter notes.
29+
30+
Returns:
31+
List[StickWallResponse]: List of StickWall notes.
32+
33+
Optional:
34+
color (str): Hex color to filter notes.
35+
"""
36+
query = db.query(StickWall).filter(StickWall.user_id == current_user.id)
37+
if color:
38+
query = query.filter(StickWall.color == color)
39+
return query.all()
40+
41+
42+
@stickwall_router.post(
43+
"/", response_model=StickWallResponse, status_code=status.HTTP_201_CREATED
44+
)
45+
def create_note(
46+
note: StickWallCreate,
47+
db: Session = Depends(get_db),
48+
current_user: User = Depends(get_current_user),
49+
):
50+
"""
51+
Create a new StickWall note for the authenticated user.
52+
53+
Args:
54+
note (StickWallCreate): Data for the new note (name, text, color).
55+
db (Session): Database session.
56+
current_user (User): Authenticated user.
57+
58+
Returns:
59+
StickWallResponse: The created note.
60+
"""
61+
new_note = StickWall(
62+
user_id=current_user.id,
63+
name=note.name,
64+
text=note.text,
65+
color=note.color,
66+
)
67+
db.add(new_note)
68+
db.commit()
69+
db.refresh(new_note)
70+
return new_note
71+
72+
73+
@stickwall_router.get("/{note_id}", response_model=StickWallResponse)
74+
def get_note(
75+
note_id: int,
76+
db: Session = Depends(get_db),
77+
current_user: User = Depends(get_current_user),
78+
):
79+
"""
80+
Get a specific StickWall note by ID for the authenticated user.
81+
82+
Args:
83+
note_id (int): Note identifier.
84+
85+
Returns:
86+
StickWallResponse: The requested note.
87+
"""
88+
note = (
89+
db.query(StickWall)
90+
.filter(StickWall.id == note_id, StickWall.user_id == current_user.id)
91+
.first()
92+
)
93+
if not note:
94+
raise HTTPException(status_code=404, detail="Nota não encontrada.")
95+
return note
96+
97+
98+
@stickwall_router.put("/{note_id}", response_model=StickWallResponse)
99+
def update_note(
100+
note_id: int,
101+
updated: StickWallCreate,
102+
db: Session = Depends(get_db),
103+
current_user: User = Depends(get_current_user),
104+
):
105+
"""
106+
Update a specific StickWall note for the authenticated user.
107+
108+
Args:
109+
note_id (int): Note identifier.
110+
updated (StickWallCreate): New data (name, text, color).
111+
112+
Returns:
113+
StickWallResponse: The updated note.
114+
"""
115+
note = (
116+
db.query(StickWall)
117+
.filter(StickWall.id == note_id, StickWall.user_id == current_user.id)
118+
.first()
119+
)
120+
if not note:
121+
raise HTTPException(status_code=404, detail="Nota não encontrada.")
122+
123+
note.name = updated.name
124+
note.text = updated.text
125+
note.color = updated.color
126+
127+
db.commit()
128+
db.refresh(note)
129+
return note
130+
131+
132+
@stickwall_router.delete("/{note_id}", status_code=status.HTTP_204_NO_CONTENT)
133+
def delete_note(
134+
note_id: int,
135+
db: Session = Depends(get_db),
136+
current_user: User = Depends(get_current_user),
137+
):
138+
"""
139+
Delete a specific StickWall note for the authenticated user.
140+
141+
Args:
142+
note_id (int): Note identifier.
143+
144+
Returns:
145+
None
146+
"""
147+
note = (
148+
db.query(StickWall)
149+
.filter(StickWall.id == note_id, StickWall.user_id == current_user.id)
150+
.first()
151+
)
152+
if not note:
153+
raise HTTPException(status_code=404, detail="Nota não encontrada.")
154+
155+
db.delete(note)
156+
db.commit()

src/schemas/stickwall/__init__.py

Whitespace-only changes.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from typing import Optional
2+
from datetime import datetime
3+
4+
from pydantic import BaseModel, Field
5+
6+
7+
class StickWallBase(BaseModel):
8+
"""
9+
Base schema for StickWall notes.
10+
11+
Attributes:
12+
name (str): Note title.
13+
text (str): Note content.
14+
color (str): Hex color (e.g. "#FFF176").
15+
"""
16+
name: str
17+
text: str
18+
color: str = Field(
19+
default="#FFFF88",
20+
description="Hex color code (e.g. #FFF176)",
21+
pattern=r"^#[0-9A-Fa-f]{6}$",
22+
)
23+
24+
25+
class StickWallCreate(StickWallBase):
26+
"""
27+
Schema used for creating new StickWall notes.
28+
"""
29+
pass
30+
31+
32+
class StickWallResponse(StickWallBase):
33+
"""
34+
Schema used for API responses for StickWall notes.
35+
36+
Attributes:
37+
id (int): Unique identifier.
38+
created_at (datetime): Creation timestamp.
39+
updated_at (datetime): Last update timestamp.
40+
"""
41+
id: int
42+
created_at: datetime
43+
updated_at: datetime
44+
45+
class Config:
46+
from_attributes = True

0 commit comments

Comments
 (0)