Example #1
0
        public ProductActionResultModel Deactivate(long id, [FromBody] DeactivateProductModel 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 = product.UpdatedOn = DateTimeOffset.Now;
            product.DeletedBy = product.UpdatedBy = this.GetUserName();
            product.Version   = Guid.NewGuid();

            _ctx.SaveChanges();

            return(new ProductActionResultModel {
                Version = product.Version
            });
        }
        public IActionResult Deactivate(long id, [FromBody] DeactivateProductModel 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 = product.UpdatedOn = DateTimeOffset.Now;
                product.DeletedBy = 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
                })
            }));
        }