-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservices.py
More file actions
100 lines (81 loc) · 3.42 KB
/
services.py
File metadata and controls
100 lines (81 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
from requests import Session
from modal_backend.exceptions import AlreadyExists, ObjectNotFound
from modal_backend.models.db import Group, ModalStatus, Note, NoteType, Service
from modal_backend.schemas.models import NoteTypePost, NotificationPost
class NoteService:
"""
Сервис для работы с логикой Notifications и базой данных
"""
@classmethod
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,
):
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))
)
if status == 'active':
notes_query = notes_query.filter(Note.status == "active")
if status == 'archived':
notes_query = notes_query.filter(Note.status == "archived")
notes_query = notes_query.order_by(Note.order_by_start_ts("start_ts", asc_order))
notes = notes_query.limit(limit).offset(offset).all()
if not notes:
raise ObjectNotFound(Note, 'all')
return notes
@staticmethod
def validate_note_by_type(note: dict, db: Session):
type_id = note.type_id
note_type = NoteType.query(session=db.session).filter(NoteType.type_id == type_id).one_or_none()
if note_type is None:
raise ObjectNotFound(NoteType, type_id)
@classmethod
async def create_note(cls, db: Session, note: NotificationPost, admin_id: int) -> Note:
cls.validate_note_by_type(note, db)
new_note = Note.create(session=db.session, **note, admin_id=admin_id, status=ModalStatus.ACTIVE)
return new_note
class NoteTypeService:
"""
Сервис для работы с логикой NoteType и базой данных
"""
@classmethod
async def create_note_type(cls, db: Session, note_type: NoteTypePost) -> NoteType:
data = note_type.model_dump()
type_id = data.get("type_id")
note_types = NoteType.query(session=db.session).filter(NoteType.type_id == type_id).first()
if note_types:
raise AlreadyExists(NoteType, type_id)
new_note_type = NoteType.create(session=db.session, **data)
return new_note_type
class ServiceManager:
"""
Сервис для работы с логикой Service и базой данных
"""
@classmethod
async def create_service(cls, db: Session, service_id: int, name: str):
service = Service.query(session=db.session).filter(Service.service_id == service_id).first()
if service:
raise AlreadyExists(Service, service_id)
new_service = Service.create(session=db.session, service_id=service_id, name=name)
return new_service
class GroupService:
"""
Сервис для работы с логикой Group и базой данных
"""
@classmethod
async def create_group(cls, db: Session, group_id: int, name: str):
group = Group.query(session=db.session).filter(Group.group_id == group_id).first()
if group:
raise AlreadyExists(Group, group_id)
new_group = Group.create(session=db.session, group_id=group_id, name=name)
return new_group