Skip to content

Commit

Permalink
Merge pull request #36 from profcomff/fix-total-lecturer-num
Browse files Browse the repository at this point in the history
Fix lecturer total
  • Loading branch information
Temmmmmo authored Nov 10, 2024
2 parents ec4816d + 08cb036 commit 90afb9c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from alembic import op



revision = 'fee34ac4fcab'
down_revision = '0fbda260a023'
branch_labels = None
Expand Down
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 90afb9c

Please sign in to comment.