ajout de tous les DTO
continuous-integration/drone/push Build is passing Details

master
Thomas Chazot 2 years ago
parent 58545a51c2
commit 4295493741

@ -1,9 +1,11 @@
using System; using System;
using StubLib;
using EFLib; using EFLib;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Model;
// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 // For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
@ -13,43 +15,27 @@ namespace ApiLol.Controllers
public class ChampionController : ControllerBase public class ChampionController : ControllerBase
{ {
private readonly ILogger<ChampionController> _logger; private readonly ILogger<ChampionController> _logger;
private List<ChampionEntity> champions = new List<ChampionEntity>();
private StubData.ChampionsManager championsManager = new StubData.ChampionsManager(new StubData());
public ChampionController(ILogger<ChampionController> logger) public ChampionController(ILogger<ChampionController> logger)
{ {
_logger = logger; _logger = logger;
champions.Add(new ChampionEntity
{
Id = 1,
Name = "Aurel",
Bio = "Trop joli",
Icon = "Moi",
ChampClass = Model.ChampionClass.Tank
});
champions.Add(new ChampionEntity
{
Id = 2,
Name = "Mathilde",
Bio = "Elle est reloue",
Icon = "Macheval",
ChampClass = Model.ChampionClass.Assassin
});
} }
// GET: api/champion // GET: api/champion
[HttpGet] [HttpGet]
public ActionResult<IEnumerable<ChampionDTO>> Get() public async Task<ActionResult<IEnumerable<ChampionDTO>>> Get()
{ {
IEnumerable<ChampionDTO> champs = new List<ChampionDTO>(); IEnumerable<Champion?> champs = await championsManager.GetItems(0, await championsManager.GetNbItems());
foreach(ChampionEntity arg in champions) IEnumerable<ChampionDTO> championDTOs = champs.ToDtos();
{
champs.Append(arg.ToDto()); return Ok(championDTOs);
}
return Ok(champs);
} }
// GET api/values/5 /*
// GET api/champion/5
[HttpGet("{id}")] [HttpGet("{id}")]
public ActionResult<ChampionDTO?> Get(int id) public ActionResult<ChampionDTO?> Get(int id)
{ {
@ -60,30 +46,51 @@ namespace ApiLol.Controllers
} }
return BadRequest(); return BadRequest();
} }
*/
// POST api/values // POST api/champion
[HttpPost] [HttpPost]
public void Post([FromBody]int id, string name, string bio, string icon) public ActionResult Post([FromBody]ChampionDTO? champion)
{ {
if (string.IsNullOrWhiteSpace(name) || string.IsNullOrWhiteSpace(bio) || string.IsNullOrWhiteSpace(icon)){ if (champion == null){
BadRequest(); return BadRequest();
} }
else else
{ {
Ok(); championsManager.AddItem(champion.ToPoco());
return Ok();
} }
} }
// PUT api/values/5 // PUT api/champion/5
[HttpPut("{id}")] [HttpPut("ChampionDto")]
public void Put(int id, [FromBody]string value) public ActionResult Put([FromBody] ChampionDTO? champion, ChampionDTO? newChampion)
{
if (champion == null || newChampion == null)
{
return BadRequest();
}
else
{ {
championsManager.UpdateItem(champion.ToPoco(), newChampion.ToPoco());
return Ok();
}
} }
// DELETE api/values/5 // DELETE api/values/5
[HttpDelete("{id}")] [HttpDelete("ChampionDto")]
public void Delete(int id) public ActionResult Delete([FromBody] ChampionDTO? champion)
{
if (champion == null)
{ {
return BadRequest();
}
else
{
championsManager.DeleteItem(champion.ToPoco());
return Ok();
}
} }
} }
} }

@ -1,5 +1,7 @@
using System; using System;
using ApiLol.DTO;
using Model; using Model;
namespace ApiLol namespace ApiLol
{ {
public class ChampionDTO public class ChampionDTO
@ -13,6 +15,13 @@ namespace ApiLol
public string Bio { get; set; } public string Bio { get; set; }
public ChampionClass ChampClass { get; set; } public ChampionClass ChampClass { get; set; }
public LargeImage Image { get; set; }
public IEnumerable<CharacteristicDTO> Characteristics { get; set; }
public IEnumerable<Skin> Skins { get; set; }
} }
} }

@ -0,0 +1,11 @@
using System;
namespace ApiLol.DTO
{
public class CharacteristicDTO
{
public string Name { get; set; }
public int Val { get; set; }
}
}

