-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreate_booking_usecase.py
More file actions
84 lines (63 loc) · 2.67 KB
/
create_booking_usecase.py
File metadata and controls
84 lines (63 loc) · 2.67 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
from datetime import timedelta
import uuid
from typing import List
from src.shared.domain.entities.booking import Booking
from src.shared.domain.enums.sport import SPORT
from src.shared.domain.enums.type import BOOKING_TYPE
from src.shared.domain.repositories.booking_repository_interface import IBookingRepository
from src.shared.helpers.errors.usecase_errors import DuplicatedItem, InvalidSchedule, InvalidSchedulePeriod
class CreateBookingUsecase:
start_date: int
end_date: int
court_number: int
sport: SPORT
user_id: str
booking_id: str
materials: List[str]
booking_type: BOOKING_TYPE
def __init__(self, repo: IBookingRepository):
self.repo = repo
def __call__(self,
start_date: int,
end_date: int,
court_number: int,
sport: str,
user_id: str,
materials: List[str],
booking_type: str
) -> Booking:
booking_id = str(uuid.uuid4())
if self.repo.get_booking(booking_id):
raise DuplicatedItem("Booking already exists")
try:
self.sport = SPORT(sport)
except ValueError:
raise ValueError("Invalid sport enum value")
for material in materials:
if not isinstance(material, str):
raise ValueError("Invalid material type")
if booking_type not in [type.value for type in BOOKING_TYPE]:
raise ValueError("Invalid type enum value")
self.booking_type = BOOKING_TYPE(booking_type)
maxtime = start_date + (timedelta(weeks=12).total_seconds()*1000)
if(end_date > maxtime):
raise InvalidSchedulePeriod()
all_bookings = self.repo.get_all_bookings()
for booking in all_bookings:
if (
(
booking.start_date < end_date + (15 * 60 * 1000) #15 minutes in mseconds
and booking.end_date > start_date - (15 * 60 * 1000) #15 minutes in mseconds
and booking.court_number == court_number
)
):
raise InvalidSchedule()
resp = self.repo.create_booking(Booking(start_date,
end_date,
court_number,
self.sport,
user_id,
booking_id,
materials,
self.booking_type))
return resp