Update DTO
continuous-integration/drone/push Build is failing Details

pull/10/head
Louwar 2 years ago
parent 1a466a8b97
commit 3a3370257a

@ -1,5 +1,6 @@
using API.Dto; using API.Dto;
using API.Mapping; using API.Mapping;
using EFlib;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Model; using Model;
@ -18,41 +19,45 @@ namespace API.Controllers
public class ChampionController : ControllerBase public class ChampionController : ControllerBase
{ {
private readonly ILogger<ChampionController> _logger; private readonly ILogger<ChampionController> _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 readonly IDataManager dataManager; // Pour plus tard pour le momment c'est avec le stub
// private const string Apichampion = "api/champion"; private const string Apichampion = "api/champion";
// private readonly HttpClient _client; private readonly HttpClient _client;
public ChampionController(ILogger<ChampionController> logger) public ChampionController(ILogger<ChampionController> logger)
{ {
_logger = logger; _logger = logger;
} }
/* public championHttpManager(HttpClient client) /* public championHttpManager(HttpClient client)
{ {
_client = client; _client = client;
client.BaseAddress = new Uri("à chopper dans lauchSettings.json propriété du projet"); client.BaseAddress = new Uri("à chopper dans lauchSettings.json propriété du projet");
} }
public async Task<IEnumerable<ChampionDto>> getJson() public async Task<IEnumerable<ChampionDto>> getJson()
{ {
var champions = await _client.GetFromJsonAsync<IEnumerable<ChampionDto>>(); var champions = await _client.GetFromJsonAsync<IEnumerable<ChampionDto>>();
var reponse = await _client.GetAsync("api/champion"); var reponse = await _client.GetAsync("api/champion");
return champions; return champions;
} }*/
*/
/**** Méthodes GET **** /**** Méthodes GET ****/
[HttpGet] [HttpGet]
public async Task<ActionResult<ChampionDto>> GetChamps() public async Task<ActionResult<ChampionDto>> GetChamps()
{ {
IEnumerable<Champion?> Champs = await stub.ChampionsMgr.GetItems(0, stub.ChampionsMgr.GetNbItems().Result); // Récupération de la liste des champions
IEnumerable<Champion?> Champs = await data.ChampionsMgr.GetItems(0, data.ChampionsMgr.GetNbItems().Result);
// Création de la liste de champion Dto
List<ChampionDto> DtoChamps = new List<ChampionDto>(); List<ChampionDto> DtoChamps = new List<ChampionDto>();
// Chargement de la liste des champions Dto à partir des champions
Champs.ToList().ForEach(c => DtoChamps.Add(c.ToDto())); Champs.ToList().ForEach(c => DtoChamps.Add(c.ToDto()));
return Ok(DtoChamps); return Ok(DtoChamps);
} }
@ -62,9 +67,9 @@ namespace API.Controllers
{ {
BadRequest("404"); BadRequest("404");
IEnumerable<Champion?> Champs = await stub.ChampionsMgr.GetItems(id, 1); IEnumerable<Champion?> 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()); return Ok(Champs.First().ToDto());
} }
@ -74,65 +79,72 @@ namespace API.Controllers
[HttpGet("{id}/Skins")] [HttpGet("{id}/Skins")]
public async Task<ActionResult<SkinDto>> GetSkinsChamp(int id) public async Task<ActionResult<SkinDto>> GetSkinsChamp(int id)
{ {
IEnumerable<Champion?> lcha = await stub.ChampionsMgr.GetItems(id, 1); // Récupération du champion
IEnumerable<Champion?> 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<Skin?> lsk = await stub.SkinsMgr.GetItemsByChampion(ca, 0, stub.SkinsMgr.GetNbItemsByChampion(ca).Result); // Récupération des skin du champion
IEnumerable<Skin?> ListeSkin = await data.SkinsMgr.GetItemsByChampion(champion, 0, data.SkinsMgr.GetNbItemsByChampion(champion).Result);
// Création de la liste de skin
List<SkinDto> skins = new List<SkinDto>(); List<SkinDto> skins = new List<SkinDto>();
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 Ok(skins);
} }
return BadRequest(); return BadRequest();
} }
/**** Méthodes POST **** /**** Méthodes POST ****/
[HttpPost("Ajouter/{nom}")] [HttpPost("Ajouter/{nom}")]
public async Task<ActionResult> PostChampName(string nom) public async Task<ActionResult> 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")] [HttpPost("Ajouter")]
public async Task<IActionResult> PostChamp([FromBody] ChampionDto c) public async Task<IActionResult> PostChamp([FromBody] ChampionDto championDto)
{ {
Champion ca = c.ToModel(); Champion champion = championDto.ToModel();
await stub.ChampionsMgr.AddItem(ca); await data.ChampionsMgr.AddItem(champion);
return CreatedAtAction(nameof(stub.ChampionsMgr.GetItemsByName), new { Name = c.Name }, c); return CreatedAtAction(nameof(data.ChampionsMgr.GetItemsByName), new { Name = championDto.Name }, championDto);
} }
[HttpPost] [HttpPost]
public async Task<IActionResult> post([FromBody] ChampionDto champion) public async Task<IActionResult> post([FromBody] ChampionDto championDto)
{ {
return CreatedAtAction(nameof(GetChampById), new { id = 1 }, 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<Champion>(ApiChampion, champion); _clientLpostAsJsonAscync<Champion>(ApiChampion, champion);
} }
*/
/**** Méthodes DELETE ****/
/**** Méthodes DELETE ****
[HttpDelete("Supprimer/{id}")] [HttpDelete("Supprimer/{id}")]
public async Task<IActionResult> DeleteChamp(int id) public async Task<IActionResult> DeleteChamp(int id)
{ {
IEnumerable<Champion?> lcha = await stub.ChampionsMgr.GetItems(id, 1); IEnumerable<Champion?> 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(); Champion ca = lcha.First();
stub.ChampionsMgr.DeleteItem(ca); data.ChampionsMgr.DeleteItem(ca);
return Ok(); return Ok();
} }
return BadRequest(); 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<ActionResult> PutChampName(string nom) public async Task<ActionResult> 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")] /*[HttpPut("Modifier")]
public async Task<IActionResult> PutChamp([FromBody] ChampionDto c, [FromBody] ChampionDto cNouv) public async Task<IActionResult> PutChamp([FromBody] ChampionDto championDto)
{ {
Champion ca = c.ToModel(); Champion champion = championDto.ToModel();
Champion caNouv = cNouv.ToModel(); await data.ChampionsMgr.UpdateItem(champion);
await stub.ChampionsMgr.UpdateItem(ca, caNouv); return CreatedAtAction(nameof(GetChampById), new { id = data.ChampionsMgr.GetItems(0, data.ChampionsMgr.GetNbItems().Result).Result.ToList().IndexOf(champion) }, champion);
return CreatedAtAction(nameof(GetChampById), new { id = stub.ChampionsMgr.GetItems(0, stub.ChampionsMgr.GetNbItems().Result).Result.ToList().IndexOf(ca) }, ca);
}*/ }*/
} }

