From 4634316617c0dca9ec4e559f290c52b7e5582779 Mon Sep 17 00:00:00 2001 From: Corentin R <76619184+Koroh63@users.noreply.github.com> Date: Sun, 29 Jan 2023 18:48:02 +0100 Subject: [PATCH] =?UTF-8?q?Impl=C3=A9mentation=20des=20m=C3=A9thodes=20GET?= =?UTF-8?q?=20et=20POST=20avec=20DTO=20et=20code=20de=20retour?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Sources/API_LoL/API_LoL.csproj | 6 + .../Controllers/ChampionsController.cs | 59 ++ .../Controllers/WeatherForecastController.cs | 33 - Sources/API_LoL/Mapper/ChampionMapper.cs | 21 + Sources/API_LoL/WeatherForecast.cs | 13 - Sources/ApiTests/ApiTests.csproj | 10 + Sources/ApiTests/Program.cs | 1 + Sources/DTO/ChampionDTO.cs | 11 + Sources/DTO/DTO.csproj | 9 + Sources/LeagueOfLegends.sln | 15 +- Sources/Model/Champion.cs | 296 ++++---- Sources/Tests/ConsoleTests/Program.cs | 672 +++++++++--------- 12 files changed, 615 insertions(+), 531 deletions(-) create mode 100644 Sources/API_LoL/Controllers/ChampionsController.cs delete mode 100644 Sources/API_LoL/Controllers/WeatherForecastController.cs create mode 100644 Sources/API_LoL/Mapper/ChampionMapper.cs delete mode 100644 Sources/API_LoL/WeatherForecast.cs create mode 100644 Sources/ApiTests/ApiTests.csproj create mode 100644 Sources/ApiTests/Program.cs create mode 100644 Sources/DTO/ChampionDTO.cs create mode 100644 Sources/DTO/DTO.csproj diff --git a/Sources/API_LoL/API_LoL.csproj b/Sources/API_LoL/API_LoL.csproj index aff22dc..8902068 100644 --- a/Sources/API_LoL/API_LoL.csproj +++ b/Sources/API_LoL/API_LoL.csproj @@ -13,4 +13,10 @@ + + + + + + diff --git a/Sources/API_LoL/Controllers/ChampionsController.cs b/Sources/API_LoL/Controllers/ChampionsController.cs new file mode 100644 index 0000000..97159ef --- /dev/null +++ b/Sources/API_LoL/Controllers/ChampionsController.cs @@ -0,0 +1,59 @@ +using Microsoft.AspNetCore.Mvc; +using Model; +using StubLib; +using DTO; +using DTO.Mapper; + +// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 + +namespace API_LoL.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class ChampionsController : ControllerBase + { + private StubData.ChampionsManager ChampionsManager { get; set; } = new StubData.ChampionsManager(new StubData()); + + // GET: api/ + [HttpGet] + public async Task Get() + { + var list = await ChampionsManager.GetItems(0,await ChampionsManager.GetNbItems()); + return Ok(list.Select(champion => champion?.toDTO())); + } + + // GET api//5 + [HttpGet("{id}")] + public string Get(String name) + { + return "value"; + } + + // POST api/ + [HttpPost] + public async Task Post(ChampionDTO champion) + { + if (champion == null) + { + return UnprocessableEntity(); + } + else + { + //creation de l'objet en BDD + return CreatedAtAction("Post",champion.Name); + } + } + + // PUT api//5 + [HttpPut("{id}")] + public void Put(int id, [FromBody] string value) + { + } + + // DELETE api//5 + [HttpDelete("{id}")] + public void Delete(int id) + { + } + } +} diff --git a/Sources/API_LoL/Controllers/WeatherForecastController.cs b/Sources/API_LoL/Controllers/WeatherForecastController.cs deleted file mode 100644 index b53dbf9..0000000 --- a/Sources/API_LoL/Controllers/WeatherForecastController.cs +++ /dev/null @@ -1,33 +0,0 @@ -using Microsoft.AspNetCore.Mvc; - -namespace API_LoL.Controllers -{ - [ApiController] - [Route("[controller]")] - public class WeatherForecastController : ControllerBase - { - private static readonly string[] Summaries = new[] - { - "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" - }; - - private readonly ILogger _logger; - - public WeatherForecastController(ILogger logger) - { - _logger = logger; - } - - [HttpGet(Name = "GetWeatherForecast")] - public IEnumerable Get() - { - return Enumerable.Range(1, 5).Select(index => new WeatherForecast - { - Date = DateTime.Now.AddDays(index), - TemperatureC = Random.Shared.Next(-20, 55), - Summary = Summaries[Random.Shared.Next(Summaries.Length)] - }) - .ToArray(); - } - } -} \ No newline at end of file diff --git a/Sources/API_LoL/Mapper/ChampionMapper.cs b/Sources/API_LoL/Mapper/ChampionMapper.cs new file mode 100644 index 0000000..f091999 --- /dev/null +++ b/Sources/API_LoL/Mapper/ChampionMapper.cs @@ -0,0 +1,21 @@ +using Model; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DTO.Mapper +{ + public static class ChampionMapper + { + public static ChampionDTO toDTO(this Champion champion) + { + return new ChampionDTO() + { + Name = champion.Name, + Bio = champion.Bio, + }; + } + } +} diff --git a/Sources/API_LoL/WeatherForecast.cs b/Sources/API_LoL/WeatherForecast.cs deleted file mode 100644 index ee6f1da..0000000 --- a/Sources/API_LoL/WeatherForecast.cs +++ /dev/null @@ -1,13 +0,0 @@ -namespace API_LoL -{ - public class WeatherForecast - { - public DateTime Date { get; set; } - - public int TemperatureC { get; set; } - - public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); - - public string? Summary { get; set; } - } -} \ No newline at end of file diff --git a/Sources/ApiTests/ApiTests.csproj b/Sources/ApiTests/ApiTests.csproj new file mode 100644 index 0000000..74abf5c --- /dev/null +++ b/Sources/ApiTests/ApiTests.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/Sources/ApiTests/Program.cs b/Sources/ApiTests/Program.cs new file mode 100644 index 0000000..a5af879 --- /dev/null +++ b/Sources/ApiTests/Program.cs @@ -0,0 +1 @@ +// See https://aka.ms/new-console-template for more information diff --git a/Sources/DTO/ChampionDTO.cs b/Sources/DTO/ChampionDTO.cs new file mode 100644 index 0000000..35c9515 --- /dev/null +++ b/Sources/DTO/ChampionDTO.cs @@ -0,0 +1,11 @@ +namespace DTO +{ + public class ChampionDTO + { + public string Name { get; set; } + public string Bio { get; set; } + + + + } +} \ No newline at end of file diff --git a/Sources/DTO/DTO.csproj b/Sources/DTO/DTO.csproj new file mode 100644 index 0000000..132c02c --- /dev/null +++ b/Sources/DTO/DTO.csproj @@ -0,0 +1,9 @@ + + + + net6.0 + enable + enable + + + diff --git a/Sources/LeagueOfLegends.sln b/Sources/LeagueOfLegends.sln index 5e92cc5..5339e1f 100644 --- a/Sources/LeagueOfLegends.sln +++ b/Sources/LeagueOfLegends.sln @@ -15,7 +15,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Stub", "Stub", "{2C607793-B EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StubLib", "StubLib\StubLib.csproj", "{B01D7EF2-2D64-409A-A29A-61FB7BB7A9DB}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "API_LoL", "API_LoL\API_LoL.csproj", "{BE86E19B-3461-4EF6-8871-94E6CCB75928}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "API_LoL", "API_LoL\API_LoL.csproj", "{BE86E19B-3461-4EF6-8871-94E6CCB75928}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ApiTests", "ApiTests\ApiTests.csproj", "{AE65F7E0-FA95-4D64-938D-78DB6C905F7B}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DTO", "DTO\DTO.csproj", "{E39C3FBC-DE5E-4DAF-945A-98CE4ADE54D9}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -43,6 +47,14 @@ Global {BE86E19B-3461-4EF6-8871-94E6CCB75928}.Debug|Any CPU.Build.0 = Debug|Any CPU {BE86E19B-3461-4EF6-8871-94E6CCB75928}.Release|Any CPU.ActiveCfg = Release|Any CPU {BE86E19B-3461-4EF6-8871-94E6CCB75928}.Release|Any CPU.Build.0 = Release|Any CPU + {AE65F7E0-FA95-4D64-938D-78DB6C905F7B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {AE65F7E0-FA95-4D64-938D-78DB6C905F7B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AE65F7E0-FA95-4D64-938D-78DB6C905F7B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {AE65F7E0-FA95-4D64-938D-78DB6C905F7B}.Release|Any CPU.Build.0 = Release|Any CPU + {E39C3FBC-DE5E-4DAF-945A-98CE4ADE54D9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E39C3FBC-DE5E-4DAF-945A-98CE4ADE54D9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E39C3FBC-DE5E-4DAF-945A-98CE4ADE54D9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E39C3FBC-DE5E-4DAF-945A-98CE4ADE54D9}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -50,6 +62,7 @@ Global GlobalSection(NestedProjects) = preSolution {1889FA6E-B7C6-416E-8628-9449FB9070B9} = {C76D0C23-1FFA-4963-93CD-E12BD643F030} {B01D7EF2-2D64-409A-A29A-61FB7BB7A9DB} = {2C607793-B163-4731-A4D1-AFE8A7C4C170} + {AE65F7E0-FA95-4D64-938D-78DB6C905F7B} = {C76D0C23-1FFA-4963-93CD-E12BD643F030} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {92F3083D-793F-4552-8A9A-0AD6534159C9} 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/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