120 lines
3.8 KiB
C#
120 lines
3.8 KiB
C#
using Microsoft.AspNetCore.Mvc;
|
|
using Server.Models;
|
|
using Server.Repository;
|
|
using System.Text.Json;
|
|
|
|
// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
|
|
|
|
namespace Server.Controllers
|
|
{
|
|
[Route("api/[controller]")]
|
|
[ApiController]
|
|
public class GameController : ControllerBase
|
|
{
|
|
private IGameRepository _repository;
|
|
|
|
public GameController(IGameRepository repository)
|
|
{
|
|
_repository = repository;
|
|
}
|
|
|
|
// GET: api/<GameController>
|
|
[HttpGet]
|
|
[ProducesResponseType<List<Game>>(StatusCodes.Status200OK)]
|
|
public ActionResult<List<Game>> Get()
|
|
{
|
|
return Ok(_repository.GetAllGames());
|
|
}
|
|
|
|
// GET api/<GameController>/5
|
|
[HttpGet("{id}")]
|
|
[ProducesResponseType<Game>(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
public ActionResult<Game> Get(int id)
|
|
{
|
|
Game result = _repository.GetGame(id);
|
|
if (result != null)
|
|
{
|
|
return Ok(result);
|
|
}
|
|
else
|
|
{
|
|
return BadRequest("Spiel konnte nicht gefunden werden");
|
|
}
|
|
}
|
|
|
|
// POST api/<GameController>
|
|
[HttpPost]
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
public IActionResult Post([FromBody] string value)
|
|
{
|
|
try
|
|
{
|
|
Game newGame = JsonSerializer.Deserialize<Game>(value);
|
|
|
|
bool result = _repository.AddGame(newGame);
|
|
if (result)
|
|
{
|
|
return Ok("Aktion wurde erfolgreich durchgeführt");
|
|
}
|
|
else
|
|
{
|
|
return StatusCode(StatusCodes.Status500InternalServerError, "In der Datenbank ist ein Fehler aufgetreten");
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e);
|
|
return BadRequest("Ungültige Spieldaten");
|
|
}
|
|
}
|
|
|
|
// PUT api/<GameController>/5
|
|
[HttpPut("{id}")]
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
public IActionResult Put(int id, [FromBody] string value)
|
|
{
|
|
try
|
|
{
|
|
Game editedGame = JsonSerializer.Deserialize<Game>(value);
|
|
|
|
bool result = _repository.EditGame(id, editedGame);
|
|
if (result)
|
|
{
|
|
return Ok("Aktion wurde erfolgreich durchgeführt");
|
|
}
|
|
else
|
|
{
|
|
return StatusCode(StatusCodes.Status500InternalServerError, "In der Datenbank ist ein Fehler aufgetreten");
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e);
|
|
return BadRequest("Ungültige Spieldaten");
|
|
}
|
|
}
|
|
|
|
// DELETE api/<GameController>/5
|
|
[HttpDelete("{id}")]
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
|
public IActionResult Delete(int id)
|
|
{
|
|
bool result = _repository.RemoveGame(id);
|
|
if (result)
|
|
{
|
|
return Ok("Aktion wurde erfolgreich durchgeführt");
|
|
}
|
|
else
|
|
{
|
|
return StatusCode(StatusCodes.Status500InternalServerError, "In der Datenbank ist ein Fehler aufgetreten");
|
|
}
|
|
}
|
|
}
|
|
}
|