From 30337d1ed7ef32ded2eadef71ecdd3ce8ece177d Mon Sep 17 00:00:00 2001 From: Kevin MONDEJAR Date: Thu, 3 Apr 2025 10:41:34 +0200 Subject: [PATCH] route character --- WF_EF_Api/Contextlib/DbCharacterManager.cs | 23 +++- WF_EF_Api/Dto2Entities/Extention.cs | 1 + WF_EF_Api/ServicesApi/CharacterService.cs | 8 +- WF_EF_Api/Shared/ICharacterService.cs | 2 + .../WfApi/Controllers/CharacterController.cs | 128 ++++++++++++++++++ 5 files changed, 158 insertions(+), 4 deletions(-) create mode 100644 WF_EF_Api/WfApi/Controllers/CharacterController.cs diff --git a/WF_EF_Api/Contextlib/DbCharacterManager.cs b/WF_EF_Api/Contextlib/DbCharacterManager.cs index 7d01f7f..d7385aa 100644 --- a/WF_EF_Api/Contextlib/DbCharacterManager.cs +++ b/WF_EF_Api/Contextlib/DbCharacterManager.cs @@ -2,6 +2,7 @@ using Microsoft.EntityFrameworkCore; using Shared; using System; +using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; @@ -12,12 +13,14 @@ namespace Contextlib public class DbCharacterManager : ICharacterService { private WTFContext _context; - private GenericRepository _repo; + private GenericRepository _repo; + private DbImagesManager _dbI; public DbCharacterManager(WTFContext context) { _context = context ?? throw new ArgumentNullException(nameof(context), "Database context cannot be null."); _repo = new GenericRepository(_context); + _dbI = new DbImagesManager(context); } /// @@ -32,6 +35,12 @@ namespace Contextlib { throw new ArgumentNullException(nameof(character), "character cannot be null."); } + var image = await _dbI.GetImageByPath(character.Images.ImgPath); + if (image != null) + { + character.IdImage = image.Id; + character.Images = image; + } _repo.Insert(character); await _context.SaveChangesAsync(); } @@ -93,6 +102,12 @@ namespace Contextlib return lastCharId; } + public async Task> GetSomeChar(int page, int count) + { + var charLst = _repo.GetItems(page, count, [nameof(Character.Images)]).ToList(); + return new PaginationResult(charLst.Count, 0, charLst.Count, charLst); + } + /// /// Removes a character from the database by its ID. /// @@ -119,9 +134,11 @@ namespace Contextlib if (charac != null) { bool change = false; - if (character.IdImage != 0) + var image = await _dbI.GetImageByPath(character.Images.ImgPath); + if (image != null) { - charac.IdImage = character.IdImage; + charac.IdImage = image.Id; + charac.Images = image; change = true; } if (character.Name != null) diff --git a/WF_EF_Api/Dto2Entities/Extention.cs b/WF_EF_Api/Dto2Entities/Extention.cs index fcd8407..5b28630 100644 --- a/WF_EF_Api/Dto2Entities/Extention.cs +++ b/WF_EF_Api/Dto2Entities/Extention.cs @@ -261,6 +261,7 @@ namespace Dto2Entities Character character = new Character(); character.Id = item.Id; character.Name = item.Name; + character.Images = new Images(); character.Images.ImgPath = item.imagePath ; return character; } diff --git a/WF_EF_Api/ServicesApi/CharacterService.cs b/WF_EF_Api/ServicesApi/CharacterService.cs index 399586a..53daa1e 100644 --- a/WF_EF_Api/ServicesApi/CharacterService.cs +++ b/WF_EF_Api/ServicesApi/CharacterService.cs @@ -33,7 +33,7 @@ namespace ServicesApi public async Task GetCharById(int id) { - return characterService.GetCharById(id).Result.ToDto(); + return (await characterService.GetCharById(id)).ToDto(); } public async Task GetCharByName(string name) @@ -46,6 +46,12 @@ namespace ServicesApi return await characterService.GetLastCharId(); } + public async Task> GetSomeChar(int page, int count) + { + var characters = (await characterService.GetSomeChar(page, count)).items; + return new PaginationResult(characters.Count(), page, count, characters.ToDto()); + } + public async Task RemoveCharacter(int id) { await characterService.RemoveCharacter(id); diff --git a/WF_EF_Api/Shared/ICharacterService.cs b/WF_EF_Api/Shared/ICharacterService.cs index a1c1a57..a4fd18d 100644 --- a/WF_EF_Api/Shared/ICharacterService.cs +++ b/WF_EF_Api/Shared/ICharacterService.cs @@ -35,5 +35,7 @@ namespace Shared // Retrieves the unique identifier of the last added character. Task GetLastCharId(); + + Task> GetSomeChar(int page, int count); } } diff --git a/WF_EF_Api/WfApi/Controllers/CharacterController.cs b/WF_EF_Api/WfApi/Controllers/CharacterController.cs new file mode 100644 index 0000000..4789f1c --- /dev/null +++ b/WF_EF_Api/WfApi/Controllers/CharacterController.cs @@ -0,0 +1,128 @@ +using System.Net; +using DTO; +using Entity; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Mvc; +using Shared; + +namespace WfApi.Controllers +{ + [ApiController] + [Route("api/v1/character")] //Version API + public class CharacterController : ControllerBase + { + private readonly ICharacterService _character; + + private readonly ILogger _logger; + + public CharacterController(ICharacterService characterService, ILogger logger) + { + _character = characterService; + _logger = logger; + + } + + [HttpGet("{id}")] // Indiquer que l'id est dans l'URL + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status204NoContent)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + public async Task GetCharacter(int id) + { + try + { + try + { + var character = await _character.GetCharById(id); + return Ok(character); + } + 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 GetAllSource(int index = 0, int count = 10) + { + try + { + var result = await _character.GetSomeChar(index, count); + + if (result != null) + { + return await Task.FromResult(Ok(result)); + } + else + { + return NoContent(); + } + } + catch (Exception e) + { + return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal Server Error (" + e + ")" }); + } + } + + [HttpPost] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status204NoContent)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] + public async Task CreateCharacter([FromBody] CharacterDTO newCharacter) + { + try + { + if (newCharacter == null) + { + return BadRequest(new { message = "Source data is required." }); + } + try + { + var existingSource = await _character.GetCharById(newCharacter.Id); + return Conflict(new { message = "A source with this ID already exists." }); + } + catch (KeyNotFoundException e) + { + await _character.AddCharacter(newCharacter); + return CreatedAtAction(nameof(GetAllSource), new { id = newCharacter.Id }, newCharacter); + } + } + catch (Exception e) + { + return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal Server Error (" + e + ")" }); + } + } + + [HttpPut()] + [ProducesResponseType(StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status204NoContent)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] + public async Task UpdateCharacter([FromQuery] int id, [FromBody] CharacterDTO updatedCharacter) + { + try + { + if (updatedCharacter == null) + { + return BadRequest(new { message = "new source data is required." }); + } + + var result = _character.UpdateCharacter(id, updatedCharacter); + + return Ok(result); + } + catch (Exception e) + { + return StatusCode((int)HttpStatusCode.InternalServerError, new { message = "Internal Server Error (" + e + ")" }); + } + } + } +}