using GameLibrary.Models; using GameLibrary.Repositories; using Microsoft.AspNetCore.Mvc; using System.Collections.Generic; using System.Threading.Tasks; namespace GameLibrary.Controllers { [Route("api/[controller]")] [ApiController] public class GamesController : ControllerBase { private readonly IGameRepository _gameRepository; public GamesController(IGameRepository gameRepository) { _gameRepository = gameRepository; } // GET: api/games [HttpGet] public async Task>> GetGames(string genre = null) { var games = await _gameRepository.GetAllGames(genre); return Ok(games); } // GET: api/games/{id} [HttpGet("{id}")] public async Task> GetGame(int id) { var game = await _gameRepository.GetGameById(id); if (game == null) { return NotFound(); } return Ok(game); } // POST: api/games [HttpPost] public async Task> CreateGame(Game game) { await _gameRepository.CreateGame(game); return CreatedAtAction(nameof(GetGame), new { id = game.Id }, game); } // PUT: api/games/{id} [HttpPut("{id}")] public async Task UpdateGame(int id, Game game) { if (id != game.Id) { return BadRequest(); } await _gameRepository.UpdateGame(game); return NoContent(); } // DELETE: api/games/{id} [HttpDelete("{id}")] public async Task DeleteGame(int id) { await _gameRepository.DeleteGame(id); return NoContent(); } } }