Beispiel #1
0
        public BaseResultViewModel <string> UpdateUserDepartments(UserDepartmentsViewModel model, string userName)
        {
            model.UserDepartments = model.UserDepartments ?? new int[0];

            try
            {
                var userDeptList = _repository.GetContext().UserDepartments
                                   .Where(p => p.ApplicationUserId == model.UserId && !p.IsArchived);

                var userDepts = userDeptList.Select(p => p.Department.DepartmentId);

                var newDepts     = model.UserDepartments.Except(userDepts).ToList();
                var removedDepts = userDepts.Except(model.UserDepartments);

                foreach (var deptId in newDepts)
                {
                    var dept = new UserDepartment()
                    {
                        ApplicationUserId = model.UserId,
                        DepartmentId      = deptId
                    };

                    dept.SetCreateDetails(userName);
                    _repository.SaveNew(dept);
                }

                foreach (var dept in userDeptList.Where(p => removedDepts.Any(rd => rd == p.DepartmentId)).ToList())
                {
                    dept.SetArchiveDetails(userName);
                    _repository.SaveExisting(dept);
                }
            }
            catch (Exception e)
            {
                return(new BaseResultViewModel <string>()
                {
                    IsSuccess = false, Message = "User departments update failed."
                });
            }

            return(new BaseResultViewModel <string>()
            {
                IsSuccess = true, Message = "User departments updated successfully."
            });
        }
Beispiel #2
0
        public UserDepartmentsViewModel CreateUserDepartmentsViewModel(int id, int trustId)
        {
            var userId = GetUserIdByTempId(id);
            var model  = new UserDepartmentsViewModel()
            {
                UserId = userId
            };

            var userDepts = _repository.GetContext().UserDepartments
                            .Where(p => p.ApplicationUserId == userId && !p.IsArchived).Select(p => p.Department.DepartmentId);

            model.DepartmentList = _repository.GetAll <Department>().Where(p => p.TrustId == trustId && !p.IsArchived).Select(dept =>
                                                                                                                              new SelectListItem {
                Value = dept.DepartmentId.ToString(), Text = dept.DepartmentName, Selected = userDepts.Any(ud => ud == dept.DepartmentId)
            }).ToList();

            return(model);
        }
        public IActionResult UpdateUserDepartments(UserDepartmentsViewModel model)
        {
            var response = _userService.UpdateUserDepartments(model, CurrentUserName);

            return(Json(response));
        }