コード例 #1
0
        public async Task <IActionResult> UpdateProductManufacturer(int id, [FromBody] ProductManufacturerSaveResource saveResource)
        {
            if (!_auth.IsAppAdmin(User))
            {
                return(NoContent());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var productManufacturerFromRepo = await _repo.GetProductManufacturer(id);

            if (productManufacturerFromRepo == null)
            {
                return(BadRequest($"Product Manufacturer {id} could not be found."));
            }

            var filter = new MdaProductManufacturerQuery()
            {
                Name   = saveResource.Name,
                Active = Convert.ToByte(saveResource.Active == true ? 1 : 0)
            };

            var productManufacturersFromRepoExisting = await _repo.GetProductManufacturers(filter);

            if (productManufacturersFromRepoExisting.Any())
            {
                var existingProductManufacturer = productManufacturersFromRepoExisting.FirstOrDefault();
                if (existingProductManufacturer.Id != id)
                {
                    return(BadRequest($"Product Manufacturer {saveResource.Name} already exists."));
                }
                else
                {
                    if (existingProductManufacturer.Name == saveResource.Name)
                    {
                        return(BadRequest("Nothing was changed."));
                    }
                }
            }

            _mapper.Map <ProductManufacturerSaveResource, MdaProductManufacturer>(saveResource, productManufacturerFromRepo);
            productManufacturerFromRepo.ModifiedBy   = User.Identity.Name;
            productManufacturerFromRepo.ModifiedDate = DateTime.Now;

            if (await _repo.SaveAll())
            {
                return(NoContent());
            }

            return(BadRequest("Failed to update Product Manufacturer."));
        }
コード例 #2
0
        public async Task <IActionResult> AddProductManufacturer([FromBody] ProductManufacturerSaveResource saveResource)
        {
            if (!_auth.IsAppAdmin(User))
            {
                return(NoContent());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var filter = new MdaProductManufacturerQuery()
            {
                Name = saveResource.Name
            };

            var productManufacturersFromRepo = await _repo.GetProductManufacturers(filter);

            if (productManufacturersFromRepo.Any())
            {
                return(BadRequest($"Product Manufacturer {saveResource.Name} already exists."));
            }

            var productManufacturer = _mapper.Map <MdaProductManufacturer>(saveResource);

            productManufacturer.CreatedBy = User.Identity.Name;

            _repo.Add(productManufacturer);

            if (await _repo.SaveAll())
            {
                return(Ok(productManufacturer));
            }

            return(BadRequest("Failed to add product manufacturer."));
        }