コード例 #1
0
        public async Task <IActionResult> PutPurchaseStatusType([FromBody] PurchaseStatusType purchaseStatusType)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            if (purchaseStatusType.Id <= 0)
            {
                return(BadRequest("please provide a valid id"));
            }
            if (_context.PurchaseStatusType.Any(pst => pst.Name.Equals(purchaseStatusType.Name) && pst.Id != purchaseStatusType.Id))
            {
                return(BadRequest("This purchase status type: " + purchaseStatusType.Name + " already exists in the database"));
            }

            _context.Entry(purchaseStatusType).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!PurchaseStatusTypeExists(purchaseStatusType.Id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(Ok(_context.PurchaseStatusType.Find(purchaseStatusType.Id)));
        }
コード例 #2
0
        public async Task <IActionResult> PutInventory([FromBody] Inventory inventory)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            //validate
            if (inventory.Quantity < 0)
            {
                return(BadRequest("Can't assign value lower than 0 to inventory quantity"));
            }
            if (!_context.Employees.Any(e => e.Id == inventory.Employee))
            {
                return(BadRequest("Must provide a valid Employee Id in order to update inventory"));
            }
            if (!_context.Inventory.Any(i => i.Item == inventory.Item))
            {
                return(BadRequest("This item doesn't exist in the inventory, you could add it."));
            }

            Inventory inventoryUpdate = _context.Inventory.AsNoTracking().Single(i => i.Item == inventory.Item);

            //assign the object for updating
            inventory.LastUpdated = DateTime.Now;
            inventory.Id          = inventoryUpdate.Id;

            //update
            _context.Entry(inventory).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!InventoryExists(inventoryUpdate.Id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(Ok(inventory));
        }
コード例 #3
0
        public async Task <IActionResult> PutItemType([FromBody] ItemType itemType)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            if (itemType.Id <= 0)
            {
                return(BadRequest("please provide a valid id"));
            }
            if (_context.ItemType.Any(e => e.Name.Equals(itemType.Name) && e.Id != itemType.Id))
            {
                return(BadRequest("This Item Type: " + itemType.Name + " already exists"));
            }
            if (string.IsNullOrEmpty(itemType.Name))
            {
                return(BadRequest("Please provide a valid item name"));
            }

            if (itemType.ReturnPeriod < 0)
            {
                itemType.ReturnPeriod = 0;
            }

            _context.Entry(itemType).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ItemTypeExists(itemType.Id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(Ok(_context.ItemType.Find(itemType.Id)));
        }
コード例 #4
0
        public async Task <IActionResult> PutItems([FromBody] Items items)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (!_context.Items.Any(i => i.Id == items.Id))
            {
                return(BadRequest("Item id doesn't match any existing item in the database"));
            }
            if (!_context.ItemType.Any(e => e.Id == items.ItemType))
            {
                return(BadRequest("No Such Item Type"));
            }
            if (items.Price < 0)
            {
                return(BadRequest("Item price can't be a negative number"));
            }
            if (string.IsNullOrEmpty(items.Name))
            {
                return(BadRequest("Please provide a valid item name"));
            }
            _context.Entry(items).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ItemsExists(items.Id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(Ok(_context.Items.Find(items.Id)));
        }
コード例 #5
0
        public async Task <IActionResult> PutEmployees([FromBody] Employees employees)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (!_context.Employees.Any(e1 => e1.Id == employees.Id))
            {
                return(BadRequest("Employee doesn't exist"));
            }
            if (string.IsNullOrEmpty(employees.FirstName) || string.IsNullOrEmpty(employees.LastName))
            {
                return(BadRequest("must provide valid first name and last name for the employee"));
            }

            Employees e = _context.Employees.AsNoTracking().SingleOrDefault(ee => ee.Id == employees.Id);

            employees.StartDate = e.StartDate;

            _context.Entry(employees).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!EmployeesExists(employees.Id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(Ok(_context.Employees.Find(employees.Id)));
        }
コード例 #6
0
        public async Task <IActionResult> PutPaymentMethod([FromBody] PaymentMethod paymentMethod)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (!_context.PaymentMethod.Any(pm => pm.Id == paymentMethod.Id))
            {
                return(BadRequest("Payment method id: " + paymentMethod.Id + " doesn't exist"));
            }
            if (string.IsNullOrEmpty(paymentMethod.Name))
            {
                return(BadRequest("Payment method name can't be empty"));
            }
            if (_context.PaymentMethod.Any(pm => pm.Name.Equals(paymentMethod.Name) && pm.Id != paymentMethod.Id))
            {
                return(BadRequest("Payment method: " + paymentMethod.Name + " already exists"));
            }

            _context.Entry(paymentMethod).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!PaymentMethodExists(paymentMethod.Id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(Ok(_context.PaymentMethod.Find(paymentMethod.Id)));
        }