Fin du manager pour champion
continuous-integration/drone/push Build is failing Details

master
Thomas Chazot 2 years ago
parent 103a9f9c80
commit 56636621b6

@ -10,4 +10,23 @@
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<None Remove="DTO\" />
<None Remove="Mapping\" />
</ItemGroup>
<ItemGroup>
<Folder Include="DTO\" />
<Folder Include="Mapping\" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\EFLib\EFLib.csproj">
<GlobalPropertiesToRemove></GlobalPropertiesToRemove>
</ProjectReference>
<ProjectReference Include="..\Model\Model.csproj">
<GlobalPropertiesToRemove></GlobalPropertiesToRemove>
</ProjectReference>
<ProjectReference Include="..\StubLib\StubLib.csproj">
<GlobalPropertiesToRemove></GlobalPropertiesToRemove>
</ProjectReference>
</ItemGroup>
</Project> </Project>

@ -1,4 +1,5 @@
using System; using System;
using EFLib;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -12,62 +13,53 @@ 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>();
public ChampionController(ILogger<ChampionController> logger) public ChampionController(ILogger<ChampionController> logger)
{ {
_logger = logger; _logger = logger;
} champions.Add(new ChampionEntity
// GET: api/champion
[HttpGet]
public ActionResult<IEnumerable<ChampionDTO>> Get()
{
List<ChampionDTO> champions = new List<ChampionDTO>();
champions.Add(new ChampionDTO
{ {
Id = 1, Id = 1,
Name = "Aurel", Name = "Aurel",
Bio = "Trop joli", Bio = "Trop joli",
Icon = "Moi" Icon = "Moi",
champClass = Model.ChampionClass.Tank
}); });
champions.Add(new ChampionDTO champions.Add(new ChampionEntity
{ {
Id = 2, Id = 2,
Name = "Mathilde", Name = "Mathilde",
Bio = "Elle est reloue", Bio = "Elle est reloue",
Icon = "Macheval" Icon = "Macheval",
champClass = Model.ChampionClass.Assassin
}); });
return Ok(champions); }
// GET: api/champion
[HttpGet]
public ActionResult<IEnumerable<ChampionDTO>> Get()
{
IEnumerable<ChampionDTO> champs = new List<ChampionDTO>();
foreach(ChampionEntity arg in champions)
{
champs.Append(arg.ToDto());
}
return Ok(champs);
} }
// GET api/values/5 // GET api/values/5
[HttpGet("{id}")] [HttpGet("{id}")]
public ActionResult<ChampionDTO?> Get(int id) public ActionResult<ChampionDTO?> Get(int id)
{ {
List<ChampionDTO> champions = new List<ChampionDTO>(); ChampionDTO? champion = champions.SingleOrDefault((ChampionEntity arg) => arg.Id == id)?.ToDto();
champions.Add(new ChampionDTO if (champion != null)
{ {
Id = 1,
Name = "Aurel",
Bio = "Trop joli",
Icon = "Moi"
});
champions.Add(new ChampionDTO
{
Id = 2,
Name = "Mathilde",
Bio = "Elle est reloue",
Icon = "Macheval"
});
ChampionDTO? champion = champions.SingleOrDefault((ChampionDTO arg) => arg.Id == id);
if (champion == null)
{
return BadRequest();
}
return Ok(champion); return Ok(champion);
} }
return BadRequest();
}
// POST api/values // POST api/values
[HttpPost] [HttpPost]

@ -1,4 +1,5 @@
using System; using System;
using Model;
namespace ApiLol namespace ApiLol
{ {
public class ChampionDTO public class ChampionDTO
@ -10,6 +11,8 @@ namespace ApiLol
public string Icon { get; set; } public string Icon { get; set; }
public string Bio { get; set; } public string Bio { get; set; }
public ChampionClass champClass { get; set; }
} }
} }

@ -0,0 +1,41 @@
using System;
using EFLib;
namespace ApiLol
{
public static class ChampionMapper
{
public static ChampionDTO ToDto(this ChampionEntity 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
};
}
public static ChampionEntity ToEntity(this ChampionDTO champion)
{
if (champion == null)
{
throw new NullReferenceException();
}
return new ChampionEntity
{
Name = champion.Name,
Bio = champion.Bio,
Icon = champion.Icon,
Id = champion.Id,
champClass = champion.champClass
};
}
}
}

@ -5,7 +5,7 @@ using static System.Net.WebRequestMethods;
HttpClient http = new HttpClient(); HttpClient http = new HttpClient();
//http.BaseAddress = new Uri("https://localhost:7006/"); //http.BaseAddress = new Uri("https://localhost:7006/");
//Console.WriteLine(await http.GetStringAsync("https://localhost:7006/api/Champion")); Console.WriteLine(await http.GetStringAsync("https://localhost:7006/api/Champion"));
Console.WriteLine(await http.GetStringAsync("https://localhost:7006/api/Champion/1")); Console.WriteLine(await http.GetStringAsync("https://localhost:7006/api/Champion/1"));

