| | 1 | | namespace Syki.Back.Features.Teacher.CreateLessonAttendance; |
| | 2 | |
|
| 342 | 3 | | public class CreateLessonAttendanceService(SykiDbContext ctx) : ITeacherService |
| | 4 | | { |
| | 5 | | public async Task<OneOf<SykiSuccess, SykiError>> Create(Guid teacherId, Guid lessonId, CreateLessonAttendanceIn data |
| | 6 | | { |
| 76 | 7 | | var lesson = await ctx.Lessons.Include(l => l.Attendances).FirstOrDefaultAsync(x => x.Id == lessonId); |
| 77 | 8 | | if (lesson == null) return new LessonNotFound(); |
| | 9 | |
|
| 76 | 10 | | if (lesson.Date > DateTime.Now.ToDateOnly()) return new InvalidLesson(); |
| | 11 | |
|
| 74 | 12 | | var @class = await ctx.Classes |
| 74 | 13 | | .Include(x => x.Students) |
| 74 | 14 | | .FirstOrDefaultAsync(x => x.Id == lesson.ClassId && x.TeacherId == teacherId); |
| 75 | 15 | | if (@class == null) return new LessonNotFound(); |
| | 16 | |
|
| 144 | 17 | | var allStudents = @class.Students.Select(x => x.Id).ToList(); |
| 73 | 18 | | if (!data.PresentStudents.IsSubsetOf(allStudents)) |
| 1 | 19 | | return new InvalidStudentsList(); |
| | 20 | |
|
| 72 | 21 | | var attendances = lesson.Attendances; |
| 286 | 22 | | foreach (var studentId in allStudents) |
| | 23 | | { |
| 71 | 24 | | var present = data.PresentStudents.Contains(studentId); |
| 71 | 25 | | var attendance = attendances.FirstOrDefault(a => a.StudentId == studentId); |
| 71 | 26 | | if (attendance != null) |
| | 27 | | { |
| 0 | 28 | | attendance.Update(present); |
| | 29 | | } |
| | 30 | | else |
| | 31 | | { |
| 71 | 32 | | attendance = new LessonAttendance(@class.Id, lesson.Id, studentId, present); |
| 71 | 33 | | ctx.Add(attendance); |
| | 34 | | } |
| | 35 | | } |
| | 36 | |
|
| 72 | 37 | | lesson.Finish(); |
| | 38 | |
|
| 72 | 39 | | await ctx.SaveChangesAsync(); |
| | 40 | |
|
| 72 | 41 | | return new SykiSuccess(); |
| 76 | 42 | | } |
| | 43 | | } |