-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use user service instead of plain functions
- Loading branch information
Showing
7 changed files
with
96 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,14 @@ | ||
from src.api.v1.users import me | ||
from src.api.v1.users.deps import UserServiceDepends | ||
from src.api.v1.users.models import User | ||
from src.api.v1.users.routes import router | ||
from src.api.v1.users.service import UserService | ||
|
||
router.include_router(me.router) | ||
|
||
__all__ = [ | ||
"User", | ||
"UserService", | ||
"UserServiceDepends", | ||
"router", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from typing import Annotated | ||
|
||
from fastapi import Depends | ||
|
||
from src.api.v1.users.service import UserService | ||
|
||
UserServiceDepends = Annotated[UserService, Depends(UserService)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,12 @@ | ||
from fastapi import APIRouter | ||
|
||
from src.api.v1.users.deps import UserServiceDepends | ||
from src.api.v1.users.schemas import UserEmailRequest, UserExistenceResponse | ||
from src.api.v1.users.service import is_email_registered | ||
from src.db.deps import Session | ||
|
||
router = APIRouter(prefix="/users", tags=["Users"]) | ||
|
||
|
||
@router.post("/exists") | ||
def user_exists(args: UserEmailRequest, session: Session) -> UserExistenceResponse: | ||
exists = is_email_registered(session, args.email) | ||
def user_exists(args: UserEmailRequest, service: UserServiceDepends) -> UserExistenceResponse: | ||
exists = service.is_email_registered(args.email) | ||
return UserExistenceResponse(exists=exists) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,69 @@ | ||
from PIL.ImageFile import ImageFile | ||
from sqlalchemy.orm import Session | ||
from sqlalchemy.sql import exists | ||
|
||
from src.api.v1.users.models import User | ||
from src.api.v1.users.schemas import UserRegistrationRequest | ||
from src.db.deps import Session | ||
from src.security import get_password_hash | ||
from src.storage import fs | ||
from src.storage.images import crop_image_to_square | ||
|
||
|
||
def get_user_by_email(session: Session, email: str) -> User | None: | ||
return session.query(User).filter(User.email == email).first() | ||
class UserService: | ||
def __init__(self, session: Session) -> None: | ||
self.session = session | ||
|
||
def get_user_by_email(self, email: str) -> User | None: | ||
return self.session.query(User).filter(User.email == email).first() | ||
|
||
def is_email_registered(session: Session, email: str) -> bool: | ||
return session.query(exists().where(User.email == email)).scalar() | ||
def is_email_registered(self, email: str) -> bool: | ||
return self.session.query(exists().where(User.email == email)).scalar() | ||
|
||
def register_user(self, args: UserRegistrationRequest) -> User: | ||
user = User( | ||
email=args.email, | ||
password=get_password_hash(args.password.get_secret_value()), | ||
is_verified=True, | ||
) | ||
|
||
def register_user(session: Session, args: UserRegistrationRequest) -> User: | ||
user = User( | ||
email=args.email, | ||
password=get_password_hash(args.password.get_secret_value()), | ||
is_verified=True, | ||
) | ||
self.session.add(user) | ||
self.session.commit() | ||
self.session.refresh(user) | ||
|
||
session.add(user) | ||
session.commit() | ||
session.refresh(user) | ||
return user | ||
|
||
return user | ||
def update_email(self, user: User, new_email: str): | ||
user.is_verified = True | ||
user.email = new_email | ||
|
||
self.session.commit() | ||
self.session.refresh(user) | ||
|
||
def update_email(session: Session, user: User, new_email: str): | ||
user.is_verified = True | ||
user.email = new_email | ||
session.commit() | ||
session.refresh(user) | ||
def update_password(self, user: User, new_password: str) -> None: | ||
hashed_password = get_password_hash(password=new_password) | ||
user.password = hashed_password | ||
|
||
self.session.commit() | ||
self.session.refresh(user) | ||
|
||
def update_password(session: Session, user: User, new_password: str) -> None: | ||
hashed_password = get_password_hash(password=new_password) | ||
user.password = hashed_password | ||
session.commit() | ||
session.refresh(user) | ||
def delete_avatar(self, user: User) -> None: | ||
self.record_avatar(user) | ||
|
||
def update_avatar(self, user: User, image: ImageFile) -> None: | ||
cropped_image = crop_image_to_square(image) | ||
|
||
def record_avatar(session: Session, user: User, avatar_url: str | None = None) -> None: | ||
if user.avatar_url: | ||
fs.remove(user.avatar_url) | ||
extension = cropped_image.format or "PNG" | ||
name = fs.generate_unique_file_name_from_extension(extension) | ||
path = fs.get_system_path(name) | ||
cropped_image.save(path, extension) | ||
|
||
user.avatar_url = avatar_url | ||
session.commit() | ||
session.refresh(user) | ||
self.record_avatar(user, name) | ||
|
||
def record_avatar(self, user: User, avatar_url: str | None = None) -> None: | ||
if user.avatar_url: | ||
fs.remove(user.avatar_url) | ||
|
||
def update_avatar(session: Session, user: User, image: ImageFile) -> None: | ||
cropped_image = crop_image_to_square(image) | ||
user.avatar_url = avatar_url | ||
|
||
extension = cropped_image.format or "PNG" | ||
name = fs.generate_unique_file_name_from_extension(extension) | ||
path = fs.get_system_path(name) | ||
cropped_image.save(path, extension) | ||
|
||
record_avatar(session, user, name) | ||
|
||
|
||
def delete_avatar(session: Session, user: User) -> None: | ||
record_avatar(session, user) | ||
self.session.commit() | ||
self.session.refresh(user) |