diff --git a/Sources/ApiLol/Controllers/ChampionController.cs b/Sources/ApiLol/Controllers/ChampionController.cs index 9ab84e5..aefc8ea 100644 --- a/Sources/ApiLol/Controllers/ChampionController.cs +++ b/Sources/ApiLol/Controllers/ChampionController.cs @@ -1,9 +1,11 @@ using System; +using StubLib; using EFLib; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; +using Model; // 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 { private readonly ILogger _logger; - private List champions = new List(); + + private StubData.ChampionsManager championsManager = new StubData.ChampionsManager(new StubData()); public ChampionController(ILogger 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 [HttpGet] - public ActionResult> Get() + public async Task>> Get() { - IEnumerable champs = new List(); - foreach(ChampionEntity arg in champions) - { - champs.Append(arg.ToDto()); - } - return Ok(champs); + IEnumerable champs = await championsManager.GetItems(0, await championsManager.GetNbItems()); + IEnumerable championDTOs = champs.ToDtos(); + + return Ok(championDTOs); } - // GET api/values/5 + /* + // GET api/champion/5 [HttpGet("{id}")] public ActionResult Get(int id) { @@ -60,30 +46,51 @@ namespace ApiLol.Controllers } return BadRequest(); } + */ + - // POST api/values + // POST api/champion [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)){ - BadRequest(); + if (champion == null){ + return BadRequest(); } else { - Ok(); + championsManager.AddItem(champion.ToPoco()); + return Ok(); } } - // PUT api/values/5 - [HttpPut("{id}")] - public void Put(int id, [FromBody]string value) + // PUT api/champion/5 + [HttpPut("ChampionDto")] + 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 - [HttpDelete("{id}")] - public void Delete(int id) + [HttpDelete("ChampionDto")] + public ActionResult Delete([FromBody] ChampionDTO? champion) { + if (champion == null) + { + return BadRequest(); + } + else + { + championsManager.DeleteItem(champion.ToPoco()); + return Ok(); + } } } } diff --git a/Sources/ApiLol/DTO/ChampionDTO.cs b/Sources/ApiLol/DTO/ChampionDTO.cs index 31979a8..240ce8d 100644 --- a/Sources/ApiLol/DTO/ChampionDTO.cs +++ b/Sources/ApiLol/DTO/ChampionDTO.cs @@ -1,5 +1,7 @@ using System; +using ApiLol.DTO; using Model; + namespace ApiLol { public class ChampionDTO @@ -13,6 +15,13 @@ namespace ApiLol public string Bio { get; set; } public ChampionClass ChampClass { get; set; } + + public LargeImage Image { get; set; } + + public IEnumerable Characteristics { get; set; } + + public IEnumerable Skins { get; set; } + } } diff --git a/Sources/ApiLol/DTO/CharacteristicDTO.cs b/Sources/ApiLol/DTO/CharacteristicDTO.cs new file mode 100644 index 0000000..56bfcd6 --- /dev/null +++ b/Sources/ApiLol/DTO/CharacteristicDTO.cs @@ -0,0 +1,11 @@ +using System; +namespace ApiLol.DTO +{ + public class CharacteristicDTO + { + public string Name { get; set; } + + public int Val { get; set; } + } +} + diff --git a/Sources/ApiLol/DTO/RuneDTO.cs b/Sources/ApiLol/DTO/RuneDTO.cs new file mode 100644 index 0000000..7bbf289 --- /dev/null +++ b/Sources/ApiLol/DTO/RuneDTO.cs @@ -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; } + + + } +} + diff --git a/Sources/ApiLol/DTO/SkinDTO.cs b/Sources/ApiLol/DTO/SkinDTO.cs new file mode 100644 index 0000000..b0f1ec5 --- /dev/null +++ b/Sources/ApiLol/DTO/SkinDTO.cs @@ -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; } + + } +} + diff --git a/Sources/ApiLol/Mapping/ChampionMapper.cs b/Sources/ApiLol/Mapping/ChampionMapper.cs index bf34dcd..bc27995 100644 --- a/Sources/ApiLol/Mapping/ChampionMapper.cs +++ b/Sources/ApiLol/Mapping/ChampionMapper.cs @@ -1,41 +1,61 @@ using System; +using ApiLol.Mapping; using EFLib; +using Model; namespace ApiLol { public static class ChampionMapper { - public static ChampionDTO ToDto(this ChampionEntity champion) - { - if (champion == null) - { - throw new NullReferenceException(); - } + + public static ChampionDTO ToDto(this Champion champion) + { + if (champion == null) + { + throw new NullReferenceException(); + } return new ChampionDTO { Name = champion.Name, Bio = champion.Bio, Icon = champion.Icon, - Id = champion.Id, - ChampClass = champion.ChampClass + ChampClass = champion.Class, + Image = champion.Image, + Skins = champion.Skins, + Characteristics = champion.Characteristics.ToDto() }; } - public static ChampionEntity ToEntity(this ChampionDTO champion) - { + public static IEnumerable ToDtos(this IEnumerable champions) + { + IEnumerable dtos = new List(); + foreach (Champion champion in champions) + { + dtos = dtos.Append(champion.ToDto()); + } + return dtos; + } + + public static Champion ToPoco(this ChampionDTO champion) + { if (champion == null) { 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 ToPocos(this IEnumerable champions) + { + IEnumerable dtos = new List(); + foreach (ChampionDTO champion in champions) { - Name = champion.Name, - Bio = champion.Bio, - Icon = champion.Icon, - Id = champion.Id, - ChampClass = champion.ChampClass - }; + dtos = dtos.Append(champion.ToPoco()); + } + return dtos; } - } + + + } } diff --git a/Sources/ApiLol/Mapping/CharacteristicMapper.cs b/Sources/ApiLol/Mapping/CharacteristicMapper.cs new file mode 100644 index 0000000..0684a93 --- /dev/null +++ b/Sources/ApiLol/Mapping/CharacteristicMapper.cs @@ -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 ToDto(this ReadOnlyDictionary dico) + { + if (dico == null) + { + throw new ArgumentNullException(); + } + + IEnumerable characteristicDTOs = new List(); + + foreach(var item in dico) + { + characteristicDTOs = characteristicDTOs.Append(new CharacteristicDTO() { Name = item.Key, Val = item.Value }); + } + return characteristicDTOs; + } + + + public static ReadOnlyDictionary ToDto(this IEnumerable characs) + { + if (characs == null) + { + throw new ArgumentNullException(); + } + + Dictionary dico = new Dictionary(); + foreach (CharacteristicDTO c in characs) + { + dico.Add(c.Name, c.Val); + } + + return new ReadOnlyDictionary(dico); + + } + } +} + diff --git a/Sources/ApiLol/Mapping/SkinMapper.cs b/Sources/ApiLol/Mapping/SkinMapper.cs new file mode 100644 index 0000000..fabde1c --- /dev/null +++ b/Sources/ApiLol/Mapping/SkinMapper.cs @@ -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); + //} + } +} +