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