Skip to content

Commit 794f6de

Browse files
committed
Refactor services and repositories to consolidate session management under UnitOfWork
1 parent bc26bdc commit 794f6de

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+582
-690
lines changed

conduit/api/routes/article.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
from conduit.core.dependencies import (
1010
CurrentOptionalUser,
1111
CurrentUser,
12-
DBSession,
1312
IArticleService,
1413
Pagination,
1514
QueryFilters,
15+
UnitOfWork,
1616
)
1717

1818
router = APIRouter()
@@ -21,15 +21,15 @@
2121
@router.get("/feed", response_model=ArticlesFeedResponse)
2222
async def get_article_feed(
2323
pagination: Pagination,
24-
session: DBSession,
24+
uow: UnitOfWork,
2525
current_user: CurrentUser,
2626
article_service: IArticleService,
2727
) -> ArticlesFeedResponse:
2828
"""
2929
Get article feed from following users.
3030
"""
3131
articles_feed_dto = await article_service.get_articles_feed(
32-
session=session,
32+
uow=uow,
3333
current_user=current_user,
3434
limit=pagination.limit,
3535
offset=pagination.offset,
@@ -41,15 +41,15 @@ async def get_article_feed(
4141
async def get_global_article_feed(
4242
pagination: Pagination,
4343
articles_filters: QueryFilters,
44-
session: DBSession,
44+
uow: UnitOfWork,
4545
current_user: CurrentOptionalUser,
4646
article_service: IArticleService,
4747
) -> ArticlesFeedResponse:
4848
"""
4949
Get global article feed.
5050
"""
5151
articles_feed_dto = await article_service.get_articles_by_filters(
52-
session=session,
52+
uow=uow,
5353
current_user=current_user,
5454
tag=articles_filters.tag,
5555
author=articles_filters.author,
@@ -63,31 +63,31 @@ async def get_global_article_feed(
6363
@router.get("/{slug}", response_model=ArticleResponse)
6464
async def get_article(
6565
slug: str,
66-
session: DBSession,
66+
uow: UnitOfWork,
6767
current_user: CurrentOptionalUser,
6868
article_service: IArticleService,
6969
) -> ArticleResponse:
7070
"""
7171
Get new article by slug.
7272
"""
7373
article_dto = await article_service.get_article_by_slug(
74-
session=session, slug=slug, current_user=current_user
74+
uow=uow, slug=slug, current_user=current_user
7575
)
7676
return ArticleResponse.from_dto(dto=article_dto)
7777

7878

7979
@router.post("", response_model=ArticleResponse)
8080
async def create_article(
8181
payload: CreateArticleRequest,
82-
session: DBSession,
82+
uow: UnitOfWork,
8383
current_user: CurrentUser,
8484
article_service: IArticleService,
8585
) -> ArticleResponse:
8686
"""
8787
Create new article.
8888
"""
8989
article_dto = await article_service.create_new_article(
90-
session=session, author_id=current_user.id, article_to_create=payload.to_dto()
90+
uow=uow, author_id=current_user.id, article_to_create=payload.to_dto()
9191
)
9292
return ArticleResponse.from_dto(dto=article_dto)
9393

@@ -96,15 +96,15 @@ async def create_article(
9696
async def update_article(
9797
slug: str,
9898
payload: UpdateArticleRequest,
99-
session: DBSession,
99+
uow: UnitOfWork,
100100
current_user: CurrentUser,
101101
article_service: IArticleService,
102102
) -> ArticleResponse:
103103
"""
104104
Update an article.
105105
"""
106106
article_dto = await article_service.update_article_by_slug(
107-
session=session,
107+
uow=uow,
108108
slug=slug,
109109
article_to_update=payload.to_dto(),
110110
current_user=current_user,
@@ -115,45 +115,45 @@ async def update_article(
115115
@router.delete("/{slug}", status_code=status.HTTP_204_NO_CONTENT)
116116
async def delete_article(
117117
slug: str,
118-
session: DBSession,
118+
uow: UnitOfWork,
119119
current_user: CurrentUser,
120120
article_service: IArticleService,
121121
) -> None:
122122
"""
123123
Delete an article by slug.
124124
"""
125125
await article_service.delete_article_by_slug(
126-
session=session, slug=slug, current_user=current_user
126+
uow=uow, slug=slug, current_user=current_user
127127
)
128128

129129

130130
@router.post("/{slug}/favorite", response_model=ArticleResponse)
131131
async def favorite_article(
132132
slug: str,
133-
session: DBSession,
133+
uow: UnitOfWork,
134134
current_user: CurrentUser,
135135
article_service: IArticleService,
136136
) -> ArticleResponse:
137137
"""
138138
Favorite an article.
139139
"""
140140
article_dto = await article_service.add_article_into_favorites(
141-
session=session, slug=slug, current_user=current_user
141+
uow=uow, slug=slug, current_user=current_user
142142
)
143143
return ArticleResponse.from_dto(dto=article_dto)
144144

145145

146146
@router.delete("/{slug}/favorite", response_model=ArticleResponse)
147147
async def unfavorite_article(
148148
slug: str,
149-
session: DBSession,
149+
uow: UnitOfWork,
150150
current_user: CurrentUser,
151151
article_service: IArticleService,
152152
) -> ArticleResponse:
153153
"""
154154
Unfavorite an article.
155155
"""
156156
article_dto = await article_service.remove_article_from_favorites(
157-
session=session, slug=slug, current_user=current_user
157+
uow=uow, slug=slug, current_user=current_user
158158
)
159159
return ArticleResponse.from_dto(dto=article_dto)

conduit/api/routes/authentication.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,34 @@
55
UserLoginResponse,
66
UserRegistrationResponse,
77
)
8-
from conduit.core.dependencies import DBSession, IUserAuthService
8+
from conduit.core.dependencies import IUserAuthService, UnitOfWork
99

1010
router = APIRouter()
1111

1212

1313
@router.post("", response_model=UserRegistrationResponse)
1414
async def register_user(
1515
payload: UserRegistrationRequest,
16-
session: DBSession,
16+
uow: UnitOfWork,
1717
user_auth_service: IUserAuthService,
1818
) -> UserRegistrationResponse:
1919
"""
2020
Process user registration.
2121
"""
2222
user_dto = await user_auth_service.sign_up_user(
23-
session=session, user_to_create=payload.to_dto()
23+
uow=uow, user_to_create=payload.to_dto()
2424
)
2525
return UserRegistrationResponse.from_dto(dto=user_dto)
2626

2727

2828
@router.post("/login", response_model=UserLoginResponse)
2929
async def login_user(
30-
payload: UserLoginRequest, session: DBSession, user_auth_service: IUserAuthService
30+
payload: UserLoginRequest, uow: UnitOfWork, user_auth_service: IUserAuthService
3131
) -> UserLoginResponse:
3232
"""
3333
Process user login.
3434
"""
3535
user_dto = await user_auth_service.sign_in_user(
36-
session=session, user_to_login=payload.to_dto()
36+
uow=uow, user_to_login=payload.to_dto()
3737
)
3838
return UserLoginResponse.from_dto(dto=user_dto)

conduit/api/routes/comment.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
from conduit.core.dependencies import (
77
CurrentOptionalUser,
88
CurrentUser,
9-
DBSession,
109
ICommentService,
10+
UnitOfWork,
1111
)
1212

1313
router = APIRouter()
@@ -16,15 +16,15 @@
1616
@router.get("/{slug}/comments", response_model=CommentsListResponse)
1717
async def get_comments(
1818
slug: str,
19-
session: DBSession,
19+
uow: UnitOfWork,
2020
current_user: CurrentOptionalUser,
2121
comment_service: ICommentService,
2222
) -> CommentsListResponse:
2323
"""
2424
Get comments for an article.
2525
"""
2626
comment_list_dto = await comment_service.get_article_comments(
27-
session=session, slug=slug, current_user=current_user
27+
uow=uow, slug=slug, current_user=current_user
2828
)
2929
return CommentsListResponse.from_dto(dto=comment_list_dto)
3030

@@ -33,15 +33,15 @@ async def get_comments(
3333
async def create_comment(
3434
slug: str,
3535
payload: CreateCommentRequest,
36-
session: DBSession,
36+
uow: UnitOfWork,
3737
current_user: CurrentUser,
3838
comment_service: ICommentService,
3939
) -> CommentResponse:
4040
"""
4141
Create a comment for an article.
4242
"""
4343
comment_dto = await comment_service.create_article_comment(
44-
session=session,
44+
uow=uow,
4545
slug=slug,
4646
comment_to_create=payload.to_dto(),
4747
current_user=current_user,
@@ -52,7 +52,7 @@ async def create_comment(
5252
@router.delete("/{slug}/comments/{id}", status_code=status.HTTP_204_NO_CONTENT)
5353
async def delete_comment(
5454
slug: str,
55-
session: DBSession,
55+
uow: UnitOfWork,
5656
current_user: CurrentUser,
5757
comment_service: ICommentService,
5858
comment_id: int = Path(..., alias="id"),
@@ -61,5 +61,5 @@ async def delete_comment(
6161
Delete a comment for an article.
6262
"""
6363
await comment_service.delete_article_comment(
64-
session=session, slug=slug, comment_id=comment_id, current_user=current_user
64+
uow=uow, slug=slug, comment_id=comment_id, current_user=current_user
6565
)

conduit/api/routes/profile.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
from conduit.core.dependencies import (
55
CurrentOptionalUser,
66
CurrentUser,
7-
DBSession,
87
IProfileService,
8+
UnitOfWork,
99
)
1010

1111
router = APIRouter()
@@ -14,52 +14,52 @@
1414
@router.get("/{username}", response_model=ProfileResponse)
1515
async def get_user_profile(
1616
username: str,
17-
session: DBSession,
17+
uow: UnitOfWork,
1818
current_user: CurrentOptionalUser,
1919
profile_service: IProfileService,
2020
) -> ProfileResponse:
2121
"""
2222
Return user profile information.
2323
"""
2424
profile_dto = await profile_service.get_profile_by_username(
25-
session=session, username=username, current_user=current_user
25+
uow=uow, username=username, current_user=current_user
2626
)
2727
return ProfileResponse.from_dto(dto=profile_dto)
2828

2929

3030
@router.post("/{username}/follow", response_model=ProfileResponse)
3131
async def follow_username(
3232
username: str,
33-
session: DBSession,
33+
uow: UnitOfWork,
3434
current_user: CurrentUser,
3535
profile_service: IProfileService,
3636
) -> ProfileResponse:
3737
"""
3838
Follow profile with specific username.
3939
"""
4040
await profile_service.follow_user(
41-
session=session, username=username, current_user=current_user
41+
uow=uow, username=username, current_user=current_user
4242
)
4343
profile_dto = await profile_service.get_profile_by_username(
44-
session=session, username=username, current_user=current_user
44+
uow=uow, username=username, current_user=current_user
4545
)
4646
return ProfileResponse.from_dto(dto=profile_dto)
4747

4848

4949
@router.delete("/{username}/follow", response_model=ProfileResponse)
5050
async def unfollow_username(
5151
username: str,
52-
session: DBSession,
52+
uow: UnitOfWork,
5353
current_user: CurrentUser,
5454
profile_service: IProfileService,
5555
) -> ProfileResponse:
5656
"""
5757
Unfollow profile with specific username
5858
"""
5959
await profile_service.unfollow_user(
60-
session=session, username=username, current_user=current_user
60+
uow=uow, username=username, current_user=current_user
6161
)
6262
profile_dto = await profile_service.get_profile_by_username(
63-
session=session, username=username, current_user=current_user
63+
uow=uow, username=username, current_user=current_user
6464
)
6565
return ProfileResponse.from_dto(dto=profile_dto)

conduit/api/routes/tag.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
from fastapi import APIRouter
22

33
from conduit.api.schemas.responses.tag import TagsResponse
4-
from conduit.core.dependencies import DBSession, ITagService
4+
from conduit.core.dependencies import ITagService, UnitOfWork
55

66
router = APIRouter()
77

88

99
@router.get("", response_model=TagsResponse)
10-
async def get_all_tags(session: DBSession, tag_service: ITagService) -> TagsResponse:
10+
async def get_all_tags(uow: UnitOfWork, tag_service: ITagService) -> TagsResponse:
1111
"""
1212
Return available all tags.
1313
"""
14-
tags = await tag_service.get_all_tags(session=session)
14+
tags = await tag_service.get_all_tags(uow=uow)
1515
return TagsResponse.from_dtos(dtos=tags)

conduit/api/routes/users.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from conduit.api.schemas.requests.user import UserUpdateRequest
44
from conduit.api.schemas.responses.user import CurrentUserResponse, UpdatedUserResponse
5-
from conduit.core.dependencies import CurrentUser, DBSession, IUserService, JWTToken
5+
from conduit.core.dependencies import CurrentUser, IUserService, JWTToken, UnitOfWork
66

77
router = APIRouter()
88

@@ -21,14 +21,14 @@ async def get_current_user(
2121
async def update_current_user(
2222
payload: UserUpdateRequest,
2323
token: JWTToken,
24-
session: DBSession,
24+
uow: UnitOfWork,
2525
current_user: CurrentUser,
2626
user_service: IUserService,
2727
) -> UpdatedUserResponse:
2828
"""
2929
Update current user.
3030
"""
3131
updated_user_dto = await user_service.update_user(
32-
session=session, current_user=current_user, user_to_update=payload.to_dto()
32+
uow=uow, current_user=current_user, user_to_update=payload.to_dto()
3333
)
3434
return UpdatedUserResponse.from_dto(dto=updated_user_dto, token=token)

0 commit comments

Comments
 (0)