Starting DbDataManager implementation

Arthur_EF_Business
Arthur VALIN 2 years ago
parent 8dbccb92e7
commit 0b1090aab5

@ -12,6 +12,7 @@
<ItemGroup>
<ProjectReference Include="..\DTO\DTO.csproj" />
<ProjectReference Include="..\Entities\Entities.csproj" />
<ProjectReference Include="..\Model\Model.csproj" />
<ProjectReference Include="..\StubLib\StubLib.csproj" />
</ItemGroup>

@ -21,7 +21,7 @@ namespace API_LoL_Project.Controllers
{
var champions = await dataManager.GetItems(0, await dataManager.GetNbItems());
return Ok(new { result = champions.Select(c => c.toDTO())});
return Ok(new { result = champions.Select(c => c.ToDTO())});
}
@ -32,14 +32,14 @@ namespace API_LoL_Project.Controllers
var champion = await dataManager
.GetItemsByName(name, 0, await dataManager.GetNbItems());
return Ok(new { result = champion.First().toDTO() });
return Ok(new { result = champion.First().ToDTO() });
}
// POST api/<ChampionController>
[HttpPost]
public async Task<IActionResult> Post([FromBody] ChampionDTO value)
{
await dataManager.AddItem(value.toModel());
await dataManager.AddItem(value.ToModel());
return Ok();
}
@ -50,7 +50,7 @@ namespace API_LoL_Project.Controllers
{
var champion = await dataManager
.GetItemsByName(name, 0, await dataManager.GetNbItems());
await dataManager.UpdateItem(champion.First(), value.toModel());
await dataManager.UpdateItem(champion.First(), value.ToModel());
return Ok();
}

