public ActionResult UploadExcel(ExcelModel model, HttpPostedFileBase file) { var rowuid = new SessionRepository().Get(model.SessionId).RowGUID; try { string fileName = "~/Uploads/" + file.FileName; string filePath = Server.MapPath(fileName); file.SaveAs(filePath); var participantRepo = new ParticipiantRepository(); participant_profile participant = null; var cu = Session["user"] as ContextUser; List <participant_profile> profileList = new List <participant_profile>(); using (ExcelPackage xlPackage = new ExcelPackage(new FileInfo(filePath))) { var sheet = xlPackage.Workbook.Worksheets[1]; var rowCnt = sheet.Dimension.End.Row; for (int row = 2; row <= rowCnt; row++) { participant_profile profile = new participant_profile(); profile.Name = GetValue(sheet, row, 1); if (string.IsNullOrEmpty(profile.Name)) { continue; } profile.FatherName = GetValue(sheet, row, 2); profile.Family = GetValue(sheet, row, 3); profile.NationalID = GetValue(sheet, row, 4); profile.Mobile = GetValue(sheet, row, 5); profile.Email = GetValue(sheet, row, 6); profileList.Add(profile); } string error = ValidateParticipantRecords(profileList); if (error != null) { return(RedirectToAction("Edit", "Session", new { id = rowuid, excelerror = true, error = error })); } } foreach (var profile in profileList) { participant = participantRepo.GetParticipant(profile.NationalID); if (participant == null) { participant = new participant_profile { RowGuid = Guid.NewGuid(), CreatedAt = DateTime.Now, CreatedBy = cu.OUser.Id, Email = profile.Email }; } var isSessionAttached = participant.session_participant.Where(x => x.SessionID == model.SessionId).Any(); if (model.SessionId > 0 && !isSessionAttached) { participant.session_participant.Add( new session_participant { SessionID = model.SessionId, ParticipantID = participant.Id }); } var userRole = new RoleRepository().Get().Where(x => x.Code == (int)EnumUserRole.Participant) .FirstOrDefault(); if (participant.ParticipantUserID == 0) { participant.user = new user { RowGuid = Guid.NewGuid(), Email = profile.Email, Username = profile.Email, RegistrationDate = DateTime.Now, FirstName = profile.Name, RoleId = userRole.Id, CreatedAt = DateTime.Now, ValidFrom = DateTime.Now, FirstLogin = false, IsMobileVerified = false, IsEmailVerified = false, CreatedBy = cu.OUser.Id, Password = EncryptionKeys.Encrypt(Membership.GeneratePassword(8, 4)) } } ; participant.Name = profile.Name; participant.FatherName = profile.FatherName; participant.Family = profile.Family; participant.NationalID = profile.NationalID; participant.Mobile = profile.Mobile; participant.isActive = true; if (participant.Id == 0) { string url = System.Web.HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + "/Account/Login"; var bogusController = Util.CreateController <EmailTemplateController>(); EmailTemplateModel emodel = new EmailTemplateModel { Title = "Complete Profile", RedirectUrl = url, UserName = participant.Email, User = participant.Email, Password = EncryptionKeys.Decrypt(participant.user.Password) }; string body = Util.RenderViewToString(bogusController.ControllerContext, "CoordinatorProfile", emodel); EmailSender.SendSupportEmail(body, participant.Email); participant.IsEmailSent = true; participantRepo.Post(participant); } else { participantRepo.Put(participant.Id, participant); } } } catch (Exception ex) { return(RedirectToAction("Edit", "Session", new { id = rowuid, excelerror = true, error = Participant.UploadError })); throw ex; } return(RedirectToAction("Index", "Session")); }
public ActionResult Edit(participant_profile profile) { var accountRepo = new AccountRepository(); var participantRepo = new ParticipiantRepository(); participant_profile participant = null; var cu = Session["user"] as ContextUser; if (profile.Id == 0) { if (accountRepo.EmailExist(profile.Email)) { ViewBag.EmailExist = true; return(View(profile)); } participant = participantRepo.GetParticipant(profile.NationalID); if (participant == null) { participant = new participant_profile { RowGuid = Guid.NewGuid(), CreatedAt = DateTime.Now, CreatedBy = cu.OUser.Id, Email = profile.Email, }; } if (profile.SessionId > 0) { participant.session_participant.Add(new session_participant { SessionID = profile.SessionId, ParticipantID = participant.Id }); } } else { participant = participantRepo.Get(profile.Id); participant.UpdatedAt = DateTime.Now; participant.UpdatedBy = cu.OUser.Id; } var userRole = new RoleRepository().Get().Where(x => x.Code == (int)EnumUserRole.Participant).FirstOrDefault(); if (participant.ParticipantUserID == 0) { participant.user = new user { RowGuid = Guid.NewGuid(), Email = profile.Email, Username = profile.Email, RegistrationDate = DateTime.Now, FirstName = profile.Name, RoleId = userRole.Id, CreatedAt = DateTime.Now, ValidFrom = DateTime.Now, FirstLogin = false, IsMobileVerified = false, IsEmailVerified = false, CreatedBy = cu.OUser.Id, Password = EncryptionKeys.Encrypt(profile.Password) } } ; participant.Name = profile.Name; participant.FatherName = profile.FatherName; participant.Family = profile.Family; participant.NationalID = profile.NationalID; if (profile.MobileNo != null) { participant.Mobile = profile.MobileNo; } else { participant.Mobile = profile.Mobile; } participant.isActive = profile.isActive; participant.user.IsLocked = !participant.isActive; if (participant.Id == 0) { string url = System.Web.HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + "/Account/Login"; var bogusController = Util.CreateController <EmailTemplateController>(); EmailTemplateModel model = new EmailTemplateModel { Title = "Complete Profile", RedirectUrl = url, UserName = participant.Email, Password = EncryptionKeys.Decrypt(participant.user.Password), ParticipantName = participant.Name, User = participant.user.FirstName }; string body = Util.RenderViewToString(bogusController.ControllerContext, "ParticipantProfile", model); EmailSender.SendSupportEmail(body, participant.Email); participant.IsEmailSent = true; participantRepo.Post(participant); } else { participantRepo.Put(participant.Id, participant); } if (Request["participant"] == "true") { var rowId = new SessionRepository().Get(profile.SessionId).RowGUID; return(RedirectToAction("Edit", "Session", new { id = rowId })); } return(RedirectToAction("Index")); }