Skip to content

Commit 2566a56

Browse files
committed
Refactor: move static DTO mapping methods to consistent positions within each class
1 parent 94c82af commit 2566a56

File tree

4 files changed

+53
-53
lines changed

4 files changed

+53
-53
lines changed

conduit/infrastructure/repositories/article.py

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -29,40 +29,6 @@
2929

3030

3131
class ArticleRepository(IArticleRepository):
32-
@staticmethod
33-
def _to_article_record_dto(model: Article) -> ArticleRecordDTO:
34-
return ArticleRecordDTO(
35-
id=model.id,
36-
author_id=model.author_id,
37-
slug=model.slug,
38-
title=model.title,
39-
description=model.description,
40-
body=model.body,
41-
created_at=model.created_at,
42-
updated_at=model.updated_at,
43-
)
44-
45-
@staticmethod
46-
def _to_article_feed_record_dto(res: Any) -> ArticleFeedRecordDTO:
47-
tags = res.tags.split(", ") if res.tags else []
48-
return ArticleFeedRecordDTO(
49-
id=res.id,
50-
author_id=res.author_id,
51-
slug=res.slug,
52-
title=res.title,
53-
description=res.description,
54-
body=res.body,
55-
tags=tags,
56-
author_username=res.username,
57-
author_bio=res.bio,
58-
author_image=res.image,
59-
author_following=res.following,
60-
created_at=res.created_at,
61-
updated_at=res.updated_at,
62-
favorited=res.favorited,
63-
favorites_count=res.favorites_count,
64-
)
65-
6632
async def add(
6733
self, session: AsyncSession, author_id: int, create_item: CreateArticleDTO
6834
) -> ArticleRecordDTO:
@@ -329,3 +295,37 @@ async def count_by_filters(
329295

330296
result = await session.execute(query)
331297
return int(result.scalar_one())
298+
299+
@staticmethod
300+
def _to_article_record_dto(model: Article) -> ArticleRecordDTO:
301+
return ArticleRecordDTO(
302+
id=model.id,
303+
author_id=model.author_id,
304+
slug=model.slug,
305+
title=model.title,
306+
description=model.description,
307+
body=model.body,
308+
created_at=model.created_at,
309+
updated_at=model.updated_at,
310+
)
311+
312+
@staticmethod
313+
def _to_article_feed_record_dto(res: Any) -> ArticleFeedRecordDTO:
314+
tags = res.tags.split(", ") if res.tags else []
315+
return ArticleFeedRecordDTO(
316+
id=res.id,
317+
author_id=res.author_id,
318+
slug=res.slug,
319+
title=res.title,
320+
description=res.description,
321+
body=res.body,
322+
tags=tags,
323+
author_username=res.username,
324+
author_bio=res.bio,
325+
author_image=res.image,
326+
author_following=res.following,
327+
created_at=res.created_at,
328+
updated_at=res.updated_at,
329+
favorited=res.favorited,
330+
favorites_count=res.favorites_count,
331+
)

conduit/infrastructure/repositories/article_tag.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@
1212
class ArticleTagRepository(IArticleTagRepository):
1313
"""Repository for Article Tag model."""
1414

15-
@staticmethod
16-
def _to_tag_record_dto(model: Tag) -> TagRecordDTO:
17-
return TagRecordDTO(id=model.id, tag=model.tag, created_at=model.created_at)
18-
1915
async def add_many(
2016
self, session: AsyncSession, article_id: int, tags: list[str]
2117
) -> list[TagRecordDTO]:
@@ -58,3 +54,7 @@ async def list(self, session: AsyncSession, article_id: int) -> list[TagRecordDT
5854
)
5955
tags = await session.scalars(query)
6056
return [self._to_tag_record_dto(tag) for tag in tags]
57+
58+
@staticmethod
59+
def _to_tag_record_dto(model: Tag) -> TagRecordDTO:
60+
return TagRecordDTO(id=model.id, tag=model.tag, created_at=model.created_at)

conduit/infrastructure/repositories/comment.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,6 @@
1212

1313

1414
class CommentRepository(ICommentRepository):
15-
@staticmethod
16-
def _to_comment_record_dto(model: Comment) -> CommentRecordDTO:
17-
return CommentRecordDTO(
18-
id=model.id,
19-
body=model.body,
20-
author_id=model.author_id,
21-
article_id=model.article_id,
22-
created_at=model.created_at,
23-
updated_at=model.updated_at,
24-
)
25-
2615
async def add(
2716
self,
2817
session: AsyncSession,
@@ -72,3 +61,14 @@ async def count(self, session: AsyncSession, article_id: int) -> int:
7261
query = select(count(Comment.id)).where(Comment.article_id == article_id)
7362
result = await session.execute(query)
7463
return result.scalar_one()
64+
65+
@staticmethod
66+
def _to_comment_record_dto(model: Comment) -> CommentRecordDTO:
67+
return CommentRecordDTO(
68+
id=model.id,
69+
body=model.body,
70+
author_id=model.author_id,
71+
article_id=model.article_id,
72+
created_at=model.created_at,
73+
updated_at=model.updated_at,
74+
)

conduit/infrastructure/repositories/tag.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
class TagRepository(ITagRepository):
1010
"""Repository for Tag model."""
1111

12-
@staticmethod
13-
def _to_tag_record_dto(model: Tag) -> TagRecordDTO:
14-
return TagRecordDTO(id=model.id, tag=model.tag, created_at=model.created_at)
15-
1612
async def list(self, session: AsyncSession) -> list[TagRecordDTO]:
1713
query = select(Tag)
1814
tags = await session.scalars(query)
1915
return [self._to_tag_record_dto(tag) for tag in tags]
16+
17+
@staticmethod
18+
def _to_tag_record_dto(model: Tag) -> TagRecordDTO:
19+
return TagRecordDTO(id=model.id, tag=model.tag, created_at=model.created_at)

0 commit comments

Comments
 (0)