| | | 1 | | using Syki.Back.Domain.CourseOfferings; |
| | | 2 | | |
| | | 3 | | namespace Syki.Back.Features.CourseOfferings.CreateCourseOffering; |
| | | 4 | | |
| | 2 | 5 | | public class CreateCourseOfferingService(SykiDbContext ctx) : ISykiService |
| | | 6 | | { |
| | | 7 | | private class Validator : AbstractValidator<CreateCourseOfferingIn> |
| | | 8 | | { |
| | 2 | 9 | | public Validator() |
| | | 10 | | { |
| | 2 | 11 | | RuleFor(x => x.CourseSession).NotNull().WithError(InvalidCourseSession.I); |
| | 2 | 12 | | RuleFor(x => x.CourseSession).IsInEnum().WithError(InvalidCourseSession.I); |
| | 2 | 13 | | } |
| | | 14 | | } |
| | 2 | 15 | | private static readonly Validator V = new(); |
| | | 16 | | |
| | | 17 | | public async Task<OneOf<CreateCourseOfferingOut, SykiError>> Create(CreateCourseOfferingIn data) |
| | | 18 | | { |
| | 2 | 19 | | if (V.Run(data, out var error)) return error; |
| | | 20 | | |
| | 2 | 21 | | var institutionId = ctx.RequestUser.InstitutionId; |
| | | 22 | | |
| | 2 | 23 | | var campusOk = await ctx.Campi.AnyAsync(x => x.Id == data.CampusId && x.InstitutionId == institutionId); |
| | 2 | 24 | | if (!campusOk) return CampusNotFound.I; |
| | | 25 | | |
| | 2 | 26 | | var courseOk = await ctx.Courses.AnyAsync(x => x.Id == data.CourseId && x.InstitutionId == institutionId); |
| | 2 | 27 | | if (!courseOk) return CourseNotFound.I; |
| | | 28 | | |
| | 2 | 29 | | var courseCurriculumOk = await ctx.CourseCurriculums.AnyAsync(x => x.Id == data.CourseCurriculumId && x.CourseId |
| | 2 | 30 | | if (!courseCurriculumOk) return CourseCurriculumNotFound.I; |
| | | 31 | | |
| | 2 | 32 | | var academicPeriodOk = await ctx.AcademicPeriods.AnyAsync(x => x.Id == data.AcademicPeriodId && x.InstitutionId |
| | 2 | 33 | | if (!academicPeriodOk) return AcademicPeriodNotFound.I; |
| | | 34 | | |
| | 2 | 35 | | var courseOffering = new CourseOffering( |
| | 2 | 36 | | institutionId, |
| | 2 | 37 | | data.CampusId, |
| | 2 | 38 | | data.CourseId, |
| | 2 | 39 | | data.CourseCurriculumId, |
| | 2 | 40 | | data.AcademicPeriodId, |
| | 2 | 41 | | data.CourseSession!.Value |
| | 2 | 42 | | ); |
| | | 43 | | |
| | 2 | 44 | | await ctx.SaveChangesAsync(courseOffering); |
| | | 45 | | |
| | 2 | 46 | | return new CreateCourseOfferingOut { Id = courseOffering.Id }; |
| | 2 | 47 | | } |
| | | 48 | | } |