@ -1,21 +1,39 @@
using DTO;
using Entities;
using Model;
namespace API_LoL_Project.Mapper
{
public static class ChampionMapper
{
public static ChampionDTO toDTO(this Champion item)
public static class ChampionMapper {
public static ChampionDTO ToDTO(this Champion item)
{
return new ChampionDTO() {
return new() {
Name = item.Name,
Bio = item.Bio
};
}
public static Champion toModel(this ChampionDTO dto)
public static ChampionEntity ToEntity(this Champion item)
{
return new Champion(dto.Name);
return new()
{
Name = item.Name,
Bio = item.Bio,
Icon = item.Icon,
Class = item.Class,
Image = new() { Base64 = item.Image.Base64 },
};
}
public static Champion ToModel(this ChampionDTO dto)
{
return new(dto.Name);
}
public static Champion ToModel(this ChampionEntity entity)
{
return new(entity.Name, entity.Class, entity.Icon, entity.Image.Base64, entity.Bio);
}
}

@ -0,0 +1,27 @@
namespace API_LoL_Project.Mapper
{
using DTO;
using Entities;
using Model;
namespace API_LoL_Project.Mapper
{
public static class RuneMapper
{
public static RuneEntity ToEntity(this Rune item)
{
throw new NotImplementedException();
}
public static Rune ToModel(this RuneEntity entity)
{
throw new NotImplementedException();
}
}
}
}
}

@ -0,0 +1,19 @@
using Entities;
using Model;
namespace API_LoL_Project.Mapper
{
public static class RunePageMapper
{
public static RunePageEntity ToEntity(this RunePage item)
{
throw new NotImplementedException();
}
public static RunePage ToModel(this RunePageEntity entity)
{
throw new NotImplementedException();
}
}
}

@ -0,0 +1,23 @@
using DTO;
using Entities;
using Model;
namespace API_LoL_Project.Mapper
{
public static class SkinMapper
{
public static SkinEntity ToEntity(this Skin item)
{
throw new NotImplementedException();
}
public static Skin ToModel(this SkinEntity entity)
{
throw new NotImplementedException();
}
}
}
}

@ -7,6 +7,8 @@
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\API_LoL_Project\API_LoL_Project.csproj" />
<ProjectReference Include="..\Entities\Entities.csproj" />
<ProjectReference Include="..\Model\Model.csproj" />
</ItemGroup>

@ -1,10 +1,7 @@
using Model;
using API_LoL_Project.Mapper;
using Model;
using Shared;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlTypes;
namespace Business
{
@ -17,89 +14,112 @@ namespace Business
public ChampionsManager(DbData parent)
=> this.parent = parent;
public Task<Champion?> AddItem(Champion? item)
public async Task<Champion?> AddItem(Champion? item)
{
throw new NotImplementedException();
await parent.DbContext.champions.AddAsync(item.ToEntity());
return item;
}
public Task<bool> DeleteItem(Champion? item)
public async Task<bool> DeleteItem(Champion? item)
{
throw new NotImplementedException();
parent.DbContext.champions.Remove(item.ToEntity());
return true;
}
public Task<IEnumerable<Champion?>> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false)
public async Task<IEnumerable<Champion?>> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false)
{
throw new NotImplementedException();
return parent.DbContext.champions.Select(c =>c.ToModel())
.Skip(index * count).Take(count);
}
public Task<IEnumerable<Champion?>> GetItemsByCharacteristic(string charName, int index, int count, string? orderingPropertyName = null, bool descending = false)
public async Task<IEnumerable<Champion?>> GetItemsByCharacteristic(string charName, int index, int count, string? orderingPropertyName = null, bool descending = false)
{
throw new NotImplementedException();
return parent.DbContext.champions.Where(c => c.Characteristics.Any(ch => ch.Name.Equals(charName)))
.Select(c => c.ToModel())
.Skip(index * count).Take(count);
}
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();
return parent.DbContext.champions.Where(c => c.Class.Equals(championClass))
.Select(c => c.ToModel())
.Skip(index * count).Take(count);
}
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();
return parent.DbContext.champions.Where(c => c.Name.Contains(substring))
.Select(c => c.ToModel())
.Skip(index * count).Take(count);
}
public Task<IEnumerable<Champion?>> GetItemsByRunePage(RunePage? runePage, int index, int count, string? orderingPropertyName = null, bool descending = false)
public async Task<IEnumerable<Champion?>> GetItemsByRunePage(RunePage? runePage, int index, int count, string? orderingPropertyName = null, bool descending = false)
{
throw new NotImplementedException();
return parent.DbContext.champions.Where(c => c.runepages.Any(rp => rp.Equals(runePage.ToEntity())
.Select(c => c.ToModel())
.Skip(index * count).Take(count);
}
public Task<IEnumerable<Champion?>> GetItemsBySkill(Skill? skill, int index, int count, string? orderingPropertyName = null, bool descending = false)
public async Task<IEnumerable<Champion?>> GetItemsBySkill(Skill? skill, int index, int count, string? orderingPropertyName = null, bool descending = false)
{
throw new NotImplementedException();
return parent.DbContext.champions.Where(c => skill!=null && c.Skills.Any(s => s.Name.Equals(skill.Name)))
.Select(c => c.ToModel())
.Skip(index * count).Take(count);
}
public Task<IEnumerable<Champion?>> GetItemsBySkill(string skill, int index, int count, string? orderingPropertyName = null, bool descending = false)
public async Task<IEnumerable<Champion?>> GetItemsBySkill(string skill, int index, int count, string? orderingPropertyName = null, bool descending = false)
{
throw new NotImplementedException();
return parent.DbContext.champions.Where(c => skill != null && c.Skills.Any(s => s.Name.Equals(skill)))
.Select(c => c.ToModel())
.Skip(index * count).Take(count);
}
public Task<int> GetNbItems()
public async Task<int> GetNbItems()
{
throw new NotImplementedException();
return parent.DbContext.champions.Count();
}
public Task<int> GetNbItemsByCharacteristic(string charName)
public async Task<int> GetNbItemsByCharacteristic(string charName)
{
throw new NotImplementedException();
return parent.DbContext.champions.Where(c => c.Characteristics.Any(ch => ch.Name.Equals(charName))).Count();
}
public Task<int> GetNbItemsByClass(ChampionClass championClass)
public async Task<int> GetNbItemsByClass(ChampionClass championClass)
{
throw new NotImplementedException();
return parent.DbContext.champions.Where(c => c.Class.Equals(championClass))
.Count();
}
public Task<int> GetNbItemsByName(string substring)
public async Task<int> GetNbItemsByName(string substring)
{
throw new NotImplementedException();
return parent.DbContext.champions.Where(c => c.Name.Contains(substring))
.Count();
}
public Task<int> GetNbItemsByRunePage(RunePage? runePage)
public async Task<int> GetNbItemsByRunePage(RunePage? runePage)
{
throw new NotImplementedException();
return parent.DbContext.champions.Where(c => c.runepages.Any(rp => rp.Equals(runePage.ToEntity()))).Count();
}
public Task<int> GetNbItemsBySkill(Skill? skill)
public async Task<int> GetNbItemsBySkill(Skill? skill)
{
throw new NotImplementedException();
return parent.DbContext.champions.Where(c => skill != null && c.Skills.Any(s => s.Name.Equals(skill.Name)))
.Count();
}
public Task<int> GetNbItemsBySkill(string skill)
public async Task<int> GetNbItemsBySkill(string skill)
{
throw new NotImplementedException();
return parent.DbContext.champions.Where(c => skill != null && c.Skills.Any(s => s.Name.Equals(skill)))
.Count();
}
public Task<Champion?> UpdateItem(Champion? oldItem, Champion? newItem)
public async Task<Champion?> UpdateItem(Champion? oldItem, Champion? newItem)
{
throw new NotImplementedException();
parent.DbContext.champions.Remove(oldItem.ToEntity());
parent.DbContext.champions.Add(newItem.ToEntity());
return newItem;
}
}
}
}