@ -4,8 +4,14 @@ using Model;
Console.WriteLine("Hello, World!"); Console.WriteLine("Hello, World!");
using (var db = new DbDataManager(new EFLib.LolContext())) using (var db = new DbDataManager(new EFLib.LolContext()))
{ {
Champion champ = await db.Add(new Model.Champion("marche", Model.ChampionClass.Assassin, "ouii", "yes", "bio")); IEnumerable<Champion?> champions = await db.ChampionsMgr.GetItems(0, 10);
Console.WriteLine(champ.Name);
foreach (Champion c in champions)
{
Console.WriteLine(c.Name);
}
} }

@ -8,17 +8,36 @@ namespace DataManagers
{ {
public static Champion ToPoco(this ChampionEntity champion) public static Champion ToPoco(this ChampionEntity champion)
{ {
return new Champion(name: champion.Name, champClass: ChampionClass.Unknown, icon: champion.Icon, bio: champion.Bio); return new Champion(name: champion.Name, champClass: champion.ChampClass, icon: champion.Icon, bio: champion.Bio);
} }
public static ChampionEntity ToEntity(this Champion champion) public static ChampionEntity ToEntity(this Champion champion) => new ChampionEntity
{
return new ChampionEntity
{ {
Name = champion.Name, Name = champion.Name,
Bio = champion.Bio, Bio = champion.Bio,
Icon = champion.Icon, Icon = champion.Icon,
ChampClass = champion.Class,
//Characteristics = champion.Characteristics.Select(dict => dict).ToDictionary(pair => pair.Key, pair => pair.Value)
}; };
public static IEnumerable<Champion> ToPocos(this IEnumerable<ChampionEntity> champs)
{
List<Champion> champions = new List<Champion>();
foreach (ChampionEntity c in champs)
{
champions.Add(c.ToPoco());
}
return champions;
}
public static IEnumerable<ChampionEntity> toEntities(this IEnumerable<Champion> champs)
{
List<ChampionEntity> champions = new List<ChampionEntity>();
foreach (Champion c in champs)
{
champions.Add(c.ToEntity());
}
return champions;
} }
} }
} }

