master
Jolys Enzo 2 years ago
parent 5ed4479c7d
commit 2a1f585932

@ -33,11 +33,14 @@ namespace Api_lol.Controllers
[HttpPost]
public IActionResult Post(DtoChampions champDTO)
public async Task<IActionResult> Post(DtoChampions champDTO)
{
Champion tmp = champDTO.DtoToModel();
data.ChampionsMgr.AddItem(tmp);
return Ok();
var champion = await data.ChampionsMgr.AddItem(tmp);
//Convertir le resultat en dto
//Nom de la méthode, l'id du champion, et le champion
Console.WriteLine(champion.Name);
return CreatedAtAction(nameof(GetChampion),champion.Name,champion);
}

@ -0,0 +1,61 @@
using Api_lol.Factories;
using DTO;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Model;
namespace Api_lol.Controllers
{
[ApiController]
[Route("/Skins")]
public class Skins : Controller
{
private readonly IDataManager data;
private readonly ILogger<Champions> logger;
public Skins(ILogger<Champions> logger, IDataManager manager)
{
this.logger = logger;
data = manager;
}
[HttpGet]
public async Task<IActionResult> Get()
{
var champs = (await data.SkinsMgr.GetItems(0, await data.ChampionsMgr.GetNbItems())).Select(Model => Model.ModelToDto());
return Ok(champs);
}
[HttpGet]
[Route("{name}")]
public async Task<IActionResult> GetSkinsByName(string name)
{
Skin skin = (await data.SkinsMgr.GetItems(0, await data.ChampionsMgr.GetNbItems())).First(i => i.Name == name);
if (skin == null)
{
return BadRequest();
}
DtoSkins result = skin.ModelToDto();
return Ok(result);
}
[HttpGet]
[Route("GetSkinsByChampionName/{name}")]
public async Task<IActionResult> GetSkinsByChampionName(string name)
{
Champion champ = (await data.ChampionsMgr.GetItems(0, await data.ChampionsMgr.GetNbItems())).First(i => i.Name == name);
List<Skin> skinsModele = new List<Skin>(champ.Skins);
List<DtoSkins> skinsD = new
List<DtoSkins> skins = new List<DtoSkins>((List<Skin>)champ.Skins.Select(Model => Model.ModelToDto));
if (skins == null)
{
return BadRequest();
}
return Ok(skins);
}
}
}

@ -0,0 +1,13 @@
using DTO;
using Model;
namespace Api_lol.Factories
{
public static class FactoSkins
{
public static DtoSkins ModelToDto(this Skin skin)
{
return new DtoSkins(skin.Name);
}
}
}

@ -13,13 +13,11 @@ StubData tmp = new StubData();
var tmpListe = await tmp.ChampionsMgr.GetItemsByName("Akali", 0, 6);
Champion champ = tmpListe.First();
Factories facto = new Factories();
EntityChampions entity = facto.ChampionModelToEntity(champ);
List<EntitySkins> skins = new List<EntitySkins>(entity.Skins);
using ( BDDContext db = new BDDContext())
{
db.Add(entity);
db.SaveChanges();
}

@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DTO
{
public class DtoSkins
{
public string name { get; set; }
public DtoSkins(string name)
{
this.name = name;
}
}
}

@ -5,6 +5,7 @@ namespace EntityFramwork
{
public class BDDContext : DbContext
{
public BDDContext() { }
public BDDContext(DbContextOptions<BDDContext> option) : base(option) { }
@ -15,29 +16,31 @@ namespace EntityFramwork
//création de la table Champion
modelBuilder.Entity<EntityChampions>().HasKey(a => a.Id);
modelBuilder.Entity<EntityChampions>().Property(a => a.Id)
.ValueGeneratedOnAdd();
.ValueGeneratedOnAdd();
//création de la table Skins
modelBuilder.Entity<EntitySkins>().HasKey(m => m.Id);
modelBuilder.Entity<EntitySkins>().Property(m => m.Id)
.ValueGeneratedOnAdd();
.ValueGeneratedOnAdd();
//modelBuilder.Entity<EntitySkins>().Property<int>("ChampionForeignKey");
// Use the shadow property as a foreign key
/*
modelBuilder.Entity<EntitySkins>()
.HasOne(m => m.Champion)
.WithMany(a => a.Skins)
.HasForeignKey("ChampionsForeignKey");
.HasForeignKey("ChampionsForeignKey");*/
}
public DbSet<EntityChampions> Champions { get; set; }
public DbSet<EntitySkins> Skins { get; set; }
public DbSet<EntityLargeImage> Images { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlite($"Data Source=BDD-APILOL.db"); ;
optionsBuilder.UseSqlite($"Data Source=C:\\Users\\Jolys Enzo\\home\\BUT\\Projet\\LOL-API\\Sources\\EntityFramwork\\BDD-APILOL.db");
}
}
}

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace EntityFramwork
{
public class EntityLargeImage
{
public long Id { get; set; }
public string Base64 { get; set; }
}
}

