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" />
</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>

@ -1,4 +1,5 @@
using System;
using EFLib;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
@ -12,61 +13,52 @@ namespace ApiLol.Controllers
public class ChampionController : ControllerBase
{
private readonly ILogger<ChampionController> _logger;
private List<ChampionEntity> champions = new List<ChampionEntity>();
public ChampionController(ILogger<ChampionController> logger)
{
_logger = logger;
}
// GET: api/champion
[HttpGet]
public ActionResult<IEnumerable<ChampionDTO>> Get()
{
List<ChampionDTO> champions = new List<ChampionDTO>();
champions.Add(new ChampionDTO
champions.Add(new ChampionEntity
{
Id = 1,
Name = "Aurel",
Bio = "Trop joli",
Icon = "Moi"
Icon = "Moi",
champClass = Model.ChampionClass.Tank
});
champions.Add(new ChampionDTO
champions.Add(new ChampionEntity
{
Id = 2,
Name = "Mathilde",
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
[HttpGet("{id}")]
public ActionResult<ChampionDTO?> Get(int id)
{
List<ChampionDTO> champions = new List<ChampionDTO>();
champions.Add(new ChampionDTO
{
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)
ChampionDTO? champion = champions.SingleOrDefault((ChampionEntity arg) => arg.Id == id)?.ToDto();
if (champion != null)
{
return BadRequest();
return Ok(champion);
}
return Ok(champion);
return BadRequest();
}
// POST api/values

@ -1,4 +1,5 @@
using System;
using Model;
namespace ApiLol
{
public class ChampionDTO
@ -10,6 +11,8 @@ namespace ApiLol
public string Icon { 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();
//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"));

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

@ -8,17 +8,36 @@ namespace DataManagers
{
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,
Bio = champion.Bio,
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)
{
Name = champion.Name,
Bio = champion.Bio,
Icon = champion.Icon,
};
champions.Add(c.ToEntity());
}
return champions;
}
}
}

@ -14,19 +14,67 @@ namespace DataManagers
Context = context;
}
public Task<Champion?> AddItem(Champion? item)
{
throw new NotImplementedException();
private IEnumerable<Champion> SortChampions(IEnumerable<Champion> champs, int index, int count, string? orderingPropertyName = null, bool descending = false)
{
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 async Task<Champion?> AddItem(Champion? item)
{
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 async Task<bool> DeleteItem(Champion? item)
{
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 Task<bool> DeleteItem(Champion? item)
public async Task<int> GetNbItems()
{
throw new NotImplementedException();
}
return await Task.FromResult(Context.Champions.Count());
public Task<IEnumerable<Champion?>> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false)
{
throw new NotImplementedException();
}
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();
}
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)
@ -59,12 +109,6 @@ namespace DataManagers
throw new NotImplementedException();
}
public async Task<int> GetNbItems()
{
return await Task.Run(() => Context.Champions.Count());
}
public Task<int> GetNbItemsByCharacteristic(string charName)
{
throw new NotImplementedException();
@ -75,9 +119,9 @@ namespace DataManagers
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)
@ -95,9 +139,20 @@ namespace DataManagers
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; }
public IChampionsManager ChampionsMgr => throw new NotImplementedException();
public IChampionsManager ChampionsMgr => new DbChampionManager(Context);
public ISkinsManager SkinsMgr => throw new NotImplementedException();
@ -23,24 +23,6 @@ namespace DataManagers
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()
{
Context.Dispose();

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

Loading…
Cancel
Save