@ -1,15 +1,22 @@
namespace API.Dto using Model;
using System.Collections.ObjectModel;
namespace API.Dto
{ {
public class ChampionDto public class ChampionDto
{ {
/**** Only Attributs ****/ /**** Only Attributs ****/
public int Id { get; set; }
public string Name { get; set; } public string Name { get; set; }
public string Bio { get; set; } public string Bio { get; set; }
public string Icon { get; set; }
// Obliger de splti un dictionnaire pour le Json // Obliger de split un dictionnaire pour le Json
public IEnumerable<string> Keydic { get; set; } public IEnumerable<string> Keydic { get; set; }
public IEnumerable<int> Valuedic { get; set; } public IEnumerable<int> Valuedic { get; set; }
public ChampionClass Class { get; set; }
public LargeImage Image { get; set; }
public ReadOnlyCollection<Skin> Skins { get; set; }
} }
} }

@ -1,10 +1,15 @@
namespace API.Dto using Model;
using static System.Net.Mime.MediaTypeNames;
namespace API.Dto
{ {
public class SkinDto public class SkinDto
{ {
public string Name { get; set; } public string Name { get; set; }
public string Description { get; set; } public Champion Champion { get; set; }
public string Icon { get; set; }
public float Price { get; set; } public float Price { get; set; }
public string Icon { get; set; }
public LargeImage Image { get; set; }
public string Description { get; set; }
} }
} }

