Skip to content

Commit

Permalink
refactor: set user interests
Browse files Browse the repository at this point in the history
  • Loading branch information
Anuj-Gupta4 authored and prabinoid committed Sep 12, 2024
1 parent 9a42c6b commit 2c457af
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 13 deletions.
17 changes: 10 additions & 7 deletions backend/api/users/actions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
from flask_restful import current_app
# from flask_restful import Resource, current_app, request
# from schematics.exceptions import DataError

from backend.models.dtos.user_dto import UserDTO, UserRegisterEmailDTO
from databases import Database
from backend.db import get_db
from backend.services.users.authentication_service import login_required
from backend.models.dtos.user_dto import AuthUserDTO
from backend.services.messaging.message_service import MessageService
from backend.services.users.authentication_service import tm
from backend.services.users.user_service import UserService, UserServiceError
Expand Down Expand Up @@ -346,8 +350,7 @@ async def post(request: Request):
# class UsersActionsSetInterestsAPI(Resource):
# @token_auth.login_required
@router.post("/me/actions/set-interests/")
@requires("authenticated")
async def post(request: Request):
async def post(request: Request, db: Database = Depends(get_db), user: AuthUserDTO = Depends(login_required)):
"""
Creates a relationship between user and interests
---
Expand Down Expand Up @@ -383,10 +386,10 @@ async def post(request: Request):
description: Internal Server Error
"""
try:
data = request.get_json()
user_interests = InterestService.create_or_update_user_interests(
request.user.display_name, data["interests"]
data = await request.json()
user_interests = await InterestService.create_or_update_user_interests(
user.id, data["interests"], db
)
return user_interests.model_dump(by_alias=True), 200
return user_interests
except (ValueError, KeyError) as e:
return {"Error": str(e)}, 400
2 changes: 2 additions & 0 deletions backend/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
statistics as user_statistics,
openstreetmap as users_openstreetmap,
tasks as users_tasks,
actions as user_actions
)
from backend.api.licenses import (
resources as license_resources,
Expand Down Expand Up @@ -84,6 +85,7 @@ def add_api_end_points(api):
v2.include_router(users_openstreetmap.router)
v2.include_router(users_tasks.router)
v2.include_router(user_statistics.router)
v2.include_router(user_actions.router)

# Licenses REST endpoint
v2.include_router(license_resources.router)
Expand Down
37 changes: 31 additions & 6 deletions backend/services/interests_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
InterestRateListDTO,
InterestsListDTO,
InterestDTO,
ListInterestDTO,
)
from backend.models.postgis.interests import (
Interest,
Expand Down Expand Up @@ -109,14 +110,38 @@ def create_or_update_project_interests(project_id, interests):
return dto

@staticmethod
def create_or_update_user_interests(user_id, interests):
user = UserService.get_user_by_id(user_id)
user.create_or_update_interests(interests)
async def create_or_update_user_interests(user_id, interests_ids, db: Database):
"""
Create or update the user's interests by directly interacting with the database.
"""
async with db.transaction():
delete_query = """
DELETE FROM user_interests WHERE user_id = :user_id
"""
await db.execute(delete_query, {"user_id": user_id})
insert_query = """
INSERT INTO user_interests (user_id, interest_id)
VALUES (:user_id, :interest_id)
"""
values = [{"user_id": user_id, "interest_id": interest_id} for interest_id in interests_ids]
await db.execute_many(insert_query, values)
return await InterestService.get_user_interests(user_id, db)

# Return DTO.
dto = InterestsListDTO()
dto.interests = [i.as_dto() for i in user.interests]
@staticmethod
async def get_user_interests(user_id, db: Database) -> InterestsListDTO:
"""
Fetch the updated interests for the user and return the DTO.
"""
query = """
SELECT i.id, i.name
FROM interests i
JOIN user_interests ui ON i.id = ui.interest_id
WHERE ui.user_id = :user_id
"""
rows = await db.fetch_all(query, {"user_id": user_id})

dto = InterestsListDTO()
dto.interests = [ListInterestDTO(id=row['id'], name=row['name']) for row in rows]
return dto

@staticmethod
Expand Down

0 comments on commit 2c457af

Please sign in to comment.