From f876b294fdaae2f68ddcd0112ce0bc918aa154ae Mon Sep 17 00:00:00 2001 From: Theo DUPIN Date: Wed, 1 Feb 2023 10:49:35 +0100 Subject: [PATCH] =?UTF-8?q?ajout=20d'un=20DbContext=20donc=20cr=C3=A9tion?= =?UTF-8?q?=20de=20la=20base=20de=20donn=C3=A9es?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sources/ChampionEntity.cs | 12 + Sources/EntityFrameworkLib/ChampionContext.cs | 23 + Sources/EntityFrameworkLib/ChampionEntity.cs | 17 + Sources/EntityFrameworkLib/Class1.cs | 7 + .../EntityFrameworkLib.csproj | 24 + ...0230201082358_myFirstMigration.Designer.cs | 47 ++ .../20230201082358_myFirstMigration.cs | 36 + .../20230201084126_projet_SQLite.Designer.cs | 47 ++ .../20230201084126_projet_SQLite.cs | 22 + .../ChampionContextModelSnapshot.cs | 44 ++ Sources/EntityFrameworkLib/Skin.cs | 16 + .../EntityFrameworkLib/projet.Champions.db | Bin 0 -> 20480 bytes Sources/LeagueOfLegends.sln | 24 +- Sources/Model/Champion.cs | 296 ++++---- Sources/Model/Model.csproj | 7 + Sources/Shared/Shared.csproj | 8 + Sources/StubLib/StubLib.csproj | 8 + .../Tests/ConsoleTests/ConsoleTests.csproj | 5 + Sources/Tests/ConsoleTests/Program.cs | 672 +++++++++--------- 19 files changed, 822 insertions(+), 493 deletions(-) create mode 100644 Sources/ChampionEntity.cs create mode 100644 Sources/EntityFrameworkLib/ChampionContext.cs create mode 100644 Sources/EntityFrameworkLib/ChampionEntity.cs create mode 100644 Sources/EntityFrameworkLib/Class1.cs create mode 100644 Sources/EntityFrameworkLib/EntityFrameworkLib.csproj create mode 100644 Sources/EntityFrameworkLib/Migrations/20230201082358_myFirstMigration.Designer.cs create mode 100644 Sources/EntityFrameworkLib/Migrations/20230201082358_myFirstMigration.cs create mode 100644 Sources/EntityFrameworkLib/Migrations/20230201084126_projet_SQLite.Designer.cs create mode 100644 Sources/EntityFrameworkLib/Migrations/20230201084126_projet_SQLite.cs create mode 100644 Sources/EntityFrameworkLib/Migrations/ChampionContextModelSnapshot.cs create mode 100644 Sources/EntityFrameworkLib/Skin.cs create mode 100644 Sources/EntityFrameworkLib/projet.Champions.db diff --git a/Sources/ChampionEntity.cs b/Sources/ChampionEntity.cs new file mode 100644 index 0000000..b98fb0f --- /dev/null +++ b/Sources/ChampionEntity.cs @@ -0,0 +1,12 @@ +using Systeme; + +namespace EntityFrameworkLib +{ + public class ChampionEntity + { + public int Id { get; set; } + public string Name { get; set; } + public string Bio { get; set; } + public string Icon { get; set; } + } +} diff --git a/Sources/EntityFrameworkLib/ChampionContext.cs b/Sources/EntityFrameworkLib/ChampionContext.cs new file mode 100644 index 0000000..b79e71c --- /dev/null +++ b/Sources/EntityFrameworkLib/ChampionContext.cs @@ -0,0 +1,23 @@ +using System; +using System.Data.Common; +using Microsoft.EntityFrameworkCore; + +namespace EntityFrameworkLib +{ + public class ChampionContext : DbContext + { + public DbSet Champions { get; set; } + + public ChampionContext () { } + public ChampionContext(DbContextOptions options) + : base(options) { } + + protected override void OnConfiguring(DbContextOptionsBuilder options) + { + if (!options.IsConfigured) + { + base.OnConfiguring(options.UseSqlite($"DataSource=projet.Champions.db")); + } + } + } +} diff --git a/Sources/EntityFrameworkLib/ChampionEntity.cs b/Sources/EntityFrameworkLib/ChampionEntity.cs new file mode 100644 index 0000000..6232d8d --- /dev/null +++ b/Sources/EntityFrameworkLib/ChampionEntity.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore.Design; + +namespace EntityFrameworkLib +{ + public class ChampionEntity + { + public int Id { get; set; } + public string Name { get; set; } + public string Bio { get; set; } + public string Icon { get; set; } + } +} diff --git a/Sources/EntityFrameworkLib/Class1.cs b/Sources/EntityFrameworkLib/Class1.cs new file mode 100644 index 0000000..da896a4 --- /dev/null +++ b/Sources/EntityFrameworkLib/Class1.cs @@ -0,0 +1,7 @@ +namespace EntityFrameworkLib +{ + public class Class1 + { + + } +} \ No newline at end of file diff --git a/Sources/EntityFrameworkLib/EntityFrameworkLib.csproj b/Sources/EntityFrameworkLib/EntityFrameworkLib.csproj new file mode 100644 index 0000000..35f51dd --- /dev/null +++ b/Sources/EntityFrameworkLib/EntityFrameworkLib.csproj @@ -0,0 +1,24 @@ + + + + net6.0 + enable + enable + $(MSBuildProjectDirectory) + + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + diff --git a/Sources/EntityFrameworkLib/Migrations/20230201082358_myFirstMigration.Designer.cs b/Sources/EntityFrameworkLib/Migrations/20230201082358_myFirstMigration.Designer.cs new file mode 100644 index 0000000..37a9bd2 --- /dev/null +++ b/Sources/EntityFrameworkLib/Migrations/20230201082358_myFirstMigration.Designer.cs @@ -0,0 +1,47 @@ +// +using EntityFrameworkLib; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace EntityFrameworkLib.Migrations +{ + [DbContext(typeof(ChampionContext))] + [Migration("20230201082358_myFirstMigration")] + partial class myFirstMigration + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "7.0.2"); + + modelBuilder.Entity("EntityFrameworkLib.ChampionEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Bio") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Icon") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Name") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("Champions"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Sources/EntityFrameworkLib/Migrations/20230201082358_myFirstMigration.cs b/Sources/EntityFrameworkLib/Migrations/20230201082358_myFirstMigration.cs new file mode 100644 index 0000000..41948c8 --- /dev/null +++ b/Sources/EntityFrameworkLib/Migrations/20230201082358_myFirstMigration.cs @@ -0,0 +1,36 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace EntityFrameworkLib.Migrations +{ + /// + public partial class myFirstMigration : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Champions", + columns: table => new + { + Id = table.Column(type: "INTEGER", nullable: false) + .Annotation("Sqlite:Autoincrement", true), + Name = table.Column(type: "TEXT", nullable: false), + Bio = table.Column(type: "TEXT", nullable: false), + Icon = table.Column(type: "TEXT", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Champions", x => x.Id); + }); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Champions"); + } + } +} diff --git a/Sources/EntityFrameworkLib/Migrations/20230201084126_projet_SQLite.Designer.cs b/Sources/EntityFrameworkLib/Migrations/20230201084126_projet_SQLite.Designer.cs new file mode 100644 index 0000000..f81e67f --- /dev/null +++ b/Sources/EntityFrameworkLib/Migrations/20230201084126_projet_SQLite.Designer.cs @@ -0,0 +1,47 @@ +// +using EntityFrameworkLib; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace EntityFrameworkLib.Migrations +{ + [DbContext(typeof(ChampionContext))] + [Migration("20230201084126_projet_SQLite")] + partial class projetSQLite + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "7.0.2"); + + modelBuilder.Entity("EntityFrameworkLib.ChampionEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Bio") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Icon") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Name") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("Champions"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Sources/EntityFrameworkLib/Migrations/20230201084126_projet_SQLite.cs b/Sources/EntityFrameworkLib/Migrations/20230201084126_projet_SQLite.cs new file mode 100644 index 0000000..f946f4c --- /dev/null +++ b/Sources/EntityFrameworkLib/Migrations/20230201084126_projet_SQLite.cs @@ -0,0 +1,22 @@ +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace EntityFrameworkLib.Migrations +{ + /// + public partial class projetSQLite : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + + } + } +} diff --git a/Sources/EntityFrameworkLib/Migrations/ChampionContextModelSnapshot.cs b/Sources/EntityFrameworkLib/Migrations/ChampionContextModelSnapshot.cs new file mode 100644 index 0000000..1404409 --- /dev/null +++ b/Sources/EntityFrameworkLib/Migrations/ChampionContextModelSnapshot.cs @@ -0,0 +1,44 @@ +// +using EntityFrameworkLib; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace EntityFrameworkLib.Migrations +{ + [DbContext(typeof(ChampionContext))] + partial class ChampionContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "7.0.2"); + + modelBuilder.Entity("EntityFrameworkLib.ChampionEntity", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b.Property("Bio") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Icon") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("Name") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.ToTable("Champions"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Sources/EntityFrameworkLib/Skin.cs b/Sources/EntityFrameworkLib/Skin.cs new file mode 100644 index 0000000..f2f5c8a --- /dev/null +++ b/Sources/EntityFrameworkLib/Skin.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace EntityFrameworkLib +{ + public class Skin + { + public string Name { get; set; } + public string Description { get; set; } + public string Icon { get; set; } + public float Price { get; set; } + } +} diff --git a/Sources/EntityFrameworkLib/projet.Champions.db b/Sources/EntityFrameworkLib/projet.Champions.db new file mode 100644 index 0000000000000000000000000000000000000000..e4346c3be9b35802b6a34d11d350e9c66d0363b0 GIT binary patch literal 20480 zcmeI&v2N2q7zgn4IcXu1F`Ffpo-8C3RV&)Bra-%kYy4BMvdLtjzq`8 zo3Jym^As#hyafXz7b{7V$YHLk|4HZEozI`|``L@_^k24u)MxLac%DD=oD*H7O~(4;1w;DJWmby`Sng<&S8fKmY;|fB*y_009U<00Izz zK#oASu%+$qOM}#Vd*&w>XX0_qP5g_A9}d^M#YT_U9nPHkQH!&+xx1k^_7B97dsnaJ zZEa^q`clkdHa>Xc^B@Y7MWe71M_-KVY*#5UQFLphGs|}PbKYZi$6@wKtHl}}yYKYs zVw~ySrn_`lw`aBMy#Z_T0jr-l9m^JHXmi^+n8mZj+H~gdQxR$wZoca%h&Jr8hEce- zODXMLol4tUttNf$WZAhcKW+!_V=tYi_##NsD89@cq^nuy&bby?#GF#BEPUNzL!c#- zd8*ww8cl}jD?d)eV%Nfo)GWHCy{Sng2uJ>>+2!>nX_WQd-1E4V++kVVXm_SJn<6Sz zzZ3PB*q}fF0uX=z1Rwwb2tWV=5P$##AaFAT9#WZWreRhM)2JASk1OU=_dJe1`l;K` zzU9}-M%jEM$<3u%X7$OTJH9*);v`+XxX31!L{?P&LDb)3g8~5vKmY;|fB*y_009U< z00Izzz>O1NGME1MCV=X4^X~P8+?N2;@BiXY|0ocE00bZa0SG_<0uX=z1Rwwb2;5=; fJpbR~i;L7C009U<00Izz00bZa0SG_<0yBYs!kF<+ literal 0 HcmV?d00001 diff --git a/Sources/LeagueOfLegends.sln b/Sources/LeagueOfLegends.sln index 0ea57ff..b651117 100644 --- a/Sources/LeagueOfLegends.sln +++ b/Sources/LeagueOfLegends.sln @@ -1,19 +1,21 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 16 -VisualStudioVersion = 25.0.1704.2 +# Visual Studio Version 17 +VisualStudioVersion = 17.4.33110.190 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Model", "Model\Model.csproj", "{2960F9BA-49DE-494D-92E3-CE5A794BA1A9}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Model", "Model\Model.csproj", "{2960F9BA-49DE-494D-92E3-CE5A794BA1A9}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{C76D0C23-1FFA-4963-93CD-E12BD643F030}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleTests", "Tests\ConsoleTests\ConsoleTests.csproj", "{1889FA6E-B7C6-416E-8628-9449FB9070B9}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleTests", "Tests\ConsoleTests\ConsoleTests.csproj", "{1889FA6E-B7C6-416E-8628-9449FB9070B9}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Shared", "Shared\Shared.csproj", "{3B720C0C-53FE-4642-A2DB-87FD8634CD74}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Shared", "Shared\Shared.csproj", "{3B720C0C-53FE-4642-A2DB-87FD8634CD74}" EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Stub", "Stub", "{2C607793-B163-4731-A4D1-AFE8A7C4C170}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StubLib", "StubLib\StubLib.csproj", "{B01D7EF2-2D64-409A-A29A-61FB7BB7A9DB}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StubLib", "StubLib\StubLib.csproj", "{B01D7EF2-2D64-409A-A29A-61FB7BB7A9DB}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EntityFrameworkLib", "EntityFrameworkLib\EntityFrameworkLib.csproj", "{46FBEB1B-700D-4990-A8FC-41B044014A59}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -37,15 +39,19 @@ Global {B01D7EF2-2D64-409A-A29A-61FB7BB7A9DB}.Debug|Any CPU.Build.0 = Debug|Any CPU {B01D7EF2-2D64-409A-A29A-61FB7BB7A9DB}.Release|Any CPU.ActiveCfg = Release|Any CPU {B01D7EF2-2D64-409A-A29A-61FB7BB7A9DB}.Release|Any CPU.Build.0 = Release|Any CPU + {46FBEB1B-700D-4990-A8FC-41B044014A59}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {46FBEB1B-700D-4990-A8FC-41B044014A59}.Debug|Any CPU.Build.0 = Debug|Any CPU + {46FBEB1B-700D-4990-A8FC-41B044014A59}.Release|Any CPU.ActiveCfg = Release|Any CPU + {46FBEB1B-700D-4990-A8FC-41B044014A59}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {92F3083D-793F-4552-8A9A-0AD6534159C9} - EndGlobalSection GlobalSection(NestedProjects) = preSolution {1889FA6E-B7C6-416E-8628-9449FB9070B9} = {C76D0C23-1FFA-4963-93CD-E12BD643F030} {B01D7EF2-2D64-409A-A29A-61FB7BB7A9DB} = {2C607793-B163-4731-A4D1-AFE8A7C4C170} EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {92F3083D-793F-4552-8A9A-0AD6534159C9} + EndGlobalSection EndGlobal diff --git a/Sources/Model/Champion.cs b/Sources/Model/Champion.cs index 3a50658..b8b369b 100644 --- a/Sources/Model/Champion.cs +++ b/Sources/Model/Champion.cs @@ -1,151 +1,151 @@ using System.Collections.Immutable; using System.Collections.ObjectModel; using System.Numerics; -using System.Text; - -namespace Model; -public class Champion : IEquatable -{ - public string Name - { - get => name; - private init - { - if(string.IsNullOrWhiteSpace(value)) - { - name = "Unknown"; - return; - } - name = value; - } - } - private readonly string name = null!; - - public string Bio - { - get => bio; - set - { - if(value == null) - { - bio = ""; - return; - } - bio = value; - } - } - private string bio = ""; - - public ChampionClass Class { get; set; } - - public string Icon { get; set; } - - public LargeImage Image { get; set; } - - public Champion(string name, ChampionClass champClass = ChampionClass.Unknown, string icon = "", string image = "", string bio = "") - { - Name = name; - Class = champClass; - Icon = icon; - Image = new LargeImage(image); - Bio = bio; - Characteristics = new ReadOnlyDictionary(characteristics); - Skins = new ReadOnlyCollection(skins); - } - - public ReadOnlyCollection Skins { get; private set; } - private List skins = new (); - - public ReadOnlyDictionary Characteristics { get; private set; } - private readonly Dictionary characteristics = new Dictionary(); - - public ImmutableHashSet Skills => skills.ToImmutableHashSet(); - private HashSet skills = new HashSet(); - - internal bool AddSkin(Skin skin) - { - if (skins.Contains(skin)) - return false; - skins.Add(skin); - return true; - } - - internal bool RemoveSkin(Skin skin) - => skins.Remove(skin); - - public bool AddSkill(Skill skill) - => skills.Add(skill); - - public bool RemoveSkill(Skill skill) - => skills.Remove(skill); - - public void AddCharacteristics(params Tuple[] someCharacteristics) - { - foreach(var c in someCharacteristics) - { - characteristics[c.Item1] = c.Item2; - } - } - - public bool RemoveCharacteristics(string label) - => characteristics.Remove(label); - - public int? this[string label] - { - get - { - if(!characteristics.TryGetValue(label, out int value)) return null; - else return value; - } - set - { - if(!value.HasValue) - { - RemoveCharacteristics(label); - return; - } - characteristics[label] = value.Value; - } - } - - public override bool Equals(object? obj) - { - if(ReferenceEquals(obj, null)) return false; - if(ReferenceEquals(obj, this)) return true; - if(GetType() != obj.GetType()) return false; - return Equals(obj as Champion); - } - - public override int GetHashCode() - => Name.GetHashCode() % 997; - - public bool Equals(Champion? other) - => Name.Equals(other?.Name); - - public override string ToString() - { - StringBuilder sb = new StringBuilder($"{Name} ({Class})"); - if(!string.IsNullOrWhiteSpace(bio)) - { - sb.AppendLine($"\t{bio}"); - } - if(characteristics.Any()) - { - sb.AppendLine("\tCharacteristics:"); - foreach(var characteristic in characteristics) - { - sb.AppendLine($"\t\t{characteristic.Key} - {characteristic.Value}"); - } - } - if(skills.Any()) - { - sb.AppendLine("\tSkills:"); - foreach(var skill in Skills) - { - sb.AppendLine($"\t\t{skill.Name} - {skill.Description}"); - } - } - return sb.ToString(); - } -} - +using System.Text; + +namespace Model; +public class Champion : IEquatable +{ + public string Name + { + get => name; + private init + { + if(string.IsNullOrWhiteSpace(value)) + { + name = "Unknown"; + return; + } + name = value; + } + } + private readonly string name = null!; + + public string Bio + { + get => bio; + set + { + if(value == null) + { + bio = ""; + return; + } + bio = value; + } + } + private string bio = ""; + + public ChampionClass Class { get; set; } + + public string Icon { get; set; } + + public LargeImage Image { get; set; } + + public Champion(string name, ChampionClass champClass = ChampionClass.Unknown, string icon = "", string image = "", string bio = "") + { + Name = name; + Class = champClass; + Icon = icon; + Image = new LargeImage(image); + Bio = bio; + Characteristics = new ReadOnlyDictionary(characteristics); + Skins = new ReadOnlyCollection(skins); + } + + public ReadOnlyCollection Skins { get; private set; } + private List skins = new (); + + public ReadOnlyDictionary Characteristics { get; private set; } + private readonly Dictionary characteristics = new Dictionary(); + + public ImmutableHashSet Skills => skills.ToImmutableHashSet(); + private HashSet skills = new HashSet(); + + internal bool AddSkin(Skin skin) + { + if (skins.Contains(skin)) + return false; + skins.Add(skin); + return true; + } + + internal bool RemoveSkin(Skin skin) + => skins.Remove(skin); + + public bool AddSkill(Skill skill) + => skills.Add(skill); + + public bool RemoveSkill(Skill skill) + => skills.Remove(skill); + + public void AddCharacteristics(params Tuple[] someCharacteristics) + { + foreach(var c in someCharacteristics) + { + characteristics[c.Item1] = c.Item2; + } + } + + public bool RemoveCharacteristics(string label) + => characteristics.Remove(label); + + public int? this[string label] + { + get + { + if(!characteristics.TryGetValue(label, out int value)) return null; + else return value; + } + set + { + if(!value.HasValue) + { + RemoveCharacteristics(label); + return; + } + characteristics[label] = value.Value; + } + } + + public override bool Equals(object? obj) + { + if(ReferenceEquals(obj, null)) return false; + if(ReferenceEquals(obj, this)) return true; + if(GetType() != obj.GetType()) return false; + return Equals(obj as Champion); + } + + public override int GetHashCode() + => Name.GetHashCode() % 997; + + public bool Equals(Champion? other) + => Name.Equals(other?.Name); + + public override string ToString() + { + StringBuilder sb = new StringBuilder($"{Name} ({Class})"); + if(!string.IsNullOrWhiteSpace(bio)) + { + sb.AppendLine($"\t{bio}"); + } + if(characteristics.Any()) + { + sb.AppendLine("\tCharacteristics:"); + foreach(var characteristic in characteristics) + { + sb.AppendLine($"\t\t{characteristic.Key} - {characteristic.Value}"); + } + } + if(skills.Any()) + { + sb.AppendLine("\tSkills:"); + foreach(var skill in Skills) + { + sb.AppendLine($"\t\t{skill.Name} - {skill.Description}"); + } + } + return sb.ToString(); + } +} + diff --git a/Sources/Model/Model.csproj b/Sources/Model/Model.csproj index 27b2839..7d8ec7e 100644 --- a/Sources/Model/Model.csproj +++ b/Sources/Model/Model.csproj @@ -12,6 +12,13 @@ + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + diff --git a/Sources/Shared/Shared.csproj b/Sources/Shared/Shared.csproj index bafd05b..13dfccd 100644 --- a/Sources/Shared/Shared.csproj +++ b/Sources/Shared/Shared.csproj @@ -6,4 +6,12 @@ enable + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + diff --git a/Sources/StubLib/StubLib.csproj b/Sources/StubLib/StubLib.csproj index f05027c..32d2dd1 100644 --- a/Sources/StubLib/StubLib.csproj +++ b/Sources/StubLib/StubLib.csproj @@ -6,6 +6,14 @@ enable + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + diff --git a/Sources/Tests/ConsoleTests/ConsoleTests.csproj b/Sources/Tests/ConsoleTests/ConsoleTests.csproj index 1602b94..48218dc 100644 --- a/Sources/Tests/ConsoleTests/ConsoleTests.csproj +++ b/Sources/Tests/ConsoleTests/ConsoleTests.csproj @@ -15,6 +15,11 @@ + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/Sources/Tests/ConsoleTests/Program.cs b/Sources/Tests/ConsoleTests/Program.cs index 28dc26b..80c9d9f 100644 --- a/Sources/Tests/ConsoleTests/Program.cs +++ b/Sources/Tests/ConsoleTests/Program.cs @@ -1,338 +1,338 @@ using System.Collections.Immutable; -using System.Diagnostics; -using Microsoft.Extensions.DependencyInjection; -using Model; -using StubLib; -using static System.Console; - -namespace ConsoleTests -{ - static class Program - { - static IDataManager dataManager = null!; - - static async Task Main(string[] args) - { - try - { - using var servicesProvider = new ServiceCollection() - .AddSingleton() - .BuildServiceProvider(); - - dataManager = servicesProvider.GetRequiredService(); - - await DisplayMainMenu(); - - Console.ReadLine(); - } - catch (Exception ex) - { - Debug.WriteLine(ex, "Stopped program because of exception"); - throw; - } - } - - public static async Task DisplayMainMenu() - { - Dictionary choices = new Dictionary() - { - [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: - WriteLine("Bye bye!"); - return; - default: - break; - } - } - } - - private static int DisplayAMenu(Dictionary choices) - { - int input=-1; - while(true) - { - WriteLine("What is your choice?"); - WriteLine("--------------------"); - foreach(var choice in choices.OrderBy(kvp => kvp.Key).Select(kvp => kvp.Value)) - { - WriteLine(choice); - } - if(!int.TryParse(ReadLine(), out input) || input == -1) - { - WriteLine("I do not understand what your choice is. Please try again."); - continue; - } - break; - } - WriteLine($"You have chosen: {choices[input]}"); - WriteLine(); - return input; - } - - public static async Task DisplayChampionsMenu() - { - Dictionary choices = new Dictionary() - { - [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: - int nb = await dataManager.ChampionsMgr.GetNbItems(); - WriteLine($"There are {nb} champions"); - WriteLine("**********************"); - break; - case 1: - { - int index = ReadAnInt("Please enter the page index"); - int count = ReadAnInt("Please enter the number of elements to display"); - WriteLine($"{count} champions of page {index+1}"); - var champions = await dataManager.ChampionsMgr.GetItems(index, count, nameof(Champion.Name)); - foreach(var champion in champions) - { - WriteLine($"\t{champion}"); - } - WriteLine("**********************"); - } - break; - case 2: - { - string substring = ReadAString("Please enter the substring to look for in the name of a champion"); - int index = ReadAnInt("Please enter the page index"); - int count = ReadAnInt("Please enter the number of elements to display"); - var champions = await dataManager.ChampionsMgr.GetItemsByName(substring, index, count, nameof(Champion.Name)); - foreach(var champion in champions) - { - WriteLine($"\t{champion}"); - } - WriteLine("**********************"); - } - break; - case 3: - { - string substring = ReadAString("Please enter the substring to look for in the characteristics of champions"); - int index = ReadAnInt("Please enter the page index"); - int count = ReadAnInt("Please enter the number of elements to display"); - var champions = await dataManager.ChampionsMgr.GetItemsByCharacteristic(substring, index, count, nameof(Champion.Name)); - foreach(var champion in champions) - { - WriteLine($"\t{champion}"); - } - WriteLine("**********************"); - } - break; - case 4: - { - ChampionClass championClass = ReadAnEnum($"Please enter the champion class (possible values are: {Enum.GetNames().Aggregate("", (name, chaine) => $"{chaine} {name}")}):"); - int index = ReadAnInt("Please enter the page index"); - int count = ReadAnInt("Please enter the number of elements to display"); - var champions = await dataManager.ChampionsMgr.GetItemsByClass(championClass, index, count, nameof(Champion.Name)); - foreach(var champion in champions) - { - WriteLine($"\t{champion}"); - } - WriteLine("**********************"); - } - break; - case 5: - { - string substring = ReadAString("Please enter the substring to look for in the skills of champions"); - int index = ReadAnInt("Please enter the page index"); - int count = ReadAnInt("Please enter the number of elements to display"); - var champions = await dataManager.ChampionsMgr.GetItemsBySkill(substring, index, count, nameof(Champion.Name)); - foreach(var champion in champions) - { - WriteLine($"\t{champion}"); - } - WriteLine("**********************"); - } - break; - case 6: - { - WriteLine("You are going to create a new champion."); - string name = ReadAString("Please enter the champion name:"); - ChampionClass championClass = ReadAnEnum($"Please enter the champion class (possible values are: {Enum.GetNames().Aggregate("", (name, chaine) => $"{chaine} {name}")}):"); - string bio = ReadAString("Please enter the champion bio:"); - Champion champion = new Champion(name, championClass, bio: bio); - DisplayCreationChampionMenu(champion); - _ = await dataManager.ChampionsMgr.AddItem(champion); - } - break; - case 7: - { - WriteLine("You are going to delete a champion."); - string name = ReadAString("Please enter the champion name:"); - var somechampions = await dataManager.ChampionsMgr.GetItemsByName(name, 0, 10, nameof(Champion.Name)); - var someChampionNames = somechampions.Select(c => c!.Name); - var someChampionNamesAsOneString = someChampionNames.Aggregate("", (name, chaine) => $"{chaine} {name}"); - string champName = ReadAStringAmongPossibleValues($"Who do you want to delete among these champions? (type \"Cancel\" to ... cancel) {someChampionNamesAsOneString}", - someChampionNames.ToArray()); - if(champName != "Cancel") - { - await dataManager.ChampionsMgr.DeleteItem(somechampions.Single(c => c!.Name == champName)); - } - } - break; - case 8: - { - WriteLine("You are going to update a champion."); - string name = ReadAString("Please enter the champion name:"); - var somechampions = await dataManager.ChampionsMgr.GetItemsByName(name, 0, 10, nameof(Champion.Name)); - var someChampionNames = somechampions.Select(c => c!.Name); - var someChampionNamesAsOneString = someChampionNames.Aggregate("", (name, chaine) => $"{chaine} {name}"); - string champName = ReadAStringAmongPossibleValues($"Who do you want to update among these champions? (type \"Cancel\" to ... cancel) {someChampionNamesAsOneString}", - someChampionNames.ToArray()); - if(champName == "Cancel") break; - ChampionClass championClass = ReadAnEnum($"Please enter the champion class (possible values are: {Enum.GetNames().Aggregate("", (name, chaine) => $"{chaine} {name}")}):"); - string bio = ReadAString("Please enter the champion bio:"); - Champion champion = new Champion(champName, championClass, bio: bio); - DisplayCreationChampionMenu(champion); - await dataManager.ChampionsMgr.UpdateItem(somechampions.Single(c => c!.Name == champName), champion); - } - break; - default: - break; - } - - } - - public static void DisplayCreationChampionMenu(Champion champion) - { - Dictionary choices = new Dictionary() - { - [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: - string skillName = ReadAString("Please enter the skill name:"); - SkillType skillType = ReadAnEnum($"Please enter the skill type (possible values are: {Enum.GetNames().Aggregate("", (name, chaine) => $"{chaine} {name}")}):"); - string skillDescription = ReadAString("Please enter the skill description:"); - Skill skill = new Skill(skillName, skillType, skillDescription); - champion.AddSkill(skill); - break; - case 2: - string skinName = ReadAString("Please enter the skin name:"); - string skinDescription = ReadAString("Please enter the skin description:"); - float skinPrice = ReadAFloat("Please enter the price of this skin:"); - Skin skin = new Skin(skinName, champion, skinPrice, description: skinDescription); - break; - case 3: - string characteristic = ReadAString("Please enter the characteristic:"); - int value = ReadAnInt("Please enter the value associated to this characteristic:"); - champion.AddCharacteristics(Tuple.Create(characteristic, value)); - break; - case 99: - return; - default: - break; - } - } - } - - private static int ReadAnInt(string message) - { - while(true) - { - WriteLine(message); - if(!int.TryParse(ReadLine(), out int result)) - { - continue; - } - return result; - } - } - - private static float ReadAFloat(string message) - { - while(true) - { - WriteLine(message); - if(!float.TryParse(ReadLine(), out float result)) - { - continue; - } - return result; - } - } - - private static string ReadAString(string message) - { - while(true) - { - WriteLine(message); - string? line = ReadLine(); - if(line == null) - { - continue; - } - return line!; - } - } - - private static TEnum ReadAnEnum(string message) where TEnum :struct - { - while(true) - { - WriteLine(message); - if(!Enum.TryParse(ReadLine(), out TEnum result)) - { - continue; - } - return result; - } - } - - private static string ReadAStringAmongPossibleValues(string message, params string[] possibleValues) - { - while(true) - { - WriteLine(message); - string? result = ReadLine(); - if(result == null) continue; - if(result != "Cancel" && !possibleValues.Contains(result!)) continue; - return result!; - } - } - } +using System.Diagnostics; +using Microsoft.Extensions.DependencyInjection; +using Model; +using StubLib; +using static System.Console; + +namespace ConsoleTests +{ + static class Program + { + static IDataManager dataManager = null!; + + static async Task Main(string[] args) + { + try + { + using var servicesProvider = new ServiceCollection() + .AddSingleton() + .BuildServiceProvider(); + + dataManager = servicesProvider.GetRequiredService(); + + await DisplayMainMenu(); + + Console.ReadLine(); + } + catch (Exception ex) + { + Debug.WriteLine(ex, "Stopped program because of exception"); + throw; + } + } + + public static async Task DisplayMainMenu() + { + Dictionary choices = new Dictionary() + { + [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: + WriteLine("Bye bye!"); + return; + default: + break; + } + } + } + + private static int DisplayAMenu(Dictionary choices) + { + int input=-1; + while(true) + { + WriteLine("What is your choice?"); + WriteLine("--------------------"); + foreach(var choice in choices.OrderBy(kvp => kvp.Key).Select(kvp => kvp.Value)) + { + WriteLine(choice); + } + if(!int.TryParse(ReadLine(), out input) || input == -1) + { + WriteLine("I do not understand what your choice is. Please try again."); + continue; + } + break; + } + WriteLine($"You have chosen: {choices[input]}"); + WriteLine(); + return input; + } + + public static async Task DisplayChampionsMenu() + { + Dictionary choices = new Dictionary() + { + [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: + int nb = await dataManager.ChampionsMgr.GetNbItems(); + WriteLine($"There are {nb} champions"); + WriteLine("**********************"); + break; + case 1: + { + int index = ReadAnInt("Please enter the page index"); + int count = ReadAnInt("Please enter the number of elements to display"); + WriteLine($"{count} champions of page {index+1}"); + var champions = await dataManager.ChampionsMgr.GetItems(index, count, nameof(Champion.Name)); + foreach(var champion in champions) + { + WriteLine($"\t{champion}"); + } + WriteLine("**********************"); + } + break; + case 2: + { + string substring = ReadAString("Please enter the substring to look for in the name of a champion"); + int index = ReadAnInt("Please enter the page index"); + int count = ReadAnInt("Please enter the number of elements to display"); + var champions = await dataManager.ChampionsMgr.GetItemsByName(substring, index, count, nameof(Champion.Name)); + foreach(var champion in champions) + { + WriteLine($"\t{champion}"); + } + WriteLine("**********************"); + } + break; + case 3: + { + string substring = ReadAString("Please enter the substring to look for in the characteristics of champions"); + int index = ReadAnInt("Please enter the page index"); + int count = ReadAnInt("Please enter the number of elements to display"); + var champions = await dataManager.ChampionsMgr.GetItemsByCharacteristic(substring, index, count, nameof(Champion.Name)); + foreach(var champion in champions) + { + WriteLine($"\t{champion}"); + } + WriteLine("**********************"); + } + break; + case 4: + { + ChampionClass championClass = ReadAnEnum($"Please enter the champion class (possible values are: {Enum.GetNames().Aggregate("", (name, chaine) => $"{chaine} {name}")}):"); + int index = ReadAnInt("Please enter the page index"); + int count = ReadAnInt("Please enter the number of elements to display"); + var champions = await dataManager.ChampionsMgr.GetItemsByClass(championClass, index, count, nameof(Champion.Name)); + foreach(var champion in champions) + { + WriteLine($"\t{champion}"); + } + WriteLine("**********************"); + } + break; + case 5: + { + string substring = ReadAString("Please enter the substring to look for in the skills of champions"); + int index = ReadAnInt("Please enter the page index"); + int count = ReadAnInt("Please enter the number of elements to display"); + var champions = await dataManager.ChampionsMgr.GetItemsBySkill(substring, index, count, nameof(Champion.Name)); + foreach(var champion in champions) + { + WriteLine($"\t{champion}"); + } + WriteLine("**********************"); + } + break; + case 6: + { + WriteLine("You are going to create a new champion."); + string name = ReadAString("Please enter the champion name:"); + ChampionClass championClass = ReadAnEnum($"Please enter the champion class (possible values are: {Enum.GetNames().Aggregate("", (name, chaine) => $"{chaine} {name}")}):"); + string bio = ReadAString("Please enter the champion bio:"); + Champion champion = new Champion(name, championClass, bio: bio); + DisplayCreationChampionMenu(champion); + _ = await dataManager.ChampionsMgr.AddItem(champion); + } + break; + case 7: + { + WriteLine("You are going to delete a champion."); + string name = ReadAString("Please enter the champion name:"); + var somechampions = await dataManager.ChampionsMgr.GetItemsByName(name, 0, 10, nameof(Champion.Name)); + var someChampionNames = somechampions.Select(c => c!.Name); + var someChampionNamesAsOneString = someChampionNames.Aggregate("", (name, chaine) => $"{chaine} {name}"); + string champName = ReadAStringAmongPossibleValues($"Who do you want to delete among these champions? (type \"Cancel\" to ... cancel) {someChampionNamesAsOneString}", + someChampionNames.ToArray()); + if(champName != "Cancel") + { + await dataManager.ChampionsMgr.DeleteItem(somechampions.Single(c => c!.Name == champName)); + } + } + break; + case 8: + { + WriteLine("You are going to update a champion."); + string name = ReadAString("Please enter the champion name:"); + var somechampions = await dataManager.ChampionsMgr.GetItemsByName(name, 0, 10, nameof(Champion.Name)); + var someChampionNames = somechampions.Select(c => c!.Name); + var someChampionNamesAsOneString = someChampionNames.Aggregate("", (name, chaine) => $"{chaine} {name}"); + string champName = ReadAStringAmongPossibleValues($"Who do you want to update among these champions? (type \"Cancel\" to ... cancel) {someChampionNamesAsOneString}", + someChampionNames.ToArray()); + if(champName == "Cancel") break; + ChampionClass championClass = ReadAnEnum($"Please enter the champion class (possible values are: {Enum.GetNames().Aggregate("", (name, chaine) => $"{chaine} {name}")}):"); + string bio = ReadAString("Please enter the champion bio:"); + Champion champion = new Champion(champName, championClass, bio: bio); + DisplayCreationChampionMenu(champion); + await dataManager.ChampionsMgr.UpdateItem(somechampions.Single(c => c!.Name == champName), champion); + } + break; + default: + break; + } + + } + + public static void DisplayCreationChampionMenu(Champion champion) + { + Dictionary choices = new Dictionary() + { + [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: + string skillName = ReadAString("Please enter the skill name:"); + SkillType skillType = ReadAnEnum($"Please enter the skill type (possible values are: {Enum.GetNames().Aggregate("", (name, chaine) => $"{chaine} {name}")}):"); + string skillDescription = ReadAString("Please enter the skill description:"); + Skill skill = new Skill(skillName, skillType, skillDescription); + champion.AddSkill(skill); + break; + case 2: + string skinName = ReadAString("Please enter the skin name:"); + string skinDescription = ReadAString("Please enter the skin description:"); + float skinPrice = ReadAFloat("Please enter the price of this skin:"); + Skin skin = new Skin(skinName, champion, skinPrice, description: skinDescription); + break; + case 3: + string characteristic = ReadAString("Please enter the characteristic:"); + int value = ReadAnInt("Please enter the value associated to this characteristic:"); + champion.AddCharacteristics(Tuple.Create(characteristic, value)); + break; + case 99: + return; + default: + break; + } + } + } + + private static int ReadAnInt(string message) + { + while(true) + { + WriteLine(message); + if(!int.TryParse(ReadLine(), out int result)) + { + continue; + } + return result; + } + } + + private static float ReadAFloat(string message) + { + while(true) + { + WriteLine(message); + if(!float.TryParse(ReadLine(), out float result)) + { + continue; + } + return result; + } + } + + private static string ReadAString(string message) + { + while(true) + { + WriteLine(message); + string? line = ReadLine(); + if(line == null) + { + continue; + } + return line!; + } + } + + private static TEnum ReadAnEnum(string message) where TEnum :struct + { + while(true) + { + WriteLine(message); + if(!Enum.TryParse(ReadLine(), out TEnum result)) + { + continue; + } + return result; + } + } + + private static string ReadAStringAmongPossibleValues(string message, params string[] possibleValues) + { + while(true) + { + WriteLine(message); + string? result = ReadLine(); + if(result == null) continue; + if(result != "Cancel" && !possibleValues.Contains(result!)) continue; + return result!; + } + } + } } \ No newline at end of file