🚧 Beginning of link between DB and API
continuous-integration/drone/push Build is passing Details

EF2
Nathan BOILEAU 2 years ago
parent 569cc52146
commit 602b05abdb

@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
namespace EFLol.DBDataManager
{
public static class DataConverter
{
public static Champion ChampionToPoco(this ChampionEntity champion)
{
return new Champion(champion.Name, champion.Bio);
}
public static ChampionEntity ChampionToEntity(this Champion champion)
{
return new ChampionEntity
{
Name = champion.Name,
Bio = champion.Bio
};
}
}
}

@ -0,0 +1,132 @@
using Model;
namespace EFLol.DBDataManager
{
public class EFDataManager : IDataManager
{
public IChampionsManager ChampionsMgr => new EFChampionManager();
public ISkinsManager SkinsMgr => throw new NotImplementedException();
public IRunesManager RunesMgr => throw new NotImplementedException();
public IRunePagesManager RunePagesMgr => throw new NotImplementedException();
}
public class EFChampionManager : IChampionsManager
{
private MyDbContext _context;
public async Task<Champion?> AddItem(Champion? item)
{
if (item == null)
{
return null;
}
var addItem = await _context.AddAsync(item);
await _context.SaveChangesAsync();
return addItem.Entity;
// Va chercher les info du context (Context.champions.get)(DbSet) et les map en champion model
// Dans Program.cs de API rajouter un scope (AddScope <IDataManager, EFDataManager>) -> Ca va dire que a chaque fois que j'utilise un IDataManager, il va créer un EFDataManager
}
public async Task<bool> DeleteItem(Champion? item)
{
if (item == null)
{
return false;
}
var deletedItem = _context.Remove(item);
await _context.SaveChangesAsync();
return true;
}
private Func<Champion, string, bool> filterByName = (champion, substring) =>
champion.Name.Contains(substring, StringComparison.InvariantCultureIgnoreCase);
public Task<Champion?> UpdateItem(Champion? oldItem, Champion? newItem)
{
throw new NotImplementedException();
}
public Task<int> GetNbItems()
{
throw new NotImplementedException();
}
public async Task<IEnumerable<Champion?>> GetItems(int index, int count, string? orderingPropertyName = null, bool @descending = false)
{
IEnumerable<Champion> champions = _context.Champions.Skip(index * count).Take(count).OrderBy(champion => orderingPropertyName).Select(champion => champion.ChampionToPoco());
return champions;
}
public Task<int> GetNbItemsByName(string substring)
{
throw new NotImplementedException();
}
public Task<IEnumerable<Champion?>> GetItemsByName(string substring, int index, int count, string? orderingPropertyName = null,
bool @descending = false)
{
throw new NotImplementedException();
}
public Task<int> GetNbItemsByCharacteristic(string charName)
{
throw new NotImplementedException();
}
public Task<IEnumerable<Champion?>> GetItemsByCharacteristic(string charName, int index, int count, string? orderingPropertyName = null,
bool @descending = false)
{
throw new NotImplementedException();
}
public Task<int> GetNbItemsByClass(ChampionClass championClass)
{
throw new NotImplementedException();
}
public Task<IEnumerable<Champion?>> GetItemsByClass(ChampionClass championClass, int index, int count, string? orderingPropertyName = null,
bool @descending = false)
{
throw new NotImplementedException();
}
public Task<int> GetNbItemsBySkill(Skill? skill)
{
throw new NotImplementedException();
}
public Task<IEnumerable<Champion?>> GetItemsBySkill(Skill? skill, int index, int count, string? orderingPropertyName = null, bool @descending = false)
{
throw new NotImplementedException();
}
public Task<int> GetNbItemsByRunePage(RunePage? runePage)
{
throw new NotImplementedException();
}
public Task<IEnumerable<Champion?>> GetItemsByRunePage(RunePage? runePage, int index, int count, string? orderingPropertyName = null,
bool @descending = false)
{
throw new NotImplementedException();
}
public Task<int> GetNbItemsBySkill(string skill)
{
throw new NotImplementedException();
}
public Task<IEnumerable<Champion?>> GetItemsBySkill(string skill, int index, int count, string? orderingPropertyName = null, bool @descending = false)
{
throw new NotImplementedException();
}
}
}

