Finish link between API and DB
continuous-integration/drone/push Build is passing Details

master
Nathan BOILEAU 2 years ago
parent 3eabcfa96e
commit 76de6eccbb

@ -1,22 +1,23 @@
using Model;
using Microsoft.EntityFrameworkCore;
using Model;
namespace EFLol.DBDataManager
{
public class EFDataManager : IDataManager
{
public class EFDataManager : IDataManager
{
public IChampionsManager ChampionsMgr => new EFChampionManager();
public ISkinsManager SkinsMgr => throw new NotImplementedException();
public IRunesManager RunesMgr => throw new NotImplementedException();
public IRunesManager RunesMgr => throw new NotImplementedException();
public IRunePagesManager RunePagesMgr => throw new NotImplementedException();
}
public IRunePagesManager RunePagesMgr => throw new NotImplementedException();
}
public class EFChampionManager : IChampionsManager
{
private MyDbContext _context;
public class EFChampionManager : IChampionsManager
{
private MyDbContext _context;
public EFChampionManager()
{
@ -24,111 +25,141 @@ namespace EFLol.DBDataManager
}
public async Task<Champion?> AddItem(Champion? item)
{
if (item == null)
{
return null;
}
var addItem = await _context.AddAsync<Champion>(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();
}
{
if (item == null)
{
throw new Exception("Item is null");
}
_context.Add(item.ChampionToEntity());
_context.SaveChanges();
return item;
}
public async Task<bool> DeleteItem(Champion? item)
{
if (item == null)
{
return false;
}
var champ = _context.Champions.Select(c => c == item.ChampionToEntity());
if (champ.Count() < 1)
{
return false;
}
_context.Champions.Remove(item.ChampionToEntity());
_context.SaveChanges();
return true;
}
public async Task<Champion?> UpdateItem(Champion? oldItem, Champion? newItem)
{
if (oldItem != null && newItem != null)
{
var champEntity = await _context.Champions.FirstOrDefaultAsync(c => c.Name == oldItem.Name);
if (champEntity == null)
{
throw new Exception("Champion not found in database");
}
champEntity.Bio = newItem.Bio;
_context.SaveChanges();
return champEntity.ChampionToPoco();
}
throw new Exception("Invalid input parameters");
}
public async Task<int> GetNbItems() => _context.Champions.Count();
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());
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(champions => orderingPropertyName)
.Select(champions => champions.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();
}
}
}
private Func<Champion, string, bool> filterByName = (champion, substring) =>
champion.Name.IndexOf(substring, StringComparison.InvariantCultureIgnoreCase) >= 0;
public Task<IEnumerable<Champion?>> GetItemsByName(string substring, int index, int count,
string? orderingPropertyName = null, bool descending = false)
=> _context.Champions.Select(champion => champion.ChampionToPoco())
.GetItemsWithFilterAndOrdering(champ => filterByName(champ, substring), index, count,
orderingPropertyName, descending);
public Task<int> GetNbItemsByName(string substring)
{
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();
}
}
}

@ -0,0 +1,71 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EFLol.DBDataManager
{
static class ExtensionsDataManager
{
internal static Task<IEnumerable<T?>> GetItemsWithFilterAndOrdering<T>(this IEnumerable<T> collection,
Func<T, bool> filter, int index, int count, string? orderingPropertyName = null, bool descending = false)
{
IEnumerable<T> temp = collection;
temp = temp.Where(item => filter(item));
if (orderingPropertyName != null)
{
var prop = typeof(T).GetProperty(orderingPropertyName!);
if (prop != null)
{
temp = descending
? temp.OrderByDescending(item => prop.GetValue(item))
: temp.OrderBy(item => prop.GetValue(item));
}
}
return Task.FromResult<IEnumerable<T?>>(temp.Skip(index * count).Take(count));
}
internal static Task<int> GetNbItemsWithFilter<T>(this IEnumerable<T> collection, Func<T, bool> filter)
{
return Task.FromResult(collection.Count(item => filter(item)));
}
internal static Task<T?> AddItem<T>(this IList<T> collection, T? item)
{
if (item == null || collection.Contains(item))
{
return Task.FromResult<T?>(default(T));
}
collection.Add(item);
return Task.FromResult<T?>(item);
}
internal static Task<bool> DeleteItem<T>(this IList<T> collection, T? item)
{
if (item == null)
{
return Task.FromResult(false);
}
bool result = collection.Remove(item!);
return Task.FromResult(result);
}
internal static Task<T?> UpdateItem<T>(this IList<T> collection, T? oldItem, T? newItem)
{
if (oldItem == null || newItem == null) return Task.FromResult<T?>(default(T));
if (!collection.Contains(oldItem))
{
return Task.FromResult<T?>(default(T));
}
collection.Remove(oldItem!);
collection.Add(newItem!);
return Task.FromResult<T?>(newItem);
}
}
}

@ -27,25 +27,25 @@ namespace apiLOL.Controllers
[ProducesResponseType(typeof(ChampionPageDTO), 200)]
public async Task<IActionResult> GetChampions([FromQuery] int index = 0, int count = 10, string? name = "")
{
//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 _dataManager.GetNbItems();
_logger.LogInformation($"Nombre de champions : {nbChampions}");
var champs = (await _dataManager.GetItems(index, count)).Select(Model => Model.ToDTO());
var champs = (await _dataManager.GetItemsByName(name, index, int.MaxValue))
.Where(champ => string.IsNullOrEmpty(name) || champ.Name.Contains(name, StringComparison.InvariantCultureIgnoreCase))
.Take(count)
.Select(Model => Model.ToDTO());
var page = new ChampionPageDTO
{
Data = champs,
Index = index,
Count = count,
Count = champs.Count(),
TotalCount = nbChampions
};
return Ok(page);
}
[HttpGet]
[Route("{name}")]
[ProducesResponseType(typeof(ChampionDTO), 200)]
@ -54,8 +54,15 @@ namespace apiLOL.Controllers
_logger.LogInformation($"methode GetChampion de ControllerChampions appelée avec le paramètre {name}");
try
{
var champs = (await _dataManager.GetItemsByName(name, 0, 1));
return Ok(champs.First().ToDTO());
var champs = await _dataManager.GetItemsByName(name, 0, 1);
if (champs.Any())
{
return Ok(champs.First().ToDTO());
}
else
{
return NotFound();
}
}
catch (Exception ex)
{
@ -71,18 +78,26 @@ namespace apiLOL.Controllers
_logger.LogInformation($"methode Post de ControllerChampions appelée avec le paramètre {champDTO.Name}");
try
{
// Check if the champion already exists in the database
var champs = await _dataManager.GetItemsByName(champDTO.Name, 0, 1);
if (champs.Any())
{
return BadRequest("le champion existe deja");
}
Champion tmp = champDTO.ToModel();
Champion champion = await _dataManager.AddItem(tmp);
ChampionDTO dtoChamp = champion.ToDTO();
return CreatedAtAction(nameof(GetChampionByName), new {name = dtoChamp.Name}, dtoChamp);
return CreatedAtAction(nameof(GetChampionByName), new { name = dtoChamp.Name }, dtoChamp);
}
catch (Exception ex)
{
_logger.LogError($"erreur methode Post de ControllerChampions: {ex}");
return BadRequest("le champion existe deja");
return BadRequest("erreur lors de l'ajout du champion");
}
}
[HttpPut("{name}")]
public async Task<IActionResult> UpdateChampion(string name, string bio)
{

Loading…
Cancel
Save