public IActionResult Create([FromBody] CreateProductModel model)
        {
            if (ModelState.IsValid)
            {
                var productsSet = _ctx.Set <ProductEntity>();

                if (productsSet.Any(e => e.Code.Equals(model.Code.Trim(), StringComparison.InvariantCultureIgnoreCase)))
                {
                    return(Conflict(new {
                        Message = "Código duplicado"
                    }));
                }

                if (productsSet.Any(e => e.Name.Equals(model.Name.Trim(), StringComparison.InvariantCultureIgnoreCase)))
                {
                    return(Conflict(new {
                        Message = "Nome duplicado"
                    }));
                }

                var now      = DateTimeOffset.Now;
                var username = GetUserName();

                var product = new ProductEntity {
                    Code        = model.Code,
                    Name        = model.Name,
                    Description = model.Description,
                    Price       = model.Price,
                    CreatedOn   = now,
                    CreatedBy   = username,
                    UpdatedOn   = now,
                    UpdatedBy   = username,
                    Version     = Guid.NewGuid()
                };
                productsSet.Add(product);

                _ctx.SaveChanges();

                return(Json(new CreateProductResultModel {
                    Id = product.Id
                }));
            }

            return(UnprocessableEntity(new {
                Message = "Entidade com dados inválidos",
                ModelState = ModelState.Select(e => new {
                    Key = e.Key,
                    Value = e.Value.Errors
                })
            }));
        }
예제 #2
0
        public CreateProductResultModel Create([FromBody] CreateProductModel model)
        {
            if (!ModelState.IsValid)
            {
                throw new ValidationException(ModelState);
            }

            var productsSet = _ctx.Set <ProductEntity>();

            if (productsSet.Any(e => e.Code.Equals(model.Code.Trim(), StringComparison.InvariantCultureIgnoreCase)))
            {
                throw new BusinessException("Código duplicado");
            }

            if (productsSet.Any(e => e.Name.Equals(model.Name.Trim(), StringComparison.InvariantCultureIgnoreCase)))
            {
                throw new BusinessException("Nome duplicado");
            }

            var now      = DateTimeOffset.Now;
            var username = this.GetUserName();

            var product = new ProductEntity {
                Code        = model.Code,
                Name        = model.Name,
                Description = model.Description,
                Price       = model.Price,
                CreatedOn   = now,
                CreatedBy   = username,
                UpdatedOn   = now,
                UpdatedBy   = username,
                Version     = Guid.NewGuid()
            };

            productsSet.Add(product);

            _ctx.SaveChanges();

            return(new CreateProductResultModel {
                Id = product.Id,
                Version = product.Version
            });
        }