using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.SignalR; using Model; using StubLib; using Web_Api.Mapper; namespace Web_Api.Controllers { [ApiController] [Route("api/[controller]")] public class ChampionsController : Controller { private StubData.ChampionsManager ChampionsManager { get; set; } = new StubData.ChampionsManager(new StubData()); private readonly ILogger _logger; public ChampionsController(ILogger logger) { _logger = logger; } [HttpGet] public async Task Get() { var list = await ChampionsManager.GetItems(0, await ChampionsManager.GetNbItems()); return Ok(list.Select(champion => champion?.toDTO())); } [HttpGet("name")] public async Task GetById(string name) { var championSelected = await ChampionsManager.GetItemsByName(name, 0, await ChampionsManager.GetNbItemsByName(name), null); return Ok(championSelected.Select(c => c?.toDTO())); } [HttpPost("addChampion")] public async Task AddChampion([FromBody] ChampionDTO champion) { var newChampion = champion.toModel(); await ChampionsManager.AddItem(newChampion); if(champion.Bio == "string") { champion.Bio = "Aucune bio"; } Console.WriteLine("Le champion { " + champion.Name + " } avec pour bio { " + champion.Bio + " } a bien été ajouté"); return Ok(); } [HttpPut("updateChampion")] public async Task UpdateChampion(string name, [FromBody] ChampionDTO champion) { var championSelected = await ChampionsManager.GetItemsByName(name, 0, await ChampionsManager.GetNbItemsByName(name), null); var existingChampion = championSelected.FirstOrDefault(); if (existingChampion == null) { Console.WriteLine("Le champion { " + name + " } n'existe pas !"); return NotFound(); } var updatedChampion = champion.toModel(); await ChampionsManager.UpdateItem(existingChampion, updatedChampion); if(updatedChampion.Bio == "string") { updatedChampion.Bio = "Aucune bio"; } Console.WriteLine("Le champion { " + name + " } a été modifié en { " + updatedChampion.Name + " } avec pour bio { " + updatedChampion.Bio + " }"); return Ok(); } [HttpDelete("deleteChampion")] public async Task DeleteChampion(string name) { var championSelected = await ChampionsManager.GetItemsByName(name, 0, await ChampionsManager.GetNbItemsByName(name), null); if(!await ChampionsManager.DeleteItem(championSelected.FirstOrDefault())) { Console.WriteLine("champion { " + name + " } non trouvé !"); return NotFound(); } Console.WriteLine("champion { " + name + " } supprimé"); return Ok(); } /*[HttpGet(Name = "GetChampionsById")] public async Task GetById() { }*/ /*// GET: ChampionsController public ActionResult Index() { return View(); } // GET: ChampionsController/Details/5 public ActionResult Details(int id) { return View(); } // GET: ChampionsController/Create public ActionResult Create() { return View(); } // POST: ChampionsController/Create [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create(IFormCollection collection) { try { return RedirectToAction(nameof(Index)); } catch { return View(); } } // GET: ChampionsController/Edit/5 public ActionResult Edit(int id) { return View(); } // POST: ChampionsController/Edit/5 [HttpPost] [ValidateAntiForgeryToken] public ActionResult Edit(int id, IFormCollection collection) { try { return RedirectToAction(nameof(Index)); } catch { return View(); } } // GET: ChampionsController/Delete/5 public ActionResult Delete(int id) { return View(); } // POST: ChampionsController/Delete/5 [HttpPost] [ValidateAntiForgeryToken] public ActionResult Delete(int id, IFormCollection collection) { try { return RedirectToAction(nameof(Index)); } catch { return View(); } }*/ } }