@ -1,11 +1,14 @@
using API.Dto; using API.Dto;
using Model; using Model;
using System.Collections.ObjectModel; using System.Collections.ObjectModel;
using System.Reflection.PortableExecutable;
using static System.Net.Mime.MediaTypeNames;
using System.Security.Claims;
namespace API.Mapping namespace API.Mapping
{ {
public static class ChampionMapper public static class ChampionMapper
{/* {
public static ChampionDto ToDto(this Champion champion) public static ChampionDto ToDto(this Champion champion)
{ {
if (champion == null) if (champion == null)
@ -15,44 +18,24 @@ namespace API.Mapping
return new ChampionDto 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 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, Bio = champion.Bio,
Icon = champion.Icon,
Keydic = champion.Characteristics.Keys, 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) if (champion == null)
{ {
throw new ArgumentNullException("champion null"); throw new ArgumentNullException("championDto null");
} }
return new ChampionDto return new Champion(champion.Name, champion.Class, champion.Icon, champion.Image.Base64, champion.Bio);
{
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
};
} }
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
}
}*/
} }
} }

@ -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);
}
}
}

@ -34,7 +34,7 @@ using (var context = new SQLiteContext())
foreach (var n in context.Champions) 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") 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); context.Remove(n);
} }
} }
@ -60,7 +60,7 @@ using (var context = new SQLiteContext())
if (n.Name == "bigBoss") if (n.Name == "bigBoss")
{ {
n.Name = "miniBoss"; n.Name = "miniBoss";
Console.WriteLine($"{n.Id} - viens d'être changer - {n.Name}"); Console.WriteLine($"Viens d'être changer - {n.Name}");
} }
} }
context.SaveChanges(); context.SaveChanges();

@ -4,7 +4,7 @@ namespace EFlib
{ {
public class EFChampion 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 // https://learn.microsoft.com/fr-fr/ef/core/modeling/keyless-entity-types?tabs=data-annotations
/**** Only Attributs ****/ /**** Only Attributs ****/

@ -17,12 +17,12 @@ namespace StubEF
{ {
base.OnModelCreating(modelBuilder); base.OnModelCreating(modelBuilder);
modelBuilder.Entity<EFChampion>().HasData( modelBuilder.Entity<EFChampion>().HasData(
new EFChampion { Id = 1, Name = "Akali", Bio = "maBio1", Icon = "monIcon1" }, new EFChampion { Name = "Akali", Bio = "maBio1", Icon = "monIcon1" },
new EFChampion { Id=2, Name="Aatrox", Bio="maBio2", Icon="monIcon2" }, new EFChampion { Name="Aatrox", Bio="maBio2", Icon="monIcon2" },
new EFChampion { Id = 3, Name = "Ahri", Bio = "maBio3", Icon = "monIcon3" }, new EFChampion { Name = "Ahri", Bio = "maBio3", Icon = "monIcon3" },
new EFChampion { Id = 4, Name = "Akshan", Bio = "maBio4", Icon = "monIcon4" }, new EFChampion { Name = "Akshan", Bio = "maBio4", Icon = "monIcon4" },
new EFChampion { Id = 5, Name = "Bard", Bio = "maBio5", Icon = "monIcon5" }, new EFChampion { Name = "Bard", Bio = "maBio5", Icon = "monIcon5" },
new EFChampion { Id = 6, Name = "Alistar", Bio = "maBio6", Icon = "monIcon6" } new EFChampion { Name = "Alistar", Bio = "maBio6", Icon = "monIcon6" }
); );
} }
} }

@ -22,10 +22,10 @@ namespace TestEF
//context.Database.OpenConnection(); //context.Database.OpenConnection();
await context.Database.EnsureCreatedAsync(); //pour créer la base si elle n'existe pas déjà 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.NotNull(champs);
Assert.Equal(1, champs.Id); Assert.Equal("Akali", champs.Name);
} }
} }
/* /*

Loading…
Cancel
Save