using System;
using DTOs;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BowlingEF.Entities;
using BowlingLib.Model;
using BowlingService;
using BowlingService.Interfaces;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace BowlingApi.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class PartieController : ControllerBase
{
private IpartieService _partieService;
public PartieController(IpartieService partieService)
{
_partieService = partieService;
}
///
/// Get all partie
/// GET: api/parti
///
/// la liste des parti
/// Retourne la liste des joueurs
/// Si la liste est vide
/// Si une erreur est survenue
// GET: api/Partie
[HttpGet]
[ProducesResponseType(typeof(string), StatusCodes.Status500InternalServerError)]
[ProducesResponseType(typeof(string), StatusCodes.Status404NotFound)]
[ProducesResponseType(typeof(string), StatusCodes.Status400BadRequest)]
[ProducesResponseType(typeof(JoueurDTO), StatusCodes.Status200OK)]
public async Task Get()
{
try
{
var result = await _partieService.GetAll();
if (result == null)
{
return NotFound();
}
return Ok(result);
}
catch (Exception e)
{
return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
throw;
}
//var result = _partieService.GetAll().Result;
//return Ok(result);
}
// GET: api/Partie/djon
[HttpGet("{id}")]
public async Task Get(int id)
{
// return Ok(_partieService.GetDataWithName(name));
try
{
if (id == null)
return BadRequest("Le nom de la partie est obligatoire");
var result = _partieService.GetDataWithId(id).Result;
if (result == null)
{
return NotFound();
}
return Ok(result);
}
catch (Exception e)
{
return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
throw;
}
}
///
/// Creeation
/// POST: api/parti
///
///
/// Retourne la parti créé
/// Si la parti est null
/// Si une erreur est survenue
[HttpPost]
[ProducesResponseType(typeof(string), StatusCodes.Status500InternalServerError)]
[ProducesResponseType(typeof(string), StatusCodes.Status400BadRequest)]
[ProducesResponseType(typeof(JoueurDTO), StatusCodes.Status201Created)]
public async Task> Post([FromBody] PartieDTO parti)
{
try
{
if (parti == null)
{
return BadRequest("partie est obligatoire");
}
var createdpartie = _partieService.Add(parti).Result;
return CreatedAtAction(nameof(Get), new { id = parti.Id }, createdpartie);
}
catch (Exception e)
{
return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
throw;
}
}
///
/// Modification partie
/// PUT: api/parti/5
///
///
///
/// Retourne la modification
/// Si le partie est null
/// Si le partie n'existe pas
/// Si une erreur est survenue
[HttpPut("{id}")]
[ProducesResponseType(typeof(string), StatusCodes.Status500InternalServerError)]
[ProducesResponseType(typeof(string), StatusCodes.Status404NotFound)]
[ProducesResponseType(typeof(string), StatusCodes.Status400BadRequest)]
[ProducesResponseType(typeof(PartieDTO), StatusCodes.Status200OK)]
public async Task> Put(long id, [FromBody] PartieDTO partie)
{
try
{
if (partie == null)
return BadRequest("La partie est obligatoire");
var updatepartie = _partieService.Update(partie);
if (updatepartie.Result == null)
{
return NotFound();
}
return Ok(updatepartie);
}
catch (Exception e)
{
StatusCode(StatusCodes.Status500InternalServerError, e.Message);
throw;
}
}
}
}