Skip to content

Commit d0ed6e6

Browse files
committed
refactor: todo module uses integer ids
1 parent 3ba129a commit d0ed6e6

12 files changed

Lines changed: 44 additions & 61 deletions

File tree

src/modules/todo/application/create_todo/handler.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from uuid import UUID
2-
31
from src.modules.todo.application.create_todo.command import CreateTodoCommand
42
from src.modules.todo.application.create_todo.validation import (
53
validate_create_todo_command,
@@ -14,7 +12,7 @@ def __init__(self, todo_repo: TodoRepository, unit_of_work: UnitOfWork):
1412
self.todo_repo = todo_repo
1513
self._unit_of_work = unit_of_work
1614

17-
async def execute(self, command: CreateTodoCommand, user_id: UUID) -> Todo:
15+
async def execute(self, command: CreateTodoCommand, user_id: int) -> Todo:
1816
validate_create_todo_command(command)
1917

2018
todo = Todo.create(

src/modules/todo/application/delete_todo/handler.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from uuid import UUID
2-
31
from src.modules.todo.domain.exceptions.todo_exception import (
42
TodoNotFoundError,
53
UnauthorizedTodoAccessError,
@@ -13,7 +11,7 @@ def __init__(self, todo_repo: TodoRepository, unit_of_work: UnitOfWork):
1311
self.todo_repo = todo_repo
1412
self._unit_of_work = unit_of_work
1513

16-
async def execute(self, todo_id: UUID, user_id: UUID) -> None:
14+
async def execute(self, todo_id: int, user_id: int) -> None:
1715
todo = await self.todo_repo.get_by_id(todo_id)
1816
if not todo:
1917
raise TodoNotFoundError("Todo not found")

src/modules/todo/application/detail_todo/handler.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from uuid import UUID
2-
31
from src.modules.todo import (
42
TodoNotFoundError,
53
TodoRepository,
@@ -19,7 +17,7 @@ def __init__(
1917
self._todo_repo = todo_repo
2018
self._user_provider = user_provider
2119

22-
async def execute(self, todo_id: UUID, user_id: UUID) -> TodoWithOwnerResponse:
20+
async def execute(self, todo_id: int, user_id: int) -> TodoWithOwnerResponse:
2321
todo = await self._todo_repo.get_by_id(todo_id)
2422
if not todo:
2523
raise TodoNotFoundError("Todo not found")

src/modules/todo/application/list_todo/handler.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from datetime import datetime
2-
from uuid import UUID
32

43
from src.modules.todo.application.list_todo.query import GetTodosQuery
54
from src.modules.todo.application.list_todo.validation import validate_get_todos_query
@@ -24,9 +23,9 @@ def __init__(self, todo_repo: TodoRepository):
2423

2524
async def execute(
2625
self,
27-
user_id: UUID,
26+
user_id: int,
2827
cursor_created_at: datetime | None = None,
29-
cursor_id: UUID | None = None,
28+
cursor_id: int | None = None,
3029
limit: int = 10,
3130
direction: CursorDirection = CursorDirection.DIRECTION_NEXT,
3231
) -> tuple[list[Todo], bool]:
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
from uuid import UUID
2-
31
from pydantic import BaseModel
42

53

64
class GetTodosQuery(BaseModel):
7-
user_id: UUID
5+
user_id: int

src/modules/todo/application/update_todo/handler.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from uuid import UUID
2-
31
from src.modules.todo.application.update_todo.command import UpdateTodoCommand
42
from src.modules.todo.application.update_todo.validation import (
53
validate_update_todo_command,
@@ -19,7 +17,7 @@ def __init__(self, todo_repo: TodoRepository, unit_of_work: UnitOfWork):
1917
self._unit_of_work = unit_of_work
2018

2119
async def execute(
22-
self, todo_id: UUID, command: UpdateTodoCommand, user_id: UUID
20+
self, todo_id: int, command: UpdateTodoCommand, user_id: int
2321
) -> Todo:
2422
validate_update_todo_command(command)
2523

src/modules/todo/domain/entities/todo.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
from __future__ import annotations
22

33
from dataclasses import dataclass
4-
from uuid import UUID, uuid4
54

65

76
@dataclass
87
class Todo:
9-
id: UUID
10-
title: str
11-
description: str | None
12-
is_completed: bool
13-
user_id: UUID
8+
id: int | None = None
9+
title: str = ""
10+
description: str | None = None
11+
is_completed: bool = False
12+
user_id: int = 0
1413

1514
@classmethod
16-
def create(cls, title: str, user_id: UUID, description: str | None = None) -> Todo:
15+
def create(cls, title: str, user_id: int, description: str | None = None) -> Todo:
1716
return cls(
18-
id=uuid4(),
17+
id=None,
1918
title=title,
2019
description=description,
2120
is_completed=False,

src/modules/todo/domain/repositories/todo_repository.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,25 @@
11
from abc import ABC, abstractmethod
22
from datetime import datetime
3-
from uuid import UUID
43

54
from src.modules.todo.domain.entities.todo import Todo
65
from src.shared.utils.cursor import CursorDirection
76

87

98
class TodoRepository(ABC):
109
@abstractmethod
11-
async def get_by_id(self, todo_id: UUID) -> Todo | None:
10+
async def get_by_id(self, todo_id: int) -> Todo | None:
1211
pass
1312

1413
@abstractmethod
15-
async def get_all_by_user(self, user_id: UUID) -> list[Todo]:
14+
async def get_all_by_user(self, user_id: int) -> list[Todo]:
1615
pass
1716

1817
@abstractmethod
1918
async def get_by_user_cursor(
2019
self,
21-
user_id: UUID,
20+
user_id: int,
2221
cursor_created_at: datetime | None = None,
23-
cursor_id: UUID | None = None,
22+
cursor_id: int | None = None,
2423
limit: int = 10,
2524
direction: CursorDirection = CursorDirection.DIRECTION_NEXT,
2625
) -> tuple[list[Todo], bool]:
@@ -35,5 +34,5 @@ async def save(self, todo: Todo) -> Todo:
3534
pass
3635

3736
@abstractmethod
38-
async def delete(self, todo_id: UUID) -> None:
37+
async def delete(self, todo_id: int) -> None:
3938
pass

src/modules/todo/infrastructure/models/todo_model.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import uuid
2-
31
from sqlalchemy import Boolean, ForeignKey, String
42
from sqlalchemy.orm import Mapped, mapped_column
53

@@ -14,4 +12,4 @@ class TodoModel(Base, TimeStampMixin, SoftDeleteMixin, TenantMixin):
1412
title: Mapped[str] = mapped_column(String(255))
1513
description: Mapped[str | None] = mapped_column(String(500), nullable=True)
1614
is_completed: Mapped[bool] = mapped_column(Boolean, default=False)
17-
user_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("users.id"))
15+
user_id: Mapped[int] = mapped_column(ForeignKey("users.id"))

src/modules/todo/infrastructure/repositories/todo_repository.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from datetime import datetime
2-
from uuid import UUID
32

43
from sqlalchemy import and_, delete, or_, select
54
from sqlalchemy.ext.asyncio import AsyncSession
@@ -11,11 +10,11 @@
1110

1211

1312
class SQLAlchemyTodoRepository(TodoRepository):
14-
def __init__(self, db: AsyncSession, tenant_id: UUID | None = None):
13+
def __init__(self, db: AsyncSession, tenant_id: int | None = None):
1514
self.db = db
1615
self._tenant_id = tenant_id
1716

18-
async def get_by_id(self, todo_id: UUID) -> Todo | None:
17+
async def get_by_id(self, todo_id: int) -> Todo | None:
1918
stmt = select(TodoModel).where(TodoModel.id == todo_id)
2019
if self._tenant_id:
2120
stmt = stmt.where(TodoModel.tenant_id == self._tenant_id)
@@ -33,9 +32,9 @@ async def get_by_id(self, todo_id: UUID) -> Todo | None:
3332

3433
async def get_by_user_cursor(
3534
self,
36-
user_id: UUID,
35+
user_id: int,
3736
cursor_created_at: datetime | None = None,
38-
cursor_id: UUID | None = None,
37+
cursor_id: int | None = None,
3938
limit: int = 10,
4039
direction: CursorDirection = CursorDirection.DIRECTION_NEXT,
4140
) -> tuple[list[Todo], bool]:
@@ -97,7 +96,7 @@ async def get_by_user_cursor(
9796

9897
return [self._to_entity(m) for m in models], has_more
9998

100-
async def get_all_by_user(self, user_id: UUID) -> list[Todo]:
99+
async def get_all_by_user(self, user_id: int) -> list[Todo]:
101100
stmt = select(TodoModel).where(TodoModel.user_id == user_id)
102101
if self._tenant_id:
103102
stmt = stmt.where(TodoModel.tenant_id == self._tenant_id)
@@ -115,14 +114,16 @@ async def get_all_by_user(self, user_id: UUID) -> list[Todo]:
115114
]
116115

117116
async def save(self, todo: Todo) -> Todo:
118-
model = TodoModel(
119-
id=todo.id,
120-
title=todo.title,
121-
description=todo.description,
122-
is_completed=todo.is_completed,
123-
user_id=todo.user_id,
124-
tenant_id=self._tenant_id,
125-
)
117+
model_kwargs = {
118+
"title": todo.title,
119+
"description": todo.description,
120+
"is_completed": todo.is_completed,
121+
"user_id": todo.user_id,
122+
"tenant_id": self._tenant_id,
123+
}
124+
if todo.id is not None:
125+
model_kwargs["id"] = todo.id
126+
model = TodoModel(**model_kwargs)
126127
model = await self.db.merge(model)
127128
await self.db.flush()
128129
await self.db.refresh(model)
@@ -134,7 +135,7 @@ async def save(self, todo: Todo) -> Todo:
134135
user_id=model.user_id,
135136
)
136137

137-
async def delete(self, todo_id: UUID) -> None:
138+
async def delete(self, todo_id: int) -> None:
138139
stmt = delete(TodoModel).where(TodoModel.id == todo_id)
139140
if self._tenant_id:
140141
stmt = stmt.where(TodoModel.tenant_id == self._tenant_id)
@@ -143,7 +144,7 @@ async def delete(self, todo_id: UUID) -> None:
143144

144145
def _to_entity(self, model: TodoModel) -> Todo:
145146
return Todo(
146-
id=str(model.id),
147+
id=model.id,
147148
description=model.description,
148149
is_completed=model.is_completed,
149150
title=model.title,

0 commit comments

Comments
 (0)