| | 1 | | namespace Syki.Back.Features.Academic.CreateClass; |
| | 2 | |
|
| | 3 | | public class Schedule |
| | 4 | | { |
| 255 | 5 | | public Guid Id { get; set; } |
| 0 | 6 | | public Guid ClassId { get; set; } |
| 31062 | 7 | | public Day Day { get; set; } |
| 9509 | 8 | | public Hour StartAt { get; set; } |
| 9243 | 9 | | public Hour EndAt { get; set; } |
| | 10 | |
|
| 446 | 11 | | private Schedule() {} |
| | 12 | |
|
| 254 | 13 | | public Schedule( |
| 254 | 14 | | Day day, |
| 254 | 15 | | Hour startAt, |
| 254 | 16 | | Hour endAt |
| 254 | 17 | | ) { |
| 254 | 18 | | Id = Guid.NewGuid(); |
| 254 | 19 | | Day = day; |
| 254 | 20 | | StartAt = startAt; |
| 254 | 21 | | EndAt = endAt; |
| 254 | 22 | | } |
| | 23 | |
|
| | 24 | | public static OneOf<Schedule, SykiError> New( |
| | 25 | | Day day, |
| | 26 | | Hour startAt, |
| | 27 | | Hour endAt |
| | 28 | | ) { |
| 209 | 29 | | if (!day.IsValid()) return new InvalidDay(); |
| 208 | 30 | | if (!startAt.IsValid()) return new InvalidHour(); |
| 207 | 31 | | if (!endAt.IsValid()) return new InvalidHour(); |
| | 32 | |
|
| 205 | 33 | | if (startAt == endAt || endAt < startAt) |
| 5 | 34 | | return new InvalidSchedule(); |
| | 35 | |
|
| 200 | 36 | | return new Schedule(day, startAt, endAt); |
| | 37 | | } |
| | 38 | |
|
| | 39 | | public int GetDiff() |
| | 40 | | { |
| 4320 | 41 | | return StartAt.DiffInMinutes(EndAt); |
| | 42 | | } |
| | 43 | |
|
| | 44 | | public bool Conflict(Schedule other) |
| | 45 | | { |
| 30 | 46 | | if (Day != other.Day) |
| 7 | 47 | | return false; |
| | 48 | |
|
| 23 | 49 | | if (StartAt == other.StartAt || EndAt == other.EndAt) |
| 2 | 50 | | return true; |
| | 51 | |
|
| 21 | 52 | | if (StartAt < other.StartAt && other.StartAt < EndAt) |
| 7 | 53 | | return true; |
| | 54 | |
|
| 14 | 55 | | if (StartAt < other.EndAt && other.EndAt < EndAt) |
| 1 | 56 | | return true; |
| | 57 | |
|
| 13 | 58 | | if (other.StartAt < StartAt && StartAt < other.EndAt) |
| 2 | 59 | | return true; |
| | 60 | |
|
| 11 | 61 | | if (other.StartAt < EndAt && EndAt < other.EndAt) |
| 0 | 62 | | return true; |
| | 63 | |
|
| 11 | 64 | | return false; |
| | 65 | | } |
| | 66 | |
|
| | 67 | | public override string ToString() |
| | 68 | | { |
| 10 | 69 | | return $"{Day.GetDescription()} {StartAt.GetDescription()}-{EndAt.GetDescription()}"; |
| | 70 | | } |
| | 71 | |
|
| | 72 | | public ScheduleOut ToOut() |
| | 73 | | { |
| 223 | 74 | | return new() |
| 223 | 75 | | { |
| 223 | 76 | | Day = Day, |
| 223 | 77 | | StartAt = StartAt, |
| 223 | 78 | | EndAt = EndAt, |
| 223 | 79 | | }; |
| | 80 | | } |
| | 81 | | } |