| | 1 | | namespace Syki.Back.Features.Teacher.CreateLessonAttendance; |
| | 2 | |
|
| 206 | 3 | | public class CreateLessonAttendanceService(SykiDbContext ctx) : ITeacherService |
| | 4 | | { |
| | 5 | | public async Task<OneOf<SykiSuccess, SykiError>> Create(Guid teacherId, Guid lessonId, CreateLessonAttendanceIn data |
| | 6 | | { |
| 206 | 7 | | var lesson = await ctx.Lessons.Include(l => l.Attendances).FirstOrDefaultAsync(x => x.Id == lessonId); |
| 208 | 8 | | if (lesson == null) return new LessonNotFound(); |
| | 9 | |
|
| 204 | 10 | | var @class = await ctx.Classes |
| 204 | 11 | | .Include(x => x.Students) |
| 204 | 12 | | .FirstOrDefaultAsync(x => x.Id == lesson.ClassId && x.TeacherId == teacherId); |
| 206 | 13 | | if (@class == null) return new ClassNotFound(); |
| | 14 | |
|
| 400 | 15 | | var allStudents = @class.Students.Select(x => x.Id).ToList(); |
| 202 | 16 | | if (!data.PresentStudents.IsSubsetOf(allStudents)) |
| 2 | 17 | | return new InvalidStudentsList(); |
| | 18 | |
|
| 200 | 19 | | var attendances = lesson.Attendances; |
| 796 | 20 | | foreach (var studentId in allStudents) |
| | 21 | | { |
| 198 | 22 | | var present = data.PresentStudents.Contains(studentId); |
| 198 | 23 | | var attendance = attendances.FirstOrDefault(a => a.StudentId == studentId); |
| 198 | 24 | | if (attendance != null) |
| | 25 | | { |
| 0 | 26 | | attendance.Update(present); |
| | 27 | | } |
| | 28 | | else |
| | 29 | | { |
| 198 | 30 | | attendance = new ClassLessonAttendance(@class.Id, lesson.Id, studentId, present); |
| 198 | 31 | | ctx.Add(attendance); |
| | 32 | | } |
| | 33 | | } |
| | 34 | |
|
| 200 | 35 | | lesson.Finish(); |
| | 36 | |
|
| 200 | 37 | | await ctx.SaveChangesAsync(); |
| | 38 | |
|
| 200 | 39 | | return new SykiSuccess(); |
| 206 | 40 | | } |
| | 41 | |
|
| | 42 | | public async Task CreateWithThrowOnError(Guid teacherId, Guid lessonId, CreateLessonAttendanceIn data) |
| | 43 | | { |
| 0 | 44 | | (await Create(teacherId, lessonId, data)).ThrowOnError(); |
| 0 | 45 | | } |
| | 46 | | } |