public async Task <IActionResult> UpdateProfile(UserDto.Register model, IFormFile photo)
        {
            string uniqueFileName = null;

            if (photo != null)
            {
                string uploadsFolder = Path.Combine(_env.WebRootPath, "images");
                uniqueFileName = Guid.NewGuid().ToString() + "_" + photo.FileName;
                string filePath = Path.Combine(uploadsFolder, uniqueFileName);
                using (var fileStream = new FileStream(filePath, FileMode.Create))
                {
                    photo.CopyTo(fileStream);
                }
            }
            string userId = User.Claims.Where(c => c.Type == "UserId").FirstOrDefault().Value;
            var    user   = _context.User.Where(m => m.Id == Int32.Parse(userId)).FirstOrDefault();

            user.Username  = model.Username;
            user.Password  = model.Password;
            user.FirstName = model.FirstName;
            user.LastName  = model.LastName;
            user.BirthDate = (model.BirthDate != null) ? model.BirthDate : user.BirthDate;
            user.City      = model.City;
            user.Photo     = (photo != null) ? uniqueFileName : user.Photo;
            user.Phone     = (model.Phone != null) ? model.Phone : user.Phone;

            _context.User.Update(user);
            _context.SaveChanges();
            return(RedirectToAction("Account", new { id = user.Id }));
        }
Exemple #2
0
        public async Task <IActionResult> Register(UserDto.Register model, IFormFile photo)
        {
            string uniqueFileName = null;

            if (photo != null)
            {
                string uploadsFolder = Path.Combine(_env.WebRootPath, "images");
                uniqueFileName = Guid.NewGuid().ToString() + "_" + photo.FileName;
                string filePath = Path.Combine(uploadsFolder, uniqueFileName);
                using (var fileStream = new FileStream(filePath, FileMode.Create))
                {
                    photo.CopyTo(fileStream);
                }
            }

            var user = new User
            {
                Username  = model.Username,
                Password  = model.Password,
                FirstName = model.FirstName,
                LastName  = model.LastName,
                BirthDate = model.BirthDate,
                City      = model.City,
                Photo     = uniqueFileName,
                Phone     = model.Phone,
                RoleId    = 1,
            };

            _context.User.Add(user);
            _context.SaveChanges();
            return(RedirectToAction("Login", new UserDto.Login
            {
                Username = model.Username,
                Password = model.Password
            }));
        }