public async Task <IActionResult> PutReview(int id, Review review)
        {
            if (id != review.ID || !_context.Games.Any(g => g.ID == review.GameId) ||
                !_context.Users.Any(u => u.ID == review.UserId) ||
                !_context.Languages.Any(l => l.ID == review.LanguageId))
            {
                return(BadRequest());
            }

            review.Game     = null;
            review.User     = null;
            review.Language = null;

            _context.Entry(review).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ReviewExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Example #2
0
        public async Task <IActionResult> PutPlatform(int id, Platform platform)
        {
            if (id != platform.ID)
            {
                return(BadRequest());
            }

            _context.Entry(platform).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!PlatformExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task <IActionResult> PutUser(int id, User user)
        {
            if (id != user.ID)
            {
                return(BadRequest());
            }

            _context.Entry(user).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!UserExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Example #4
0
        public static void UpdateGamePlatforms(this GameReviewsContext context, Game game)
        {
            foreach (GamePlatform gamePlatform in game.GamePlatforms)
            {
                if (gamePlatform.GameId != game.ID)
                {
                    throw new ArgumentException("Platform gameId doesnt match with game id");
                }
            }

            int newItemsCount = game.GamePlatforms.Count;

            context.Games
            .Include(x => x.GamePlatforms)
            .FirstOrDefault(x => x.ID == game.ID);

            int oldItemsCount = game.GamePlatforms.Count - newItemsCount;

            for (int i = newItemsCount; i < newItemsCount + oldItemsCount; ++i)
            {
                context.GamePlatforms.Remove(game.GamePlatforms[i]);
            }

            for (int i = 0; i < newItemsCount; ++i)
            {
                var existingGamePlatform = context.GamePlatforms.Where(gg => gg.GameId == game.GamePlatforms[i].GameId && gg.PlatformId == game.GamePlatforms[i].PlatformId).FirstOrDefault();

                if (existingGamePlatform != null && context.Entry(existingGamePlatform).State != EntityState.Deleted)
                {
                    throw new ArgumentException("Found duplicate of game platform");
                }

                context.GamePlatforms.Add(game.GamePlatforms[i]);
            }
        }
Example #5
0
        public async Task <IActionResult> PutGame(int id, Game game)
        {
            if (id != game.ID || !_context.Publishers.Any(p => p.ID == game.PublisherId) ||
                !_context.Developers.Any(d => d.ID == game.DeveloperId))
            {
                return(BadRequest());
            }

            game.Developer = null;
            game.Publisher = null;

            _context.Entry(game).State = EntityState.Modified;

            try
            {
                _context.UpdateGameGenres(game);
                _context.UpdateGamePlatforms(game);
            }
            catch (ArgumentException)
            {
                return(BadRequest());
            }

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!GameExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }