public JsonResult Create(EmployeeModel model) { ApiResult <Employee> apiResult; if (ModelState.IsValid) { if (model.Id > 0) { apiResult = TryExecute(() => { var newPerson = new Person { Id = model.PersonId, FirstName = model.FirstName, MiddleName = model.MiddleName, LastName = model.LastName, Gender = model.Gender, Email = model.Email, SecondaryEmail = model.SecondaryEmail, PhoneNo = model.PhoneNo, Address = model.Address, CommunicationAddress = model.CommunicationAddress, PassportNo = model.PassportNo, DateOfBirth = model.DateOfBirth, BloodGroup = model.BloodGroup, MaritalStatus = model.MaritalStatus, MarriageAnniversary = model.MarriageAnniversary, PhotoPath = model.PhotoPath }; _personRepository.Update(newPerson); var user = _userRepository.GetBy(i => i.Id == model.UserId); user.Id = model.UserId; user.PersonId = model.PersonId; user.Username = model.Username; user.EmployeeCode = model.EmployeeCode; user.DepartmentId = model.DepartmentId; user.LocationId = model.LocationId; user.DesignationId = model.DesignationId; user.ShiftId = model.ShiftId; user.Salary = model.Salary; user.Bank = model.Bank; user.BankAccountNumber = model.BankAccountNumber; user.PANCard = model.PANCard; user.PaymentMode = model.PaymentMode; user.Experience = model.Experience; user.DateOfJoin = model.DateOfJoin; user.ConfirmationDate = model.ConfirmationDate; user.DateOfResignation = model.DateOfResignation; user.LastDate = model.LastDate; user.OfficialEmail = model.OfficialEmail; user.OfficialPhone = model.OfficialPhone; user.OfficialMessengerId = model.OfficialMessengerId; user.EmployeeStatus = model.EmployeeStatus; user.RequiresTimeSheet = model.RequiresTimeSheet; user.SeatNo = model.SeatNo; user.AccessRuleId = model.AccessRuleId; _userRepository.Update(user); var newEmployee = new Employee { Id = model.EmployeeId, UserId = model.UserId, EmployeeCode = model.EmployeeCode, DepartmentId = model.DepartmentId, LocationId = model.LocationId, DesignationId = model.DesignationId, ShiftId = model.ShiftId, Salary = model.Salary, Bank = model.Bank, BankAccountNumber = model.BankAccountNumber, PANCard = model.PANCard, PaymentMode = model.PaymentMode, ReportingPersonId = model.ReportingPersonId, ManagerId = model.ManagerId, Experience = model.Experience, DateOfJoin = model.DateOfJoin, ConfirmationDate = model.ConfirmationDate, DateOfResignation = model.DateOfResignation, LastDate = model.LastDate, OfficialEmail = model.OfficialEmail, OfficialPhone = model.OfficialPhone, OfficialMessengerId = model.OfficialMessengerId, EmployeeStatus = model.EmployeeStatus, RequiresTimeSheet = model.RequiresTimeSheet, SeatNo = model.SeatNo }; _employeeRepository.Update(newEmployee); _unitOfWork.Commit(); var User = _userRepository.Get(model.UserId); if (User != null) { var reportingPerson = _employeeRepository.Get(newEmployee.ReportingPersonId.Value); var manager = _employeeRepository.Get(newEmployee.ManagerId.Value); User.ReportingPersonId = reportingPerson.UserId; User.ManagerId = manager.UserId; _userRepository.Update(User); _unitOfWork.Commit(); } // Remove the existing mapped Roles var existingRoles = _roleMemberRepository.GetAllBy(m => m.UserId == user.Id); foreach (var map in existingRoles) { _roleMemberRepository.Delete(map); } if (model.RoleIds != null) { // Map the New Technologies foreach (var roleId in model.RoleIds) { var newMap = new RoleMember { UserId = model.UserId, RoleId = roleId }; _roleMemberRepository.Create(newMap); } _unitOfWork.Commit(); } // Remove the existing mapped Technologies var existingMaps = _userTechnologyMapRepository.GetAllBy(m => m.UserId == model.UserId); foreach (var map in existingMaps) { _userTechnologyMapRepository.Delete(map); } _unitOfWork.Commit(); if (model.TechnologyIds != null) { // Map the New Technologies foreach (var technologyId in model.TechnologyIds) { var newMap = new UserTechnologyMap { UserId = model.UserId, TechnologyId = technologyId }; _userTechnologyMapRepository.Create(newMap); } _unitOfWork.Commit(); } return(newEmployee); }, "Employee updated sucessfully"); } else { apiResult = TryExecute(() => { var newPerson = new Person { FirstName = model.FirstName, MiddleName = model.MiddleName, LastName = model.LastName, Gender = model.Gender, Email = model.Email, SecondaryEmail = model.SecondaryEmail, PhoneNo = model.PhoneNo, Address = model.Address, CommunicationAddress = model.CommunicationAddress, PassportNo = model.PassportNo, DateOfBirth = model.DateOfBirth, BloodGroup = model.BloodGroup, MaritalStatus = model.MaritalStatus, MarriageAnniversary = model.MarriageAnniversary, PhotoPath = model.PhotoPath }; var person = _personRepository.Create(newPerson); var newUser = new User { PersonId = person.Id, Username = model.Username, Password = HashHelper.Hash(model.Password), AccessRule = AccessRule.CreateNewUserAccessRule(true), EmployeeCode = model.EmployeeCode, DepartmentId = model.DepartmentId, LocationId = model.LocationId, DesignationId = model.DesignationId, ShiftId = model.ShiftId, Salary = model.Salary, Bank = model.Bank, BankAccountNumber = model.BankAccountNumber, PANCard = model.PANCard, PaymentMode = model.PaymentMode, Experience = model.Experience, DateOfJoin = model.DateOfJoin, ConfirmationDate = model.ConfirmationDate, DateOfResignation = model.DateOfResignation, LastDate = model.LastDate, OfficialEmail = model.OfficialEmail, OfficialPhone = model.OfficialPhone, OfficialMessengerId = model.OfficialMessengerId, EmployeeStatus = model.EmployeeStatus, RequiresTimeSheet = model.RequiresTimeSheet, SeatNo = model.SeatNo, }; var user = _userRepository.Create(newUser); var newEmployee = new Employee { UserId = user.Id, EmployeeCode = model.EmployeeCode, DepartmentId = model.DepartmentId, LocationId = model.LocationId, DesignationId = model.DesignationId, ShiftId = model.ShiftId, Salary = model.Salary, Bank = model.Bank, BankAccountNumber = model.BankAccountNumber, PANCard = model.PANCard, PaymentMode = model.PaymentMode, ReportingPersonId = model.ReportingPersonId, ManagerId = model.ManagerId, Experience = model.Experience, DateOfJoin = model.DateOfJoin, ConfirmationDate = model.ConfirmationDate, DateOfResignation = model.DateOfResignation, LastDate = model.LastDate, OfficialEmail = model.OfficialEmail, OfficialPhone = model.OfficialPhone, OfficialMessengerId = model.OfficialMessengerId, EmployeeStatus = model.EmployeeStatus, RequiresTimeSheet = model.RequiresTimeSheet, SeatNo = model.SeatNo }; _employeeRepository.Create(newEmployee); _unitOfWork.Commit(); var User = _userRepository.Get(newUser.Id); if (User != null) { var reportingPerson = _employeeRepository.Get(newEmployee.ReportingPersonId.Value); var manager = _employeeRepository.Get(newEmployee.ManagerId.Value); User.ReportingPersonId = reportingPerson.UserId; User.ManagerId = manager.UserId; _userRepository.Update(User); _unitOfWork.Commit(); } // Map the Technologies if (model.TechnologyIds != null) { foreach (var technologyId in model.TechnologyIds) { var newMap = new UserTechnologyMap { UserId = newUser.Id, TechnologyId = technologyId }; _userTechnologyMapRepository.Create(newMap); } _unitOfWork.Commit(); } // Map the Roles if (model.RoleIds != null) { foreach (var roleId in model.RoleIds) { var newMap = new RoleMember { UserId = newUser.Id, RoleId = roleId }; _roleMemberRepository.Create(newMap); } _unitOfWork.Commit(); } return(newEmployee); }, "Employee created sucessfully"); } } else { apiResult = ApiResultFromModelErrors <Employee>(); var data = ApiResultFromModelErrors <Employee>(); } return(Json(apiResult, JsonRequestBehavior.AllowGet)); }
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 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)); }