public IActionResult EditStaff(string id, DigiBadges.DataAccess.Users users) { var a = _u.FindById(id); a.FirstName = users.FirstName; a.LastName = users.LastName; a.Email = users.Email; var userid = User.Claims.FirstOrDefault(c => c.Type == AppUtility.UserId).Value; DigiBadges.Models.Users usr = new DigiBadges.Models.Users() { //CreatedBy = usr.CreatedBy, FirstName = users.FirstName, LastName = users.LastName, Email = users.Email, UserId = new ObjectId(id), RoleId = a.RoleId, Password = a.Password, CreatedBy = a.CreatedBy, CreatedDate = a.CreatedDate, IsUserVerified = a.IsUserVerified //UserId = users.Id }; SolrUsersModel su = new SolrUsersModel(usr); _solr.Add(su); _solr.Commit(); List <DataAccess.Issuers> issuerlist1 = _i.FilterBy(e => e.UserId == new ObjectId(userid)).ToList(); DataAccess.Issuers issuers = new DataAccess.Issuers(); string issuerid = ""; foreach (var item in issuerlist1) { issuerid = item.Id.ToString(); } var issuerlist = _i.FindById(issuerid); DataAccess.Issuers i = new DataAccess.Issuers(); i.Staffsobject = issuerlist.Staffsobject; i.Id = new ObjectId(issuerid); i.Image = issuerlist.Image; i.Name = issuerlist.Name; i.WebsiteUrl = issuerlist.WebsiteUrl; i.Description = issuerlist.Description; i.Email = issuerlist.Email; i.UserId = issuerlist.UserId; i.StaffsIds = issuerlist.StaffsIds; i.CreatedDate = issuerlist.CreatedDate; foreach (var j in i.Staffsobject) { if (j.Id == new ObjectId(id)) { j.FirstName = users.FirstName; j.LastName = users.LastName; j.Email = users.Email; _i.ReplaceOne(i); } } _u.ReplaceOne(a); return(RedirectToAction("ViewStaff")); }
public IActionResult IssuersDelete(string id) { ObjectId oId = new ObjectId(id); var issuer = _i.FindById(id); var users = _user.AsQueryable().ToList(); var userToDelete = users.Where(e => e.Id == issuer.UserId).FirstOrDefault(); DigiBadges.Models.Users usrMod = new DigiBadges.Models.Users() { CreatedBy = userToDelete.CreatedBy, CreatedDate = userToDelete.CreatedDate, Email = userToDelete.Email, FirstName = userToDelete.FirstName, IsUserVerified = userToDelete.IsUserVerified, Password = userToDelete.Password, RoleId = userToDelete.RoleId, UserId = userToDelete.Id }; DigiBadges.Models.Issuers issMod = new DigiBadges.Models.Issuers() { IssuerId = issuer.Id, Image = issuer.Image, Name = issuer.Name, WebsiteUrl = issuer.WebsiteUrl, Email = issuer.Email, Description = issuer.Description, UserId = issuer.UserId, StaffsIds = issuer.StaffsIds, CreatedDate = issuer.CreatedDate }; SolrUsersModel solUserMod = new SolrUsersModel(usrMod); SolrIssuersModel sissuser = new SolrIssuersModel(issMod); _user.DeleteById(userToDelete.Id.ToString()); //Deleting user from user collection var result = collection.DeleteOne <Issuers>(e => e.IssuerId == oId); //Deleting user from issuer collection if (result.DeletedCount > 0) { var results = _solr.Delete(solUserMod); //Saving the changes _solr.Commit(); _solrIssuer.Delete(sissuser); _solrIssuer.Commit(); } var badges = _b.FilterBy(e => e.IssuerId == issuer.Id).ToList(); if (badges != null) { _b.DeleteMany(e => e.IssuerId == issuer.Id); } return(RedirectToAction("Index")); }
public async Task <IActionResult> CreateAsync(Issuers issuers) { if (ModelState.IsValid) { try { var claimsIdentity = (ClaimsIdentity)User.Identity; var claim = claimsIdentity.Claims.ToArray(); var useid = User.Claims.FirstOrDefault(c => c.Type == AppUtility.UserId).Value; //to get userId of loggedIn user var userRole = _userRoles.AsQueryable().ToList(); //to get userRoleList var issuerRoles = userRole.Where(e => e.Role == AppUtility.IssuerRole).FirstOrDefault(); //find the object of issuer role string webRootPath = _hostEnvironment.WebRootPath; var files = HttpContext.Request.Form.Files; if (files.Count > 0) { string fileName = Guid.NewGuid().ToString(); var uploads = Path.Combine(webRootPath, @"Images\issuers"); var extenstion = Path.GetExtension(files[0].FileName); using (var filesStreams = new FileStream(Path.Combine(uploads, fileName + extenstion), FileMode.Create)) { files[0].CopyTo(filesStreams); } issuers.Image = @"\images\issuers\" + fileName + extenstion; } DateTime today = DateTime.Now; var isEmailExistInUser = _user.FilterBy(e => e.Email == issuers.Email).ToList(); var isEmailExistInIssuer = collection.Find(e => e.Email == issuers.Email).ToList(); if (isEmailExistInIssuer.Count() > 0 || isEmailExistInUser.Count() > 0) { ModelState.AddModelError(string.Empty, "User with this email already exist"); //show popup if same email exists return(View()); } DigiBadges.DataAccess.Users user = new DigiBadges.DataAccess.Users() { CreatedBy = claim[0].Value, CreatedDate = today, Email = issuers.Email, FirstName = issuers.Name, IsUserVerified = true, Password = AppUtility.Encrypt(AppUtility.IssuerPassword), RoleId = issuerRoles.Id.ToString() }; _user.InsertOne(user); //Inserting object in issuer table DigiBadges.Models.Users users = new DigiBadges.Models.Users() { CreatedBy = claim[0].Value, CreatedDate = today, Email = issuers.Email, FirstName = issuers.Name, IsUserVerified = true, Password = AppUtility.Encrypt(AppUtility.IssuerPassword), RoleId = issuerRoles.Id.ToString(), UserId = user.Id }; SolrUsersModel su = new SolrUsersModel(users); _solr.Add(su); //Adding data in solr _solr.Commit(); var userIdInUserTable = _user.AsQueryable().ToList(); var uid = userIdInUserTable.Where(e => e.Email == issuers.Email).FirstOrDefault(); if (user.Id != null) { issuers.UserId = user.Id; //setting the userId which we got after inserting the above data in user collection issuers.CreatedDate = today; } collection.InsertOne(issuers); //To post the issuer object SolrIssuersModel sissuser = new SolrIssuersModel(issuers); _solrIssuer.Add(sissuser); //Adding data in solr _solrIssuer.Commit(); await _emailSender.SendEmailAsync(issuers.Email, //to send email to new issuer "Congatulations you are invited as a issuer", $"<h4 class='m-2'>Your Email id is -{HtmlEncoder.Default.Encode(issuers.Email)}</h4></div>" + "Your password is - Welcome@123"); } catch (Exception) { ModelState.AddModelError(string.Empty, "Please try again later."); return(View()); } return(RedirectToAction("Index")); } return(View()); }
public IActionResult IssuersEdit(string id, Issuers issuer) { if (ModelState.IsValid) { string webRootPath = _hostEnvironment.WebRootPath; var files = HttpContext.Request.Form.Files; ObjectId oId = new ObjectId(id); Issuers issuers = collection.Find(e => e.IssuerId == oId).FirstOrDefault(); if (files.Count > 0) { string fileName = Guid.NewGuid().ToString(); var uploads = Path.Combine(webRootPath, @"images/issuers"); var extenstion = Path.GetExtension(files[0].FileName); if (issuer.Image != null) { //this is an edit and we need to remove old image var imagePath = Path.Combine(webRootPath, issuer.Image.TrimStart('\\')); if (System.IO.File.Exists(imagePath)) { System.IO.File.Delete(imagePath); } } using (var filesStreams = new FileStream(Path.Combine(uploads, fileName + extenstion), FileMode.Create)) { files[0].CopyTo(filesStreams); } issuer.Image = @"\images/issuers\" + fileName + extenstion; } else { //update when they do not change the image if (issuer.IssuerId != null) { issuer.Image = issuers.Image; } } var usr = _user.FindById(issuers.UserId.ToString()); usr.Email = issuer.Email; usr.FirstName = issuer.Name; _user.ReplaceOne(usr); //updating data in user collection var filter = Builders <Issuers> .Filter.Eq("IssuerId", oId); var updateDef = Builders <Issuers> .Update. Set("Name", issuer.Name); updateDef = updateDef.Set("Email", issuer.Email); updateDef = updateDef.Set("WebsiteUrl", issuer.WebsiteUrl); //updating data in Issuer collection updateDef = updateDef.Set("Image", issuer.Image); updateDef = updateDef.Set("Description", issuer.Description); var result = collection.UpdateOne(filter, updateDef); DigiBadges.Models.Users users = new DigiBadges.Models.Users() { CreatedBy = usr.CreatedBy, Email = issuer.Email, FirstName = issuer.Name, IsUserVerified = usr.IsUserVerified, Password = usr.Password, RoleId = usr.RoleId, UserId = usr.Id }; SolrUsersModel su = new SolrUsersModel(users); _solr.Add(su); _solr.Commit(); issuer.IssuerId = oId; issuer.UserId = usr.Id; SolrIssuersModel sissuser = new SolrIssuersModel(issuer); _solrIssuer.Add(sissuser); _solrIssuer.Commit(); return(RedirectToAction("Index")); } return(View()); }