| | 1 | | namespace Syki.Back.Features.Academic.CreateCourseOffering; |
| | 2 | |
|
| 217 | 3 | | public class CreateCourseOfferingService(SykiDbContext ctx) : IAcademicService |
| | 4 | | { |
| | 5 | | public async Task<OneOf<CourseOfferingOut, SykiError>> Create(Guid institutionId, CreateCourseOfferingIn data) |
| | 6 | | { |
| 217 | 7 | | var campusOk = await ctx.Campi |
| 217 | 8 | | .AnyAsync(c => c.InstitutionId == institutionId && c.Id == data.CampusId); |
| 219 | 9 | | if (!campusOk) return new CampusNotFound(); |
| | 10 | |
|
| 215 | 11 | | var courseOk = await ctx.Courses |
| 215 | 12 | | .AnyAsync(c => c.InstitutionId == institutionId && c.Id == data.CourseId); |
| 217 | 13 | | if (!courseOk) return new CourseNotFound(); |
| | 14 | |
|
| 213 | 15 | | var courseCurriculumOk = await ctx.CourseCurriculums |
| 213 | 16 | | .AnyAsync(g => g.InstitutionId == institutionId && g.Id == data.CourseCurriculumId && g.CourseId == data.Cou |
| 215 | 17 | | if (!courseCurriculumOk) return new CourseCurriculumNotFound(); |
| | 18 | |
|
| 211 | 19 | | var periodExists = await ctx.AcademicPeriodExists(institutionId, data.Period); |
| 213 | 20 | | if (!periodExists) return new AcademicPeriodNotFound(); |
| | 21 | |
|
| 210 | 22 | | if (!data.Shift.IsValid()) return new InvalidShift(); |
| | 23 | |
|
| 208 | 24 | | var courseOffering = new CourseOffering( |
| 208 | 25 | | institutionId, |
| 208 | 26 | | data.CampusId, |
| 208 | 27 | | data.CourseId, |
| 208 | 28 | | data.CourseCurriculumId, |
| 208 | 29 | | data.Period!, |
| 208 | 30 | | data.Shift |
| 208 | 31 | | ); |
| | 32 | |
|
| 208 | 33 | | ctx.CourseOfferings.Add(courseOffering); |
| 208 | 34 | | await ctx.SaveChangesAsync(); |
| | 35 | |
|
| 208 | 36 | | courseOffering = await ctx.CourseOfferings.AsNoTracking() |
| 208 | 37 | | .Include(x => x.Campus) |
| 208 | 38 | | .Include(x => x.Course) |
| 208 | 39 | | .Include(x => x.CourseCurriculum) |
| 208 | 40 | | .FirstAsync(x => x.Id == courseOffering.Id); |
| | 41 | |
|
| 208 | 42 | | return courseOffering.ToOut(); |
| 217 | 43 | | } |
| | 44 | | } |