@ -1,9 +1,5 @@
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using API_LoL_Project.Mapper;
using Model;
namespace Business
{
@ -16,59 +12,72 @@ namespace Business
public RunePagesManager(DbData parent)
=> this.parent = parent;
public Task<RunePage?> AddItem(RunePage? item)
public async Task<RunePage?> AddItem(RunePage? item)
{
throw new NotImplementedException();
await parent.DbContext.runepages.AddAsync(item.ToEntity());
return item;
}
public Task<bool> DeleteItem(RunePage? item)
public async Task<bool> DeleteItem(RunePage? item)
{
throw new NotImplementedException();
parent.DbContext.runepages.Remove(item.ToEntity());
return true;
}
public Task<IEnumerable<RunePage?>> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false)
public async Task<IEnumerable<RunePage?>> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false)
{
throw new NotImplementedException();
return parent.DbContext.runepages.Select(rp => rp.ToModel())
.Skip(index * count).Take(count);
}
public Task<IEnumerable<RunePage?>> GetItemsByChampion(Champion? champion, int index, int count, string? orderingPropertyName = null, bool descending = false)
public async Task<IEnumerable<RunePage?>> GetItemsByChampion(Champion? champion, int index, int count, string? orderingPropertyName = null, bool descending = false)
{
throw new NotImplementedException();
return parent.DbContext.runepages.Where(rp => rp.champions.Any(c => c.Name.Equals(champion.Name)))
.Select(rp => rp.ToModel())
.Skip(index * count).Take(count);
}
public Task<IEnumerable<RunePage?>> GetItemsByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false)
public async Task<IEnumerable<RunePage?>> GetItemsByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false)
{
throw new NotImplementedException();
return parent.DbContext.runepages.Where(rp => rp.Name.Contains(substring))
.Select(rp => rp.ToModel())
.Skip(index * count).Take(count);
}
public Task<IEnumerable<RunePage?>> GetItemsByRune(Model.Rune? rune, int index, int count, string? orderingPropertyName = null, bool descending = false)
public async Task<IEnumerable<RunePage?>> GetItemsByRune(Rune? rune, int index, int count, string? orderingPropertyName = null, bool descending = false)
{
throw new NotImplementedException();
return parent.DbContext.runepages.Where(rp => rp.runes.Any(r => r.Name.Equals(rune.Name)))
.Select(rp => rp.ToModel())
.Skip(index * count).Take(count);
}
public Task<int> GetNbItems()
public async Task<int> GetNbItems()
{
throw new NotImplementedException();
return parent.DbContext.runepages.Count();
}
public Task<int> GetNbItemsByChampion(Champion? champion)
public async Task<int> GetNbItemsByChampion(Champion? champion)
{
throw new NotImplementedException();
return parent.DbContext.runepages.Where(rp => rp.champions.Any(c => c.Name.Equals(champion.Name))).Count();
}
public Task<int> GetNbItemsByName(string substring)
public async Task<int> GetNbItemsByName(string substring)
{
throw new NotImplementedException();
return parent.DbContext.runepages.Where(rp => rp.Name.Contains(substring)).Count();
}
public Task<int> GetNbItemsByRune(Model.Rune? rune)
public async Task<int> GetNbItemsByRune(Model.Rune? rune)
{
throw new NotImplementedException();
return parent.DbContext.runepages.Where(rp => rp.runes.Any(r => r.Name.Equals(rune.Name))).Count();
}
public Task<RunePage?> UpdateItem(RunePage? oldItem, RunePage? newItem)
public async Task<RunePage?> UpdateItem(RunePage? oldItem, RunePage? newItem)
{
throw new NotImplementedException();
parent.DbContext.runepages.Remove(oldItem.ToEntity());
parent.DbContext.runepages.Add(newItem.ToEntity());
return newItem;
}
}
}

