Пример #1
0
        public async Task <IActionResult> Update([FromBody] UserUpdatableDeserializer userDsl)
        {
            string email = UserServices.ReadJwtTokenClaims(Request.Headers["Authorization"]);
            User   user  = await Context.User
                           .Include(i => i.Role)
                           .FirstOrDefaultAsync((User u) => u.Email == email);

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

            if (!string.IsNullOrEmpty(userDsl.Password) && !UserServices.IsStudent(user))
            {
                UserServices.GeneratePasswordSha256(userDsl.Password, out var encodedPassword);
                UserServices.GeneratePasswordSha256(userDsl.PasswordConfirmation, out var confirmedPassword);
                userDsl.Password             = encodedPassword;
                userDsl.PasswordConfirmation = confirmedPassword;
            }

            try {
                UserServices.Update(userDsl, user);
                Context.Update(user);
                Context.SaveChanges();
            } catch (DbUpdateException update) {
                return(BadRequest(update.Message));
            } catch (PasswordConfirmationException e) {
                return(BadRequest(e.Message));
            }

            return(Ok());
        }
Пример #2
0
        public void Update(UserUpdatableDeserializer userDsl, User user)
        {
            if (userDsl.Password != null && !userDsl.Password.Equals(userDsl.PasswordConfirmation))
            {
                throw new PasswordConfirmationException("Confirmed password is not valid");
            }

            if (userDsl.LastName != null)
            {
                user.LastName = userDsl.LastName;
            }
            if (userDsl.FirstName != null)
            {
                user.FirstName = userDsl.FirstName;
            }
            if (userDsl.Email != null)
            {
                user.Email = userDsl.Email;
            }
            if (userDsl.Password != null)
            {
                user.Password = userDsl.Password;
            }
            if (userDsl.PhoneNumber != null)
            {
                user.PhoneNumber = int.Parse(userDsl.PhoneNumber);
            }
            if (userDsl.ImageUrl != null)
            {
                user.ImageUrl = userDsl.ImageUrl;
            }
            if (userDsl.Location != null)
            {
                user.Location = userDsl.Location;
            }
            if (userDsl.DateOfBirth.HasValue)
            {
                user.DateOfBirth = userDsl.DateOfBirth;
            }
        }