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.
163 lines
5.0 KiB
163 lines
5.0 KiB
using Microsoft.AspNetCore.Mvc;
|
|
using StubLib;
|
|
using Web_Api.Mapper;
|
|
using static StubLib.StubData;
|
|
|
|
namespace Web_Api.Controllers
|
|
{
|
|
[ApiController]
|
|
[Route("api/[controller]")]
|
|
public class SkinsController : Controller
|
|
{
|
|
private StubData.SkinsManager SkinsManager { get; set; } = new StubData.SkinsManager(new StubData());
|
|
|
|
private readonly ILogger<SkinsController> _logger;
|
|
|
|
public SkinsController(ILogger<SkinsController> logger)
|
|
{
|
|
_logger = logger;
|
|
}
|
|
|
|
[HttpGet("GET")]
|
|
public async Task<IActionResult> Get()
|
|
{
|
|
var list = await SkinsManager.GetItems(0, await SkinsManager.GetNbItems());
|
|
return Ok(list.Select(skin => skin?.toDTO()));
|
|
}
|
|
|
|
[HttpGet("GETBYNAME")]
|
|
public async Task<IActionResult> GetByName(string name)
|
|
{
|
|
var skinsSelected = await SkinsManager.GetItemsByName(name, 0, await SkinsManager.GetNbItemsByName(name), null);
|
|
if (skinsSelected.Count() == 0)
|
|
{
|
|
Console.WriteLine("Aucun skin ne correspond au nom { " + name + " } !");
|
|
throw new Exception("Aucun skin ne correspond au nom { " + name + " } !");
|
|
}
|
|
Console.WriteLine("Le skin { " + name + " } existe");
|
|
return Ok(skinsSelected.Select(skins => skins?.toDTO()));
|
|
}
|
|
|
|
[HttpPost("ADD")]
|
|
public async Task<IActionResult> AddSkin(SkinDTO skin)
|
|
{
|
|
var newSkin = skin.toModel();
|
|
await SkinsManager.AddItem(newSkin);
|
|
if(skin.Description == "string")
|
|
{
|
|
skin.Description = "Aucune bio";
|
|
}
|
|
Console.WriteLine("Le skin { " + skin.Name + " } avec pour description { " + skin.Description + " } a bien été ajouté");
|
|
return Ok(newSkin);
|
|
}
|
|
|
|
[HttpPut("UPDATE")]
|
|
public async Task<IActionResult> UpdateChampion(string name, SkinDTO skin)
|
|
{
|
|
var skinSelected = await SkinsManager.GetItemsByName(name, 0, await SkinsManager.GetNbItemsByName(name), null);
|
|
var existingSkin = skinSelected.FirstOrDefault();
|
|
if(existingSkin == null)
|
|
{
|
|
Console.WriteLine("Le skin { " + name + " } n'existe pas !");
|
|
return NotFound();
|
|
}
|
|
|
|
var updatedSkin = skin.toModel();
|
|
await SkinsManager.UpdateItem(existingSkin, updatedSkin);
|
|
if(skin.Description == "string")
|
|
{
|
|
skin.Description = "Aucune bio";
|
|
}
|
|
Console.WriteLine("Le skin { " + name + " } a été modifié en " + " { " + updatedSkin.Name + " } avec pour description { " + updatedSkin.Description + " }<");
|
|
return Ok();
|
|
}
|
|
|
|
[HttpDelete("DELETE")]
|
|
public async Task<IActionResult> DeleteChampion(string name)
|
|
{
|
|
var skinSelected = await SkinsManager.GetItemsByName(name, 0, await SkinsManager.GetNbItemsByName(name), null);
|
|
if (!await SkinsManager.DeleteItem(skinSelected.FirstOrDefault()))
|
|
{
|
|
Console.WriteLine("skin { " + name + " } non trouvé !");
|
|
return NotFound();
|
|
}
|
|
Console.WriteLine("skin { " + name + " } supprimé");
|
|
return Ok();
|
|
}
|
|
/*// GET: SkinsController
|
|
public ActionResult Index()
|
|
{
|
|
return View();
|
|
}
|
|
|
|
// GET: SkinsController/Details/5
|
|
public ActionResult Details(int id)
|
|
{
|
|
return View();
|
|
}
|
|
|
|
// GET: SkinsController/Create
|
|
public ActionResult Create()
|
|
{
|
|
return View();
|
|
}
|
|
|
|
// POST: SkinsController/Create
|
|
[HttpPost]
|
|
[ValidateAntiForgeryToken]
|
|
public ActionResult Create(IFormCollection collection)
|
|
{
|
|
try
|
|
{
|
|
return RedirectToAction(nameof(Index));
|
|
}
|
|
catch
|
|
{
|
|
return View();
|
|
}
|
|
}
|
|
|
|
// GET: SkinsController/Edit/5
|
|
public ActionResult Edit(int id)
|
|
{
|
|
return View();
|
|
}
|
|
|
|
// POST: SkinsController/Edit/5
|
|
[HttpPost]
|
|
[ValidateAntiForgeryToken]
|
|
public ActionResult Edit(int id, IFormCollection collection)
|
|
{
|
|
try
|
|
{
|
|
return RedirectToAction(nameof(Index));
|
|
}
|
|
catch
|
|
{
|
|
return View();
|
|
}
|
|
}
|
|
|
|
// GET: SkinsController/Delete/5
|
|
public ActionResult Delete(int id)
|
|
{
|
|
return View();
|
|
}
|
|
|
|
// POST: SkinsController/Delete/5
|
|
[HttpPost]
|
|
[ValidateAntiForgeryToken]
|
|
public ActionResult Delete(int id, IFormCollection collection)
|
|
{
|
|
try
|
|
{
|
|
return RedirectToAction(nameof(Index));
|
|
}
|
|
catch
|
|
{
|
|
return View();
|
|
}
|
|
}*/
|
|
}
|
|
}
|