@ -14,7 +14,7 @@ namespace EntityFramwork.Factories
entity.Icon = champ.Icon;
if ( sansSkin == 0)
{
entity.Skins = champ.Skins.Select(Model => this.SkinsModelToEntity(Model)).ToList();
//entity.Skins = champ.Skins.Select(Model => this.SkinsModelToEntity(Model)).ToList();
}
return entity;
@ -27,9 +27,11 @@ namespace EntityFramwork.Factories
entity.Price = skin.Price;
entity.Icon = skin.Icon;
entity.Description= skin.Description;
entity.Champion = ChampionModelToEntity(skin.Champion,1);
//entity.Champion = ChampionModelToEntity(skin.Champion,1);
return entity;
}
}
}

@ -1,22 +0,0 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace EntityFramwork.Migrations
{
/// <inheritdoc />
public partial class TEST : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
}
}
}

@ -10,8 +10,8 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
namespace EntityFramwork.Migrations
{
[DbContext(typeof(BDDContext))]
[Migration("20230204085418_TEST")]
partial class TEST
[Migration("20230206102420_testMigra")]
partial class testMigra
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
@ -48,7 +48,7 @@ namespace EntityFramwork.Migrations
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<int>("ChampionsForeignKey")
b.Property<int>("ChampionId")
.HasColumnType("INTEGER");
b.Property<string>("Description")
@ -64,7 +64,7 @@ namespace EntityFramwork.Migrations
b.HasKey("Id");
b.HasIndex("ChampionsForeignKey");
b.HasIndex("ChampionId");
b.ToTable("Skins");
});
@ -73,7 +73,7 @@ namespace EntityFramwork.Migrations
{
b.HasOne("DTO.EntityChampions", "Champion")
.WithMany("Skins")
.HasForeignKey("ChampionsForeignKey")
.HasForeignKey("ChampionId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();

@ -0,0 +1,66 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace EntityFramwork.Migrations
{
/// <inheritdoc />
public partial class testMigra : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Champions",
columns: table => new
{
Id = table.Column<int>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Name = table.Column<string>(type: "TEXT", nullable: false),
Bio = table.Column<string>(type: "TEXT", nullable: false),
Icon = table.Column<string>(type: "TEXT", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Champions", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Skins",
columns: table => new
{
Id = table.Column<int>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Description = table.Column<string>(type: "TEXT", nullable: false),
Icon = table.Column<string>(type: "TEXT", nullable: false),
Price = table.Column<float>(type: "REAL", nullable: false),
ChampionId = table.Column<int>(type: "INTEGER", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Skins", x => x.Id);
table.ForeignKey(
name: "FK_Skins_Champions_ChampionId",
column: x => x.ChampionId,
principalTable: "Champions",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_Skins_ChampionId",
table: "Skins",
column: "ChampionId");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Skins");
migrationBuilder.DropTable(
name: "Champions");
}
}
}

@ -45,7 +45,7 @@ namespace EntityFramwork.Migrations
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<int>("ChampionsForeignKey")
b.Property<int>("ChampionId")
.HasColumnType("INTEGER");
b.Property<string>("Description")
@ -61,7 +61,7 @@ namespace EntityFramwork.Migrations
b.HasKey("Id");
b.HasIndex("ChampionsForeignKey");
b.HasIndex("ChampionId");
b.ToTable("Skins");
});
@ -70,7 +70,7 @@ namespace EntityFramwork.Migrations
{
b.HasOne("DTO.EntityChampions", "Champion")
.WithMany("Skins")
.HasForeignKey("ChampionsForeignKey")
.HasForeignKey("ChampionId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();

@ -23,7 +23,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EntityFramwork", "EntityFra
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Client", "Client\Client.csproj", "{B1BDE539-7DC3-4DC7-A908-36119E468FA8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestUnitaireApi", "TestUnitaireApi\TestUnitaireApi.csproj", "{1FB0F7BE-AEEE-42FD-BCE9-C11AE6923E6C}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TestUnitaireApi", "TestUnitaireApi\TestUnitaireApi.csproj", "{1FB0F7BE-AEEE-42FD-BCE9-C11AE6923E6C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestUnitaireBDD", "TestUnitaireBDD\TestUnitaireBDD.csproj", "{138B858C-11AA-4915-B3D0-DAF11268A434}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -67,6 +69,10 @@ Global
{1FB0F7BE-AEEE-42FD-BCE9-C11AE6923E6C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1FB0F7BE-AEEE-42FD-BCE9-C11AE6923E6C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1FB0F7BE-AEEE-42FD-BCE9-C11AE6923E6C}.Release|Any CPU.Build.0 = Release|Any CPU
{138B858C-11AA-4915-B3D0-DAF11268A434}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{138B858C-11AA-4915-B3D0-DAF11268A434}.Debug|Any CPU.Build.0 = Debug|Any CPU
{138B858C-11AA-4915-B3D0-DAF11268A434}.Release|Any CPU.ActiveCfg = Release|Any CPU
{138B858C-11AA-4915-B3D0-DAF11268A434}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -75,6 +81,7 @@ Global
{1889FA6E-B7C6-416E-8628-9449FB9070B9} = {C76D0C23-1FFA-4963-93CD-E12BD643F030}
{B01D7EF2-2D64-409A-A29A-61FB7BB7A9DB} = {2C607793-B163-4731-A4D1-AFE8A7C4C170}
{1FB0F7BE-AEEE-42FD-BCE9-C11AE6923E6C} = {C76D0C23-1FFA-4963-93CD-E12BD643F030}
{138B858C-11AA-4915-B3D0-DAF11268A434} = {C76D0C23-1FFA-4963-93CD-E12BD643F030}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {92F3083D-793F-4552-8A9A-0AD6534159C9}

@ -28,7 +28,7 @@ public class Champion : IEquatable<Champion>
{
if(value == null)
{
bio = "";
bio = "Bio inconnu";
return;
}
bio = value;
@ -42,7 +42,7 @@ public class Champion : IEquatable<Champion>
public LargeImage Image { get; set; }
public Champion(string name, ChampionClass champClass = ChampionClass.Unknown, string icon = "", string image = "", string bio = "")
public Champion(string name, ChampionClass champClass = ChampionClass.Unknown, string icon = "Icon inconnu !", string image = "", string bio = "Bio inconnu !")
{
Name = name;
Class = champClass;

@ -0,0 +1,100 @@
using DTO;
using EntityFramwork;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
namespace TestUnitaireBDD
{
public class TestChampions
{
[Fact]
public void Add_Test()
{
//connection must be opened to use In-memory database
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var options = new DbContextOptionsBuilder<BDDContext>()
.UseSqlite(connection)
.Options;
//prepares the database with one instance of the context
using (var context = new BDDContext(options))
{
//context.Database.OpenConnection();
context.Database.EnsureCreated();
EntityChampions chewie = new EntityChampions { Name = "Chewbacca",Bio ="Inconnu",Icon="Inconnu" };
EntityChampions yoda = new EntityChampions { Name = "Yoda",Bio = "Inconnu", Icon = "Inconnu" };
EntityChampions ewok = new EntityChampions { Name = "Ewok",Bio = "Inconnu", Icon = "Inconnu" };
context.Add(chewie);
context.Add(yoda);
context.Add(ewok);
context.SaveChanges();
}
//uses another instance of the context to do the tests
using (var context = new BDDContext(options))
{
context.Database.EnsureCreated();
Assert.Equal(3, context.Champions.Count());
Assert.Equal("Chewbacca", context.Champions.First().Name);
}
}
[Fact]
public void Modify_Test()
{
//connection must be opened to use In-memory database
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var options = new DbContextOptionsBuilder<BDDContext>()
.UseSqlite(connection)
.Options;
//prepares the database with one instance of the context
using (var context = new BDDContext(options))
{
//context.Database.OpenConnection();
context.Database.EnsureCreated();
EntityChampions chewie = new EntityChampions { Name = "Chewbacca", Bio = "Inconnu", Icon = "Inconnu" };
EntityChampions yoda = new EntityChampions { Name = "Yoda", Bio = "Inconnu", Icon = "Inconnu" };
EntityChampions ewok = new EntityChampions { Name = "Ewok", Bio = "Inconnu", Icon = "Inconnu" };
context.Add(chewie);
context.Add(yoda);
context.Add(ewok);
context.SaveChanges();
}
//uses another instance of the context to do the tests
using (var context = new BDDContext(options))
{
context.Database.EnsureCreated();
string nameToFind = "ew";
Assert.Equal(2, context.Champions.Where(n => n.Name.ToLower().Contains(nameToFind)).Count());
nameToFind = "wo";
Assert.Equal(1, context.Champions.Where(n => n.Name.ToLower().Contains(nameToFind)).Count());
var ewok = context.Champions.Where(n => n.Name.ToLower().Contains(nameToFind)).First();
ewok.Name = "Wicket";
context.SaveChanges();
}
//uses another instance of the context to do the tests
using (var context = new BDDContext(options))
{
context.Database.EnsureCreated();
string nameToFind = "ew";
Assert.Equal(1, context.Champions.Where(n => n.Name.ToLower().Contains(nameToFind)).Count());
nameToFind = "wick";
Assert.Equal(1, context.Champions.Where(n => n.Name.ToLower().Contains(nameToFind)).Count());
}
}
}
}

@ -0,0 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\EntityFramwork\EntityFramwork.csproj" />
</ItemGroup>
</Project>

@ -0,0 +1 @@
global using Xunit;
Loading…
Cancel
Save