@ -1,10 +1,7 @@
using Model;
using API_LoL_Project.Mapper;
using API_LoL_Project.Mapper.API_LoL_Project.Mapper;
using Model;
using Shared;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Business
{
@ -16,49 +13,59 @@ namespace Business
public RunesManager(DbData parent)
=> this.parent = parent;
public Task<Model.Rune?> AddItem(Model.Rune? item)
public async Task<Rune?> AddItem(Rune? item)
{
throw new NotImplementedException();
await parent.DbContext.runes.AddAsync(item.ToEntity());
return item;
}
public Task<bool> DeleteItem(Model.Rune? item)
public async Task<bool> DeleteItem(Rune? item)
{
throw new NotImplementedException();
parent.DbContext.runes.Remove(item.ToEntity());
return true;
}
public Task<IEnumerable<Model.Rune?>> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false)
public async Task<IEnumerable<Rune?>> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false)
{
throw new NotImplementedException();
return parent.DbContext.runes
.Select(r => r.ToModel())
.Skip(index * count).Take(count);
}
public Task<IEnumerable<Model.Rune?>> GetItemsByFamily(RuneFamily family, int index, int count, string? orderingPropertyName = null, bool descending = false)
public async Task<IEnumerable<Rune?>> GetItemsByFamily(RuneFamily family, int index, int count, string? orderingPropertyName = null, bool descending = false)
{
throw new NotImplementedException();
return parent.DbContext.runes.Where(r => r.RuneFamily.Equals(family))
.Select(r => r.ToModel())
.Skip(index * count).Take(count);
}
public Task<IEnumerable<Model.Rune?>> GetItemsByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false)
public async Task<IEnumerable<Rune?>> GetItemsByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false)
{
throw new NotImplementedException();
return parent.DbContext.runes.Where(r => r.Name.Contains(substring))
.Select(r => r.ToModel())
.Skip(index * count).Take(count);
}
public Task<int> GetNbItems()
public async Task<int> GetNbItems()
{
throw new NotImplementedException();
return parent.DbContext.runes.Count();
}
public Task<int> GetNbItemsByFamily(RuneFamily family)
public async Task<int> GetNbItemsByFamily(RuneFamily family)
{
throw new NotImplementedException();
return parent.DbContext.runes.Where(r => r.RuneFamily.Equals(family)).Count();
}
public Task<int> GetNbItemsByName(string substring)
public async Task<int> GetNbItemsByName(string substring)
{
throw new NotImplementedException();
return parent.DbContext.runes.Where(r => r.Name.Contains(substring)).Count();
}
public Task<Model.Rune?> UpdateItem(Model.Rune? oldItem, Model.Rune? newItem)
public async Task<Rune?> UpdateItem(Rune? oldItem, Rune? newItem)
{
throw new NotImplementedException();
parent.DbContext.runes.Remove(oldItem.ToEntity());
parent.DbContext.runes.Add(newItem.ToEntity());
return newItem;
}
}
}

@ -1,9 +1,5 @@
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using API_LoL_Project.Mapper;
using Model;
namespace Business
{
@ -16,49 +12,61 @@ namespace Business
public SkinsManager(DbData parent)
=> this.parent = parent;
public Task<Skin?> AddItem(Skin? item)
public async Task<Skin?> AddItem(Skin? item)
{
throw new NotImplementedException();
await parent.DbContext.skins.AddAsync(item.ToEntity());
return item;
}
public Task<bool> DeleteItem(Skin? item)
public async Task<bool> DeleteItem(Skin? item)
{
throw new NotImplementedException();
parent.DbContext.skins.Remove(item.ToEntity());
return true;
}
public Task<IEnumerable<Skin?>> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false)
public async Task<IEnumerable<Skin?>> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false)
{
throw new NotImplementedException();
return parent.DbContext.skins
.Select(s => s.ToModel())
.Skip(index * count).Take(count);
}
public Task<IEnumerable<Skin?>> GetItemsByChampion(Champion? champion, int index, int count, string? orderingPropertyName = null, bool descending = false)
public async Task<IEnumerable<Skin?>> GetItemsByChampion(Champion? champion, int index, int count, string? orderingPropertyName = null, bool descending = false)
{
throw new NotImplementedException();
return parent.DbContext.skins.Where(s => s.Champion.Name.Equals(champion.Name))
.Select(s => s.ToModel())
.Skip(index * count).Take(count);
}
public Task<IEnumerable<Skin?>> GetItemsByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false)
public async Task<IEnumerable<Skin?>> GetItemsByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false)
{
throw new NotImplementedException();
return parent.DbContext.skins.Where(s => s.Name.Contains(substring))
.Select(s => s.ToModel())
.Skip(index * count).Take(count);
}
public Task<int> GetNbItems()
public async Task<int> GetNbItems()
{
throw new NotImplementedException();
return parent.DbContext.skins.Count();
}
public Task<int> GetNbItemsByChampion(Champion? champion)
public async Task<int> GetNbItemsByChampion(Champion? champion)
{
throw new NotImplementedException();
return parent.DbContext.skins.Where(s => s.Champion.Name.Equals(champion.Name))
.Count();
}
public Task<int> GetNbItemsByName(string substring)
public async Task<int> GetNbItemsByName(string substring)
{
throw new NotImplementedException();
return parent.DbContext.skins.Where(s => s.Name.Contains(substring))
.Count();
}
public Task<Skin?> UpdateItem(Skin? oldItem, Skin? newItem)
public async Task<Skin?> UpdateItem(Skin? oldItem, Skin? newItem)
{
throw new NotImplementedException();
parent.DbContext.skins.Remove(oldItem.ToEntity());
parent.DbContext.skins.Add(newItem.ToEntity());
return newItem;
}
}
}

