Skip to content

Commit

Permalink
refactor: get user interests
Browse files Browse the repository at this point in the history
  • Loading branch information
Anuj-Gupta4 committed Sep 9, 2024
1 parent 3ccb5f0 commit 7897f34
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
24 changes: 17 additions & 7 deletions backend/api/users/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,15 @@
# from backend.services.users.authentication_service import token_auth
from backend.services.users.user_service import UserService
from backend.services.project_service import ProjectService
from backend.services.users.authentication_service import login_required
from backend.models.dtos.user_dto import AuthUserDTO
from fastapi import APIRouter, Depends, Request
from backend.db import get_session
from starlette.authentication import requires
from sqlalchemy.ext.asyncio import AsyncSession
from databases import Database
from backend.db import get_db


router = APIRouter(
prefix="/users",
Expand Down Expand Up @@ -326,11 +331,8 @@ async def get(request: Request):
return locked_tasks.model_dump(by_alias=True), 200


# class UsersQueriesInterestsAPI():
# @token_auth.login_required
@router.get("/{username}/queries/interests/")
@requires("authenticated")
async def get(request: Request, username):
async def get(request: Request, username: str, db: Database = Depends(get_db), user: AuthUserDTO = Depends(login_required)):
"""
Get interests by username
---
Expand Down Expand Up @@ -358,9 +360,17 @@ async def get(request: Request, username):
500:
description: Internal Server Error
"""
user = UserService.get_user_by_username(username)
interests_dto = UserService.get_interests(user)
return interests_dto.model_dump(by_alias=True), 200
query = """
SELECT u.id, u.username, array_agg(i.name) AS interests
FROM users u
LEFT JOIN user_interests ui ON u.id = ui.user_id
LEFT JOIN interests i ON ui.interest_id = i.id
WHERE u.username = :username
GROUP BY u.id, u.username
"""
user = await db.fetch_one(query, {"username": username})
interests_dto = await UserService.get_interests(user, db)
return interests_dto


# class UsersRecommendedProjectsAPI():
Expand Down
20 changes: 13 additions & 7 deletions backend/services/users/user_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
UserCountryContributed,
UserCountriesContributed,
)
from backend.models.dtos.interests_dto import InterestsListDTO, InterestDTO
from backend.models.dtos.interests_dto import InterestsListDTO, InterestDTO, ListInterestDTO
from backend.models.postgis.interests import Interest, project_interests
from backend.models.postgis.message import Message, MessageType
from backend.models.postgis.project import Project
Expand Down Expand Up @@ -840,12 +840,18 @@ def register_user_with_email(user_dto: UserRegisterEmailDTO):
return user

@staticmethod
def get_interests(user: User) -> InterestsListDTO:
dto = InterestsListDTO()
for interest in Interest.query.all():
int_dto = interest.as_dto()
async def get_interests(user: User, db: Database) -> InterestsListDTO:
query = """
SELECT * FROM interests
"""
interests = await db.fetch_all(query)
interest_list_dto = InterestsListDTO()

for interest in interests:
int_dto = ListInterestDTO(**interest)

if interest in user.interests:
int_dto.user_selected = True
dto.interests.append(int_dto)
interest_list_dto.interests.append(int_dto)

return dto
return interest_list_dto

0 comments on commit 7897f34

Please sign in to comment.