diff --git a/Sources/API/Controllers/ChampionController.cs b/Sources/API/Controllers/ChampionController.cs index a8b40e0..b5d4b0b 100644 --- a/Sources/API/Controllers/ChampionController.cs +++ b/Sources/API/Controllers/ChampionController.cs @@ -1,5 +1,6 @@ using API.Dto; using API.Mapping; +using EFlib; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Model; @@ -18,41 +19,45 @@ namespace API.Controllers public class ChampionController : ControllerBase { private readonly ILogger _logger; - private readonly StubData stub = new StubData(); + private readonly StubData data = new StubData(); private readonly IDataManager dataManager; // Pour plus tard pour le momment c'est avec le stub - // private const string Apichampion = "api/champion"; - // private readonly HttpClient _client; + private const string Apichampion = "api/champion"; + private readonly HttpClient _client; public ChampionController(ILogger logger) { _logger = logger; } - /* public championHttpManager(HttpClient client) - { - _client = client; - client.BaseAddress = new Uri("à chopper dans lauchSettings.json propriété du projet"); - } + /* public championHttpManager(HttpClient client) + { + _client = client; + client.BaseAddress = new Uri("à chopper dans lauchSettings.json propriété du projet"); + } - public async Task> getJson() + public async Task> getJson() { var champions = await _client.GetFromJsonAsync>(); var reponse = await _client.GetAsync("api/champion"); return champions; - } - */ + }*/ + - /**** Méthodes GET **** + /**** Méthodes GET ****/ [HttpGet] public async Task> GetChamps() { - IEnumerable Champs = await stub.ChampionsMgr.GetItems(0, stub.ChampionsMgr.GetNbItems().Result); + // Récupération de la liste des champions + IEnumerable Champs = await data.ChampionsMgr.GetItems(0, data.ChampionsMgr.GetNbItems().Result); + // Création de la liste de champion Dto List DtoChamps = new List(); + // Chargement de la liste des champions Dto à partir des champions Champs.ToList().ForEach(c => DtoChamps.Add(c.ToDto())); + return Ok(DtoChamps); } @@ -62,9 +67,9 @@ namespace API.Controllers { BadRequest("404"); - IEnumerable Champs = await stub.ChampionsMgr.GetItems(id, 1); + IEnumerable Champs = await data.ChampionsMgr.GetItems(id, 1); - if (id >= 0 && id < stub.ChampionsMgr.GetNbItems().Result) + if (id >= 0 && id < data.ChampionsMgr.GetNbItems().Result) { return Ok(Champs.First().ToDto()); } @@ -74,65 +79,72 @@ namespace API.Controllers [HttpGet("{id}/Skins")] public async Task> GetSkinsChamp(int id) { - IEnumerable lcha = await stub.ChampionsMgr.GetItems(id, 1); + // Récupération du champion + IEnumerable ChampNum = await data.ChampionsMgr.GetItems(id, 1); + - if (id >= 0 && id < stub.ChampionsMgr.GetNbItems().Result) + if (id >= 0 && id < data.ChampionsMgr.GetNbItems().Result) { - Champion ca = lcha.First(); + // Converstion en Champion au lieu de champion IEnumerable + Champion champion = ChampNum.First(); - IEnumerable lsk = await stub.SkinsMgr.GetItemsByChampion(ca, 0, stub.SkinsMgr.GetNbItemsByChampion(ca).Result); + // Récupération des skin du champion + IEnumerable ListeSkin = await data.SkinsMgr.GetItemsByChampion(champion, 0, data.SkinsMgr.GetNbItemsByChampion(champion).Result); + // Création de la liste de skin List skins = new List(); - lsk.ToList().ForEach(s => skins.Add(s.ToSkin())); + + // Ajout des skins dans la nouvelle liste + ListeSkin.ToList().ForEach(Skin => skins.Add(Skin.ToDto())); return Ok(skins); } return BadRequest(); } - /**** Méthodes POST **** + /**** Méthodes POST ****/ [HttpPost("Ajouter/{nom}")] public async Task PostChampName(string nom) { - Champion ca = new Champion(nom); + Champion champion = new Champion(nom); - await stub.ChampionsMgr.AddItem(ca); + await data.ChampionsMgr.AddItem(champion); - return CreatedAtAction(nameof(GetChampById), new { id = stub.ChampionsMgr.GetNbItems().Result - 1 }, ca.ToDto()); + return CreatedAtAction(nameof(GetChampById), new { id = data.ChampionsMgr.GetNbItems().Result - 1 }, champion.ToDto()); } [HttpPost("Ajouter")] - public async Task PostChamp([FromBody] ChampionDto c) + public async Task PostChamp([FromBody] ChampionDto championDto) { - Champion ca = c.ToModel(); - await stub.ChampionsMgr.AddItem(ca); - return CreatedAtAction(nameof(stub.ChampionsMgr.GetItemsByName), new { Name = c.Name }, c); + Champion champion = championDto.ToModel(); + await data.ChampionsMgr.AddItem(champion); + return CreatedAtAction(nameof(data.ChampionsMgr.GetItemsByName), new { Name = championDto.Name }, championDto); } [HttpPost] - public async Task post([FromBody] ChampionDto champion) + public async Task post([FromBody] ChampionDto championDto) { return CreatedAtAction(nameof(GetChampById), new { id = 1 }, - await dataManager.ChampionsMgr.AddItem(champion.ToModel)); + await data.ChampionsMgr.AddItem(championDto.ToModel())); } - public async void addchampion(ChampionDto champion) + /*public async void addchampion(ChampionDto champion) { _clientLpostAsJsonAscync(ApiChampion, champion); } +*/ - - /**** Méthodes DELETE **** + /**** Méthodes DELETE ****/ [HttpDelete("Supprimer/{id}")] public async Task DeleteChamp(int id) { - IEnumerable lcha = await stub.ChampionsMgr.GetItems(id, 1); + IEnumerable lcha = await data.ChampionsMgr.GetItems(id, 1); - if (id >= 0 && id < stub.ChampionsMgr.GetNbItems().Result) + if (id >= 0 && id < data.ChampionsMgr.GetNbItems().Result) { Champion ca = lcha.First(); - stub.ChampionsMgr.DeleteItem(ca); + data.ChampionsMgr.DeleteItem(ca); return Ok(); } return BadRequest(); @@ -140,25 +152,24 @@ namespace API.Controllers - /**** Méthodes PUT **** + /**** Méthodes PUT ****/ - [HttpPut("Modifier/{nom}")]// CA C4EST PAS FINI + [HttpPut("Modifier/{nom}")] public async Task PutChampName(string nom) { - Champion ca = new Champion(nom); + Champion champion = new Champion(nom); - await stub.ChampionsMgr.AddItem(ca); + await data.ChampionsMgr.AddItem(champion); - return CreatedAtAction(nameof(GetChampById), new { id = stub.ChampionsMgr.GetNbItems().Result - 1 }, ca.ToDto()); + return CreatedAtAction(nameof(GetChampById), new { id = data.ChampionsMgr.GetNbItems().Result - 1 }, champion.ToDto()); } - [HttpPut("Modifier")] - public async Task PutChamp([FromBody] ChampionDto c, [FromBody] ChampionDto cNouv) + /*[HttpPut("Modifier")] + public async Task PutChamp([FromBody] ChampionDto championDto) { - Champion ca = c.ToModel(); - Champion caNouv = cNouv.ToModel(); - await stub.ChampionsMgr.UpdateItem(ca, caNouv); - return CreatedAtAction(nameof(GetChampById), new { id = stub.ChampionsMgr.GetItems(0, stub.ChampionsMgr.GetNbItems().Result).Result.ToList().IndexOf(ca) }, ca); + Champion champion = championDto.ToModel(); + await data.ChampionsMgr.UpdateItem(champion); + return CreatedAtAction(nameof(GetChampById), new { id = data.ChampionsMgr.GetItems(0, data.ChampionsMgr.GetNbItems().Result).Result.ToList().IndexOf(champion) }, champion); }*/ } diff --git a/Sources/API/Dto/ChampionDto.cs b/Sources/API/Dto/ChampionDto.cs index 8756c51..59dda41 100644 --- a/Sources/API/Dto/ChampionDto.cs +++ b/Sources/API/Dto/ChampionDto.cs @@ -1,15 +1,22 @@ -namespace API.Dto +using Model; +using System.Collections.ObjectModel; + +namespace API.Dto { public class ChampionDto { /**** Only Attributs ****/ - public int Id { get; set; } public string Name { get; set; } public string Bio { get; set; } - - // Obliger de splti un dictionnaire pour le Json + public string Icon { get; set; } + + // Obliger de split un dictionnaire pour le Json public IEnumerable Keydic { get; set; } public IEnumerable Valuedic { get; set; } + + public ChampionClass Class { get; set; } + public LargeImage Image { get; set; } + public ReadOnlyCollection Skins { get; set; } } } diff --git a/Sources/API/Dto/SkinDto.cs b/Sources/API/Dto/SkinDto.cs index 097551a..ddb27d8 100644 --- a/Sources/API/Dto/SkinDto.cs +++ b/Sources/API/Dto/SkinDto.cs @@ -1,10 +1,15 @@ -namespace API.Dto +using Model; +using static System.Net.Mime.MediaTypeNames; + +namespace API.Dto { public class SkinDto { public string Name { get; set; } - public string Description { get; set; } - public string Icon { get; set; } + public Champion Champion { get; set; } public float Price { get; set; } + public string Icon { get; set; } + public LargeImage Image { get; set; } + public string Description { get; set; } } } diff --git a/Sources/API/Mapping/ChampionMapper.cs b/Sources/API/Mapping/ChampionMapper.cs index b8babf5..c1ad840 100644 --- a/Sources/API/Mapping/ChampionMapper.cs +++ b/Sources/API/Mapping/ChampionMapper.cs @@ -1,11 +1,14 @@ using API.Dto; using Model; using System.Collections.ObjectModel; +using System.Reflection.PortableExecutable; +using static System.Net.Mime.MediaTypeNames; +using System.Security.Claims; namespace API.Mapping { public static class ChampionMapper - {/* + { public static ChampionDto ToDto(this Champion champion) { if (champion == null) @@ -15,44 +18,24 @@ namespace API.Mapping return new ChampionDto { - Id = champion.Id, Name = champion.Name, // je peux décider de mettre le nom en minuscule pour que le json est des noms en minuscule Bio = champion.Bio, + Icon = champion.Icon, Keydic = champion.Characteristics.Keys, - Valuedic = champion.Characteristics.Values + Valuedic = champion.Characteristics.Values, + Class = champion.Class, + Image = champion.Image, + Skins = champion.Skins }; } - public static ChampionDto ToDto(this Champion champion) + public static Champion ToModel(this ChampionDto champion) { if (champion == null) { - throw new ArgumentNullException("champion null"); + throw new ArgumentNullException("championDto null"); } - return new ChampionDto - { - Id = champion.Id, - Name = champion.Name, // je peux décider de mettre le nom en minuscule pour que le json est des noms en minuscule - Bio = champion.Bio, - Keydic = champion.Characteristics.Keys, - Valuedic = champion.Characteristics.Values - }; + return new Champion(champion.Name, champion.Class, champion.Icon, champion.Image.Base64, champion.Bio); } - - public static Champion ToSkin(this SkinDto skinDto) - { - if (skinDto == null) - { - throw new ArgumentNullException("Dto null"); - } - - return new SkinDto() - { - Name = skinDto.Name, - Description = skinDto.Description, - Icon = skinDto.Icon, - Price = skinDto.Price - } - }*/ } } diff --git a/Sources/API/Mapping/SkinMapper.cs b/Sources/API/Mapping/SkinMapper.cs new file mode 100644 index 0000000..9ef0014 --- /dev/null +++ b/Sources/API/Mapping/SkinMapper.cs @@ -0,0 +1,37 @@ +using API.Dto; +using Model; + +namespace API.Mapping +{ + public static class SkinMapper + { + + public static SkinDto ToDto(this Skin skin) + { + if (skin == null) + { + throw new ArgumentNullException("Skin null"); + } + + return new SkinDto() + { + Name = skin.Name, + Champion = skin.Champion, + Price = skin.Price, + Icon = skin.Icon, + Image = skin.Image, + Description = skin.Description + }; + } + + public static Skin ToSkin(this SkinDto skinDto) + { + if (skinDto == null) + { + throw new ArgumentNullException("SkinDto null"); + } + + return new Skin(skinDto.Name, skinDto.Champion, skinDto.Price, skinDto.Icon, skinDto.Image.Base64, skinDto.Description); + } + } +} diff --git a/Sources/ConsoleDB/Program.cs b/Sources/ConsoleDB/Program.cs index 9b914df..8b994be 100644 --- a/Sources/ConsoleDB/Program.cs +++ b/Sources/ConsoleDB/Program.cs @@ -34,7 +34,7 @@ using (var context = new SQLiteContext()) foreach (var n in context.Champions) { - Console.WriteLine($"{n.Id} - {n.Name}"); + Console.WriteLine($"{n.Name}"); } } @@ -45,7 +45,7 @@ using (var context = new SQLiteContext()) { if (n.Name == "Soon") { - Console.WriteLine($"{n.Id} - viens d'être supprimer - {n.Name}"); + Console.WriteLine($"Viens d'être supprimer - {n.Name}"); context.Remove(n); } } @@ -60,7 +60,7 @@ using (var context = new SQLiteContext()) if (n.Name == "bigBoss") { n.Name = "miniBoss"; - Console.WriteLine($"{n.Id} - viens d'être changer - {n.Name}"); + Console.WriteLine($"Viens d'être changer - {n.Name}"); } } context.SaveChanges(); diff --git a/Sources/ConsoleDB/projet.dbloulou.db b/Sources/ConsoleDB/projet.dbloulou.db index 111a941..5606563 100644 Binary files a/Sources/ConsoleDB/projet.dbloulou.db and b/Sources/ConsoleDB/projet.dbloulou.db differ diff --git a/Sources/ConsoleDB/projet.dbloulou.db-shm b/Sources/ConsoleDB/projet.dbloulou.db-shm deleted file mode 100644 index 4c39f78..0000000 Binary files a/Sources/ConsoleDB/projet.dbloulou.db-shm and /dev/null differ diff --git a/Sources/ConsoleDB/projet.dbloulou.db-wal b/Sources/ConsoleDB/projet.dbloulou.db-wal deleted file mode 100644 index 7054dbd..0000000 Binary files a/Sources/ConsoleDB/projet.dbloulou.db-wal and /dev/null differ diff --git a/Sources/EFlib/EFChampion.cs b/Sources/EFlib/EFChampion.cs index 9fb46e8..681e77e 100644 --- a/Sources/EFlib/EFChampion.cs +++ b/Sources/EFlib/EFChampion.cs @@ -4,7 +4,7 @@ namespace EFlib { public class EFChampion { - public int Id { get; set; } + // public int Id { get; set; } // https://learn.microsoft.com/fr-fr/ef/core/modeling/keyless-entity-types?tabs=data-annotations /**** Only Attributs ****/ diff --git a/Sources/StubEF/StubEFChampions.cs b/Sources/StubEF/StubEFChampions.cs index 5ce347c..40f18f3 100644 --- a/Sources/StubEF/StubEFChampions.cs +++ b/Sources/StubEF/StubEFChampions.cs @@ -17,12 +17,12 @@ namespace StubEF { base.OnModelCreating(modelBuilder); modelBuilder.Entity().HasData( - new EFChampion { Id = 1, Name = "Akali", Bio = "maBio1", Icon = "monIcon1" }, - new EFChampion { Id=2, Name="Aatrox", Bio="maBio2", Icon="monIcon2" }, - new EFChampion { Id = 3, Name = "Ahri", Bio = "maBio3", Icon = "monIcon3" }, - new EFChampion { Id = 4, Name = "Akshan", Bio = "maBio4", Icon = "monIcon4" }, - new EFChampion { Id = 5, Name = "Bard", Bio = "maBio5", Icon = "monIcon5" }, - new EFChampion { Id = 6, Name = "Alistar", Bio = "maBio6", Icon = "monIcon6" } + new EFChampion { Name = "Akali", Bio = "maBio1", Icon = "monIcon1" }, + new EFChampion { Name="Aatrox", Bio="maBio2", Icon="monIcon2" }, + new EFChampion { Name = "Ahri", Bio = "maBio3", Icon = "monIcon3" }, + new EFChampion { Name = "Akshan", Bio = "maBio4", Icon = "monIcon4" }, + new EFChampion { Name = "Bard", Bio = "maBio5", Icon = "monIcon5" }, + new EFChampion { Name = "Alistar", Bio = "maBio6", Icon = "monIcon6" } ); } } diff --git a/Sources/TestEF/UnitTestChampion.cs b/Sources/TestEF/UnitTestChampion.cs index cc5885d..a9a5be1 100644 --- a/Sources/TestEF/UnitTestChampion.cs +++ b/Sources/TestEF/UnitTestChampion.cs @@ -22,10 +22,10 @@ namespace TestEF //context.Database.OpenConnection(); await context.Database.EnsureCreatedAsync(); //pour créer la base si elle n'existe pas déjà - var champs = context.Champions.SingleOrDefault(c=> c.Id == 1); + var champs = context.Champions.SingleOrDefault(c=> c.Name == "Akali"); Assert.NotNull(champs); - Assert.Equal(1, champs.Id); + Assert.Equal("Akali", champs.Name); } } /*