/// <exception cref="InvalidModelException"/> /// <exception cref="NotFoundException"/> public async Task UpdateAsync(Dtos.RatingForDiscipline dto) { if (dto.Id.Equals(Guid.Empty)) { throw new InvalidModelException("Property Id failed validation. Error was: Id is empty"); } var validator = new Dtos.RatingForDisciplineValidator(); ValidationResult result = validator.Validate(dto); if (!result.IsValid) { string errMess = string.Empty; foreach (var failure in result.Errors) { errMess += $"Property { failure.PropertyName } failed validation. Error was: { failure.ErrorMessage }\n"; } throw new InvalidModelException(errMess); } if (!await _context.Set <Entities.AcademicDiscipline>().AnyAsync(d => d.Id == dto.AcademicDisciplineId)) { throw new InvalidModelException($"Academic discipline with id: {dto.AcademicDisciplineId} not found"); } if (!await _context.Set <Entities.AcademicGroup>().AnyAsync(d => d.Id == dto.AcademicGroupId)) { throw new InvalidModelException($"Academic group with id: {dto.AcademicGroupId} not found"); } if (!await _context.Set <Entities.ExamsGradesSpreadsheet>().AnyAsync(d => d.Id == dto.ExamsGradesSpreadsheetId)) { throw new InvalidModelException($"Exams grades spreadsheet with id: {dto.ExamsGradesSpreadsheetId} not found"); } if (!await _context.Set <Entities.Student>().AnyAsync(d => d.Id == dto.StudentId)) { throw new InvalidModelException($"Student with id: {dto.StudentId} not found"); } if (!await _context.Set <Entities.Teacher>().AnyAsync(d => d.Id == dto.TeacherId)) { throw new InvalidModelException($"Teacher with id: {dto.TeacherId} not found"); } var ratingForDiscipline = await _context.FindAsync <Entities.RatingForDiscipline>(dto.Id); if (ratingForDiscipline == null) { throw new NotFoundException($"Entity with id: {dto.Id} not found."); } ratingForDiscipline.UpdatedAt = DateTime.UtcNow; ratingForDiscipline.AcademicDisciplineId = dto.AcademicDisciplineId; ratingForDiscipline.AcademicGroupId = dto.AcademicGroupId; ratingForDiscipline.Date = dto.Date; ratingForDiscipline.ExamsGradesSpreadsheetId = dto.ExamsGradesSpreadsheetId; ratingForDiscipline.Score = dto.Score; ratingForDiscipline.StudentId = dto.StudentId; ratingForDiscipline.TeacherId = dto.TeacherId; await _context.SaveChangesAsync(); }
/// <exception cref="InvalidModelException"/> /// <exception cref="NotFoundException"/> public async Task <Guid> AddAsync(Dtos.RatingForDiscipline dto) { var validator = new Dtos.RatingForDisciplineValidator(); ValidationResult result = validator.Validate(dto); if (!result.IsValid) { string errMess = string.Empty; foreach (var failure in result.Errors) { errMess += $"Property { failure.PropertyName } failed validation. Error was: { failure.ErrorMessage }\n"; } throw new InvalidModelException(errMess); } if (!await _context.Set <Entities.AcademicDiscipline>().AnyAsync(d => d.Id == dto.AcademicDisciplineId)) { throw new InvalidModelException($"Academic discipline with id: {dto.AcademicDisciplineId} not found"); } if (!await _context.Set <Entities.AcademicGroup>().AnyAsync(d => d.Id == dto.AcademicGroupId)) { throw new InvalidModelException($"Academic group with id: {dto.AcademicGroupId} not found"); } if (!await _context.Set <Entities.ExamsGradesSpreadsheet>().AnyAsync(d => d.Id == dto.ExamsGradesSpreadsheetId)) { throw new InvalidModelException($"Exams grades spreadsheet with id: {dto.ExamsGradesSpreadsheetId} not found"); } if (!await _context.Set <Entities.Student>().AnyAsync(d => d.Id == dto.StudentId)) { throw new InvalidModelException($"Student with id: {dto.StudentId} not found"); } if (!await _context.Set <Entities.Teacher>().AnyAsync(d => d.Id == dto.TeacherId)) { throw new InvalidModelException($"Teacher with id: {dto.TeacherId} not found"); } var id = Guid.NewGuid(); var now = DateTime.UtcNow; var ratingForDiscipline = new Entities.RatingForDiscipline { Id = id, CreatedAt = now, UpdatedAt = now, AcademicDisciplineId = dto.AcademicDisciplineId, AcademicGroupId = dto.AcademicGroupId, Date = dto.Date, ExamsGradesSpreadsheetId = dto.ExamsGradesSpreadsheetId, Score = dto.Score, StudentId = dto.StudentId, TeacherId = dto.TeacherId }; await _context.AddAsync(ratingForDiscipline); await _context.SaveChangesAsync(); return(id); }