From 6f9e06a43748e8f620f53a23d709e676199c6a56 Mon Sep 17 00:00:00 2001 From: Zimovchik <63729114+Zimovchik@users.noreply.github.com> Date: Sat, 3 May 2025 11:01:49 +0300 Subject: [PATCH 1/4] added filter by subject --- rating_api/models/db.py | 8 ++++++-- rating_api/routes/comment.py | 2 ++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/rating_api/models/db.py b/rating_api/models/db.py index 28b75fa..4eaf996 100644 --- a/rating_api/models/db.py +++ b/rating_api/models/db.py @@ -137,14 +137,18 @@ def order_by_mark(self, query: str, asc_order: bool) -> UnaryExpression[float] | def search_by_lectorer_id(self, query: int) -> bool: if not query: return true() - return and_(Comment.review_status == ReviewStatus.APPROVED, Comment.lecturer_id == query) + return Comment.lecturer_id == query @hybrid_method def search_by_user_id(self, query: int) -> bool: if not query: return true() return Comment.user_id == query - + @hybrid_method + def search_by_subject(self, query: str) -> bool: + if not query: + return true() + return func.lower(Comment.subject).contains(query) class LecturerUserComment(BaseDbModel): id: Mapped[int] = mapped_column(Integer, primary_key=True) diff --git a/rating_api/routes/comment.py b/rating_api/routes/comment.py index 787113a..529398c 100644 --- a/rating_api/routes/comment.py +++ b/rating_api/routes/comment.py @@ -173,6 +173,7 @@ async def get_comments( offset: int = 0, lecturer_id: int | None = None, user_id: int | None = None, + subject: str | None = None, order_by: str = Query( enum=["create_ts", "mark_kindness", "mark_freebie", "mark_clarity", "mark_general"], default="create_ts", @@ -206,6 +207,7 @@ async def get_comments( Comment.query(session=db.session) .filter(Comment.search_by_lectorer_id(lecturer_id)) .filter(Comment.search_by_user_id(user_id)) + .filter(Comment.search_by_subject(subject)) .order_by( Comment.order_by_mark(order_by, asc_order) if "mark" in order_by From 7735a6488fa49245900197e78779edf08926381e Mon Sep 17 00:00:00 2001 From: Zimovchik <63729114+Zimovchik@users.noreply.github.com> Date: Sat, 3 May 2025 11:02:04 +0300 Subject: [PATCH 2/4] lint --- rating_api/models/db.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/rating_api/models/db.py b/rating_api/models/db.py index 4eaf996..1ee6954 100644 --- a/rating_api/models/db.py +++ b/rating_api/models/db.py @@ -144,12 +144,14 @@ def search_by_user_id(self, query: int) -> bool: if not query: return true() return Comment.user_id == query + @hybrid_method def search_by_subject(self, query: str) -> bool: if not query: return true() return func.lower(Comment.subject).contains(query) + class LecturerUserComment(BaseDbModel): id: Mapped[int] = mapped_column(Integer, primary_key=True) user_id: Mapped[int] = mapped_column(Integer, nullable=False) From 5121f96f84f0e9fd19953acb1fa41494d33fe9c0 Mon Sep 17 00:00:00 2001 From: Zimovchik <63729114+Zimovchik@users.noreply.github.com> Date: Sat, 3 May 2025 11:07:01 +0300 Subject: [PATCH 3/4] fix --- rating_api/models/db.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rating_api/models/db.py b/rating_api/models/db.py index 1ee6954..24d2ad4 100644 --- a/rating_api/models/db.py +++ b/rating_api/models/db.py @@ -149,7 +149,7 @@ def search_by_user_id(self, query: int) -> bool: def search_by_subject(self, query: str) -> bool: if not query: return true() - return func.lower(Comment.subject).contains(query) + return func.lower(Comment.subject).contains(querylower()) class LecturerUserComment(BaseDbModel): From efd2d8674404bb0e6b782a22fb03d3ebfd4ec4b6 Mon Sep 17 00:00:00 2001 From: Zimovchik <63729114+Zimovchik@users.noreply.github.com> Date: Sat, 3 May 2025 11:07:33 +0300 Subject: [PATCH 4/4] fix --- rating_api/models/db.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rating_api/models/db.py b/rating_api/models/db.py index 24d2ad4..99d46d6 100644 --- a/rating_api/models/db.py +++ b/rating_api/models/db.py @@ -149,7 +149,7 @@ def search_by_user_id(self, query: int) -> bool: def search_by_subject(self, query: str) -> bool: if not query: return true() - return func.lower(Comment.subject).contains(querylower()) + return func.lower(Comment.subject).contains(query.lower()) class LecturerUserComment(BaseDbModel):