Beispiel #1
0
        public IActionResult Put(int id, [FromBody] Product data, [FromServices] UpdateProductValidation validator)
        {
            var product = InMemmoryDatabase.Products.FirstOrDefault(x => x.Id == id);

            if (product == null)
            {
                return(NotFound());
            }

            var validate = validator.Validate(data);

            if (!validate.IsValid)
            {
                return(UnprocessableEntity(validate.Errors.ToList().Select(x => new
                {
                    PropertyName = x.PropertyName,
                    Error = x.ErrorMessage
                })));
            }

            product.Name     = data.Name;
            product.Price    = data.Price;
            product.Category = InMemmoryDatabase.Categories.AsQueryable().FirstOrDefault(x => x.Id == data.CategoryId);

            return(NoContent());
        }
Beispiel #2
0
        public UpdateProductCommand(int productId, int categoryId, string name, decimal price)
        {
            CategoryId = categoryId;
            Name       = name;
            Price      = price;
            ProductId  = productId;

            // Validação do comando
            var validator = new UpdateProductValidation()
                            .ValidateName()
                            .ValidateProduct()
                            .ValidateCategory()
                            .ValidatePrice()
                            .Validate(this);

            IsValid = validator.IsValid;
            Errors  = validator.Errors;
        }
 public override bool IsValid()
 {
     ValidationResult = new UpdateProductValidation().Validate(this);
     return(ValidationResult.IsValid);
 }