diff --git a/Sources/Api_UT/UnitTest1.cs b/Sources/Api_UT/UnitTest1.cs index de78d2b..4c934b2 100644 --- a/Sources/Api_UT/UnitTest1.cs +++ b/Sources/Api_UT/UnitTest1.cs @@ -2,8 +2,12 @@ using API_LoL.Controllers; using DTO; using FluentAssertions; 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 { @@ -34,7 +38,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(champ,((CreatedAtActionResult)a).Value); + Assert.IsTrue(champ.equals((ChampionDTO)((CreatedAtActionResult)a).Value)); } } diff --git a/Sources/ConsoleTestAPI/ConsoleTestAPI.csproj b/Sources/ConsoleTestAPI/ConsoleTestAPI.csproj new file mode 100644 index 0000000..a2be5fa --- /dev/null +++ b/Sources/ConsoleTestAPI/ConsoleTestAPI.csproj @@ -0,0 +1,20 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + + + + + + + diff --git a/Sources/ConsoleTestAPI/Program.cs b/Sources/ConsoleTestAPI/Program.cs new file mode 100644 index 0000000..0f9440f --- /dev/null +++ b/Sources/ConsoleTestAPI/Program.cs @@ -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 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 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: + Console.WriteLine("Bye bye!"); + return; + default: + break; + } + } + } + + private static int DisplayAMenu(Dictionary 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 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: + + case 1: + { + var response = await client.GetFromJsonAsync( + "https://localhost:7144/api/Champions"); + + + + Console.WriteLine(response.ToString()); + string? json = response.ToString(); + List f = System.Text.Json.JsonSerializer.Deserialize>(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 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: + + case 2: + + case 3: + + case 99: + return; + default: + break; + } + } + } +} \ No newline at end of file diff --git a/Sources/DTO/ChampionDTO.cs b/Sources/DTO/ChampionDTO.cs index c6bb27c..ddef07e 100644 --- a/Sources/DTO/ChampionDTO.cs +++ b/Sources/DTO/ChampionDTO.cs @@ -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==this.Name && other.Bio==this.Bio && other.Icon==this.Icon; + } + + public string toString() + { + return Name + Bio + Icon; + } } } \ No newline at end of file diff --git a/Sources/EF_UT/EntityTest.cs b/Sources/EF_UT/EntityTest.cs index 45716d8..4d4ce34 100644 --- a/Sources/EF_UT/EntityTest.cs +++ b/Sources/EF_UT/EntityTest.cs @@ -23,9 +23,9 @@ namespace EF_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","",""); + ChampionEntity yoda = new ChampionEntity("Yoda", "", ""); + ChampionEntity ewok = new ChampionEntity("Ewok", "", ""); Console.WriteLine("Creates and inserts new Champion for tests"); @@ -39,7 +39,7 @@ namespace EF_UT using (var context = new LoLDbContext(options)) { Assert.AreEqual(3, context.Champions.Count()); - Assert.AreEqual("Chewbacca", context.Champions.First().name); + Assert.AreEqual("Chewbacca", context.Champions.First().Name); } } [TestMethod] @@ -52,9 +52,9 @@ namespace EF_UT //prepares the database with one instance of the context 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", "", ""); + ChampionEntity yoda = new ChampionEntity ("Yoda", "", ""); + ChampionEntity ewok = new ChampionEntity("Ewok", "", ""); context.Add(chewie); context.Add(yoda); @@ -65,22 +65,22 @@ namespace EF_UT //prepares the database with one instance of the context using (var context = new LoLDbContext(options)) { - string nameToFind = "ew"; - Assert.AreEqual(2, context.Champions.Where(n => n.name.ToLower().Contains(nameToFind)).Count()); - nameToFind = "ewo"; - Assert.AreEqual(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"; + string NameToFind = "ew"; + Assert.AreEqual(2, context.Champions.Where(n => n.Name.ToLower().Contains(NameToFind)).Count()); + NameToFind = "ewo"; + Assert.AreEqual(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(); } //prepares the database with one instance of the context using (var context = new LoLDbContext(options)) { - string nameToFind = "ew"; - Assert.AreEqual(1, context.Champions.Where(n => n.name.ToLower().Contains(nameToFind)).Count()); - nameToFind = "wick"; - Assert.AreEqual(1, context.Champions.Where(n => n.name.ToLower().Contains(nameToFind)).Count()); + string NameToFind = "ew"; + Assert.AreEqual(1, context.Champions.Where(n => n.Name.ToLower().Contains(NameToFind)).Count()); + NameToFind = "wick"; + Assert.AreEqual(1, context.Champions.Where(n => n.Name.ToLower().Contains(NameToFind)).Count()); } } } diff --git a/Sources/EntityFramework/ChampionEntity.cs b/Sources/EntityFramework/ChampionEntity.cs index a943aee..2f859e1 100644 --- a/Sources/EntityFramework/ChampionEntity.cs +++ b/Sources/EntityFramework/ChampionEntity.cs @@ -15,14 +15,31 @@ namespace EntityFramework [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } - public string name { get; set; } - public ChampionClass Class { get; set; } + + [Required] + [MaxLength(50)] + public string Name { get; set; } + + + [MaxLength(500)] + [Column("Bio", TypeName = "string")] + public string Bio { get; set; } + + [Required] + public string Icon { get; set; } public ImmutableHashSet Skills => skills.ToImmutableHashSet(); private HashSet skills = new HashSet(); - public ChampionEntity(string name) { - this.name = name; + public ChampionEntity(string name,string bio,string icon) { + this.Name = name; + this.Bio = bio; + this.Icon = icon; + } + + public override string ToString() + { + return Name; } diff --git a/Sources/EntityFramework/LoLDbContext.cs b/Sources/EntityFramework/LoLDbContext.cs index 4b89834..9fa1567 100644 --- a/Sources/EntityFramework/LoLDbContext.cs +++ b/Sources/EntityFramework/LoLDbContext.cs @@ -26,5 +26,28 @@ namespace EntityFramework options.UseSqlite("Data Source=champion.db"); } } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder.Entity().HasKey(entity => entity.Id); + modelBuilder.Entity().ToTable("Champion"); + + modelBuilder.Entity().Property(entity => entity.Id) + .ValueGeneratedOnAdd(); + + modelBuilder.Entity().Property(entity => entity.Name) + .IsRequired() + .HasMaxLength(50); + + modelBuilder.Entity().Property(entity => entity.Bio) + .HasMaxLength(500) + .HasColumnName("Bio") + .HasColumnType("string"); + + modelBuilder.Entity().Property(entity => entity.Icon) + .IsRequired(); + + + } } } diff --git a/Sources/EntityFramework/Program.cs b/Sources/EntityFramework/Program.cs index 29ee9d8..d3d785d 100644 --- a/Sources/EntityFramework/Program.cs +++ b/Sources/EntityFramework/Program.cs @@ -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(); -} \ No newline at end of file + + ChampionEntity champ = context.Find(1); + + if( champ != null) + { + Console + .WriteLine(champ.ToString()); + + } + else + { + Console.WriteLine("Not Found"); + } +} diff --git a/Sources/EntityFramework/champion.db b/Sources/EntityFramework/champion.db deleted file mode 100644 index f367404..0000000 Binary files a/Sources/EntityFramework/champion.db and /dev/null differ