public ActionResult AddHobby(int Id, int?UserId, bool IsDelete = false) { if (UserId == null) { UserId = Int32.Parse(Session["UserId"].ToString()); } int roleId = Int32.Parse(Session["RoleId"].ToString()); using (SussexDBEntities db = new SussexDBEntities()) { if (IsDelete) { UserHobby userHobby = db.UserHobbies.Where(w => w.UserId == UserId && w.HobbyId == Id).FirstOrDefault(); db.UserHobbies.Remove(userHobby); db.SaveChanges(); } else { UserHobby hobby = new UserHobby(); hobby.UserId = (int)UserId; hobby.HobbyId = Id; db.UserHobbies.Add(hobby); db.SaveChanges(); } } if (roleId == RoleTypes.CUSTOMER_ID) { return(Redirect("/User/MyProfile")); } return(Redirect("/Admin/HobbiesAndPersonalities/" + UserId)); }
public async Task <IActionResult> AddHobbyFromList(UserHobby UserHobbyInfo) { var User_Id = UserHobbyInfo.UserId; await m_hobbiesRepository.AddUserHobby(UserHobbyInfo); return(RedirectToAction("EditUser", "Users", new { UserId = User_Id })); }
public IActionResult AddToHobbies(UserHobby newUserHobby, int hobbyId) { UserHobby hobbyToAdd = new UserHobby() { HobbyId = hobbyId, UserId = (int)uid, }; // UserHobby userAddedToEnthusiast = db.UserHobbies.FirstOrDefault(userHobby => userHobby.UserId == uid && userHobby.HobbyId == newUserHobby.HobbyId); // if (userAddedToEnthusiast != null) // { // ModelState.AddModelError("Body", "Already Reviewed"); // return View("HobbyDetails", hobbyToAdd); // } db.UserHobbies.Add(hobbyToAdd); db.SaveChanges(); return(RedirectToAction("HobbyDetails", "HobbyHub", new { hobbyId = hobbyId })); }
public async Task <int> AddUserHobby(UserHobby addHobby) { using (var context = ContextFactory.CreateDbContext(ConnectionString)) { context.UserHobbies.Add(addHobby); await context.SaveChangesAsync(); } return(0); }
public IActionResult Hobby(int HobbyId) { if (ModelState.IsValid) { UserHobby Join = new UserHobby { UserId = CurrUser.UserId, HobbyId = HobbyId, }; dbContext.Add(Join); dbContext.SaveChanges(); } return(RedirectToAction("AllHobbies")); }
[HttpGet("join/{HobbyId}")] //ADD/REMOVE RELATIONSHIP public RedirectToActionResult Join(int HobbyId) { int UserId = (int)HttpContext.Session.GetInt32("UserId"); UserHobby HobbyList = _context.UserHobbies.Include(b => b.Hobbyist).FirstOrDefault(a => a.Hobbyist.UserId == UserId && a.Hobby.HobbyId == HobbyId); if (HobbyList == null) { UserHobby NewHobby = new UserHobby { }; User ExistingUser = _context.Users.FirstOrDefault(u => u.UserId == UserId); Hobby ExistingHobby = _context.Hobbies.FirstOrDefault(w => w.HobbyId == HobbyId); NewHobby.Hobbyist = ExistingUser; NewHobby.Hobby = ExistingHobby; _context.Add(NewHobby); _context.SaveChanges(); } return(RedirectToAction("Dashboard", "Home")); }
public async Task <int> DeleteUserHobby(UserHobby removeItem) { int retResult = 0; using (var context = ContextFactory.CreateDbContext(ConnectionString)) { UserHobby currUserHobby = context.UserHobbies.FirstOrDefault(uh => uh.UserId.Equals(removeItem.UserId) & uh.HobbyId.Equals(removeItem.HobbyId)); if (currUserHobby == null) { retResult = -1; } else { context.UserHobbies.Remove(currUserHobby); await context.SaveChangesAsync(); } } return(retResult); }
[HttpPost("Hobby/New")] //CREATE NEW/SAVE TO DB public IActionResult CreateHobby(Hobby fromForm) { Hobby Hobby = new Hobby { }; if (ModelState.IsValid) { List <Hobby> ExistHobby = _context.Hobbies.Where(h => h.Name == fromForm.Name).ToList(); if (ExistHobby.Count < 1) { int UserId = (int)HttpContext.Session.GetInt32("UserId"); User ExistingUser = _context.Users.FirstOrDefault(u => u.UserId == UserId); fromForm.Creator = ExistingUser; _context.Add(fromForm); _context.SaveChanges(); UserHobby NewHobby = new UserHobby { }; Hobby ExistingHobby = _context.Hobbies.FirstOrDefault(w => w.HobbyId == fromForm.HobbyId); NewHobby.Hobbyist = ExistingUser; NewHobby.Hobby = ExistingHobby; _context.Add(NewHobby); _context.SaveChanges(); int HobbyId = ExistingHobby.HobbyId; return(RedirectToAction("Hobby", "Home", new { HobbyId = HobbyId })); } else { ModelState.AddModelError("Name", "Name must be unique"); return(View("NewHobby", fromForm)); } } return(View("NewHobby", Hobby)); }
public ActionResult AddUpdateUser() { try { #region Variable and Model Declaration User user = new User(); UserHobby userHobbies = new UserHobby(); UserProduct userProduct = new UserProduct(); string filename = ""; int userId = 0; #endregion // Get Model data from js side in string format string json = Request.Form["model"]; // Deserialization of Usermodel var model = JsonConvert.DeserializeObject <UserModel>(json); HttpFileCollectionBase files = Request.Files; // Check file exist or not if (files.Count > 0) { for (int i = 0; i < files.Count; i++) { // Give File Path var uploads = Path.Combine("~/Uploads"); if (!Directory.Exists(uploads)) { Directory.CreateDirectory(uploads); } HttpPostedFileBase file = files[i]; filename = Path.GetFileNameWithoutExtension(file.FileName) + "_" + DateTime.Now.Ticks + Path.GetExtension(file.FileName); var filePath = Path.Combine(Server.MapPath(uploads), filename); // Get the complete folder path and store the file inside it. file.SaveAs(filePath); } model.FileName = filename; model.FilePath = "/Uploads/" + filename; } if (model.UserId > 0) { // Get Db User Data By UserId For Update user = db.Users.Where(x => x.UserId == model.UserId).FirstOrDefault(); } user.Firstname = model.Firstname; user.Lastname = model.Lastname; user.Dateofbirth = model.Dateofbirth; // If File is not null then insert new file path if (model.FilePath != null) { user.Profilepicture = model.FilePath; } // else use db file path else { user.Profilepicture = user.Profilepicture; } user.Age = model.Age; user.Gender = model.Gender; user.CityId = model.CityId; user.Address = model.Address; user.Phoneno = model.Phoneno; // For Update Process if (model.UserId > 0) { // If any of above data has changed it will updated db.Entry(user).State = EntityState.Modified; } //For Insert Process else { db.Users.Add(user); } db.SaveChanges(); if (model.UserId > 0) { // For Update use db Id userId = model.UserId; // For Update checkboxes and Jquery table delete previous inserted data from database cd.DeleteData(userId); } else { // For Insert use Inserted Id userId = user.UserId; } // Fetch Hobbies List and insert into Hobbies Table foreach (var item in model.Hobbies) { userHobbies.UserId = userId; userHobbies.HobbyId = item.HobbyId; db.UserHobbies.Add(userHobbies); db.SaveChanges(); } // Fetch Product List and insert into Product Table foreach (var item in model.Products) { userProduct.UserId = userId; userProduct.ProductId = item.ProductId; userProduct.Recievername = item.Recievername; db.UserProducts.Add(userProduct); db.SaveChanges(); } return(RedirectToAction("Index")); } catch (Exception ex) { throw ex; } }
public ActionResult AddUpdateUserSP() { try { #region Variable and Model Declaration UserHobby userHobbies = new UserHobby(); UserProduct userProduct = new UserProduct(); string filename = ""; int userId = 0; #endregion // Get Model data from js side in string format string json = Request.Form["model"]; // Deserialization of Usermodel var model = JsonConvert.DeserializeObject <UserModel>(json); HttpFileCollectionBase files = Request.Files; // Check file exist or not if (files.Count > 0) { for (int i = 0; i < files.Count; i++) { // Give File Path var uploads = Path.Combine("~/Uploads"); if (!Directory.Exists(uploads)) { Directory.CreateDirectory(uploads); } HttpPostedFileBase file = files[i]; filename = Path.GetFileNameWithoutExtension(file.FileName) + "_" + DateTime.Now.Ticks + Path.GetExtension(file.FileName); var filePath = Path.Combine(Server.MapPath(uploads), filename); // Get the complete folder path and store the file inside it. file.SaveAs(filePath); } model.FileName = filename; model.FilePath = "/Uploads/" + filename; } ObjectParameter myOutputParamInt = new ObjectParameter("UserScalarId", typeof(Int32)); // For Update Process if (model.UserId > 0) { var data = db.SP_InsertUpdateUser(model.UserId, model.Firstname, model.Lastname, model.Dateofbirth, model.FilePath, model.Age, model.Gender, model.CityId, model.Address, model.Phoneno, myOutputParamInt); db.Database.Connection.Close(); } //For Insert Process else { userId = db.SP_InsertUpdateUser(model.UserId, model.Firstname, model.Lastname, model.Dateofbirth, model.FilePath, model.Age, model.Gender, model.CityId, model.Address, model.Phoneno, myOutputParamInt).FirstOrDefault().Value; } if (model.UserId > 0) { // For Update use db Id userId = model.UserId; } // Fetch Hobbies List and insert into Hobbies Table foreach (var item in model.Hobbies.ToList()) { db.SP_InsertHobbies(userId, item.HobbyId); } // Fetch Product List and insert into Product Table foreach (var item in model.Products) { db.SP_InsertProducts(userId, item.ProductId, item.Recievername); } return(RedirectToAction("Index")); } catch (Exception ex) { throw ex; } }
public ActionResult EditUser(Int32 id) { try { #region Variable and Model Declaration List <UserHobby> userHobbies = new List <UserHobby>(); List <Hobby> Hobbies = new List <Hobby>(); int?statedId = 0; int?countryId = 0; // Get Current Domain HttpContext httpContextAccessor = System.Web.HttpContext.Current; var request = httpContextAccessor.Request; absoluteUri = request.Url.GetLeftPart(UriPartial.Authority); #endregion // Get Data from UserId var data = db.Users.Where(x => x.UserId == id).FirstOrDefault(); var map = Mapper.CreateMap <User, UserModel>(); var userDto = Mapper.Map <User, UserModel>(data); userDto.convertedDate = data.Dateofbirth.Value.ToString("yyyy/MM/dd"); ViewBag.profilepicture = (data.Profilepicture == null ? null : absoluteUri + data.Profilepicture); // Get StateId from CityId statedId = db.Cities.Where(room => room.CityId == data.City.CityId).ToList().Select(x => x.StateId).FirstOrDefault(); // Get countryId from StateId countryId = db.States.Where(room => room.StateId == statedId).ToList().Select(x => x.CountryId).FirstOrDefault(); // Bind CountryList with Selected value userDto.CountryId = countryId.Value; SelectList CountryList = new SelectList(cd.GetCountryList(), "Value", "Text"); ViewBag.CountryList = CountryList; // Bind StateList with Selected value userDto.StateId = statedId.Value; SelectList StateList = new SelectList(cd.GetStatesListbyCountryId(countryId.Value), "Value", "Text"); ViewBag.StateList = StateList; // Bind CityList with Selected value SelectList CityList = new SelectList(cd.GetCitiesListbyStateId(statedId.Value), "Value", "Text"); ViewBag.CityList = CityList; // Bind HobbiList userDto.Hobbies = db.Hobbies.ToList(); foreach (var item in userDto.Hobbies) { // Get User Selected Hobbies From Database and match with HobbyId UserHobby obj = db.UserHobbies.Where(x => x.UserId == id).ToList().Where(x => x.HobbyId == item.HobbyId).FirstOrDefault(); // If HobbyId matches With User Selected Hobby Table and Hobby Table then we will check that hobby to true else False if (obj != null) { Hobbies.Add(new Hobby { HobbyId = obj.HobbyId.Value, HobbyName = item.HobbyName, IsChecked = true }); } else { Hobbies.Add(new Hobby { HobbyId = item.HobbyId, HobbyName = item.HobbyName, IsChecked = false }); } } // Bind HobbiList with checked values userDto.Hobbies = Hobbies; // Bind Product List SelectList ProductList = new SelectList(cd.GetProductList(), "Value", "Text"); ViewBag.ProductList = ProductList; userDto.UserId = id; return(View(userDto)); } catch (Exception ex) { throw ex; } }
public ActionResult Update(NewUserViewModel vm) { ApiResult <User> apiResult; if (ModelState.IsValid) { if (vm.Id > 0) { apiResult = TryExecute(() => { var selectedUser = _userRepository.Get(vm.Id, "Person"); selectedUser.EmployeeCode = vm.EmployeeCode; selectedUser.Person.FirstName = vm.Person.FirstName; selectedUser.Person.MiddleName = vm.Person.MiddleName; selectedUser.Person.LastName = vm.Person.LastName; selectedUser.Person.Gender = vm.Person.Gender; selectedUser.Person.Email = vm.Person.Email; selectedUser.Person.PhoneNo = vm.Person.PhoneNo; selectedUser.Person.SecondaryEmail = vm.Person.SecondaryEmail; selectedUser.Person.Address = vm.Person.Address; selectedUser.Person.CommunicationAddress = vm.Person.CommunicationAddress; selectedUser.Person.PassportNo = vm.Person.PassportNo; selectedUser.Person.DateOfBirth = vm.Person.DateOfBirth; selectedUser.Person.BloodGroup = vm.Person.BloodGroup; selectedUser.Person.MaritalStatus = vm.Person.MaritalStatus; selectedUser.Person.MarriageAnniversary = vm.Person.MarriageAnniversary; selectedUser.DepartmentId = vm.DepartmentId; selectedUser.LocationId = vm.LocationId; selectedUser.DesignationId = vm.DesignationId; selectedUser.ShiftId = vm.ShiftId; selectedUser.ReportingPersonId = vm.ReportingPersonId; selectedUser.Experience = vm.Experience; selectedUser.DateOfJoin = vm.DateOfJoin; selectedUser.ConfirmationDate = vm.ConfirmationDate; selectedUser.DateOfResignation = vm.DateOfResignation; selectedUser.LastDate = vm.LastDate; selectedUser.OfficialEmail = vm.OfficialEmail; selectedUser.OfficialPhone = vm.OfficialPhone; selectedUser.OfficialMessengerId = vm.OfficialMessengerId; selectedUser.EmployeeStatus = vm.EmployeeStatus; selectedUser.RequiresTimeSheet = vm.RequiresTimeSheet; selectedUser.Salary = vm.Salary; selectedUser.Bank = vm.Bank; selectedUser.BankAccountNumber = vm.BankAccountNumber; selectedUser.PANCard = vm.PANCard; selectedUser.PaymentMode = vm.PaymentMode; _userRepository.Update(selectedUser); _unitOfWork.Commit(); // Remove the existing mapped Roles var existingRoles = _roleMemberRepository.GetAllBy(m => m.UserId == selectedUser.Id); foreach (var map in existingRoles) { _roleMemberRepository.Delete(map); } if (vm.RoleIds != null) { // Map the New Technologies foreach (var roleId in vm.RoleIds) { var newMap = new RoleMember { UserId = vm.Id, RoleId = roleId }; _roleMemberRepository.Create(newMap); } _unitOfWork.Commit(); } // Remove the existing mapped Technologies var existingMaps = _userTechnologyMapRepository.GetAllBy(m => m.UserId == selectedUser.Id); foreach (var map in existingMaps) { _userTechnologyMapRepository.Delete(map); } _unitOfWork.Commit(); if (vm.TechnologyIds != null) { // Map the New Technologies foreach (var technologyId in vm.TechnologyIds) { var newMap = new UserTechnologyMap { UserId = vm.Id, TechnologyId = technologyId }; _userTechnologyMapRepository.Create(newMap); } _unitOfWork.Commit(); } // Remove the existing mapped Skills var existingSkillMaps = _userSkillRepository.GetAllBy(m => m.UserId == selectedUser.Id); foreach (var map in existingSkillMaps) { _userSkillRepository.Delete(map); } _unitOfWork.Commit(); if (vm.SkillIds != null) { // Map the New Technologies foreach (var skillId in vm.SkillIds) { var newMap = new UserSkill { UserId = vm.Id, SkillId = skillId }; _userSkillRepository.Create(newMap); } _unitOfWork.Commit(); } // Remove the existing mapped Skills var existingHobbyMaps = _userHobbyRepository.GetAllBy(m => m.UserId == selectedUser.Id); foreach (var map in existingHobbyMaps) { _userHobbyRepository.Delete(map); } _unitOfWork.Commit(); if (vm.HobbiesId != null) { // Map the New Technologies foreach (var hobbyId in vm.HobbiesId) { var newMap = new UserHobby { UserId = vm.Id, HobbyId = hobbyId }; _userHobbyRepository.Create(newMap); } _unitOfWork.Commit(); } // Remove the existing mapped Certifications var existingCertificationMaps = _userCertificationRepository.GetAllBy(m => m.UserId == selectedUser.Id); foreach (var map in existingCertificationMaps) { _userCertificationRepository.Delete(map); } _unitOfWork.Commit(); if (vm.CertificationIds != null) { // Map the New Technologies foreach (var certificateId in vm.CertificationIds) { var newMap = new UserCertification { UserId = vm.Id, CertificationId = certificateId }; _userCertificationRepository.Create(newMap); } _unitOfWork.Commit(); } return(selectedUser); }, "User updated sucessfully"); } else { apiResult = TryExecute(() => { var newUser = new User { EmployeeCode = vm.EmployeeCode, Username = vm.Username, Password = HashHelper.Hash(vm.Password), AccessRule = AccessRule.CreateNewUserAccessRule(true), Person = vm.Person, DepartmentId = vm.DepartmentId, LocationId = vm.LocationId, DesignationId = vm.DesignationId, ShiftId = vm.ShiftId, ReportingPersonId = vm.ReportingPersonId, Experience = vm.Experience, DateOfJoin = vm.DateOfJoin, ConfirmationDate = vm.ConfirmationDate, DateOfResignation = vm.DateOfResignation, LastDate = vm.LastDate, OfficialEmail = vm.OfficialEmail, OfficialPhone = vm.OfficialPhone, OfficialMessengerId = vm.OfficialMessengerId, EmployeeStatus = vm.EmployeeStatus, RequiresTimeSheet = vm.RequiresTimeSheet, Salary = vm.Salary, Bank = vm.Bank, BankAccountNumber = vm.BankAccountNumber, PANCard = vm.PANCard, PaymentMode = vm.PaymentMode }; _userRepository.Create(newUser); _unitOfWork.Commit(); // Map the Technologies if (vm.TechnologyIds != null) { foreach (var technologyId in vm.TechnologyIds) { var newMap = new UserTechnologyMap { UserId = newUser.Id, TechnologyId = technologyId }; _userTechnologyMapRepository.Create(newMap); } _unitOfWork.Commit(); } // Map the Technologies if (vm.RoleIds != null) { foreach (var roleId in vm.RoleIds) { var newMap = new RoleMember { UserId = newUser.Id, RoleId = roleId }; _roleMemberRepository.Create(newMap); } _unitOfWork.Commit(); } return(newUser); }, "User created sucessfully"); } } else { apiResult = ApiResultFromModelErrors <User>(); } return(Json(apiResult, JsonRequestBehavior.AllowGet)); }
public async Task <IActionResult> DeleteUserHobby(UserHobby removeItem) { await m_hobbiesRepository.DeleteUserHobby(removeItem); return(RedirectToAction("EditUser", "Users", new { UserId = removeItem.UserId })); }
public static void Initialize(RepositoryContext dbContext) { if (!dbContext.Users.Any()) { var listUsers = new User[] { new User { Name = "Петренко Евгений", Sex = "Муж", BirthDay = "20.10.2000", Age = 18 }, new User { Name = "Зырянов Василий", Sex = "Муж", BirthDay = "20.10.2001", Age = 17 }, new User { Name = "Федоравичус Виктор", Sex = "Муж", BirthDay = "20.10.2002", Age = 16 }, new User { Name = "Антонов Евгений", Sex = "Жен", BirthDay = "20.10.2003", Age = 15 }, new User { Name = "Царёва Светлана", Sex = "Жен", BirthDay = "20.10.2004", Age = 14 }, new User { Name = "Зырянова Ольга", Sex = "Жен", BirthDay = "20.10.1999", Age = 19 }, new User { Name = "Смирнова Тоня", Sex = "Жен", BirthDay = "20.10.1998", Age = 20 } }; foreach (User currUser in listUsers) { dbContext.Users.Add(currUser); } dbContext.SaveChanges(); } if (!dbContext.HobbyTypes.Any()) { var listTypesHobbies = new HobbyType[] { new HobbyType { Name = "Спорт" }, new HobbyType { Name = "Кино" }, new HobbyType { Name = "Рукоделие" }, }; foreach (HobbyType currType in listTypesHobbies) { dbContext.HobbyTypes.Add(currType); } dbContext.SaveChanges(); } if (!dbContext.HobbyNames.Any()) { var listNamesHobbies = new HobbyName[] { new HobbyName { Name = "Хоккей" }, new HobbyName { Name = "Футбол" }, new HobbyName { Name = "Теннис" }, new HobbyName { Name = "Боевик" }, new HobbyName { Name = "Драма" }, new HobbyName { Name = "Кинокомедия" }, new HobbyName { Name = "Вышивание" }, new HobbyName { Name = "Выжигание" }, new HobbyName { Name = "Рисование" }, }; foreach (HobbyName currName in listNamesHobbies) { dbContext.HobbyNames.Add(currName); } dbContext.SaveChanges(); } if (!dbContext.Hobbies.Any()) { var listHobbies = new Hobby[] { new Hobby { HobbyTypeId = 1, HobbyNameId = 1 }, new Hobby { HobbyTypeId = 1, HobbyNameId = 2 }, new Hobby { HobbyTypeId = 1, HobbyNameId = 3 }, new Hobby { HobbyTypeId = 2, HobbyNameId = 4 }, new Hobby { HobbyTypeId = 2, HobbyNameId = 5 }, new Hobby { HobbyTypeId = 2, HobbyNameId = 6 }, new Hobby { HobbyTypeId = 3, HobbyNameId = 7 }, new Hobby { HobbyTypeId = 3, HobbyNameId = 8 }, new Hobby { HobbyTypeId = 3, HobbyNameId = 9 }, }; foreach (Hobby currType in listHobbies) { dbContext.Hobbies.Add(currType); } dbContext.SaveChanges(); } if (!dbContext.UserHobbies.Any()) { var listUserHobbies = new UserHobby[] { new UserHobby { UserId = 1, HobbyId = 1 }, new UserHobby { UserId = 1, HobbyId = 4 }, new UserHobby { UserId = 1, HobbyId = 8 }, new UserHobby { UserId = 2, HobbyId = 2 }, new UserHobby { UserId = 2, HobbyId = 6 }, new UserHobby { UserId = 3, HobbyId = 3 }, new UserHobby { UserId = 3, HobbyId = 4 }, new UserHobby { UserId = 4, HobbyId = 2 }, new UserHobby { UserId = 4, HobbyId = 6 }, new UserHobby { UserId = 4, HobbyId = 7 }, new UserHobby { UserId = 5, HobbyId = 5 }, new UserHobby { UserId = 5, HobbyId = 7 }, }; foreach (UserHobby currUserHobby in listUserHobbies) { dbContext.UserHobbies.Add(currUserHobby); } dbContext.SaveChanges(); } }
public ActionResult Edit(EditUserViewModel vm) { var selectedUser = _userRepository.GetBy(u => u.Id == WebUser.Id, "Person"); if (selectedUser != null) { selectedUser.Person.FirstName = vm.Person.FirstName; selectedUser.Person.MiddleName = vm.Person.MiddleName; selectedUser.Person.LastName = vm.Person.LastName; selectedUser.Person.Gender = vm.Person.Gender; selectedUser.Person.Email = vm.Person.Email; selectedUser.Person.SecondaryEmail = vm.Person.SecondaryEmail; selectedUser.Person.PhoneNo = vm.Person.PhoneNo; selectedUser.Person.OfficePhone = vm.Person.OfficePhone; selectedUser.Person.Website = vm.Person.Website; selectedUser.Person.Skype = vm.Person.Skype; selectedUser.Person.Facebook = vm.Person.Facebook; selectedUser.Person.Twitter = vm.Person.Twitter; selectedUser.Person.GooglePlus = vm.Person.GooglePlus; selectedUser.Person.LinkedIn = vm.Person.LinkedIn; selectedUser.Person.City = vm.Person.City; selectedUser.Person.Country = vm.Person.Country; selectedUser.Person.Address = vm.Person.Address; selectedUser.Person.CommunicationAddress = vm.Person.CommunicationAddress; selectedUser.Person.PassportNo = vm.Person.PassportNo; selectedUser.Person.DateOfBirth = vm.Person.DateOfBirth; selectedUser.Person.BloodGroup = vm.Person.BloodGroup; selectedUser.Person.MaritalStatus = vm.Person.MaritalStatus; selectedUser.Person.MarriageAnniversary = vm.Person.MarriageAnniversary; selectedUser.OfficialPhone = vm.OfficialPhone; _userRepository.Update(selectedUser); _unitOfWork.Commit(); // Remove the existing mapped Technologies var existingMaps = _userTechnologyMapRepository.GetAllBy(m => m.UserId == selectedUser.Id); foreach (var map in existingMaps) { _userTechnologyMapRepository.Delete(map); } _unitOfWork.Commit(); if (vm.TechnologyIds != null) { // Map the New Technologies foreach (var technologyId in vm.TechnologyIds) { var newMap = new UserTechnologyMap { UserId = vm.Id, TechnologyId = technologyId }; _userTechnologyMapRepository.Create(newMap); } _unitOfWork.Commit(); } // Remove the existing mapped Skills var existingSkillMaps = _userSkillRepository.GetAllBy(m => m.UserId == selectedUser.Id).ToList(); foreach (var map in existingSkillMaps) { _userSkillRepository.Delete(map); } _unitOfWork.Commit(); if (vm.SkillIds != null) { // Map the New Technologies foreach (var skillId in vm.SkillIds) { var newMap = new UserSkill { UserId = vm.Id, SkillId = skillId }; _userSkillRepository.Create(newMap); } _unitOfWork.Commit(); } // Remove the existing mapped Skills var existingHobbyMaps = _userHobbyRepository.GetAllBy(m => m.UserId == selectedUser.Id); foreach (var map in existingHobbyMaps) { _userHobbyRepository.Delete(map); } _unitOfWork.Commit(); if (vm.HobbiesId != null) { // Map the New Technologies foreach (var hobbyId in vm.HobbiesId) { var newMap = new UserHobby { UserId = vm.Id, HobbyId = hobbyId }; _userHobbyRepository.Create(newMap); } _unitOfWork.Commit(); } // Remove the existing mapped Certifications var existingCertificationMaps = _userCertificationRepository.GetAllBy(m => m.UserId == selectedUser.Id); foreach (var map in existingCertificationMaps) { _userCertificationRepository.Delete(map); } _unitOfWork.Commit(); if (vm.CertificationIds != null) { // Map the New Technologies foreach (var certificateId in vm.CertificationIds) { var newMap = new UserCertification { UserId = vm.Id, CertificationId = certificateId }; _userCertificationRepository.Create(newMap); } _unitOfWork.Commit(); } return(RedirectToAction("Index")); } return(View(vm)); }
public ActionResult Edit(EditUserViewModel vm) { var selectedUser = _userRepository.Get(vm.Id, "Person"); if (selectedUser != null) { selectedUser.EmployeeCode = vm.EmployeeCode; selectedUser.Person.FirstName = vm.Person.FirstName; selectedUser.Person.MiddleName = vm.Person.MiddleName; selectedUser.Person.LastName = vm.Person.LastName; selectedUser.Person.Gender = vm.Person.Gender; selectedUser.Person.Email = vm.Person.Email; selectedUser.Person.PhoneNo = vm.Person.PhoneNo; selectedUser.Person.SecondaryEmail = vm.Person.SecondaryEmail; selectedUser.Person.Address = vm.Person.Address; selectedUser.Person.CommunicationAddress = vm.Person.CommunicationAddress; selectedUser.Person.PassportNo = vm.Person.PassportNo; selectedUser.Person.DateOfBirth = vm.Person.DateOfBirth; selectedUser.Person.BloodGroup = vm.Person.BloodGroup; selectedUser.Person.MaritalStatus = vm.Person.MaritalStatus; selectedUser.Person.MarriageAnniversary = vm.Person.MarriageAnniversary; selectedUser.DepartmentId = vm.DepartmentId; selectedUser.LocationId = vm.LocationId; selectedUser.DesignationId = vm.DesignationId; selectedUser.ShiftId = vm.ShiftId; selectedUser.ReportingPersonId = vm.ReportingPersonId; selectedUser.ManagerId = vm.ManagerId; selectedUser.Experience = vm.Experience; selectedUser.DateOfJoin = vm.DateOfJoin; selectedUser.ConfirmationDate = vm.ConfirmationDate; selectedUser.DateOfResignation = vm.DateOfResignation; selectedUser.LastDate = vm.LastDate; selectedUser.OfficialEmail = vm.OfficialEmail; selectedUser.OfficialPhone = vm.OfficialPhone; selectedUser.OfficialMessengerId = vm.OfficialMessengerId; selectedUser.EmployeeStatus = vm.EmployeeStatus; selectedUser.RequiresTimeSheet = vm.RequiresTimeSheet; selectedUser.Salary = vm.Salary; selectedUser.Bank = vm.Bank; selectedUser.BankAccountNumber = vm.BankAccountNumber; selectedUser.PANCard = vm.PANCard; selectedUser.PaymentMode = vm.PaymentMode; _userRepository.Update(selectedUser); _unitOfWork.Commit(); // Remove the existing mapped Roles var existingRoles = _roleMemberRepository.GetAllBy(m => m.UserId == selectedUser.Id); foreach (var map in existingRoles) { _roleMemberRepository.Delete(map); } if (vm.RoleIds != null) { // Map the New Technologies foreach (var roleId in vm.RoleIds) { var newMap = new RoleMember { UserId = vm.Id, RoleId = roleId }; _roleMemberRepository.Create(newMap); } _unitOfWork.Commit(); } // Remove the existing mapped Technologies var existingMaps = _userTechnologyMapRepository.GetAllBy(m => m.UserId == selectedUser.Id); foreach (var map in existingMaps) { _userTechnologyMapRepository.Delete(map); } _unitOfWork.Commit(); if (vm.TechnologyIds != null) { // Map the New Technologies foreach (var technologyId in vm.TechnologyIds) { var newMap = new UserTechnologyMap { UserId = vm.Id, TechnologyId = technologyId }; _userTechnologyMapRepository.Create(newMap); } _unitOfWork.Commit(); } // Remove the existing mapped Skills var existingSkillMaps = _userSkillRepository.GetAllBy(m => m.UserId == selectedUser.Id); foreach (var map in existingSkillMaps) { _userSkillRepository.Delete(map); } _unitOfWork.Commit(); if (vm.SkillIds != null) { // Map the New Technologies foreach (var skillId in vm.SkillIds) { var newMap = new UserSkill { UserId = vm.Id, SkillId = skillId }; _userSkillRepository.Create(newMap); } _unitOfWork.Commit(); } // Remove the existing mapped Skills var existingHobbyMaps = _userHobbyRepository.GetAllBy(m => m.UserId == selectedUser.Id); foreach (var map in existingHobbyMaps) { _userHobbyRepository.Delete(map); } _unitOfWork.Commit(); if (vm.HobbiesId != null) { // Map the New Technologies foreach (var hobbyId in vm.HobbiesId) { var newMap = new UserHobby { UserId = vm.Id, HobbyId = hobbyId }; _userHobbyRepository.Create(newMap); } _unitOfWork.Commit(); } // Remove the existing mapped Certifications var existingCertificationMaps = _userCertificationRepository.GetAllBy(m => m.UserId == selectedUser.Id); foreach (var map in existingCertificationMaps) { _userCertificationRepository.Delete(map); } _unitOfWork.Commit(); if (vm.CertificationIds != null) { // Map the New Technologies foreach (var certificateId in vm.CertificationIds) { var newMap = new UserCertification { UserId = vm.Id, CertificationId = certificateId }; _userCertificationRepository.Create(newMap); } _unitOfWork.Commit(); } return(RedirectToAction("Index")); } var allErrors = ModelState.Values.SelectMany(v => v.Errors); ViewBag.Roles = new MultiSelectList(_roleRepository.GetAll(), "Id", "Name", vm.RoleIds); ViewBag.DepartmentId = new SelectList(_departmentRepository.GetAll(), "Id", "Title", vm.DepartmentId); ViewBag.DesignationId = new SelectList(_designationRepository.GetAllBy(d => d.DepartmentId == vm.DepartmentId), "Id", "Title", vm.DesignationId); ViewBag.LocationId = new SelectList(_locationRepository.GetAll(), "Id", "Title", vm.LocationId); ViewBag.ReportingPersonId = new SelectList(_userRepository.GetAllBy(u => u.EmployeeStatus != EmployeeStatus.Ex && u.Id != 1, "Person"), "Id", "Person.Name", vm.ReportingPersonId); ViewBag.ManagerId = new SelectList(_userRepository.GetAllBy(u => u.EmployeeStatus != EmployeeStatus.Ex && u.Id != 1, "Person"), "Id", "Person.Name", vm.ManagerId); ViewBag.ShiftId = new SelectList(_shiftRepository.GetAll(), "Id", "Title", vm.ShiftId); return(View(vm)); }