Ajout de la fluent API

pull/10/head
Corentin R 2 years ago
parent 0b529ea16a
commit de27dfe029

@ -23,9 +23,9 @@ namespace Api_UT
using (var context = new LoLDbContext(options))
{
ChampionEntity chewie = new ChampionEntity("Chewbacca");
ChampionEntity yoda = new ChampionEntity("Yoda");
ChampionEntity ewok = new ChampionEntity("Ewok");
ChampionEntity chewie = new ChampionEntity("Chewbacca","bio","icon");
ChampionEntity yoda = new ChampionEntity("Yoda", "bio", "icon");
ChampionEntity ewok = new ChampionEntity("Ewok", "bio", "icon");
Console.WriteLine("Creates and inserts new Champion for tests");

@ -1,8 +1,12 @@
using API_LoL.Controllers;
using DTO;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore.Query;
using Model;
using StubLib;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
namespace Api_UT
{
@ -14,9 +18,9 @@ namespace Api_UT
{
List<ChampionDTO> list = new List<ChampionDTO> {new ChampionDTO("Akali","",""), new ChampionDTO("Aatrox", "", ""), new ChampionDTO("Ahri", "", ""), new ChampionDTO("Akshan", "", ""), new ChampionDTO("Bard", "", ""), new ChampionDTO("Alistar", "", "") };
ChampionsController api = new ChampionsController(new StubData());
IActionResult a = await api.Get();
IEnumerable<ChampionDTO> a = ((OkObjectResult)await api.Get()).Value as IEnumerable<ChampionDTO>;
Assert.IsNotNull(a);
Assert.AreEqual(list,((OkObjectResult)a).Value);
Assert.IsTrue(list.SequenceEqual(a));
}
[TestMethod]
@ -26,7 +30,7 @@ namespace Api_UT
IActionResult a = await api.Post(new ChampionDTO("nom","bio","icon"));
Assert.IsNotNull(a);
ChampionDTO champ = new ChampionDTO("nom", "bio", "icon");
//Assert.AreEqual<ChampionDTO>(champ,((CreatedAtActionResult)a).Value);
Assert.IsTrue(champ.equals((ChampionDTO)((CreatedAtActionResult)a).Value));
}
}

@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Core" Version="2.2.5" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\DTO\DTO.csproj" />
<ProjectReference Include="..\Model\Model.csproj" />
</ItemGroup>
</Project>

