diff --git a/WF_EF_Api/Contextlib/DbImagesManager.cs b/WF_EF_Api/Contextlib/DbImagesManager.cs index e549cf9..0fcd548 100644 --- a/WF_EF_Api/Contextlib/DbImagesManager.cs +++ b/WF_EF_Api/Contextlib/DbImagesManager.cs @@ -35,7 +35,12 @@ namespace Contextlib public async Task GetImageById(int id) { - return _repository.GetById(id); + var image = _repository.GetById(id); + if(image == null) + { + throw new KeyNotFoundException($"No image with the id {id}"); + } + return image; } public async Task GetLastImageId() diff --git a/WF_EF_Api/ServicesApi/ImageService.cs b/WF_EF_Api/ServicesApi/ImageService.cs index caefb31..0427f0f 100644 --- a/WF_EF_Api/ServicesApi/ImageService.cs +++ b/WF_EF_Api/ServicesApi/ImageService.cs @@ -7,6 +7,7 @@ using DTO; using Entity; using Shared; using Dto2Entities; +using static System.Net.Mime.MediaTypeNames; namespace ServicesApi { @@ -32,7 +33,22 @@ namespace ServicesApi public async Task GetImageById(int id) { - return imageService.GetImageById(id).Result.ToDto(); + var image = await imageService.GetImageById(id); + if (image == null) + { + throw new KeyNotFoundException($"No image with the id {id}"); + } + return image.ToDto(); + } + + public async Task GetImageByPath(string path) + { + var image = await imageService.GetImageByPath(path); + if (image == null) + { + return null; + } + return image.ToDto(); } public async Task GetLastImageId() diff --git a/WF_EF_Api/Shared/IImagesService.cs b/WF_EF_Api/Shared/IImagesService.cs index 0bc6bf0..7cf0cd3 100644 --- a/WF_EF_Api/Shared/IImagesService.cs +++ b/WF_EF_Api/Shared/IImagesService.cs @@ -35,5 +35,7 @@ namespace Shared // Retrieves the last Image ID. Task GetLastImageId(); + + Task GetImageByPath(string path); } } diff --git a/WF_EF_Api/WfApi/Controllers/ImageController.cs b/WF_EF_Api/WfApi/Controllers/ImageController.cs new file mode 100644 index 0000000..1ffd55b --- /dev/null +++ b/WF_EF_Api/WfApi/Controllers/ImageController.cs @@ -0,0 +1,141 @@ +using DTO; +using System.Net; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Shared; +using Entity; + +namespace WfApi.Controllers +{ + [ApiController] + [Route("api/v1/image")] //Version API + public class ImageController : ControllerBase + { + private readonly IImagesService _img; + private readonly ILogger _logger; + + public ImageController(IImagesService imgService, ILogger logger) + { + _img = imgService; + _logger = logger; + } + + + [HttpGet("{id}")] // Indiquer que l'id est dans l'URL + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + public async Task GetImageId(int id) + { + try + { + try + { + var image = await _img.GetImageById(id); + return Ok(image); + } + catch (KeyNotFoundException e) + { + return NotFound(); + } + } + catch (Exception e) + { + return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal Server Error (" + e + ")" }); + } + } + + [HttpGet("all")] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status204NoContent)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] + public async Task GetAllImage(int index = 0, int count = 10) + { + try + { + var result = await _img.GetSomeImage(index, count); + if (result == null) + { + return NotFound(); + } + return Ok(result); + } + catch (KeyNotFoundException e) + { + return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal Server Error" }); + } + } + + [HttpPost] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status204NoContent)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] + public async Task CreateImage([FromBody] ImageDTO newImage) + { + try + { + if(newImage == null) + { + return BadRequest(new { message = "Source data is required." }); + } + try + { + var existImage = await _img.GetImageById(newImage.IdImage); + return Conflict(new { message = $"A Image with the ID {newImage.IdImage} already exists." }); + } + catch(KeyNotFoundException e) + { + var existPath = await _img.GetImageByPath(newImage.ImagePath); + if(existPath == null) + { + await _img.AddImage(newImage); + return Ok(newImage); + } + return Conflict(new { message = $"A Image with the path {newImage.ImagePath} already exists." }); + } + } + catch(Exception e) + { + return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal Server Error" }); + } + } + + + + [HttpPut()] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status204NoContent)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] + public async Task UpdateImage([FromQuery] int id, [FromBody] ImageDTO updatedImage) + { + try + { + if (updatedImage == null) + { + return BadRequest(new { message = "new source data is required." }); + } + try + { + var existImage = await _img.GetImageById(id); + var existPath = await _img.GetImageByPath(updatedImage.ImagePath); + if (existPath == null) + { + await _img.UpdateImage(id, updatedImage); + return Ok(updatedImage); + } + return Conflict(new { message = $"A Image with the path {updatedImage.ImagePath} already exists." }); + } + catch (KeyNotFoundException e) + { + return Conflict(new { message = $"A Image with the ID {id} dosen't exists." }); + } + } + catch (Exception e) + { + return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal Server Error (" + e + ")" }); + } + } + + } +}