Skip to content

Commit

Permalink
feat: add likes to user_questions
Browse files Browse the repository at this point in the history
  • Loading branch information
seelengxd committed Sep 25, 2024
1 parent f6bdc7e commit 99ab969
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
31 changes: 29 additions & 2 deletions backend/src/user_questions/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
from typing import Annotated
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy import select
from sqlalchemy.orm import with_polymorphic
from sqlalchemy.orm import with_polymorphic, aliased, selectinload
from src.auth.dependencies import get_current_user
from src.auth.models import User
from src.common.dependencies import get_session
from src.events.models import Analysis, Event
from src.likes.models import Like
from src.notes.models import Note
from src.user_questions.models import Answer, Point, UserQuestion
from src.user_questions.schemas import CreateUserQuestion, UserQuestionMiniDTO
Expand All @@ -23,15 +24,40 @@ def get_user_questions(
user: Annotated[User, Depends(get_current_user)],
session=Depends(get_session),
) -> list[UserQuestionMiniDTO]:
# Create an alias for the Point table to use for the Like condition
point_alias = aliased(Point)
user_questions = session.scalars(
select(UserQuestion)
.where(UserQuestion.user_id == user.id)
.join(UserQuestion.answer)
.join(Answer.points)
.join(Point.analysises)
.join(point_alias.analysises)
.join(Analysis.event)
.join(Event.original_article)
.join(Analysis.category)
.join(Analysis.likes)
.where(Like.point_id == point_alias.id)
.options(
selectinload(
UserQuestion.answer,
Answer.points.of_type(point_alias),
point_alias.analysises,
Analysis.event,
Event.original_article,
),
selectinload(
UserQuestion.answer,
Answer.points.of_type(point_alias),
point_alias.analysises,
Analysis.category,
),
selectinload(
UserQuestion.answer,
Answer.points.of_type(point_alias),
point_alias.analysises,
Analysis.likes,
),
)
)
return user_questions

Expand All @@ -52,6 +78,7 @@ def get_user_question(
.join(Analysis.event)
.join(Event.original_article)
.join(Analysis.category)
.join(Analysis.likes)
)
if not user_question:
raise HTTPException(HTTPStatus.NOT_FOUND)
Expand Down
13 changes: 12 additions & 1 deletion backend/src/user_questions/schemas.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from pydantic import BaseModel, ConfigDict
from pydantic import BaseModel, ConfigDict, model_validator
from src.events.schemas import MiniEventDTO
from src.likes.schemas import LikeDTO


class AnalysisDTO(BaseModel):
model_config = ConfigDict(from_attributes=True)
id: int
content: str
event: MiniEventDTO
likes: list[LikeDTO]


class PointMiniDTO(BaseModel):
Expand All @@ -16,6 +18,15 @@ class PointMiniDTO(BaseModel):
body: str
analysises: list[AnalysisDTO]

@model_validator(mode="after")
def filter(self):
# i gave up on using the orm to filter the ones relevant to the point
for analysis in self.analysises:
analysis.likes = [
like for like in analysis.likes if like.point_id == self.id
]
return self


class AnswerDTO(BaseModel):
model_config = ConfigDict(from_attributes=True)
Expand Down

0 comments on commit 99ab969

Please sign in to comment.