Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion modal_backend/models/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
ForeignKey,
Integer,
String,
cast,
or_,
true,
)
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.ext.hybrid import hybrid_method
from sqlalchemy.orm import Mapped, mapped_column

Expand Down Expand Up @@ -79,10 +82,32 @@ class Note(BaseDbModel):

@hybrid_method
def search_by_type_id(self, query: int) -> bool:
if not self.query:
if query is None:
return true()
return Note.type_id == query

@hybrid_method
def search_by_group_ids(self, query: list[int]) -> bool:
if not query:
return true()
group_ids_jsonb = cast(Note.group_ids, JSONB)
return or_(*(group_ids_jsonb.contains([qid]) for qid in query))

@hybrid_method
def search_by_service_ids(self, query: list[int]) -> bool:
if not query:
return true()
service_ids_jsonb = cast(Note.service_ids, JSONB)
return or_(*(service_ids_jsonb.contains([qid]) for qid in query))

@hybrid_method
def search_by_status(self, status: Optional[str] = None) -> bool:
if status == "active":
return Note.status == "active"
elif status == "archived":
return Note.status == "archived"
Comment thread
ArtemBratyashin marked this conversation as resolved.
return True


class NoteResponse(BaseDbModel):
__tablename__ = "note_response"
Expand Down
28 changes: 24 additions & 4 deletions modal_backend/routes/notes.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@

@note.get("", response_model=list[NoteGet])
async def get_notes(
limit: int = 10,
offset: int = 0,
type_id: int = Query(None),
groups_id: list[int] = Query(None),
services_id: list[int] = Query(None),
Expand All @@ -40,15 +38,37 @@ async def get_notes(
default=None,
),
asc_order: bool = False,
limit: int = 10,
Comment thread
ArtemBratyashin marked this conversation as resolved.
offset: int = 0,
user=Depends(UnionAuth()),
) -> list[NoteGet]:
"""
Получить список модалок по type_id.
Получить список модалок по фильтрам

`limit` - максимальное количество возвращаемых модалок

`offset` - смещение, определяющее, с какой по порядку модалки начинать выборку.
Если без смещения возвращается модалка с условным номером N,
то при значении offset = X будет возвращаться модалка с номером N + X

`status` - возможные значения `"active", "archived"`.
Если передано `'active'` - возвращается список активных модалок
Если передано `'archived'` - возвращается список архивированных модалок

`type_id` - вернет все модалки конкретного типа по type_id типа

`groups_id` - вернет все модалки с конкретным groups_id

`services_id` - вернет все модалки сервиса по id

`asc_order` -Если передано true, сортировать в порядке возрастания. Иначе - в порядке убывания

В случае несуществующего type_id ошибка ObjectNotFound
"""

notes = await NoteService.get_note_by_type_id(db, type_id, limit, offset, groups_id, services_id, status, asc_order)
notes = await NoteService.get_notes_by_filters(
db, limit, offset, asc_order, type_id, groups_id, services_id, status
)
return [NoteGet.model_validate(note) for note in notes]


Expand Down
28 changes: 21 additions & 7 deletions modal_backend/utils/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,33 @@ class NoteService:
"""

@classmethod
async def get_note_by_type_id(
async def get_notes_by_filters(
cls,
db: Session,
type_id: int,
limit: int,
offset: int,
groups_id: list[int],
services_id: list[int],
status: str,
asc_order: bool,
type_id: int = None,
groups_id: list[int] = None,
services_id: list[int] = None,
status: str = None,
):
# add filter logic
notes = Note.query(session=db.session).filter(Note.search_by_type_id(type_id)).limit(limit).offset(offset).all()
notes_query = (
Note.query(session=db.session)
.filter(Note.search_by_type_id(type_id))
.filter(Note.search_by_group_ids(groups_id))
.filter(Note.search_by_service_ids(services_id))
.filter(Note.search_by_status(status))
)

order_expr = Note.start_ts.asc() if asc_order else Note.start_ts.desc()
notes_query = notes_query.order_by(order_expr)

notes = notes_query.limit(limit).offset(offset).all()

if not notes:
raise ObjectNotFound(Note, 'all')

return notes

@staticmethod
Expand Down
Loading