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