public async Task RemoveStudentCourseGradeAsync(int gradeId, int teacherId)
        {
            var grade = await _gradesUnitOfWork.Repository.GetByIdAsync(gradeId).ConfigureAwait(false);

            if (grade == null)
            {
                throw new ResourceNotFoundException($"Оцінка {gradeId} не знайдена");
            }

            var finalGrade = await GetStudentSubjectFinalGradeAsync(grade.StudentRefId, grade.Gradebook.SemesterRefId)
                             .ConfigureAwait(false);

            if (finalGrade != null)
            {
                throw new ResourceOperationException($"Студент {grade.Student.Account.LastName} {grade.Student.Account.FirstName} ({grade.StudentRefId}) вже має кінцеву оцінку по предмету {grade.Gradebook.Subject.Name}");
            }

            var gradebook = await _gradebooksService.GetGradebookAsync(grade.GradebookRefId)
                            .ConfigureAwait(false);

            if (gradebook == null)
            {
                throw new ResourceNotFoundException($"Журнал {grade.GradebookRefId} не знайдено");
            }

            if (gradebook.Teachers.All(t => t.Id != teacherId))
            {
                throw new ResourceAccessPermissionException($"Викладач {teacherId} не має доступу до журналу {gradebook.Id}");
            }

            _gradesUnitOfWork.Repository.Delete(grade);

            await _gradesUnitOfWork.SaveChangesAsync().ConfigureAwait(false);
        }
Beispiel #2
0
        public async Task DeleteSubjectFromSemester(int subjectId, int groupId, int yearNumber, int semesterNumber)
        {
            var group = await _groupsService.GetGroupAsync(groupId).ConfigureAwait(false);

            if (group == null)
            {
                throw new ResourceNotFoundException($"Група {groupId} не знайдена");
            }

            var semester = await _groupSemestersService.GetGroupSemesterAsync(groupId, yearNumber, semesterNumber)
                           .ConfigureAwait(false);

            if (semester == null)
            {
                throw new ResourceNotFoundException($"Семестр {yearNumber}/{semesterNumber} для групи {group.Code} не знайдений");
            }

            var semesterSubject = (await _semesterScheduleUnitOfWork.Repository
                                   .GetAllAsync(s => s.SemesterRefId == semester.Id && s.SubjectRefId == subjectId)
                                   .ConfigureAwait(false)).FirstOrDefault();

            if (semesterSubject == null)
            {
                throw new ResourceNotFoundException($"Предмет {subjectId} у семестрі {semester.Id} не знайдено");
            }

            using (var transaction = await _semesterScheduleUnitOfWork.BeginTransactionAsync().ConfigureAwait(false))
            {
                var gradebook = await _gradebooksService.GetGradebookAsync(semester.Id, subjectId).ConfigureAwait(false);

                await _gradebooksService.RemoveGradebookAsync(gradebook.Id);

                _semesterScheduleUnitOfWork.Repository.Delete(semesterSubject);

                await _semesterScheduleUnitOfWork.SaveChangesAsync().ConfigureAwait(false);

                transaction.Commit();
            }
        }