Esempio n. 1
0
        public async Task <IActionResult> UpdateProductCapacity(int id, [FromBody] ProductCapacitySaveResource saveResource)
        {
            if (!_auth.IsAppAdmin(User))
            {
                return(NoContent());
            }

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

            var productCapacityFromRepo = await _repo.GetProductCapacity(id);

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

            var filter = new MdaProductCapacityQuery()
            {
                Name           = saveResource.Name,
                ProductModelId = saveResource.ProductModelId,
                Active         = 2
            };

            var productCapacityFromRepoExisting = await _repo.GetProductCapacities(filter);

            if (productCapacityFromRepoExisting.Any())
            {
                var existingProductCapacity = productCapacityFromRepoExisting.FirstOrDefault();
                if (existingProductCapacity.Id != id)
                {
                    return(BadRequest($"Product Capacity {saveResource.Name} already exists for the specified model."));
                }
                else
                {
                    if (existingProductCapacity.Name.ToLower() == saveResource.Name.ToLower() &&
                        existingProductCapacity.ProductModelId == saveResource.ProductModelId)
                    {
                        if (existingProductCapacity.Active == Convert.ToByte(saveResource.Active == true ? 1 : 0))
                        {
                            return(BadRequest("Nothing has changed."));
                        }
                    }
                }
            }

            _mapper.Map <ProductCapacitySaveResource, MdaProductCapacity>(saveResource, productCapacityFromRepo);
            productCapacityFromRepo.ModifiedBy   = User.Identity.Name;
            productCapacityFromRepo.ModifiedDate = DateTime.Now;

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

            return(BadRequest("Failed to update Product Capacity"));
        }
Esempio n. 2
0
        public async Task <IActionResult> AddProductCapacity([FromBody] ProductCapacitySaveResource saveResource)
        {
            if (!_auth.IsAppAdmin(User))
            {
                return(NoContent());
            }

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

            var filter = new MdaProductCapacityQuery()
            {
                Name           = saveResource.Name,
                ProductModelId = saveResource.ProductModelId
            };

            var productCapacityFromRepo = await _repo.GetProductCapacities(filter);

            if (productCapacityFromRepo.Any())
            {
                return(BadRequest($"Product Capacity {saveResource.Name} already exists for the specified model."));
            }

            var productCapacity = _mapper.Map <MdaProductCapacity>(saveResource);

            productCapacity.CreatedBy = User.Identity.Name;

            _repo.Add(productCapacity);

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

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