public async Task <UserPutDTO> PutUser(string id, UserPutDTO userPutDTO)
        {
            if (userPutDTO == null)
            {
                throw new ArgumentNullException(nameof(userPutDTO));
            }

            try
            {
                User user = await _context.Users.Include(u => u.WatchLists)
                            .FirstOrDefaultAsync(u => u.Id == id).ConfigureAwait(false);

                user.Name     = userPutDTO.Name;
                user.LastName = userPutDTO.LastName;
                user.Email    = userPutDTO.Email;
                user.UserName = userPutDTO.Username;
                user.Birthday = userPutDTO.Birthday;
                user.About    = userPutDTO.About;
                user.Image    = userPutDTO.Image;

                _context.WatchLists.RemoveRange(user.WatchLists);

                foreach (WatchListDTO watchListDTO in userPutDTO.WatchListDTOs)
                {
                    Movie movie = _context.Movies.Find(watchListDTO.MovieId);
                    user.WatchLists.Add(new WatchList()
                    {
                        UserId  = user.Id,
                        User    = user,
                        MovieId = movie.Id,
                        Movie   = movie,
                        Status  = getStatus(watchListDTO.Status),
                        Score   = watchListDTO.Score
                    });
                }
                await _userManager.UpdateAsync(user).ConfigureAwait(false);
            }
            catch (DbUpdateConcurrencyException)
            {
                if (await UserExists(id).ConfigureAwait(false) == false)
                {
                    return(null);
                }
                else
                {
                    throw;
                }
            }
            return(userPutDTO);
        }
Пример #2
0
        public async Task <IActionResult> PutUser(string id, UserPutDTO userPutDTO)
        {
            if (userPutDTO == null)
            {
                throw new ArgumentNullException(nameof(userPutDTO));
            }

            if (id != userPutDTO.Id)
            {
                return(BadRequest());
            }

            var userResult = await _userRepository.PutUser(id, userPutDTO).ConfigureAwait(false);

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

            return(NoContent());
        }
Пример #3
0
        public async Task <UserPutDTO> PutUser(string id, UserPutDTO userPutDTO)
        {
            if (userPutDTO == null)
            {
                throw new ArgumentNullException(nameof(userPutDTO));
            }

            try
            {
                User user = await _userManager.FindByIdAsync(id).ConfigureAwait(false);

                user.FirstName = userPutDTO.FirstName;
                user.LastName  = userPutDTO.LastName;
                user.Email     = userPutDTO.Email;
                //user.SetAvatar(userPutDTO.Avatar);

                var roles = await _userManager.GetRolesAsync(user).ConfigureAwait(false);

                await _userManager.RemoveFromRolesAsync(user, roles.ToArray()).ConfigureAwait(false);

                await _userManager.AddToRolesAsync(user, userPutDTO.Roles).ConfigureAwait(false);

                await _userManager.UpdateAsync(user).ConfigureAwait(false);
            }
            catch (DbUpdateConcurrencyException)
            {
                if (await UserExists(id).ConfigureAwait(false) == false)
                {
                    return(null);
                }
                else
                {
                    throw;
                }
            }

            return(userPutDTO);
        }