| | 1 | | namespace Syki.Back.Features.Academic.CreateAcademicPeriod; |
| | 2 | |
|
| | 3 | | public class AcademicPeriod |
| | 4 | | { |
| 480 | 5 | | public string Id { get; } |
| 6 | 6 | | public Guid InstitutionId { get; } |
| 897 | 7 | | public DateOnly StartAt { get; private set; } |
| 31066 | 8 | | public DateOnly EndAt { get; private set; } |
| | 9 | |
|
| 449 | 10 | | public AcademicPeriod( |
| 449 | 11 | | string id, |
| 449 | 12 | | Guid institutionId, |
| 449 | 13 | | DateOnly startAt, |
| 449 | 14 | | DateOnly endAt |
| 449 | 15 | | ) { |
| 449 | 16 | | Id = id; |
| 449 | 17 | | InstitutionId = institutionId; |
| 449 | 18 | | StartAt = startAt; |
| 449 | 19 | | EndAt = endAt; |
| 449 | 20 | | } |
| | 21 | |
|
| | 22 | | public static OneOf<AcademicPeriod, SykiError> New( |
| | 23 | | string id, |
| | 24 | | Guid institutionId, |
| | 25 | | DateOnly startAt, |
| | 26 | | DateOnly endAt |
| | 27 | | ) { |
| 282 | 28 | | var result = Validate(id, startAt, endAt); |
| | 29 | |
|
| 313 | 30 | | if (result.IsError()) return result.GetError(); |
| | 31 | |
|
| 251 | 32 | | return new AcademicPeriod(result.GetSuccess(), institutionId, startAt, endAt); |
| | 33 | | } |
| | 34 | |
|
| | 35 | | private static OneOf<string, SykiError> Validate( |
| | 36 | | string id, |
| | 37 | | DateOnly startAt, |
| | 38 | | DateOnly endAt |
| | 39 | | ) { |
| 282 | 40 | | var numbers = id.OnlyNumbers(); |
| | 41 | |
|
| 282 | 42 | | if (numbers.Length != 5) |
| 14 | 43 | | return new InvalidAcademicPeriod(); |
| | 44 | |
|
| 268 | 45 | | var year = int.Parse(numbers.Substring(0, 4)); |
| 268 | 46 | | var digit = int.Parse(numbers.Substring(4, 1)); |
| | 47 | |
|
| 268 | 48 | | if (year < 1970 || year > 2070) |
| 2 | 49 | | return new InvalidAcademicPeriod(); |
| | 50 | |
|
| 266 | 51 | | if (digit < 1 || digit > 2) |
| 8 | 52 | | return new InvalidAcademicPeriod(); |
| | 53 | |
|
| 258 | 54 | | if (startAt.Year != year) |
| 2 | 55 | | return new InvalidAcademicPeriodStartDate(); |
| | 56 | |
|
| 256 | 57 | | if (endAt.Year != year) |
| 2 | 58 | | return new InvalidAcademicPeriodEndDate(); |
| | 59 | |
|
| 254 | 60 | | if (startAt >= endAt) |
| 3 | 61 | | return new InvalidAcademicPeriodDates(); |
| | 62 | |
|
| 251 | 63 | | return $"{year}.{digit}"; |
| | 64 | | } |
| | 65 | |
|
| | 66 | | public AcademicPeriodOut ToOut() |
| | 67 | | { |
| 237 | 68 | | return new() |
| 237 | 69 | | { |
| 237 | 70 | | Id = Id, |
| 237 | 71 | | StartAt = StartAt, |
| 237 | 72 | | EndAt = EndAt, |
| 237 | 73 | | }; |
| | 74 | | } |
| | 75 | |
|
| | 76 | | public static implicit operator AcademicPeriod(OneOf<AcademicPeriod, SykiError> value) |
| | 77 | | { |
| 9 | 78 | | return value.GetSuccess(); |
| | 79 | | } |
| | 80 | | } |