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.
103 lines
3.6 KiB
103 lines
3.6 KiB
using System.Net;
|
|
using DTO;
|
|
using Entity;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Shared;
|
|
|
|
namespace WfApi.Controllers
|
|
{
|
|
[ApiController]
|
|
[Route("api/v1/commentary")] //Version API
|
|
public class CommentariesController : ControllerBase
|
|
{
|
|
private readonly ICommentaryService<CommentaryDTO> _commentary;
|
|
|
|
private readonly ILogger<CommentariesController> _logger;
|
|
public CommentariesController(ICommentaryService<CommentaryDTO> commentaryService, ILogger<CommentariesController> logger)
|
|
{
|
|
_commentary = commentaryService;
|
|
_logger = logger;
|
|
}
|
|
|
|
|
|
|
|
[HttpGet("{id}")] // Indiquer que l'id est dans l'URL
|
|
public async Task<IActionResult> GetCommentary(int id, int index = 0, int count = 5)
|
|
{
|
|
try
|
|
{
|
|
var result = _commentary.GetCommentaryByQuote(id, index, count);
|
|
|
|
if (result.IsCompletedSuccessfully)
|
|
{
|
|
return await Task.FromResult<IActionResult>(Ok(result));
|
|
}
|
|
else
|
|
{
|
|
return NoContent();
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal Server Error" });
|
|
}
|
|
}
|
|
|
|
[HttpPost]
|
|
[ProducesResponseType(StatusCodes.Status201Created)]
|
|
[ProducesResponseType(StatusCodes.Status400BadRequest)]
|
|
[ProducesResponseType(StatusCodes.Status409Conflict)]
|
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
|
public async Task<IActionResult> CreateCommentary([FromBody] CommentaryDTO newCommentary)
|
|
{
|
|
try
|
|
{
|
|
if (newCommentary == null)
|
|
{
|
|
return BadRequest(new { message = "Comment data is required." });
|
|
}
|
|
|
|
var existingPlayer = _commentary.GetCommentaryById(newCommentary.Id).Result;
|
|
if (existingPlayer != null)
|
|
{
|
|
return Conflict(new { message = "A comment with this ID already exists." });
|
|
}
|
|
|
|
_commentary.AddComment(newCommentary);
|
|
|
|
return CreatedAtAction(nameof(GetCommentary), new { id = newCommentary.Id }, newCommentary);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Erreur interne du serveur." });
|
|
}
|
|
}
|
|
|
|
|
|
[HttpDelete] // /api/v1/commentary?id=51
|
|
[ProducesResponseType(StatusCodes.Status200OK)]
|
|
[ProducesResponseType(StatusCodes.Status404NotFound)]
|
|
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
|
|
public async Task<IActionResult> DeleteCommentary([FromQuery] int id)
|
|
{
|
|
try
|
|
{
|
|
|
|
var existingCommentary = _commentary.GetCommentaryById(id).Result;
|
|
if (existingCommentary == null)
|
|
{
|
|
return NotFound(new { message = "Commentary not found." });
|
|
}
|
|
|
|
_commentary.RemoveCommentary(existingCommentary.Id);
|
|
|
|
return Ok(new { message = $"Commentary {id} deleted successfully." });
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal server error." });
|
|
}
|
|
}
|
|
}
|
|
}
|