You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
244 lines
8.4 KiB
244 lines
8.4 KiB
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;
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// Get all partie
|
|
/// GET: api/parti
|
|
/// </summary>
|
|
/// <returns>la liste des parti </returns>
|
|
/// <response code="200">Retourne la liste des joueurs</response>
|
|
/// <response code="404">Si la liste est vide</response>
|
|
/// <response code="500">Si une erreur est survenue</response>
|
|
// 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<IActionResult> 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);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get a partie by id
|
|
/// GET: api/parti/Djon
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <response code="200">Retourne la parti</response>
|
|
/// <response code="400">Si le id null</response>
|
|
/// <response code="404">Si la partie existe pas</response>
|
|
/// <response code="500">Si une erreur est survenue</response>
|
|
[HttpGet("{id}")]
|
|
public async Task<IActionResult> 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;
|
|
}
|
|
|
|
}
|
|
/// <summary>
|
|
/// Creeation
|
|
/// POST: api/parti
|
|
/// </summary>
|
|
/// <param name="parti"></param>
|
|
/// <response code="201">Retourne la parti créé</response>
|
|
/// <response code="400">Si la parti est null</response>
|
|
/// <response code="500">Si une erreur est survenue</response>
|
|
[HttpPost]
|
|
[ProducesResponseType(typeof(string), StatusCodes.Status500InternalServerError)]
|
|
[ProducesResponseType(typeof(string), StatusCodes.Status400BadRequest)]
|
|
[ProducesResponseType(typeof(JoueurDTO), StatusCodes.Status201Created)]
|
|
public async Task<ActionResult<PartieDTO>> 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;
|
|
}
|
|
|
|
}/// <summary>
|
|
/// Get player with pagination
|
|
/// Get : api/Joueur?page=1&pageSize=10
|
|
/// </summary>
|
|
/// <returns>la liste des Joueurs </returns>
|
|
/// <response code="200">Retourne la liste des joueurs</response>
|
|
/// <response code="404">Si la liste est vide</response>
|
|
/// <response code="500">Si une erreur est survenue</response>
|
|
|
|
[HttpGet("{page}/{pageSize}")]
|
|
[ProducesResponseType(typeof(string), StatusCodes.Status500InternalServerError)]
|
|
[ProducesResponseType(typeof(string), StatusCodes.Status404NotFound)]
|
|
[ProducesResponseType(typeof(IEnumerable<PartieDTO>), StatusCodes.Status200OK)]
|
|
public async Task<IActionResult> Get(int page = 1, int pageSize = 10)
|
|
{
|
|
try
|
|
{
|
|
var result = await _partieService.GetAll();
|
|
if (result == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
var data = result.Skip((page - 1) * pageSize).Take(pageSize);
|
|
Response.Headers.Add("X-Pagination", Newtonsoft.Json.JsonConvert.SerializeObject(new
|
|
{
|
|
totalCount = result.Count(),
|
|
pageSize = pageSize,
|
|
currentPage = page,
|
|
totalPages = (int)Math.Ceiling(result.Count() / (double)pageSize)
|
|
}));
|
|
return Ok(data);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
|
|
throw;
|
|
}
|
|
}/// <summary>
|
|
/// Supprimer une partie par son id
|
|
/// DELETE: api/parti/5
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <response code="200">Retourne la partie supprimé</response>
|
|
/// <response code="404">Si la partie n'existe pas</response>
|
|
/// <response code="500">Si une erreur est survenue</response>
|
|
[HttpDelete("{id}")]
|
|
[ProducesResponseType(typeof(string), StatusCodes.Status500InternalServerError)]
|
|
[ProducesResponseType(typeof(string), StatusCodes.Status404NotFound)]
|
|
[ProducesResponseType(typeof(bool), StatusCodes.Status200OK)]
|
|
// [MapToApiVersion("2")]
|
|
//public async Task<ActionResult<bool>> Delete(long id)
|
|
//{
|
|
// try
|
|
// {
|
|
// var parti = _partieService.Delete(id);
|
|
// if (parti.Result == false)
|
|
// {
|
|
// return NotFound();
|
|
// }
|
|
// return Ok(parti);
|
|
// }
|
|
// catch (Exception e)
|
|
// {
|
|
// StatusCode(StatusCodes.Status500InternalServerError, e.Message);
|
|
// throw;
|
|
// }
|
|
// }
|
|
|
|
|
|
/// <summary>
|
|
/// Modification partie
|
|
/// PUT: api/parti/5
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <param name="partie"></param>
|
|
/// <response code="200">Retourne la modification</response>
|
|
/// <response code="400">Si le partie est null</response>
|
|
/// <response code="404">Si le partie n'existe pas</response>
|
|
/// <response code="500">Si une erreur est survenue</response>
|
|
[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<ActionResult<PartieDTO>> 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;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
|