@ -1,17 +1,22 @@
using Model;
using Entities;
using Microsoft.EntityFrameworkCore;
using Model;
namespace Business
{
public partial class DbData : IDataManager
{
public DbData()
public DbData(LolDbContext dbContext)
{
DbContext = dbContext;
ChampionsMgr = new ChampionsManager(this);
SkinsMgr = new SkinsManager(this);
RunesMgr = new RunesManager(this);
RunePagesMgr = new RunePagesManager(this);
}
protected LolDbContext DbContext{ get; }
public IChampionsManager ChampionsMgr { get; }
public ISkinsManager SkinsMgr { get; }

@ -1,4 +1,5 @@
using Shared;
using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
@ -15,14 +16,12 @@ namespace Entities
public string? Icon { get; set; }
[Required]
public ChampionClass Class { get; set;}
public virtual ICollection<SkillEntity>? Skills { get; set; }
public virtual ICollection<SkillEntity> Skills { get; set; }
public virtual ICollection<CharacteristicEntity> Characteristics { get; set; }
public ICollection<RunePageEntity> runepages { get; set; }
public Guid? ImageId { get; set; }
[ForeignKey("ImageId")]
public LargeImageEntity? Image { get; set; }
}
}

@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Entities
{
public class CharacteristicEntity
{
[Key]
[MaxLength(256)]
public string Name { get; set; }
[Required]
public int Value { get; set; }
[Required]
public string ChampionForeignKey { get; set; }
[Key]
[Required]
[ForeignKey("ChampionForeignKey")]
public ChampionEntity Champion { get; set; }
}
}

@ -22,7 +22,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Model\Model.csproj" />
<ProjectReference Include="..\Shared\Shared.csproj" />
</ItemGroup>
</Project>

@ -6,10 +6,11 @@ using System.Xml.Linq;
namespace Entities
{
public class ChampionDbContext : DbContext
public class LolDbContext : DbContext
{
public DbSet<SkinEntity> skins { get; set; }
public DbSet<ChampionEntity> champions { get; set; }
public DbSet<CharacteristicEntity> characteristics { get; set; }
public DbSet<SkillEntity> skills { get; set; }
public DbSet<RuneEntity> runes { get; set; }
public DbSet<RunePageEntity> runepages { get; set; }
@ -58,6 +59,21 @@ namespace Entities
}
});
modelBuilder.Entity<CharacteristicEntity>().HasData(new List<CharacteristicEntity>() {
new()
{
Name = "Force",
Value = 50,
ChampionForeignKey = "Dave",
},
new()
{
Name = "Défense",
Value = 75,
ChampionForeignKey = "Armure",
}
});
modelBuilder.Entity<SkinEntity>().HasData(new List<SkinEntity>() {
new SkinEntity
{

@ -7,7 +7,7 @@ ChampionEntity imri = new()
Bio = "Fou Furieux",
Class = ChampionClass.Assassin
};
using (var context = new ChampionDbContext())
using (var context = new LolDbContext())
{
// Crée des nounours et les insère dans la base
Console.WriteLine("Creates and inserts new Champion");

@ -10,7 +10,6 @@ namespace Entities
{
public class RunePageEntity
{
[Key]
public Guid Id { get; set; }
@ -18,6 +17,7 @@ namespace Entities
public string Name { get; set; }
public ICollection<RuneEntity> runes { get; set; }
public ICollection<ChampionEntity> champions { get; set; }
}

Loading…
Cancel
Save