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.
157 lines
4.7 KiB
157 lines
4.7 KiB
using Microsoft.AspNetCore.Http;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using StubLib;
|
|
using Web_Api.Mapper;
|
|
using static StubLib.StubData;
|
|
|
|
namespace Web_Api.Controllers
|
|
{
|
|
[ApiController]
|
|
[Route("api/[controller]")]
|
|
public class RunesController : Controller
|
|
{
|
|
private StubData.RunesManager RunesManager { get; set; } = new StubData.RunesManager(new StubData());
|
|
|
|
private readonly ILogger<RunesController> _logger;
|
|
|
|
public RunesController(ILogger<RunesController> logger)
|
|
{
|
|
_logger = logger;
|
|
}
|
|
|
|
[HttpGet]
|
|
public async Task<IActionResult> Get()
|
|
{
|
|
var list = await RunesManager.GetItems(0, await RunesManager.GetNbItems());
|
|
return Ok(list.Select(rune => rune?.toDTO()));
|
|
}
|
|
|
|
[HttpGet("name")]
|
|
public async Task<IActionResult> GetById(string name)
|
|
{
|
|
var runeSelected = await RunesManager.GetItemsByName(name, 0, await RunesManager.GetNbItemsByName(name), null);
|
|
return Ok(runeSelected.Select(rune => rune?.toDTO()));
|
|
}
|
|
|
|
[HttpPost("addRune")]
|
|
public async Task<IActionResult> AddRune(RuneDTO rune)
|
|
{
|
|
var newRune = rune.toModel();
|
|
await RunesManager.AddItem(newRune);
|
|
if(rune.Description == "string")
|
|
{
|
|
rune.Description = "Aucune bio";
|
|
}
|
|
Console.WriteLine("La rune { " + rune.Name + " } avec pour descrption { " + rune.Description + " } a bien été ajoutée");
|
|
return Ok(newRune);
|
|
}
|
|
|
|
[HttpPut("updateRune")]
|
|
public async Task<IActionResult> UpdateRune(string name, RuneDTO rune)
|
|
{
|
|
var runeSelected = await RunesManager.GetItemsByName(name, 0, await RunesManager.GetNbItemsByName(name), null);
|
|
var existingRune = runeSelected.FirstOrDefault();
|
|
if (existingRune == null)
|
|
{
|
|
Console.WriteLine("La rune { " + name + " } n'existe pas !");
|
|
return NotFound();
|
|
}
|
|
var updatedRune = rune.toModel();
|
|
await RunesManager.UpdateItem(existingRune, updatedRune);
|
|
if (rune.Description == "string")
|
|
{
|
|
rune.Description = "Aucune bio";
|
|
}
|
|
Console.WriteLine("La rune { " + name + " } a été modifiée en { " + updatedRune.Name + " } avec pour description { " + rune.Description + " }");
|
|
return Ok();
|
|
}
|
|
|
|
[HttpDelete("deleteRune")]
|
|
public async Task<IActionResult> DeleteRune(string name)
|
|
{
|
|
var runeSelected = await RunesManager.GetItemsByName(name, 0, await RunesManager.GetNbItemsByName(name), null);
|
|
if(!await RunesManager.DeleteItem(runeSelected.FirstOrDefault()))
|
|
{
|
|
Console.WriteLine("rune { " + name + " } non trouvée !");
|
|
return NotFound();
|
|
}
|
|
Console.WriteLine("rune { " + name + " } supprimée");
|
|
return Ok();
|
|
}
|
|
/*// GET: RuneController
|
|
public ActionResult Index()
|
|
{
|
|
return View();
|
|
}
|
|
|
|
// GET: RuneController/Details/5
|
|
public ActionResult Details(int id)
|
|
{
|
|
return View();
|
|
}
|
|
|
|
// GET: RuneController/Create
|
|
public ActionResult Create()
|
|
{
|
|
return View();
|
|
}
|
|
|
|
// POST: RuneController/Create
|
|
[HttpPost]
|
|
[ValidateAntiForgeryToken]
|
|
public ActionResult Create(IFormCollection collection)
|
|
{
|
|
try
|
|
{
|
|
return RedirectToAction(nameof(Index));
|
|
}
|
|
catch
|
|
{
|
|
return View();
|
|
}
|
|
}
|
|
|
|
// GET: RuneController/Edit/5
|
|
public ActionResult Edit(int id)
|
|
{
|
|
return View();
|
|
}
|
|
|
|
// POST: RuneController/Edit/5
|
|
[HttpPost]
|
|
[ValidateAntiForgeryToken]
|
|
public ActionResult Edit(int id, IFormCollection collection)
|
|
{
|
|
try
|
|
{
|
|
return RedirectToAction(nameof(Index));
|
|
}
|
|
catch
|
|
{
|
|
return View();
|
|
}
|
|
}
|
|
|
|
// GET: RuneController/Delete/5
|
|
public ActionResult Delete(int id)
|
|
{
|
|
return View();
|
|
}
|
|
|
|
// POST: RuneController/Delete/5
|
|
[HttpPost]
|
|
[ValidateAntiForgeryToken]
|
|
public ActionResult Delete(int id, IFormCollection collection)
|
|
{
|
|
try
|
|
{
|
|
return RedirectToAction(nameof(Index));
|
|
}
|
|
catch
|
|
{
|
|
return View();
|
|
}
|
|
}*/
|
|
}
|
|
}
|