public async Task <IActionResult> Edit(UserUpdateInputModel inputModel)
        {
            if (!this.ModelState.IsValid)
            {
                var location  = this.GetLocation();
                var countryId = await this.countriesService.GetIdAsync(location.Country);

                inputModel.Sports = await this.sportsService.GetAllAsSelectListAsync();

                inputModel.Countries = await this.countriesService.GetAllAsSelectListAsync();

                inputModel.Cities = await this.citiesService.GetAllInCountryByIdAsync(countryId);

                inputModel.CountryId = countryId;
                inputModel.CityId    = await this.citiesService.GetIdAsync(location.City, countryId);

                return(this.View(inputModel));
            }

            var user = await this.userManager.GetUserAsync(this.User);

            await this.usersService.UpdateAsync(inputModel, user.Id, user.Email, user.UserName);

            this.TempData[TempDataMessage] = UserUpdated;

            return(this.RedirectToAction(nameof(this.Index)));
        }
Exemple #2
0
        public async Task UpdateAsync(UserUpdateInputModel inputModel, string userId, string userEmail, string username)
        {
            var user = await this.GetUserByIdAsIQueryable(userId).FirstAsync();

            user.FirstName            = inputModel.FirstName;
            user.LastName             = inputModel.LastName;
            user.Gender               = inputModel.Gender;
            user.SportId              = inputModel.SportId;
            user.Status               = inputModel.Status;
            user.CountryId            = inputModel.CountryId;
            user.CityId               = inputModel.CityId;
            user.PhoneNumber          = inputModel.PhoneNumber;
            user.FaceBookAccount      = inputModel.FaceBookAccount;
            user.Age                  = inputModel.Age;
            user.Occupation           = inputModel.Occupation;
            user.IsUserProfileUpdated = true;

            if (inputModel.AvatarImage != null)
            {
                var avatar = await this.imagesService.CreateAsync(inputModel.AvatarImage);

                user.AvatarId = avatar.Id;
            }

            this.usersRepository.Update(user);
            await this.usersRepository.SaveChangesAsync();

            await this.emailSender.SendEmailAsync(
                userEmail,
                EmailSubjectConstants.ProfileUpdated,
                EmailHtmlMessages.GetUpdateProfileHtml(username));
        }
        public async Task IsProfileUpdatedAsyncReturnsTrueIfUpdated()
        {
            var inputModel = new UserUpdateInputModel
            {
                Age       = 20,
                FirstName = "Test",
                LastName  = "Testov",
                Status    = UserStatus.ProposalOpen,
            };

            await this.Service.UpdateAsync(inputModel, this.userId, "email", "username");

            Assert.True(await this.Service.IsProfileUpdatedAsync(this.userId));
        }
        public Task <ActionResult <UserViewModel> > PatchAsync([FromBody] UserUpdateInputModel inputModel)
        => ExecuteAsync <UserViewModel>(async() =>
        {
            var userId = User.UserId();
            var user   = await _usersRepository.GetByIdAsync(userId);

            if (user == null)
            {
                return(NotFound());
            }

            if (inputModel.Name != null)
            {
                user.SetName(inputModel.Name);
            }

            if (inputModel.Email != null)
            {
                user.SetEmail(inputModel.Email);
            }

            if (inputModel.Password != null)
            {
                user.SetPassword(inputModel.Password);
            }

            if (inputModel.BirthDate != null)
            {
                user.SetBirthDate(inputModel.BirthDate.Value);
            }

            if (inputModel.Sex != null)
            {
                var sex = Gender.NotDisclosed;
                Enum.TryParse <Gender>(inputModel.Sex, true, out sex);
                user.Gender = sex;
            }

            await _usersRepository.UpdateAsync(user);

            return(Ok((UserViewModel)user));
        });
        public async Task UpdateAsyncUpdatesAvatarImage()
        {
            using var stream = File.OpenRead(TestImagePath);
            var file = new FormFile(stream, 0, stream.Length, null, Path.GetFileName(stream.Name))
            {
                Headers     = new HeaderDictionary(),
                ContentType = TestImageContentType,
            };
            var inputModel = new UserUpdateInputModel
            {
                AvatarImage = file,
            };

            await this.Service.UpdateAsync(inputModel, this.userId, "email", "username");

            var image = this.DbContext.Images.Where(i => i.User.Id == this.userId).First();

            Assert.NotNull(image.Url);
            Assert.StartsWith("v", image.Url);
        }
        public async Task GetUserAvatarUrlRerunsValidAvatarUrl()
        {
            using var stream = File.OpenRead(TestImagePath);
            var file = new FormFile(stream, 0, stream.Length, null, Path.GetFileName(stream.Name))
            {
                Headers     = new HeaderDictionary(),
                ContentType = TestImageContentType,
            };
            var inputModel = new UserUpdateInputModel
            {
                AvatarImage = file,
            };

            await this.Service.UpdateAsync(inputModel, this.userId, "email", "username");

            var result   = this.Service.GetUserAvatarUrl(this.userId);
            var imageUrl = this.DbContext.Images
                           .Where(i => i.User.Id == this.userId).Select(i => i.Url).First();

            Assert.Contains(imageUrl, result);
        }
Exemple #7
0
        public async Task <ActionResult <ResponseModel <UserModel> > > UpdateAsync([FromBody] UserUpdateInputModel input)
        {
            var user = await _userManager.UpdateAsync(
                CurrentUser.Id,
                input.FirstName,
                input.LastName,
                input.Email,
                input.Address,
                input.Password,
                input.Currency,
                input.HourlyRate
                );

            if (user != null)
            {
                return(Ok(new ResponseModel <UserModel>(new UserModel(user))));
            }
            else
            {
                return(BadRequest(new ErrorModel("Unable to update User.")));
            }
        }
        public async Task UpdateAsyncUpdatesDataCorrectly()
        {
            var inputModel = new UserUpdateInputModel
            {
                Age             = 20,
                FirstName       = "Test",
                LastName        = "Testov",
                FaceBookAccount = "https://facebookAccount.com",
                Gender          = Gender.Male,
                Occupation      = "student",
            };

            await this.Service.UpdateAsync(inputModel, this.userId, "email", "username");

            var user = this.DbContext.ApplicationUsers.Where(u => u.Id == this.userId).First();

            Assert.Equal(20, user.Age);
            Assert.Equal("Test", user.FirstName);
            Assert.Equal("Testov", user.LastName);
            Assert.Equal("https://facebookAccount.com", user.FaceBookAccount);
            Assert.Equal("student", user.Occupation);
        }