| | 1 | | namespace Syki.Back.Features.Academic.CreateCourseCurriculum; |
| | 2 | |
|
| 227 | 3 | | public class CreateCourseCurriculumService(SykiDbContext ctx) : IAcademicService |
| | 4 | | { |
| | 5 | | public async Task<OneOf<CourseCurriculumOut, SykiError>> Create(Guid institutionId, CreateCourseCurriculumIn data) |
| | 6 | | { |
| 227 | 7 | | var courseOk = await ctx.Courses |
| 227 | 8 | | .AnyAsync(c => c.InstitutionId == institutionId && c.Id == data.CourseId); |
| 229 | 9 | | if (!courseOk) return new CourseNotFound(); |
| | 10 | |
|
| 225 | 11 | | var disciplines = await ctx.CoursesDisciplines.AsNoTracking() |
| 225 | 12 | | .Where(x => x.CourseId == data.CourseId) |
| 225 | 13 | | .Select(x => x.DisciplineId) |
| 225 | 14 | | .ToListAsync(); |
| | 15 | |
|
| 1992 | 16 | | if (!data.Disciplines.ConvertAll(d => d.Id).IsSubsetOf(disciplines)) |
| 4 | 17 | | return new InvalidDisciplinesList(); |
| | 18 | |
|
| 221 | 19 | | var courseCurriculum = new CourseCurriculum(institutionId, data.CourseId, data.Name); |
| | 20 | |
|
| 221 | 21 | | data.Disciplines.ForEach(d => |
| 1758 | 22 | | courseCurriculum.Links.Add(new(d.Id, d.Period, d.Credits, d.Workload)) |
| 221 | 23 | | ); |
| | 24 | |
|
| 221 | 25 | | ctx.Add(courseCurriculum); |
| 221 | 26 | | await ctx.SaveChangesAsync(); |
| | 27 | |
|
| 221 | 28 | | courseCurriculum = await ctx.CourseCurriculums.AsNoTracking() |
| 221 | 29 | | .Include(g => g.Course) |
| 221 | 30 | | .Include(x => x.Disciplines) |
| 221 | 31 | | .Include(g => g.Links) |
| 221 | 32 | | .FirstAsync(x => x.Id == courseCurriculum.Id); |
| | 33 | |
|
| 221 | 34 | | return courseCurriculum.ToOut(); |
| 227 | 35 | | } |
| | 36 | | } |