コード例 #1
0
        public IActionResult ManageInventory(InventoryViewModel inventoryViewModel)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(new { success = false, errors = ModelState.Values.Where(i => i.Errors.Count > 0) }));
            }

            if (inventoryViewModel.Id == null || inventoryViewModel.Id == Guid.Empty)
            {
                //Add new
                var inventoryId = _inventoryService.AddNewInventory(inventoryViewModel);

                if (inventoryId == null)
                {
                    ModelState.AddModelError("error", "Sorry! Something went wrong!");
                    return(BadRequest(new { success = false, errors = ModelState.Values.Where(i => i.Errors.Count > 0) }));
                }

                //return new id here
                return(Ok(new { success = true, inventoryId = inventoryId }));
            }
            else
            {
                //Edit existing
                var result = _inventoryService.EditInventory(inventoryViewModel);
                if (result == false)
                {
                    ModelState.AddModelError("error", "Sorry! Something went wrong!");
                    return(BadRequest(new { success = false, errors = ModelState.Values.Where(i => i.Errors.Count > 0) }));
                }

                //return new id here
                return(Ok(new { success = true, inventoryId = inventoryViewModel.Id }));
            }
        }