Skip to content

Commit

Permalink
Fix lecturer total
Browse files Browse the repository at this point in the history
  • Loading branch information
DaymasS committed Nov 10, 2024
1 parent 3d32b8a commit f0a6fd5
Show file tree
Hide file tree
Showing 2 changed files with 17 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) == query)
return response


class Comment(BaseDbModel):
uuid: Mapped[uuid.UUID] = mapped_column(UUID, primary_key=True, default=uuid.uuid4)
Expand Down
13 changes: 7 additions & 6 deletions rating_api/routes/lecturer.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,13 @@ async def get_lecturers(
`name`
Поле для ФИО. Если передано `name` - возвращает всех преподователей, для которых нашлись совпадения с переданной строкой
"""
lecturers = Lecturer.query(session=db.session).filter(Lecturer.search(name)).all()
lecturers = (
Lecturer.query(session=db.session)
.join(Lecturer.comments)
.filter(Lecturer.search_by_name(name))
.filter(Lecturer.search_by_subject(subject))
.all()
)
if not lecturers:
raise ObjectNotFound(Lecturer, 'all')
result = LecturerGetAll(limit=limit, offset=offset, total=len(lecturers))
Expand Down Expand Up @@ -137,11 +143,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 f0a6fd5

Please sign in to comment.