@ -1,131 +0,0 @@
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EFLol
{
public class EFDataManager : IDataManager
{
// Connect to the MyDbContext class
private readonly MyDbContext _context;
// Constructor
public EFDataManager(MyDbContext context)
{
_context = context;
}
public IChampionsManager ChampionsMgr => throw new NotImplementedException();
public ISkinsManager SkinsMgr => throw new NotImplementedException();
public IRunesManager RunesMgr => throw new NotImplementedException();
public IRunePagesManager RunePagesMgr => throw new NotImplementedException();
}
public class EFChampionManager : IChampionsManager
{
private readonly MyDbContext _context;
public EFChampionManager(MyDbContext context)
{
_context = context;
}
public Task<Champion?> AddItem(Champion? item)
{
// Va chercher les info du context (Context.champions.get)(DbSet) et les map en champion model
// Dans Program.cs de API rajouter un scope (AddScope <IDataManager, EFDataManager>) -> Ca va dire que a chaque fois que j'utilise un IDataManager, il va créer un EFDataManager
throw new NotImplementedException();
}
public Task<bool> DeleteItem(Champion? item)
{
throw new NotImplementedException();
}
public Task<Champion?> UpdateItem(Champion? oldItem, Champion? newItem)
{
throw new NotImplementedException();
}
public Task<int> GetNbItems()
{
throw new NotImplementedException();
}
public Task<IEnumerable<Champion?>> GetItems(int index, int count, string? orderingPropertyName = null, bool @descending = false)
{
throw new NotImplementedException();
}
public Task<int> GetNbItemsByName(string substring)
{
throw new NotImplementedException();
}
public Task<IEnumerable<Champion?>> GetItemsByName(string substring, int index, int count, string? orderingPropertyName = null,
bool @descending = false)
{
throw new NotImplementedException();
}
public Task<int> GetNbItemsByCharacteristic(string charName)
{
throw new NotImplementedException();
}
public Task<IEnumerable<Champion?>> GetItemsByCharacteristic(string charName, int index, int count, string? orderingPropertyName = null,
bool @descending = false)
{
throw new NotImplementedException();
}
public Task<int> GetNbItemsByClass(ChampionClass championClass)
{
throw new NotImplementedException();
}
public Task<IEnumerable<Champion?>> GetItemsByClass(ChampionClass championClass, int index, int count, string? orderingPropertyName = null,
bool @descending = false)
{
throw new NotImplementedException();
}
public Task<int> GetNbItemsBySkill(Skill? skill)
{
throw new NotImplementedException();
}
public Task<IEnumerable<Champion?>> GetItemsBySkill(Skill? skill, int index, int count, string? orderingPropertyName = null, bool @descending = false)
{
throw new NotImplementedException();
}
public Task<int> GetNbItemsByRunePage(RunePage? runePage)
{
throw new NotImplementedException();
}
public Task<IEnumerable<Champion?>> GetItemsByRunePage(RunePage? runePage, int index, int count, string? orderingPropertyName = null,
bool @descending = false)
{
throw new NotImplementedException();
}
public Task<int> GetNbItemsBySkill(string skill)
{
throw new NotImplementedException();
}
public Task<IEnumerable<Champion?>> GetItemsBySkill(string skill, int index, int count, string? orderingPropertyName = null, bool @descending = false)
{
throw new NotImplementedException();
}
}
}

@ -53,6 +53,12 @@ public class Champion : IEquatable<Champion>
Skins = new ReadOnlyCollection<Skin>(skins);
}
public Champion(string name, string bio)
{
this.name = name;
this.bio = bio;
}
public ReadOnlyCollection<Skin> Skins { get; private set; }
private List<Skin> skins = new ();

@ -11,12 +11,13 @@ namespace apiLOL.Controllers
public class ControllerChampions : Controller
{
private readonly IDataManager data;
//private readonly IDataManager data;
private IChampionsManager _dataManager;
private readonly ILogger _logger;
public ControllerChampions(IDataManager manager, ILogger<ControllerChampions> log)
{
data = manager;
_dataManager = manager.ChampionsMgr;
_logger = log;
}
@ -28,10 +29,10 @@ namespace apiLOL.Controllers
//FromQuery permet de filtrer dans la collection de champions en fonction du nom
// Possible de faire une classe PageRequest pour gérer les paramètres index et count
_logger.LogInformation($"methode Get de ControllerChampions appelée");
int nbChampions = await data.ChampionsMgr.GetNbItems();
int nbChampions = await _dataManager.GetNbItems();
_logger.LogInformation($"Nombre de champions : {nbChampions}");
var champs = (await data.ChampionsMgr.GetItems(index, count)).Select(Model => Model.ToDTO());
var champs = (await _dataManager.GetItems(index, count)).Select(Model => Model.ToDTO());
var page = new ChampionPageDTO
{
@ -52,7 +53,7 @@ namespace apiLOL.Controllers
_logger.LogInformation($"methode GetChampion de ControllerChampions appelée avec le paramètre {name}");
try
{
var champs = (await data.ChampionsMgr.GetItemsByName(name, 0, 1));
var champs = (await _dataManager.GetItemsByName(name, 0, 1));
return Ok(champs.First().ToDTO());
}
catch
@ -72,7 +73,7 @@ namespace apiLOL.Controllers
try
{
Champion tmp = champDTO.ToModel();
Champion champion = await data.ChampionsMgr.AddItem(tmp);
Champion champion = await _dataManager.AddItem(tmp);
ChampionDTO dtoChamp = champion.ToDTO();
return CreatedAtAction(nameof(GetChampion), new { name = dtoChamp.Name }, dtoChamp);
}
@ -90,7 +91,7 @@ namespace apiLOL.Controllers
try
{
var champs = (await data.ChampionsMgr.GetItemsByName(name, 0, 1)).First();
var champs = (await _dataManager.GetItemsByName(name, 0, 1)).First();
champs.Bio = bio;
return Ok(champs.ToDTO());
}
@ -106,8 +107,8 @@ namespace apiLOL.Controllers
{
try
{
var champ = (await data.ChampionsMgr.GetItemsByName(name, 0, 1)).First();
data.ChampionsMgr.DeleteItem(champ);
var champ = (await _dataManager.GetItemsByName(name, 0, 1)).First();
_dataManager.DeleteItem(champ);
return Ok(champ.ToDTO());
}
catch

@ -1,3 +1,4 @@
using EFLol.DBDataManager;
using Model;
using StubLib;
@ -10,7 +11,8 @@ builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddSingleton<IDataManager,StubData>();
//builder.Services.AddSingleton<IDataManager,StubData>();
builder.Services.AddScoped<IDataManager, EFDataManager>();
var app = builder.Build();
@ -21,6 +23,7 @@ if (app.Environment.IsDevelopment())
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();

@ -20,6 +20,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\EFLol\EFLol.csproj" />
<ProjectReference Include="..\Model\Model.csproj" />
<ProjectReference Include="..\StubLib\StubLib.csproj" />
</ItemGroup>

Loading…
Cancel
Save