/// <summary> /// Save user details /// </summary> /// <param name="model">user model</param> public ActionResult UserSave(MODEL.UserModel model) { try { if (ModelState.IsValid) { //Get user id int?userId = Session.GetUserId(); //UserVO userVO = new UserVO(model, userId); UserVO userVO = model.Transpose(userId); UserService userService = new UserService(); userService.SaveUser(userVO); return(new HttpStatusCodeResult(200)); } else { throw new ApplicationException(String.Format(Constants.CANNOT_SAVE, Constants.USER)); } } catch (Exception e) { return(new HttpStatusCodeAndErrorResult(500, e.Message)); } }
/// <summary> /// Edit user details /// </summary> /// <param name="id">The selected user id</param> /// <returns>The user details view</returns> public ActionResult UserEdit(int id) { MODEL.UserModel userModel = new MODEL.UserModel(); try { UserService userService = new UserService(); DivisionService divisionService = new DivisionService(); UserVO userVO = userService.GetUserById(id); //Add 'super user' as a user type for super user if (userVO.UserType == Constants.SUPER_USER) { //gets Super User userVO.UserTypes = userService.GetSuperUserType(); } else { userVO.UserTypes = userService.GetUserTypes(); } List <MODEL.Company> companyList = GetCompanyList(); //Transpose the user value object to model objecct userModel = new MODEL.UserModel(userVO); //TO DO - Need to implement better appproach to while editing password //While editing user details password should not be visible but model is getting binded //so always return invalid model // userModel.Password = "******"; //Clear the company list userModel.CompanyList.Clear(); //Iterate through each company check whether company is associated with user or not foreach (var item in companyList) { var temp = userVO.AssociatedCompanyList.Find(c => c.CompanyID == item.ID); if (temp != null) { item.IsSelected = temp.IsSelected; } userModel.CompanyList.Add(new MODEL.Company(item)); } //if all company are associated with user set IsCheckAll = true if (userModel.CompanyList.TrueForAll(c => c.IsSelected == true)) { userModel.IsCheckAll = true; } } catch (Exception e) { ModelState.AddModelError("", e.Message); } return(PartialView("UserDetails", userModel)); }
/// <summary> /// Create new user /// </summary> /// <returns>The user details view</returns> public ActionResult UserCreate() { try { MODEL.UserModel user = new MODEL.UserModel(); UserService userService = new UserService(); List <MODEL.Company> companyList = GetCompanyList(); user.UserTypeList = userService.GetUserTypes(); //by default set the user to active user.IsActive = true; foreach (var item in companyList) { user.CompanyList.Add(new MODEL.Company(item)); } return(PartialView("UserDetails", user)); } catch (Exception e) { return(new HttpStatusCodeAndErrorResult(500, e.Message)); } }