public async Task <bool> UpdateUserProfile(UserProfileUpdateDTO dto)
        {
            var rao    = _mapper.Map <UserProfileUpdateRAO>(dto);
            var engine = new SaveFileEngine();
            var uri    = engine.Upload(dto.PhotoUpload);

            rao.ProfilePicture = uri;

            if (await _repository.UpdateUserProfile(rao))
            {
                return(true);
            }

            throw new Exception("User Profile could not be updated.");
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> UpdateUserProfileV2([FromForm] UserProfileUpdateDTO model)
        {
            await CheckIsSignoutedAsync();

            if (ModelState.IsValid)
            {
                // check user exist
                var user = await _context.Users.FindAsync(this.CurrentUserId);

                if (user == null)
                {
                    throw new ApiException("User not found", (int)HttpStatusCode.BadRequest);
                }

                // check password
                if (!_authRepository.VerifyPasswordHash(model.CurrentPassword, user.HashPassword, user.SaltPassword))
                {
                    throw new ApiException("Wrong password.", (int)HttpStatusCode.BadRequest);
                }
                // update user
                _context.Entry(user).State = EntityState.Modified;
                user.FirstName             = model.FirstName;
                user.LastName    = model.LastName;
                user.Gender      = model.Gender;
                user.PhoneNumber = model.PhoneNumber;
                user.UpdatedAt   = DateTime.UtcNow;

                // Update Birth day
                if (model.YearOfBirth.HasValue && model.MonthOfBirth.HasValue && model.DayOfBirth.HasValue)
                {
                    user.BirthDay = new DateTime(model.YearOfBirth.Value, model.MonthOfBirth.Value, model.DayOfBirth.Value);
                }

                if (!string.IsNullOrEmpty(model.NewPassword))
                {
                    var hashPass = Helpers.SercurityHelper.GenerateSaltedHash(model.NewPassword);
                    user.HashPassword = hashPass.Hash;
                    user.SaltPassword = hashPass.Salt;
                }

                // add User Detail
                var userDetail = await _context.UserDetails.FindAsync(user.Id);

                if (userDetail != null)
                {
                    // update
                    userDetail.ProvideId = model.ProviderId ?? string.Empty;
                    userDetail.Address1  = model.Address1 ?? string.Empty;
                    userDetail.Address2  = model.Address2 ?? string.Empty;
                    userDetail.Address3  = model.Address3 ?? string.Empty;
                    userDetail.GAvartar  = model.GAvartar ?? string.Empty;
                }
                else
                {
                    UserDetail newUserDetail = new UserDetail();
                    newUserDetail.ProvideId = model.ProviderId ?? string.Empty;
                    newUserDetail.Address1  = model.Address1 ?? string.Empty;
                    newUserDetail.Address2  = model.Address2 ?? string.Empty;
                    newUserDetail.Address3  = model.Address3 ?? string.Empty;
                    newUserDetail.GAvartar  = model.GAvartar ?? string.Empty;
                    _context.UserDetails.Add(newUserDetail);
                }

                // save to database
                try
                {
                    await _context.SaveChangesAsync();
                }
                catch (System.Exception ex)
                {
                    {
                        _logger.Log(LogLevel.Warning, "Có lỗi xảy ra khi update user profile", ex.Message);
#if DEBUG
                        throw new ApiException(ex);
#else
                        throw new ApiException("Có lỗi xảy ra khi update database");
#endif
                    }
                }
            }

            return(Ok(1));
        }