Example #1
0
        public async Task <ActionResult> SaveUserFavoriteMovies([FromBody] AddUserFavoriteMovieDTO addUserFavoriteMovieDTO)
        {
            var result = await _moviesService.SaveUserFavoriteMoviesAsync(addUserFavoriteMovieDTO);

            if (result == -2)
            {
                ModelState.TryAddModelError("movieId", $"Movie with ID {addUserFavoriteMovieDTO.MovieId} was not found.");
                return(NotFound(ModelState));
            }
            else if (result == -3)
            {
                ModelState.TryAddModelError("userId", $"User with ID {addUserFavoriteMovieDTO.UserId} was not found.");
                return(NotFound(ModelState));
            }
            else if (result == -1)
            {
                ModelState.TryAddModelError("saveChangesAsync", "Failed to save changes async.");
                return(NotFound(ModelState));
            }
            else if (result == 0)
            {
                ModelState.TryAddModelError("alreadyExists", "User already favorited this movie.");
                return(BadRequest(ModelState));
            }
            else
            {
                return(NoContent());
            }
        }
Example #2
0
        public async Task <int> SaveUserFavoriteMovieAsync(AddUserFavoriteMovieDTO addUserFavoriteMovieDTO)
        {
            var movie = await _dbContext.Movies.AsTracking()
                        .FirstOrDefaultAsync(m => m.Id == addUserFavoriteMovieDTO.MovieId);

            if (movie == null)
            {
                return(-2);
            }

            var user = await _dbContext.Users.AsTracking()
                       .Include(x => x.FavoriteMovies)
                       .FirstOrDefaultAsync(u => u.Id == addUserFavoriteMovieDTO.UserId);

            if (user == null)
            {
                return(-3);
            }

            if (user.FavoriteMovies.Contains(movie))
            {
                return(0);
            }

            user.FavoriteMovies.Add(movie);

            try
            {
                await _dbContext.SaveChangesAsync();

                return(1);
            }
            catch (System.Exception ex)
            {
                _logger.LogWarn($"Failed to SaveUserFavoriteMoviesAsync. Exception: {ex}");
                return(-1);
            }
        }
Example #3
0
        public async Task <int> SaveUserFavoriteMoviesAsync(AddUserFavoriteMovieDTO addUserFavoriteMovieDTO)
        {
            addUserFavoriteMovieDTO.UserId = await _accountsService.GetCurrentUserIdAsync();

            return(await _userFavoriteService.SaveUserFavoriteMovieAsync(addUserFavoriteMovieDTO));
        }