Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Temmmmmo committed Nov 10, 2024
2 parents 7bc64ca + 2da78e9 commit 634b765
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
12 changes: 10 additions & 2 deletions rating_api/models/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from sqlalchemy import UUID, DateTime
from sqlalchemy import Enum as DbEnum
from sqlalchemy import ForeignKey, Integer, String, and_, or_, true
from sqlalchemy import ForeignKey, Integer, String, and_, func, or_, true
from sqlalchemy.ext.hybrid import hybrid_method
from sqlalchemy.orm import Mapped, mapped_column, relationship

Expand Down Expand Up @@ -36,7 +36,7 @@ class Lecturer(BaseDbModel):
comments: Mapped[list[Comment]] = relationship("Comment", back_populates="lecturer")

@hybrid_method
def search(self, query: str) -> bool:
def search_by_name(self, query: str) -> bool:
response = true
query = query.split(' ')
for q in query:
Expand All @@ -45,6 +45,14 @@ def search(self, query: str) -> bool:
)
return response

@hybrid_method
def search_by_subject(self, query: str) -> bool:
query = query.lower()
response = true
if query:
response = and_(Comment.review_status == ReviewStatus.APPROVED, func.lower(Comment.subject).contains(query))
return response


class Comment(BaseDbModel):
uuid: Mapped[uuid.UUID] = mapped_column(UUID, primary_key=True, default=uuid.uuid4)
Expand Down
11 changes: 5 additions & 6 deletions rating_api/routes/lecturer.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,11 @@ async def get_lecturers(
`name`
Поле для ФИО. Если передано `name` - возвращает всех преподователей, для которых нашлись совпадения с переданной строкой
"""
lecturers = Lecturer.query(session=db.session).filter(Lecturer.search(name)).all()
lecturers_query = Lecturer.query(session=db.session)
if subject:
lecturers_query = lecturers_query.join(Lecturer.comments).filter(Lecturer.search_by_subject(subject))
lecturers_query = lecturers_query.filter(Lecturer.search_by_name(name))
lecturers = lecturers_query.all()
if not lecturers:
raise ObjectNotFound(Lecturer, 'all')
result = LecturerGetAll(limit=limit, offset=offset, total=len(lecturers))
Expand Down Expand Up @@ -137,11 +141,6 @@ async def get_lecturers(
result.lecturers.append(lecturer_to_result)
if "general" in order_by:
result.lecturers.sort(key=lambda item: (item.mark_general is None, item.mark_general))
if subject:
result.lecturers = [
lecturer for lecturer in result.lecturers if lecturer.subjects and subject in lecturer.subjects
]
result.total = len(result.lecturers)
return result


Expand Down

0 comments on commit 634b765

Please sign in to comment.