public IResult Add(Color color) { var result = colorValidator.Validate(color); if (result.IsValid == true) { _colorDal.Add(color); return(new SuccessfulResult()); } return(new ErrorResult()); }
private void CheckColor(string color, string paramName) { if (!_colorValidator.Validate(color, out var message)) { throw new ArgumentException(string.Format(Resource.ExceptionColorInvalid, message), paramName); } }
public IResult Add(Color color) { var context = new ValidationContext <Color>(color); ColorValidator colorValidator = new ColorValidator(); var result = colorValidator.Validate(context); if (!result.IsValid) { throw new ValidationException(result.Errors); } _colorDal.Add(color); return(new SuccessResult(Messages.Added)); }
public async Task ChangePropertyAsync(long id, TimelineChangePropertyParams newProperties) { if (newProperties is null) { throw new ArgumentNullException(nameof(newProperties)); } if (newProperties.Name is not null) { CheckTimelineName(newProperties.Name, nameof(newProperties)); } if (newProperties.Color is not null) { var(result, message) = _colorValidator.Validate(newProperties.Color); if (!result) { throw new ArgumentException(message, nameof(newProperties)); } } var entity = await _database.Timelines.Where(t => t.Id == id).SingleOrDefaultAsync(); if (entity is null) { throw CreateTimelineNotExistException(id); } var changed = false; var nameChanged = false; if (newProperties.Name is not null) { var conflict = await _database.Timelines.AnyAsync(t => t.Name == newProperties.Name); if (conflict) { throw CreateTimelineConflictException(newProperties.Name); } entity.Name = newProperties.Name; changed = true; nameChanged = true; } if (newProperties.Title != null) { changed = true; entity.Title = newProperties.Title; } if (newProperties.Description != null) { changed = true; entity.Description = newProperties.Description; } if (newProperties.Visibility.HasValue) { changed = true; entity.Visibility = newProperties.Visibility.Value; } if (newProperties.Color is not null) { changed = true; entity.Color = newProperties.Color; } if (changed) { var currentTime = _clock.GetCurrentTime(); entity.LastModified = currentTime; if (nameChanged) { entity.NameLastModified = currentTime; } } await _database.SaveChangesAsync(); _logger.LogInformation(Resource.LogTimelineUpdated, id); }