/// <exception cref="InvalidModelException"/> /// <exception cref="NotFoundException"/> public async Task <Guid> AddAsync(Dtos.EducationLevel dto) { var validator = new Dtos.EducationLevelValidator(); 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); } var id = Guid.NewGuid(); var now = DateTime.UtcNow; var educationLevel = new Entities.EducationLevel { Id = id, CreatedAt = now, UpdatedAt = now, Name = dto.Name, Number = dto.Number }; await _context.AddAsync(educationLevel); await _context.SaveChangesAsync(); return(id); }
/// <exception cref="InvalidModelException"/> /// <exception cref="NotFoundException"/> public async Task UpdateAsync(Dtos.EducationLevel dto) { if (dto.Id.Equals(Guid.Empty)) { throw new InvalidModelException("Property Id failed validation. Error was: Id is empty"); } var validator = new Dtos.EducationLevelValidator(); 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); } var educationLevel = await _context.FindAsync <Entities.EducationLevel>(dto.Id); if (educationLevel == null) { throw new NotFoundException($"Entity with id: {dto.Id} not found."); } educationLevel.Name = dto.Name; educationLevel.Number = dto.Number; educationLevel.UpdatedAt = DateTime.UtcNow; await _context.SaveChangesAsync(); }