Esempio n. 1
0
        public ProductActionResultModel Activate(long id, [FromBody] ActivateProductModel model)
        {
            if (!ModelState.IsValid)
            {
                throw new ValidationException(ModelState);
            }

            var product = _ctx.Set <ProductEntity>().SingleOrDefault(e => e.Id == id);

            if (product == null)
            {
                throw new NotFoundException();
            }

            if (product.Version != model.Version)
            {
                throw new BusinessException("Entidade foi alterada por outro utilizador");
            }

            product.DeletedOn = null;
            product.DeletedBy = null;
            product.UpdatedOn = DateTimeOffset.Now;
            product.UpdatedBy = this.GetUserName();
            product.Version   = Guid.NewGuid();

            _ctx.SaveChanges();

            return(new ProductActionResultModel {
                Version = product.Version
            });
        }
        public IActionResult Activate(long id, [FromBody] ActivateProductModel model)
        {
            if (ModelState.IsValid)
            {
                var product = _ctx.Set <ProductEntity>().SingleOrDefault(e => e.Id == id);
                if (product == null)
                {
                    return(NotFound());
                }

                if (product.Version != model.Version)
                {
                    return(Conflict(new {
                        Message = "Entidade foi alterada por outro utilizador"
                    }));
                }

                product.DeletedOn = null;
                product.DeletedBy = null;
                product.UpdatedOn = DateTimeOffset.Now;
                product.UpdatedBy = GetUserName();
                product.Version   = Guid.NewGuid();

                _ctx.SaveChanges();

                return(Json(new ProductActionResultModel {
                    Version = product.Version
                }));
            }

            return(UnprocessableEntity(new {
                Message = "Entidade com dados inválidos",
                ModelState = ModelState.Select(e => new {
                    Key = e.Key,
                    Value = e.Value.Errors
                })
            }));
        }