| | 1 | | namespace Syki.Back.Features.Academic.CreateClass; |
| | 2 | |
|
| 608 | 3 | | public class CreateClassService(SykiDbContext ctx) : IAcademicService |
| | 4 | | { |
| | 5 | | public async Task<OneOf<ClassOut, SykiError>> Create(Guid institutionId, CreateClassIn data) |
| | 6 | | { |
| 608 | 7 | | var disciplineOk = await ctx.Disciplines.AnyAsync(x => x.InstitutionId == institutionId && x.Id == data.Discipli |
| 610 | 8 | | if (!disciplineOk) return new DisciplineNotFound(); |
| | 9 | |
|
| 606 | 10 | | var teacherOk = await ctx.Teachers.AnyAsync(p => p.InstitutionId == institutionId && p.Id == data.TeacherId); |
| 608 | 11 | | if (!teacherOk) return new TeacherNotFound(); |
| | 12 | |
|
| 604 | 13 | | var periodExists = await ctx.AcademicPeriodExists(institutionId, data.Period); |
| 606 | 14 | | if (!periodExists) return new AcademicPeriodNotFound(); |
| | 15 | |
|
| 1212 | 16 | | var schedules = data.Schedules.ConvertAll(h => Schedule.New(h.Day, h.Start, h.End)); |
| 2416 | 17 | | foreach (var schedule in schedules) |
| | 18 | | { |
| 612 | 19 | | if (schedule.IsError) return schedule.Error; |
| | 20 | | } |
| | 21 | |
|
| 598 | 22 | | var result = Class.New( |
| 598 | 23 | | institutionId, |
| 598 | 24 | | data.DisciplineId, |
| 598 | 25 | | data.TeacherId, |
| 598 | 26 | | data.Period, |
| 598 | 27 | | data.Vacancies, |
| 602 | 28 | | schedules.ConvertAll(x => x.Success) |
| 598 | 29 | | ); |
| | 30 | |
|
| 600 | 31 | | if (result.IsError) return result.Error; |
| | 32 | |
|
| 596 | 33 | | var @class = result.Success; |
| | 34 | |
|
| 596 | 35 | | ctx.Add(@class); |
| 596 | 36 | | await ctx.SaveChangesAsync(); |
| | 37 | |
|
| 596 | 38 | | @class = await ctx.Classes |
| 596 | 39 | | .Include(c => c.Period) |
| 596 | 40 | | .Include(t => t.Lessons) |
| 596 | 41 | | .Include(t => t.Schedules) |
| 596 | 42 | | .FirstAsync(x => x.Id == @class.Id); |
| | 43 | |
|
| 596 | 44 | | @class.CreateLessons(); |
| 596 | 45 | | await ctx.SaveChangesAsync(); |
| | 46 | |
|
| 596 | 47 | | @class = await ctx.Classes.AsNoTracking() |
| 596 | 48 | | .Include(t => t.Discipline) |
| 596 | 49 | | .Include(t => t.Teacher) |
| 596 | 50 | | .Include(t => t.Schedules) |
| 596 | 51 | | .Include(t => t.Lessons) |
| 596 | 52 | | .ThenInclude(l => l.Attendances) |
| 596 | 53 | | .FirstAsync(x => x.Id == @class.Id); |
| | 54 | |
|
| 596 | 55 | | return @class.ToOut(); |
| 608 | 56 | | } |
| | 57 | |
|
| | 58 | | public async Task CreateWithThrowOnError(Guid institutionId, CreateClassIn data) |
| | 59 | | { |
| 0 | 60 | | (await Create(institutionId, data)).ThrowOnError(); |
| 0 | 61 | | } |
| | 62 | | } |