示例#1
0
        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 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());
        }