Пример #1
0
        public async Task Add(JobForTableDTO jobDTO)
        {
            var       UserId   = Convert.ToInt32(_context.HttpContext.User.Claims.Where(x => x.Type == "UserId").First().Value);
            Job       job      = new Job();
            IFormFile file     = jobDTO.Image;
            string    fullPath = null;
            var       imageId  = 0;

            if (file != null)
            {
                string folderName  = "Upload";
                string webRootPath = _hostingEnvironment.WebRootPath;
                if (string.IsNullOrWhiteSpace(webRootPath))
                {
                    webRootPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot");
                }
                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('"');
                    fullPath = Path.Combine(newPath, fileName);
                    using (var stream = new FileStream(fullPath, FileMode.Create))
                    {
                        file.CopyTo(stream);
                    }
                }
                imageId     = _serviceImage.GetIdInsertedImage(fullPath);
                job.ImageId = imageId;
            }


            job.UserId     = UserId;
            job.PostDate   = DateTime.Now;
            job.CategoryId = jobDTO.CategoryId;
            job.CityId     = jobDTO.CityId;
            job.Title      = jobDTO.Title;
            job.Salary     = jobDTO.Salary;
            job.TypeJobId  = jobDTO.TypeJobId;
            job.EndDate    = jobDTO.FinishedOn;
            job.Contact    = jobDTO.Contact;

            await _jobRepository.Add(job);
        }
Пример #2
0
        public async Task UpdateProfile(UserRegisterDto userRegisterDto)
        {
            var UserId      = Convert.ToInt32(_context.HttpContext.User.Claims.Where(x => x.Type == "UserId").First().Value);
            var userProfile = await _profileRepository.GetAuthUserProfile(UserId);

            Domain.Entities.Profile profile = new Domain.Entities.Profile();


            IFormFile file     = userRegisterDto.Image;
            string    fullPath = null;
            var       imageId  = 0;

            if (file != null)
            {
                string folderName  = "Upload";
                string webRootPath = _hostingEnvironment.WebRootPath;
                if (string.IsNullOrWhiteSpace(webRootPath))
                {
                    webRootPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot");
                }
                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('"');
                    fullPath = Path.Combine(newPath, fileName);
                    using (var stream = new FileStream(fullPath, FileMode.Create))
                    {
                        file.CopyTo(stream);
                    }
                }
                imageId             = _serviceImage.GetIdInsertedImage(fullPath);
                userProfile.ImageId = imageId;
            }

            userProfile.FirstName   = userRegisterDto.FirstName;
            userProfile.LastName    = userRegisterDto.LastName;
            userProfile.PhoneNumber = userRegisterDto.PhoneNumber;
            userProfile.DateOfBirth = userRegisterDto.DateOfBirth;
            userProfile.Email       = userProfile.Email;

            await _profileRepository.UpdateProfile(userProfile);
        }
Пример #3
0
        public async Task <IActionResult> AddUser(UserRegisterDto userRegisterDto)
        {
            UserRegisterDto userRegister = new UserRegisterDto();

            JobSolution.Domain.Entities.Profile userProfile = new Domain.Entities.Profile();

            IFormFile file     = userRegisterDto.Image;
            string    fullPath = null;
            int?      imageId  = null;

            if (file != null)
            {
                string folderName  = "Profile";
                string webRootPath = _hostingEnvironment.WebRootPath;
                if (string.IsNullOrWhiteSpace(webRootPath))
                {
                    webRootPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot");
                }
                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('"');
                    fullPath = Path.Combine(newPath, fileName);
                    using (var stream = new FileStream(fullPath, FileMode.Create))
                    {
                        file.CopyTo(stream);
                    }
                }

                imageId             = _serviceImage.GetIdInsertedImage(fullPath);
                userProfile.ImageId = imageId;
            }

            if (userRegisterDto == null)
            {
                return(new StatusCodeResult(500));
            }

            var findExistedProfile = await _userManager.FindByEmailAsync(userRegisterDto.Email);

            if (findExistedProfile != null)
            {
                return(new BadRequestObjectResult("Email or username exist!"));
            }

            var CreateUserToAdd = new User()
            {
                SecurityStamp = Guid.NewGuid().ToString(),
                UserName      = userRegisterDto.UserName,
                Email         = userRegisterDto.Email
            };


            await _userManager.CreateAsync(CreateUserToAdd, userRegisterDto.Password);

            await _userManager.AddToRoleAsync(CreateUserToAdd, userRegisterDto.RoleFromRegister);


            var Profile = new JobSolution.Domain.Entities.Profile
            {
                FirstName   = userRegisterDto.FirstName,
                LastName    = userRegisterDto.LastName,
                Email       = userRegisterDto.Email,
                PhoneNumber = userRegisterDto.PhoneNumber,
                UserId      = _userManager.FindByEmailAsync(CreateUserToAdd.Email).Result.Id,
                ImageId     = imageId
            };

            _dbContext.Profiles.Add(Profile);
            _dbContext.SaveChanges();


            var signinCredentials = new SigningCredentials(_authOptions.GetSymmetricSecurityKey(), SecurityAlgorithms.HmacSha256);
            var jwtSecurityToken  = new JwtSecurityToken(
                issuer: _authOptions.Issuer,
                audience: _authOptions.Audience,
                claims: new List <Claim>()
            {
                new Claim(ClaimTypes.Role, userRegisterDto.RoleFromRegister),
                new Claim(ClaimTypes.NameIdentifier, Profile.UserId.ToString()),
                new Claim("UserId", Profile.UserId.ToString())
            },
                expires: DateTime.Now.AddDays(30),
                signingCredentials: signinCredentials);

            var tokenHandler = new JwtSecurityTokenHandler();

            var encodedToken = tokenHandler.WriteToken(jwtSecurityToken);

            return(new OkObjectResult(new { AccessToken = encodedToken }));
        }