Esempio n. 1
0
        private void UpdateLabourStaffs(string[] selectedOptions, LabourDepartment departmentToUpdate)
        {
            if (selectedOptions == null)
            {
                departmentToUpdate.LabourStaffs = new List <LabourStaff>();
                return;
            }

            var selectedOptionsHS = new HashSet <string>(selectedOptions);
            var labourStaffs      = new HashSet <int>(departmentToUpdate.LabourStaffs.Select(b => b.StaffID));

            foreach (var s in _context.Staffs)
            {
                if (selectedOptionsHS.Contains(s.ID.ToString()))
                {
                    if (!labourStaffs.Contains(s.ID))
                    {
                        departmentToUpdate.LabourStaffs.Add(new LabourStaff
                        {
                            StaffID           = s.ID,
                            LabourDeparmentID = departmentToUpdate.ID
                        });
                    }
                }
                else
                {
                    if (labourStaffs.Contains(s.ID))
                    {
                        LabourStaff specToRemove = departmentToUpdate.LabourStaffs.SingleOrDefault(d => d.StaffID == s.ID);
                        _context.Remove(specToRemove);
                    }
                }
            }
        }
Esempio n. 2
0
        private void PopulateAssignedStaffData(LabourDepartment department)
        {
            var allStaff  = _context.Staffs;
            var depStaff  = new HashSet <int>(department.LabourStaffs.Select(b => b.StaffID));
            var selected  = new List <OptionVM>();
            var available = new List <OptionVM>();

            foreach (var s in allStaff)
            {
                if (depStaff.Contains(s.ID))
                {
                    selected.Add(new OptionVM
                    {
                        ID          = s.ID,
                        DisplayText = s.Name
                    });
                }
                else
                {
                    available.Add(new OptionVM
                    {
                        ID          = s.ID,
                        DisplayText = s.Name
                    });
                }
            }

            ViewData["selOpts"]   = new MultiSelectList(selected.OrderBy(s => s.DisplayText), "ID", "DisplayText");
            ViewData["availOpts"] = new MultiSelectList(available.OrderBy(s => s.DisplayText), "ID", "DisplayText");
        }
Esempio n. 3
0
        // GET: LabourDepartments/Create
        public IActionResult Create()
        {
            LabourDepartment labourDepartment = new LabourDepartment();

            PopulateAssignedStaffData(labourDepartment);
            return(View());
        }
Esempio n. 4
0
        // GET: LabourDepartments/Create
        public IActionResult Create()
        {
            LabourDepartment labourDepartment = new LabourDepartment();

            PopulateAssignedStaffData(labourDepartment);
            ViewData["ProductionID"] = new SelectList(_context.Productions, "ID", "ID");
            return(View());
        }
Esempio n. 5
0
        public async Task <IActionResult> Create([Bind("ID,Name,DepartmentDescription")] LabourDepartment labourDepartment, string[] selectedOptions)
        {
            try
            {
                UpdateLabourStaffs(selectedOptions, labourDepartment);
                if (ModelState.IsValid)
                {
                    _context.Add(labourDepartment);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction(nameof(Index)));
                }
            }
            catch (DbUpdateException)
            {
                ModelState.AddModelError("", "Something went wrong in the database.");
            }
            PopulateAssignedStaffData(labourDepartment);
            return(View(labourDepartment));
        }
Esempio n. 6
0
        public async Task <IActionResult> Edit(int id, [Bind("ID,Name,DepartmentDescription,ProductionID")] LabourDepartment labourDepartment, string[] selectedOptions)
        {
            var departmentToUpdate = await _context.LabourDepartments
                                     .Include(d => d.LabourStaffs).ThenInclude(d => d.Staff)
                                     .SingleOrDefaultAsync(d => d.ID == id);

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

            UpdateLabourStaffs(selectedOptions, departmentToUpdate);

            if (await TryUpdateModelAsync <LabourDepartment>(departmentToUpdate, "", d => d.Name, d => d.DepartmentDescription))
            {
                try
                {
                    _context.Update(departmentToUpdate);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!LabourDepartmentExists(departmentToUpdate.ID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            PopulateAssignedStaffData(departmentToUpdate);
            ViewData["ProductionID"] = new SelectList(_context.Productions, "ID", "ID", labourDepartment.ProductionID);
            return(View(departmentToUpdate));
        }