public IActionResult Registration([FromBody] RegistrationVM req)
        {
            var   currentUser = HttpContext.User;
            Int32 userId      = Convert.ToInt32(currentUser.Claims.FirstOrDefault(c => c.Type == "user_id").Value);

            LoginCredentials objLoginCredentials = new LoginCredentials();

            try
            {
                if (req != null && req.UserId > 0)
                {
                    LoginCredentials getloginCredentials = _dbContext.LoginCredentials.FirstOrDefault(x => x.UserId == req.UserId);
                    if (getloginCredentials != null)
                    {
                        getloginCredentials = _mapper.Map(req, getloginCredentials);
                        _dbContext.Entry(getloginCredentials).State = EntityState.Modified;
                        _dbContext.SaveChanges();
                    }
                }
                else
                {
                    objLoginCredentials             = _mapper.Map <LoginCredentials>(req);
                    objLoginCredentials.Dob         = req.Dob.Value.AddDays(1).Date;
                    objLoginCredentials.Username    = req.EmailId;
                    objLoginCredentials.IsActive    = true;
                    objLoginCredentials.IsDelete    = false;
                    objLoginCredentials.Profession  = "Agent";
                    objLoginCredentials.RoleId      = (Int32)Helper.EnumList.Roles.Agent;
                    objLoginCredentials.CreatedBy   = userId;
                    objLoginCredentials.CreatedDate = DateTime.Now;
                    objLoginCredentials.Password    = Helper.Helper.CreateRandomPassword();


                    _dbContext.Add(objLoginCredentials);
                    _dbContext.SaveChanges();

                    if (objLoginCredentials.UserId > 0)
                    {
                        #region Send Email for Reset password Link
                        Dictionary <string, string> replacement = new Dictionary <string, string>();
                        replacement.Add("#url", "http://localhost:4200/#/auth/login");
                        replacement.Add("#user", objLoginCredentials.Firstname + " " + objLoginCredentials.Lastname);
                        replacement.Add("#username", objLoginCredentials.EmailId);
                        replacement.Add("#password", Helper.Helper.Decrypt(objLoginCredentials.Password));
                        #endregion

                        bool IsSendMail = Helper.EmailSender.SendEmail(Convert.ToInt64(Helper.EnumList.EmailTemplateType.AgentRegistration), replacement, objLoginCredentials.EmailId);
                        if (IsSendMail)
                        {
                            objLoginCredentials = _dbContext.LoginCredentials.FirstOrDefault(x => x.UserId == objLoginCredentials.UserId);
                        }
                        else
                        {
                            objLoginCredentials = new LoginCredentials();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                return(StatusCode((int)HttpStatusCode.ExpectationFailed));
            }
            return(Ok(objLoginCredentials));
        }
Esempio n. 2
0
        public async Task <IActionResult> CustomerRegistration([FromForm] CustomerAddRequest request)
        {
            var   currentUser = HttpContext.User;
            Int32 userId      = Convert.ToInt32(currentUser.Claims.FirstOrDefault(c => c.Type == "user_id").Value);

            LoginCredentials objLoginCredentials = new LoginCredentials();
            string           folderName          = "Images";

            try
            {
                var customer = JsonConvert.DeserializeObject <CustomerViewModel>(request.customer);
                if (customer.UserID == 0)
                {
                    objLoginCredentials             = _mapper.Map <LoginCredentials>(customer);
                    objLoginCredentials.IsActive    = true;
                    objLoginCredentials.IsDelete    = false;
                    objLoginCredentials.Username    = customer.EmailID;
                    objLoginCredentials.RoleId      = (Int32)Helper.EnumList.Roles.Customer;
                    objLoginCredentials.CreatedBy   = userId;
                    objLoginCredentials.CreatedDate = DateTime.Now;
                    objLoginCredentials.Password    = Helper.Helper.CreateRandomPassword();

                    #region image upload

                    //string webRootPath = _hostingEnvironment.WebRootPath;
                    //string newPath = Path.Combine(webRootPath, folderName);
                    //if (!Directory.Exists(newPath))
                    //{
                    //    Directory.CreateDirectory(newPath);
                    //}
                    //if (file.Length > 0)
                    //{
                    //    string fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
                    //    string fullPath = Path.Combine(newPath, fileName);
                    //    using (var stream = new FileStream(fullPath, FileMode.Create))
                    //    {
                    //        file.CopyTo(stream);
                    //    }
                    //}

                    if (request.file != null) //return BadRequest("Null File");
                    {
                        if (request.file.Length == 0)
                        {
                            return(BadRequest("Empty File"));
                        }
                        if (request.file.Length > 2 * 1024 * 1024)
                        {
                            return(BadRequest("Max file size exceeded."));
                        }
                        string webRootPath     = _hostingEnvironment.WebRootPath;
                        string uploadFilesPath = Path.Combine(webRootPath, folderName);
                        if (!Directory.Exists(uploadFilesPath))
                        {
                            Directory.CreateDirectory(uploadFilesPath);
                        }
                        var fileName = Guid.NewGuid().ToString() + Path.GetExtension(request.file.FileName);
                        var filePath = Path.Combine(uploadFilesPath, fileName);
                        using (var stream = new FileStream(filePath, FileMode.Create))
                        {
                            await request.file.CopyToAsync(stream);
                        }

                        objLoginCredentials.ProfileImage = fileName;
                    }

                    #endregion

                    _context.Add(objLoginCredentials);
                    _context.SaveChanges();
                }
                else
                {
                    var data   = _context.LoginCredentials.FirstOrDefault(q => q.UserId == customer.UserID && q.IsActive == true && q.IsDelete == false);
                    var result = _mapper.Map(customer, data);

                    result.Username = customer.EmailID;

                    #region remove existing profile picture

                    if (request.file != null)
                    {
                        string webRootPath1 = _hostingEnvironment.WebRootPath;
                        string imagename1   = data.ProfileImage;
                        imagename1 = imagename1 == null?Guid.NewGuid().ToString() + Path.GetExtension(request.file.FileName) : imagename1;

                        string uploadFilesPath1 = Path.Combine(webRootPath1, folderName, imagename1);
                        using (var stream = new FileStream(uploadFilesPath1, FileMode.Create))
                        {
                            await request.file.CopyToAsync(stream);

                            result.ProfileImage = imagename1;
                        }
                    }

                    #endregion

                    _context.Entry(result).State = EntityState.Modified;
                    _context.SaveChanges();
                }

                return(Ok(new { success = true }));
            }
            catch (Exception e)
            {
                return(StatusCode((int)HttpStatusCode.ExpectationFailed));
            }
        }