@ -14,19 +14,67 @@ namespace DataManagers
Context = context; Context = context;
} }
public Task<Champion?> AddItem(Champion? item) private IEnumerable<Champion> SortChampions(IEnumerable<Champion> champs, int index, int count, string? orderingPropertyName = null, bool descending = false)
{ {
throw new NotImplementedException(); switch (orderingPropertyName?.ToLower())
{
case "name":
champs = champs.OrderBy(arg => arg.Name).ToList();
break;
case "bio":
champs = champs.OrderBy(arg => arg.Bio).ToList();
break;
case "class":
champs = champs.OrderBy(arg => arg.Class).ToList();
break;
default:
break;
}
if (descending)
{
champs = champs.Reverse().ToList();
}
if (index + count > champs.ToList().Count) return champs.ToList().GetRange(index, champs.ToList().Count - index);
return champs.ToList().GetRange(index, count);
} }
public Task<bool> DeleteItem(Champion? item) public async Task<Champion?> AddItem(Champion? item)
{ {
throw new NotImplementedException(); if (item != null)
{
await Context.Champions.AddAsync(item.ToEntity());
await Context.SaveChangesAsync();
return await Task.Run(() => Context.Champions.SingleOrDefault((ChampionEntity arg) => arg.Name == item.Name)?.ToPoco());
}
return null;
} }
public Task<IEnumerable<Champion?>> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false) public async Task<bool> DeleteItem(Champion? item)
{ {
throw new NotImplementedException(); if (item != null)
{
ChampionEntity? entity = await Context.Champions.SingleOrDefaultAsync((ChampionEntity arg) => arg.Name == item.Name);
if (entity != null)
{
Context.Champions.Remove(entity);
int test = await Context.SaveChangesAsync();
return test==1;
}
}
return false;
}
public async Task<IEnumerable<Champion?>> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false)
{
IEnumerable<Champion> champs = await Task.Run(() => Context.Champions.ToList().ToPocos());
return SortChampions(champs, index, count,orderingPropertyName, descending);
}
public async Task<int> GetNbItems()
{
return await Task.FromResult(Context.Champions.Count());
} }
public Task<IEnumerable<Champion?>> GetItemsByCharacteristic(string charName, int index, int count, string? orderingPropertyName = null, bool descending = false) public Task<IEnumerable<Champion?>> GetItemsByCharacteristic(string charName, int index, int count, string? orderingPropertyName = null, bool descending = false)
@ -34,14 +82,16 @@ namespace DataManagers
throw new NotImplementedException(); throw new NotImplementedException();
} }
public Task<IEnumerable<Champion?>> GetItemsByClass(ChampionClass championClass, int index, int count, string? orderingPropertyName = null, bool descending = false) public async Task<IEnumerable<Champion?>> GetItemsByClass(ChampionClass championClass, int index, int count, string? orderingPropertyName = null, bool descending = false)
{ {
throw new NotImplementedException(); IEnumerable<Champion> champs = await Task.Run(() => Context.Champions.ToList().ToPocos().Where(arg => arg.Class == championClass));
return SortChampions(champs, index, count, orderingPropertyName, descending);
} }
public Task<IEnumerable<Champion?>> GetItemsByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false) public async Task<IEnumerable<Champion?>> GetItemsByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false)
{ {
throw new NotImplementedException(); IEnumerable<Champion> champs = await Task.Run(() => Context.Champions.ToList().ToPocos().Where(arg => arg.Name.Contains(substring)));
return SortChampions(champs, index, count, orderingPropertyName, descending);
} }
public Task<IEnumerable<Champion?>> GetItemsByRunePage(RunePage? runePage, int index, int count, string? orderingPropertyName = null, bool descending = false) public Task<IEnumerable<Champion?>> GetItemsByRunePage(RunePage? runePage, int index, int count, string? orderingPropertyName = null, bool descending = false)
@ -59,12 +109,6 @@ namespace DataManagers
throw new NotImplementedException(); throw new NotImplementedException();
} }
public async Task<int> GetNbItems()
{
return await Task.Run(() => Context.Champions.Count());
}
public Task<int> GetNbItemsByCharacteristic(string charName) public Task<int> GetNbItemsByCharacteristic(string charName)
{ {
throw new NotImplementedException(); throw new NotImplementedException();
@ -75,9 +119,9 @@ namespace DataManagers
throw new NotImplementedException(); throw new NotImplementedException();
} }
public Task<int> GetNbItemsByName(string substring) public async Task<int> GetNbItemsByName(string substring)
{ {
throw new NotImplementedException(); return await Task.FromResult(Context.Champions.Where((arg) => arg.Name.Contains(substring)).Count());
} }
public Task<int> GetNbItemsByRunePage(RunePage? runePage) public Task<int> GetNbItemsByRunePage(RunePage? runePage)
@ -95,9 +139,20 @@ namespace DataManagers
throw new NotImplementedException(); throw new NotImplementedException();
} }
public Task<Champion?> UpdateItem(Champion? oldItem, Champion? newItem) public async Task<Champion?> UpdateItem(Champion? oldItem, Champion? newItem)
{ {
throw new NotImplementedException(); ChampionEntity? champ = await Context.Champions.SingleOrDefaultAsync((arg) => arg.Name == oldItem.Name);
if (champ != null)
{
champ.Bio = newItem.Bio;
champ.Name = newItem.Name;
champ.ChampClass = newItem.Class;
champ.Icon = newItem.Icon;
Context.Champions.Update(champ);
await Context.SaveChangesAsync();
return champ.ToPoco();
}
return null;
} }
} }
} }

@ -9,7 +9,7 @@ namespace DataManagers
private LolContext Context { get; set; } private LolContext Context { get; set; }
public IChampionsManager ChampionsMgr => throw new NotImplementedException(); public IChampionsManager ChampionsMgr => new DbChampionManager(Context);
public ISkinsManager SkinsMgr => throw new NotImplementedException(); public ISkinsManager SkinsMgr => throw new NotImplementedException();
@ -23,24 +23,6 @@ namespace DataManagers
Context = context; Context = context;
} }
public async Task<int> GetNumbersOfElement()
{
return await Task.Run(() => Context.Champions.Count());
}
public async Task<Champion> GetById(int id)
{
return await Task.Run(() => Context.Champions.SingleOrDefault((ChampionEntity arg) => arg.Id == id).ToPoco());
}
public async Task<Champion> Add(Champion champ)
{
ChampionEntity entity = champ.ToEntity();
await Context.Champions.AddAsync(entity);
Context.SaveChanges();
return await Task.Run(() => Context.Champions.SingleOrDefault((ChampionEntity arg) => arg.Name == champ.Name && arg.Bio == champ.Bio).ToPoco());
}
public void Dispose() public void Dispose()
{ {
Context.Dispose(); Context.Dispose();

@ -14,7 +14,7 @@ namespace EFLib
public string Bio { get; set; } public string Bio { get; set; }
public ChampionClass champClass { get; set;} public ChampionClass ChampClass { get; set;}
} }
} }

Loading…
Cancel
Save