@ -0,0 +1,194 @@
// See https://aka.ms/new-console-template for more information
using System.Net.Security;
using Model;
using System.Net.Http;
using System.Reflection.Metadata;
using Newtonsoft.Json;
using DTO;
using System.Text.Json;
using Microsoft.AspNetCore.Mvc;
using System.Net.Http.Json;
class APIResponse
{
public string status { get; set; }
public List<string> champions { get; set; }
}
static class Program
{
static HttpClient client = new HttpClient();
static async Task Main(string[] args) {
HttpClient client = new HttpClient();
await DisplayMainMenu();
}
public static async Task DisplayMainMenu()
{
Dictionary<int, string> choices = new Dictionary<int, string>()
{
[1] = "1- Manage Champions",
[2] = "2- Manage Skins",
[3] = "3- Manage Runes",
[4] = "4- Manage Rune Pages",
[99] = "99- Quit"
};
while (true)
{
int input = DisplayAMenu(choices);
switch (input)
{
case 1:
await DisplayChampionsMenu();
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 99:
Console.WriteLine("Bye bye!");
return;
default:
break;
}
}
}
private static int DisplayAMenu(Dictionary<int, string> choices)
{
int input = -1;
while (true)
{
Console.WriteLine("What is your choice?");
Console.WriteLine("--------------------");
foreach (var choice in choices.OrderBy(kvp => kvp.Key).Select(kvp => kvp.Value))
{
Console.WriteLine(choice);
}
if (!int.TryParse(Console.ReadLine(), out input) || input == -1)
{
Console.WriteLine("I do not understand what your choice is. Please try again.");
continue;
}
break;
}
Console.WriteLine($"You have chosen: {choices[input]}");
Console.WriteLine();
return input;
}
public static async Task DisplayChampionsMenu()
{
Dictionary<int, string> choices = new Dictionary<int, string>()
{
[0] = "0- Get number of champions",
[1] = "1- Get champions",
[2] = "2- Find champions by name",
[3] = "3- Find champions by characteristic",
[4] = "4- Find champions by class",
[5] = "5- Find champions by skill",
[6] = "6- Add new champion",
[7] = "7- Delete a champion",
[8] = "8- Update a champion",
};
int input = DisplayAMenu(choices);
switch (input)
{
case 0:
case 1:
{
var response = await client.GetFromJsonAsync<Object>(
"https://localhost:7144/api/Champions");
Console.WriteLine(response.ToString());
string? json = response.ToString();
List<ChampionDTO> f = System.Text.Json.JsonSerializer.Deserialize<List<ChampionDTO>>(json: json);
foreach(var c in f)
{
Console.WriteLine(c.ToString());
}
}
break;
case 2:
{
}
break;
case 3:
{
}
break;
case 4:
{
}
break;
case 5:
{
}
break;
case 6:
{
}
break;
case 7:
{
}
break;
case 8:
{
}
break;
default:
break;
}
}
public static void DisplayCreationChampionMenu(Champion champion)
{
Dictionary<int, string> choices = new Dictionary<int, string>()
{
[1] = "1- Add a skill",
[2] = "2- Add a skin",
[3] = "3- Add a characteristic",
[99] = "99- Finish"
};
while (true)
{
int input = DisplayAMenu(choices);
switch (input)
{
case 1:
case 2:
case 3:
case 99:
return;
default:
break;
}
}
}
}

@ -1,4 +1,6 @@
namespace DTO
using Model;
namespace DTO
{
public class ChampionDTO
{
@ -11,10 +13,20 @@
public string Name { get; set; }
public string Bio { get; set; }
//public ChampionClass Class { get; set; }
public string Icon { get; set; }
public bool equals(ChampionDTO other)
{
return other.Name==Name && other.Bio==Bio && other.Icon==Icon;
}
public string toString()
{
return Name + Bio + Icon;
}
}
}

@ -6,4 +6,8 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Model\Model.csproj" />
</ItemGroup>
</Project>

@ -14,11 +14,29 @@ namespace EntityFramework
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string name { get; set; }
[Required]
[MaxLength(50)]
public string Name { get; set; }
public ChampionEntity(string name) {
this.name = name;
[MaxLength(500)]
[Column("Bio", TypeName = "string")]
public string Bio { get; set; }
[Required]
public string Icon { get; set; }
public ChampionEntity(string name,string bio,string icon) {
this.Name = name;
this.Bio = bio;
this.Icon = icon;
}
public override string ToString()
{
return Name;
}
}
}

@ -26,5 +26,28 @@ namespace EntityFramework
options.UseSqlite("Data Source=champion.db");
}
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<ChampionEntity>().HasKey(entity => entity.Id);
modelBuilder.Entity<ChampionEntity>().ToTable("Champion");
modelBuilder.Entity<ChampionEntity>().Property(entity => entity.Id)
.ValueGeneratedOnAdd();
modelBuilder.Entity<ChampionEntity>().Property(entity => entity.Name)
.IsRequired()
.HasMaxLength(50);
modelBuilder.Entity<ChampionEntity>().Property(entity => entity.Bio)
.HasMaxLength(500)
.HasColumnName("Bio")
.HasColumnType("string");
modelBuilder.Entity<ChampionEntity>().Property(entity => entity.Icon)
.IsRequired();
}
}
}

