diff --git a/Sources/EFLol/DBDataManager/DataConverter.cs b/Sources/EFLol/DBDataManager/DataConverter.cs new file mode 100644 index 0000000..d0e23f8 --- /dev/null +++ b/Sources/EFLol/DBDataManager/DataConverter.cs @@ -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 + }; + } + } +} diff --git a/Sources/EFLol/DBDataManager/EFDataManager.cs b/Sources/EFLol/DBDataManager/EFDataManager.cs new file mode 100644 index 0000000..5537631 --- /dev/null +++ b/Sources/EFLol/DBDataManager/EFDataManager.cs @@ -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 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 ) -> Ca va dire que a chaque fois que j'utilise un IDataManager, il va créer un EFDataManager + } + + + public async Task DeleteItem(Champion? item) + { + if (item == null) + { + return false; + } + var deletedItem = _context.Remove(item); + await _context.SaveChangesAsync(); + return true; + } + + private Func filterByName = (champion, substring) => + champion.Name.Contains(substring, StringComparison.InvariantCultureIgnoreCase); + + + public Task UpdateItem(Champion? oldItem, Champion? newItem) + { + throw new NotImplementedException(); + } + + public Task GetNbItems() + { + throw new NotImplementedException(); + } + + public async Task> GetItems(int index, int count, string? orderingPropertyName = null, bool @descending = false) + { + IEnumerable champions = _context.Champions.Skip(index * count).Take(count).OrderBy(champion => orderingPropertyName).Select(champion => champion.ChampionToPoco()); + return champions; + } + + public Task GetNbItemsByName(string substring) + { + throw new NotImplementedException(); + } + + public Task> GetItemsByName(string substring, int index, int count, string? orderingPropertyName = null, + bool @descending = false) + { + throw new NotImplementedException(); + } + + + public Task GetNbItemsByCharacteristic(string charName) + { + throw new NotImplementedException(); + } + + public Task> GetItemsByCharacteristic(string charName, int index, int count, string? orderingPropertyName = null, + bool @descending = false) + { + throw new NotImplementedException(); + } + + public Task GetNbItemsByClass(ChampionClass championClass) + { + throw new NotImplementedException(); + } + + public Task> GetItemsByClass(ChampionClass championClass, int index, int count, string? orderingPropertyName = null, + bool @descending = false) + { + throw new NotImplementedException(); + } + + public Task GetNbItemsBySkill(Skill? skill) + { + throw new NotImplementedException(); + } + + public Task> GetItemsBySkill(Skill? skill, int index, int count, string? orderingPropertyName = null, bool @descending = false) + { + throw new NotImplementedException(); + } + + public Task GetNbItemsByRunePage(RunePage? runePage) + { + throw new NotImplementedException(); + } + + public Task> GetItemsByRunePage(RunePage? runePage, int index, int count, string? orderingPropertyName = null, + bool @descending = false) + { + throw new NotImplementedException(); + } + + public Task GetNbItemsBySkill(string skill) + { + throw new NotImplementedException(); + } + + public Task> GetItemsBySkill(string skill, int index, int count, string? orderingPropertyName = null, bool @descending = false) + { + throw new NotImplementedException(); + } + } +} diff --git a/Sources/EFLol/EFDataManager.cs b/Sources/EFLol/EFDataManager.cs deleted file mode 100644 index 66a7efc..0000000 --- a/Sources/EFLol/EFDataManager.cs +++ /dev/null @@ -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 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 ) -> Ca va dire que a chaque fois que j'utilise un IDataManager, il va créer un EFDataManager - throw new NotImplementedException(); - } - - - public Task DeleteItem(Champion? item) - { - throw new NotImplementedException(); - } - public Task UpdateItem(Champion? oldItem, Champion? newItem) - { - throw new NotImplementedException(); - } - - public Task GetNbItems() - { - throw new NotImplementedException(); - } - - public Task> GetItems(int index, int count, string? orderingPropertyName = null, bool @descending = false) - { - throw new NotImplementedException(); - } - - public Task GetNbItemsByName(string substring) - { - throw new NotImplementedException(); - } - - public Task> GetItemsByName(string substring, int index, int count, string? orderingPropertyName = null, - bool @descending = false) - { - throw new NotImplementedException(); - } - - - public Task GetNbItemsByCharacteristic(string charName) - { - throw new NotImplementedException(); - } - - public Task> GetItemsByCharacteristic(string charName, int index, int count, string? orderingPropertyName = null, - bool @descending = false) - { - throw new NotImplementedException(); - } - - public Task GetNbItemsByClass(ChampionClass championClass) - { - throw new NotImplementedException(); - } - - public Task> GetItemsByClass(ChampionClass championClass, int index, int count, string? orderingPropertyName = null, - bool @descending = false) - { - throw new NotImplementedException(); - } - - public Task GetNbItemsBySkill(Skill? skill) - { - throw new NotImplementedException(); - } - - public Task> GetItemsBySkill(Skill? skill, int index, int count, string? orderingPropertyName = null, bool @descending = false) - { - throw new NotImplementedException(); - } - - public Task GetNbItemsByRunePage(RunePage? runePage) - { - throw new NotImplementedException(); - } - - public Task> GetItemsByRunePage(RunePage? runePage, int index, int count, string? orderingPropertyName = null, - bool @descending = false) - { - throw new NotImplementedException(); - } - - public Task GetNbItemsBySkill(string skill) - { - throw new NotImplementedException(); - } - - public Task> GetItemsBySkill(string skill, int index, int count, string? orderingPropertyName = null, bool @descending = false) - { - throw new NotImplementedException(); - } - } -} diff --git a/Sources/Model/Champion.cs b/Sources/Model/Champion.cs index fd4a5ca..0e33455 100644 --- a/Sources/Model/Champion.cs +++ b/Sources/Model/Champion.cs @@ -53,7 +53,13 @@ public class Champion : IEquatable Skins = new ReadOnlyCollection(skins); } - public ReadOnlyCollection Skins { get; private set; } + public Champion(string name, string bio) + { + this.name = name; + this.bio = bio; + } + + public ReadOnlyCollection Skins { get; private set; } private List skins = new (); public ReadOnlyDictionary Characteristics { get; private set; } diff --git a/Sources/apiLOL/Controllers/ControllerChampions.cs b/Sources/apiLOL/Controllers/ControllerChampions.cs index 5e129e5..20e85b6 100644 --- a/Sources/apiLOL/Controllers/ControllerChampions.cs +++ b/Sources/apiLOL/Controllers/ControllerChampions.cs @@ -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 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 diff --git a/Sources/apiLOL/Program.cs b/Sources/apiLOL/Program.cs index 1a8abfa..ec1f140 100644 --- a/Sources/apiLOL/Program.cs +++ b/Sources/apiLOL/Program.cs @@ -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(); +//builder.Services.AddSingleton(); +builder.Services.AddScoped(); var app = builder.Build(); @@ -21,6 +23,7 @@ if (app.Environment.IsDevelopment()) app.UseSwaggerUI(); } + app.UseHttpsRedirection(); app.UseAuthorization(); diff --git a/Sources/apiLOL/apiLOL.csproj b/Sources/apiLOL/apiLOL.csproj index b90f766..d8a99b3 100644 --- a/Sources/apiLOL/apiLOL.csproj +++ b/Sources/apiLOL/apiLOL.csproj @@ -20,6 +20,7 @@ +