public IActionResult AddEquipmentToBundle(EquipmentBundleDetails viewModel)
        {
            var bundle = viewModel.EquipmentBundle;

            var unpreparedEquipmentIds = Request.Form["equipmentListItems"];

            var bundleInDb = _context.EquipmentBundles.Include(b => b.EquipmentsInBundles).ThenInclude(c => c.Equipment).Single(b => b.Id == bundle.Id);

            if (bundleInDb == null)
            {
                return(RedirectToAction(nameof(Details), new { id = bundle.Id }));
            }

            foreach (var equipmentId in unpreparedEquipmentIds)
            {
                var equipment = _context.Equipments.Single(e => e.Id == Convert.ToInt32(equipmentId));

                if (equipment == null)
                {
                    return(RedirectToAction(nameof(Details), new { id = bundle.Id }));
                }

                bundleInDb.EquipmentsInBundles.Add(new EquipmentsInBundles {
                    Equipment = equipment, EquipmentBundle = bundleInDb
                });
            }

            bundleInDb.UpdatedOn = DateTime.Now;

            _context.SaveChanges();

            return(RedirectToAction(nameof(Details), new { id = bundle.Id }));
        }
        public IActionResult Details(int id)
        {
            var bundle = _context.EquipmentBundles
                         .Include(m => m.EquipmentsInBundles).ThenInclude(e => e.Equipment)
                         .Include(b => b.Notifications)
                         .FirstOrDefault(b => b.Id == id);

            if (bundle == null)
            {
                return(RedirectToAction("Index"));
            }

            var equipmentIds = bundle.EquipmentsInBundles.Select(eb => eb.EquipmentId);

            var availableEquipments = _context.Equipments
                                      .Where(e => !equipmentIds.Contains(e.Id) && e.Status == EquipmentStatus.Public)
                                      .ToList();

            var model = new EquipmentBundleDetails
            {
                EquipmentBundle     = bundle,
                AvailableEquipments = availableEquipments
            };

            return(View(model));
        }
Example #3
0
        public ActionResult Details(int id)
        {
            var bundle = _context.EquipmentBundles.Include(x => x.Equipments).SingleOrDefault(x => x.Id == id);
            var availableEquipments = _context.Equipments
                                      .Where(x => x.IsAvailable)
                                      .OrderBy(x => x.Title)
                                      .ToList();

            if (bundle == null)
            {
                return(HttpNotFound());
            }

            foreach (var bundleEquipment in bundle.Equipments)
            {
                availableEquipments.Remove(bundleEquipment);
            }

            var viewModel = new EquipmentBundleDetails
            {
                EquipmentBundle = bundle,
                Equipments      = availableEquipments,
            };

            return(View(viewModel));
        }