@ -0,0 +1,50 @@
// <auto-generated />
using EntityFramework;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace EntityFramework.Migrations
{
[DbContext(typeof(LoLDbContext))]
[Migration("20230222160559_myMigration")]
partial class myMigration
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "7.0.2");
modelBuilder.Entity("EntityFramework.ChampionEntity", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Bio")
.IsRequired()
.HasMaxLength(500)
.HasColumnType("string")
.HasColumnName("Bio");
b.Property<string>("Icon")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Name")
.IsRequired()
.HasMaxLength(50)
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("Champion", (string)null);
});
#pragma warning restore 612, 618
}
}
}

@ -0,0 +1,36 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace EntityFramework.Migrations
{
/// <inheritdoc />
public partial class myMigration : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Champion",
columns: table => new
{
Id = table.Column<int>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Name = table.Column<string>(type: "TEXT", maxLength: 50, nullable: false),
Bio = table.Column<string>(type: "string", maxLength: 500, nullable: false),
Icon = table.Column<string>(type: "TEXT", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Champion", x => x.Id);
});
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Champion");
}
}
}

@ -0,0 +1,47 @@
// <auto-generated />
using EntityFramework;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace EntityFramework.Migrations
{
[DbContext(typeof(LoLDbContext))]
partial class LoLDbContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "7.0.2");
modelBuilder.Entity("EntityFramework.ChampionEntity", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<string>("Bio")
.IsRequired()
.HasMaxLength(500)
.HasColumnType("string")
.HasColumnName("Bio");
b.Property<string>("Icon")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Name")
.IsRequired()
.HasMaxLength(50)
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("Champion", (string)null);
});
#pragma warning restore 612, 618
}
}
}

@ -1,10 +1,21 @@
// See https://aka.ms/new-console-template for more information
using EntityFramework;
Console.WriteLine("Hello, World!");
using( var context = new LoLDbContext())
{
context.Add(new ChampionEntity("test") );
context.Add(new ChampionEntity("test","test","test") );
context.SaveChanges();
}
ChampionEntity champ = context.Find<ChampionEntity>(1);
if( champ != null)
{
Console
.WriteLine(champ.ToString());
}
else
{
Console.WriteLine("Not Found");
}
}

@ -23,6 +23,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EntityFramework", "EntityFr
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Api_UT", "Api_UT\Api_UT.csproj", "{20A1A7DC-1E93-4506-BD32-8597A5DADD7B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleTestAPI", "ConsoleTestAPI\ConsoleTestAPI.csproj", "{04114DB7-8427-4141-930D-73E430DD1D99}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -61,6 +63,10 @@ Global
{20A1A7DC-1E93-4506-BD32-8597A5DADD7B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{20A1A7DC-1E93-4506-BD32-8597A5DADD7B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{20A1A7DC-1E93-4506-BD32-8597A5DADD7B}.Release|Any CPU.Build.0 = Release|Any CPU
{04114DB7-8427-4141-930D-73E430DD1D99}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{04114DB7-8427-4141-930D-73E430DD1D99}.Debug|Any CPU.Build.0 = Debug|Any CPU
{04114DB7-8427-4141-930D-73E430DD1D99}.Release|Any CPU.ActiveCfg = Release|Any CPU
{04114DB7-8427-4141-930D-73E430DD1D99}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -69,6 +75,7 @@ Global
{1889FA6E-B7C6-416E-8628-9449FB9070B9} = {C76D0C23-1FFA-4963-93CD-E12BD643F030}
{B01D7EF2-2D64-409A-A29A-61FB7BB7A9DB} = {2C607793-B163-4731-A4D1-AFE8A7C4C170}
{20A1A7DC-1E93-4506-BD32-8597A5DADD7B} = {C76D0C23-1FFA-4963-93CD-E12BD643F030}
{04114DB7-8427-4141-930D-73E430DD1D99} = {C76D0C23-1FFA-4963-93CD-E12BD643F030}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {92F3083D-793F-4552-8A9A-0AD6534159C9}

Loading…
Cancel
Save