Beispiel #1
0
        public async Task <bool> UpdateTruckInventory(TruckInventory truckInventory)
        {
            if (truckInventory == null)
            {
                _logger.LogError("Truck to update is null");
                throw new ArgumentNullException(nameof(truckInventory));
            }

            var truckToUpdate = await _context.Trucks.SingleOrDefaultAsync(t => t.TruckId == truckInventory.TruckId);

            if (truckToUpdate == null)
            {
                _logger.LogError("Truck to update doesn't exist in the db");
                throw new ArgumentNullException(nameof(truckInventory));
            }

            if (truckToUpdate.Quantity - truckInventory.Quantity < 0)
            {
                truckToUpdate.Quantity = 0;
                // throw new exception should not be less than zero but ill ignore for now.
            }
            else
            {
                truckToUpdate.Quantity -= truckInventory.Quantity;
            }
            truckToUpdate.OutOfStock = truckToUpdate.Quantity == 0;
            return(await SaveChanges());
        }
Beispiel #2
0
        public ActionResult DeleteConfirmed(int id)
        {
            TruckInventory truckInventory = db.TruckInventory.Find(id);

            db.TruckInventory.Remove(truckInventory);
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
Beispiel #3
0
 public ActionResult Edit([Bind(Include = "ID,Quantity,InventoryID,TruckID")] TruckInventory truckInventory)
 {
     if (ModelState.IsValid)
     {
         db.Entry(truckInventory).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     ViewBag.InventoryID = new SelectList(db.Inventory, "ID", "Name", truckInventory.InventoryID);
     ViewBag.TruckID     = new SelectList(db.Truck, "ID", "Name", truckInventory.TruckID);
     return(View(truckInventory));
 }
Beispiel #4
0
        // GET: TruckInventories/Details/5
        public ActionResult Details(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            TruckInventory truckInventory = db.TruckInventory.Find(id);

            if (truckInventory == null)
            {
                return(HttpNotFound());
            }
            return(View(truckInventory));
        }
Beispiel #5
0
        // GET: TruckInventories/Edit/5
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            TruckInventory truckInventory = db.TruckInventory.Find(id);

            if (truckInventory == null)
            {
                return(HttpNotFound());
            }
            ViewBag.InventoryID = new SelectList(db.Inventory, "ID", "Name", truckInventory.InventoryID);
            ViewBag.TruckID     = new SelectList(db.Truck, "ID", "Name", truckInventory.TruckID);
            return(View(truckInventory));
        }
Beispiel #6
0
        public async Task Consume(ConsumeContext <InventoryToUpdate> context)
        {
            if (context == null)
            {
                _logger.LogWarning("Failed to consume Inventory update as context is null");
                return;
            }
            TruckInventory tid = _mapper.Map <TruckInventory>(context.Message);

            if (await _service.UpdateTruckInventory(tid))
            {
                TruckInventory newDetails = await _service.GetTruckInventory(tid.TruckId);

                var eventMessage = new UpdatedInventory();
                eventMessage.TruckId = newDetails.TruckId;
                // eventMessage.TruckName = newDetails.TruckName;
                eventMessage.NewQuantity = newDetails.Quantity;
                _logger.LogInformation($"Updated Catalog. New qty for {eventMessage.TruckId} is {eventMessage.NewQuantity}.  Name is {eventMessage.TruckName}");
                await _publishEndpoint.Publish(eventMessage);
            }
        }