private InventoryApiContext SetUpContext(IEnumerable <Items> items)
        {
            //set up the mock context for new tests
            var dbCOB = new DbContextOptionsBuilder <InventoryApiContext>();

            //use a different database for testing than the default in memory database
            dbCOB.UseInMemoryDatabase("ItemControllerTestDB");

            var mockContext = new InventoryApiContext(dbCOB.Options);

            //delete any existing test items
            mockContext.Items.RemoveRange(mockContext.Items);
            mockContext.SaveChanges();
            //if items were sent as a parameter, add them to the database
            if (!Object.ReferenceEquals(items, null))
            {
                mockContext.Items.AddRange(items);
            }
            mockContext.SaveChanges();

            return(mockContext);
        }
Example #2
0
        //DELETE: api/items/Label
        public IActionResult Delete(string Label)
        {
            //TODO: test async/await for performance with large datasets and heavy traffic
            var items = _context.Items.Where(i => i.Label.Equals(Label));

            if (items.Count() == 1)
            {
                var item = items.Single();

                _context.Items.Remove(item);
                _context.SaveChanges();

                var stMessage = "Item " + Label + " removed from inventory at " + DateTime.Now;
                _logger.LogInformation(stMessage);
                //Item is to be deleted, remove its timer
                _itemMonitor.RemoveScheduledExpiration(item);

                return(Ok());
            }
            else
            {
                return(NotFound());
            }
        }