Example #1
0
        // GET: ProductKits/Checkout/5
        public async Task <IActionResult> CheckIn(int?customerId)
        {
            if (customerId == null)
            {
                return(NotFound());
            }

            var customer = await _context.Customer.SingleOrDefaultAsync(m => m.Id == customerId);

            if (customer == null)
            {
                return(NotFound());
            }

            //if i want accurate identifiers here i can like loop through all the isntances of a given product and
            //event for each product they have checked out, incrementing an identifier up for each one and then assigning
            //that identifer to this model instnace name once i get to it in the instances incrementing for loop

            var model = new CheckIn_ViewModel();

            model.CustomerId   = customer.Id;
            model.CustomerName = customer.FirstName + " " + customer.LastName;
            var productInstance_CustomerEntries = _context.ProductInstance_Customer.Where(pic => pic.CustomerId == customer.Id).ToList();
            var productInstanceIds = new List <int>();

            foreach (var productInstanceCustomerEntry in productInstance_CustomerEntries)
            {
                productInstanceIds.Add(productInstanceCustomerEntry.ProductInstanceId);
            }
            model.productInstances = _context.ProductInstance.Where(pi => productInstanceIds.Contains(pi.Id) && pi.CheckedOut == true).ToList();//only show ones that are checked out ot be checked in

            return(View(model));
        }
Example #2
0
        public async Task <IActionResult> CheckIn(CheckIn_ViewModel model)
        {
            if (model.productInstances == null)
            {
                return(RedirectToAction("Details", "Customers", new { id = model.CustomerId }));
            }

            //first clear out all the instance_customer entries with this instances id
            var customer_ProductInstances_ForRemoval = new List <ProductInstance_Customer>();

            for (int i = 0; i < model.productInstances.Count(); i++)
            {
                var CPIForRemoval = _context.ProductInstance_Customer.FirstOrDefault(pic => pic.ProductInstanceId == model.productInstances[i].Id);//i could find all productinstance_customer entries all at once with just the customer id
                if (CPIForRemoval != null)
                {
                    customer_ProductInstances_ForRemoval.Add(CPIForRemoval);
                }

                var instance = _context.ProductInstance.FirstOrDefault(pi => pi.Id == model.productInstances[i].Id);
                if (instance != null)
                {
                    instance.CheckedOut = model.productInstances[i].CheckedOut;
                }
                _context.Update(instance);

                //yeah I'm adding all of the product_instance_customer entries back in
                //because on checkin you dont get rid of those you just change their
                //respective instance checkins based on selection
                _context.ProductInstance_Customer.Add(new ProductInstance_Customer
                {
                    ProductInstanceId = instance.Id,
                    CustomerId        = model.CustomerId
                });

                await _context.SaveChangesAsync();
            }

            if (customer_ProductInstances_ForRemoval.Count != 0)
            {
                _context.ProductInstance_Customer.RemoveRange(customer_ProductInstances_ForRemoval);//this is removing all the old customer_productInstances (but not before adding the new ones
            }
            await _context.SaveChangesAsync();



            if (ModelState.IsValid)//this should probably be higher up on the page...
            {
                return(RedirectToAction("Details", "Customers", new { id = model.CustomerId }));
            }
            return(View(model));
        }