| | 1 | | namespace Syki.Back.Features.Academic.CreateCourseOffering; |
| | 2 | |
|
| 528 | 3 | | public class CreateCourseOfferingService(SykiDbContext ctx, HybridCache cache) : IAcademicService |
| | 4 | | { |
| | 5 | | private class Validator : AbstractValidator<CreateCourseOfferingIn> |
| | 6 | | { |
| 2 | 7 | | public Validator() |
| | 8 | | { |
| 2 | 9 | | RuleFor(x => x.Shift).NotNull().WithError(InvalidShift.I); |
| 2 | 10 | | RuleFor(x => x.Shift).IsInEnum().WithError(InvalidShift.I); |
| 2 | 11 | | } |
| | 12 | | } |
| 2 | 13 | | private static readonly Validator V = new(); |
| | 14 | |
|
| | 15 | | public async Task<OneOf<CourseOfferingOut, SykiError>> Create(CreateCourseOfferingIn data) |
| | 16 | | { |
| 532 | 17 | | if (V.Run(data, out var error)) return error; |
| | 18 | |
|
| 528 | 19 | | if (await ctx.CampusNotFound(data.CampusId)) return CampusNotFound.I; |
| 524 | 20 | | if (await ctx.CourseNotFound(data.CourseId)) return CourseNotFound.I; |
| 520 | 21 | | if (await ctx.CourseCurriculumNotFound(data.CourseCurriculumId, data.CourseId)) return CourseCurriculumNotFound. |
| 516 | 22 | | if (await ctx.AcademicPeriodNotFound(data.Period)) return AcademicPeriodNotFound.I; |
| | 23 | |
|
| 508 | 24 | | var courseOffering = new CourseOffering( |
| 508 | 25 | | ctx.InstitutionId, |
| 508 | 26 | | data.CampusId, |
| 508 | 27 | | data.CourseId, |
| 508 | 28 | | data.CourseCurriculumId, |
| 508 | 29 | | data.Period!, |
| 508 | 30 | | data.Shift!.Value |
| 508 | 31 | | ); |
| | 32 | |
|
| 508 | 33 | | await ctx.SaveChangesAsync(courseOffering); |
| | 34 | |
|
| 508 | 35 | | await cache.RemoveAsync($"courseOfferings:{ctx.InstitutionId}"); |
| | 36 | |
|
| 508 | 37 | | courseOffering = await ctx.CourseOfferings.AsNoTracking() |
| 508 | 38 | | .Include(x => x.Campus) |
| 508 | 39 | | .Include(x => x.Course) |
| 508 | 40 | | .Include(x => x.CourseCurriculum) |
| 508 | 41 | | .FirstAsync(x => x.Id == courseOffering.Id); |
| | 42 | |
|
| 508 | 43 | | return courseOffering.ToOut(); |
| 528 | 44 | | } |
| | 45 | | } |