@ -0,0 +1,23 @@
using System;
using Model;
namespace ApiLol.DTO
{
public class RuneDTO
{
public string Nom { get; set; }
public string Description { get; set; }
public string Famille { get; set; }
public string Icon { get; set; }
public string Name { get; set; }
public LargeImage Image { get; set; }
}
}

@ -0,0 +1,20 @@
using System;
using Model;
namespace ApiLol.DTO
{
public class SkinDTO
{
public string Name { get; set; }
public string Description { get; set; }
public float Price { get; set; }
public string Icon { get; set; }
public LargeImage Image { get; set; }
}
}

@ -1,11 +1,14 @@
using System; using System;
using ApiLol.Mapping;
using EFLib; using EFLib;
using Model;
namespace ApiLol namespace ApiLol
{ {
public static class ChampionMapper public static class ChampionMapper
{ {
public static ChampionDTO ToDto(this ChampionEntity champion)
public static ChampionDTO ToDto(this Champion champion)
{ {
if (champion == null) if (champion == null)
{ {
@ -16,26 +19,43 @@ namespace ApiLol
Name = champion.Name, Name = champion.Name,
Bio = champion.Bio, Bio = champion.Bio,
Icon = champion.Icon, Icon = champion.Icon,
Id = champion.Id, ChampClass = champion.Class,
ChampClass = champion.ChampClass Image = champion.Image,
Skins = champion.Skins,
Characteristics = champion.Characteristics.ToDto()
}; };
} }
public static ChampionEntity ToEntity(this ChampionDTO champion) public static IEnumerable<ChampionDTO> ToDtos(this IEnumerable<Champion> champions)
{
IEnumerable<ChampionDTO> dtos = new List<ChampionDTO>();
foreach (Champion champion in champions)
{
dtos = dtos.Append(champion.ToDto());
}
return dtos;
}
public static Champion ToPoco(this ChampionDTO champion)
{ {
if (champion == null) if (champion == null)
{ {
throw new NullReferenceException(); throw new NullReferenceException();
} }
return new ChampionEntity return new Champion(name: champion.Name, champClass: champion.ChampClass, icon: champion.Icon, bio: champion.Bio, image: champion.Image.Base64);
}
public static IEnumerable<Champion> ToPocos(this IEnumerable<ChampionDTO> champions)
{ {
Name = champion.Name, IEnumerable<Champion> dtos = new List<Champion>();
Bio = champion.Bio, foreach (ChampionDTO champion in champions)
Icon = champion.Icon, {
Id = champion.Id, dtos = dtos.Append(champion.ToPoco());
ChampClass = champion.ChampClass }
}; return dtos;
} }
} }
} }

@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using ApiLol.DTO;
using Model;
namespace ApiLol.Mapping
{
public static class CharacteristicMapper
{
public static IEnumerable<CharacteristicDTO> ToDto(this ReadOnlyDictionary<string, int> dico)
{
if (dico == null)
{
throw new ArgumentNullException();
}
IEnumerable<CharacteristicDTO> characteristicDTOs = new List<CharacteristicDTO>();
foreach(var item in dico)
{
characteristicDTOs = characteristicDTOs.Append(new CharacteristicDTO() { Name = item.Key, Val = item.Value });
}
return characteristicDTOs;
}
public static ReadOnlyDictionary<string, int> ToDto(this IEnumerable<CharacteristicDTO> characs)
{
if (characs == null)
{
throw new ArgumentNullException();
}
Dictionary<string, int> dico = new Dictionary<string, int>();
foreach (CharacteristicDTO c in characs)
{
dico.Add(c.Name, c.Val);
}
return new ReadOnlyDictionary<string, int>(dico);
}
}
}

@ -0,0 +1,29 @@
using System;
using ApiLol.DTO;
using System.Collections.ObjectModel;
using Model;
namespace ApiLol.Mapping
{
public static class SkinMapper
{
public static SkinDTO ToDto(this Skin skin)
{
return new SkinDTO
{
Name = skin.Name,
Description = skin.Description,
Price = skin.Price,
Icon = skin.Icon,
Image = skin.Image
};
}
//public static Skin ToPoco(this SkinDTO skin)
//{
// return new Skin(skin.Name, skin.Price, skin.Icon, skin.Image.Base64, skin.Description);
//}
}
}
Loading…
Cancel
Save