Skip to content
This repository has been archived by the owner on Dec 15, 2024. It is now read-only.

Commit

Permalink
feat: favorites
Browse files Browse the repository at this point in the history
  • Loading branch information
TatarinAlba committed Nov 24, 2024
1 parent 21e864e commit 24ff9d2
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 2 deletions.
6 changes: 5 additions & 1 deletion backend/src/modules/users/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from src.modules.users.schemas import CreateUser
from src.storages.mongo.users import User

from fastapi import HTTPException

# noinspection PyMethodMayBeStatic
class UserRepository:
Expand Down Expand Up @@ -34,4 +34,8 @@ async def is_banned(self, user_id: str | PydanticObjectId) -> bool:
return False


async def upsert_favorites(self, user_id: PydanticObjectId, favorite_items: list[PydanticObjectId]) -> list[PydanticObjectId]:
user = await User.find_one(User.id == user_id).update({"$set": {"favorites": list(set(favorite_items))}})
return favorite_items

user_repository: UserRepository = UserRepository()
7 changes: 6 additions & 1 deletion backend/src/modules/users/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
from src.api.dependencies import USER_AUTH
from src.api.exceptions import IncorrectCredentialsException
from src.modules.users.repository import user_repository
from src.modules.users.schemas import ViewUser
from src.modules.users.schemas import ViewUser, UpdateFavoriteReq
from beanie import PydanticObjectId

router = APIRouter(
prefix="/users",
Expand Down Expand Up @@ -67,3 +68,7 @@ async def logout(request: Request) -> None:
"""
request.session.clear()
return None

@router.put("/favorites")
async def update_favorites(user: USER_AUTH, req: UpdateFavoriteReq) -> list[PydanticObjectId]:
return await user_repository.upsert_favorites(user_id=user.user_id, favorite_items=req.favorite_ids)
2 changes: 2 additions & 0 deletions backend/src/modules/users/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ class CreateUser(BaseSchema):
login: str
password: str

class UpdateFavoriteReq(BaseSchema):
favorite_ids: list[PydanticObjectId]

class ViewUser(BaseSchema):
id: PydanticObjectId
Expand Down
2 changes: 2 additions & 0 deletions backend/src/storages/mongo/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from pymongo import IndexModel

from beanie import PydanticObjectId
from src.pydantic_base import BaseSchema
from src.storages.mongo.__base__ import CustomDocument

Expand All @@ -16,6 +17,7 @@ class UserRole(StrEnum):
class UserSchema(BaseSchema):
login: str
password_hash: str
favorites: list[PydanticObjectId]
role: UserRole = UserRole.DEFAULT


Expand Down

0 comments on commit 24ff9d2

Please sign in to comment.