From b937756b4ba566fb70f83d4c82ba9fb025e8c0f3 Mon Sep 17 00:00:00 2001 From: Emre Date: Thu, 16 Mar 2023 18:38:46 +0100 Subject: [PATCH] Dictionary for ChampionDto done and beginning the RunePageController :see_no_evil: --- .../ApiLol/Controllers/RunePagesController.cs | 122 ++++++++++++++++++ .../ApiLol/Controllers/RunesController.cs | 5 +- .../ApiLol/Controllers/SkinsController.cs | 1 + .../Controllers/v2/ChampionsController.cs | 1 + .../Sources/ApiLol/Mapper/ChampionMapper.cs | 6 +- .../Sources/ApiLol/Mapper/RunePageMapper.cs | 31 +++++ .../ApiLol/Mapper/enums/CategoryMapper.cs | 50 +++++++ .../Sources/DTO/ChampionDto.cs | 5 +- .../Sources/DTO/RunePageDto.cs | 15 +++ .../DTO/enums/RunePageDto.CategoryDto.cs | 21 +++ 10 files changed, 251 insertions(+), 6 deletions(-) create mode 100644 src/EntityFramework_LoL/Sources/ApiLol/Controllers/RunePagesController.cs create mode 100644 src/EntityFramework_LoL/Sources/ApiLol/Mapper/RunePageMapper.cs create mode 100644 src/EntityFramework_LoL/Sources/ApiLol/Mapper/enums/CategoryMapper.cs create mode 100644 src/EntityFramework_LoL/Sources/DTO/RunePageDto.cs create mode 100644 src/EntityFramework_LoL/Sources/DTO/enums/RunePageDto.CategoryDto.cs diff --git a/src/EntityFramework_LoL/Sources/ApiLol/Controllers/RunePagesController.cs b/src/EntityFramework_LoL/Sources/ApiLol/Controllers/RunePagesController.cs new file mode 100644 index 0000000..2f52331 --- /dev/null +++ b/src/EntityFramework_LoL/Sources/ApiLol/Controllers/RunePagesController.cs @@ -0,0 +1,122 @@ +using ApiLol.Mapper; +using DTO; +using Microsoft.AspNetCore.Mvc; +using Microsoft.IdentityModel.Tokens; +using Model; + +// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 + +namespace ApiLol.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class RunePagesController : ControllerBase + { + private readonly IDataManager _manager; + private readonly ILogger _logger; + + public RunePagesController(IDataManager dataManager, ILogger logger) + { + _manager = dataManager; + _logger = logger; + } + + + + // GET: api/ + [HttpGet] + public async Task Get([FromQuery] PageRequest pageRequest) + { + _logger.LogInformation("Executing {Action} - RUNEPAGE with parameters: {Parameters}", nameof(Get), pageRequest); + try + { + int nbTotal = await _manager.RunePagesMgr.GetNbItems(); + if (pageRequest.count == 0) + { + pageRequest = new PageRequest() { index = 0, count = nbTotal, orderingPropertyName = pageRequest.orderingPropertyName, descending = pageRequest.descending, name = pageRequest.name }; + } + else if (pageRequest.count * pageRequest.index >= nbTotal || pageRequest.count > nbTotal) + { + _logger.LogWarning("too many, maximum {number}", nbTotal); + return BadRequest($"RunePage limit exceed, max {nbTotal}"); + } + + IEnumerable dtos; + if (pageRequest.name == null) + { + dtos = (await _manager.RunePagesMgr.GetItems(pageRequest.index, pageRequest.count, pageRequest.orderingPropertyName, pageRequest.descending)) + .Select(x => x.ToDto()); + } + else + { + dtos = (await _manager.RunePagesMgr.GetItemsByName(pageRequest.name, pageRequest.index, pageRequest.count, pageRequest.orderingPropertyName, pageRequest.descending)) + .Select(x => x.ToDto()); + } + return Ok(new PageResponse { Data = dtos, index = pageRequest.index, count = pageRequest.count, total = nbTotal }); + + } + catch (Exception error) + { + _logger.LogError(error.Message); + return BadRequest(error.Message); + } + } + + // GET api//5 + [HttpGet("{name}")] + public async Task Get(string name) + { + _logger.LogInformation("method {Action} - RUNEPAGE call with {name}", nameof(Get), name); + try + { + var dtos = (await _manager.RunePagesMgr.GetItemByName(name, 0, await _manager.RunePagesMgr.GetNbItems())) + .Select(x => x.ToDto()); + if (dtos.IsNullOrEmpty()) + { + _logger.LogWarning($"{name} was not found"); + return NotFound($"{name} was not found"); + } + return Ok(dtos.First()); + } + catch (Exception error) + { + _logger.LogError(error.Message); + return BadRequest(error.Message); + } + } + + // POST api/ + [HttpPost] + public async Task Post([FromBody] RunePageDto runePage) + { + _logger.LogInformation("method {Action} - RUNEPAGE call with {item}", nameof(Post), runePage); + try + { + if (await _manager.RunePagesMgr.GetNbItemsByName(runePage.Name) == 0) + { + return CreatedAtAction(nameof(Get), + (await _manager.RunePagesMgr.AddItem(runePage.ToModel())).ToDto()); + } + _logger.LogWarning($"Name : {runePage.Name} is already exist"); + return BadRequest($"Name : {runePage.Name} is already exist"); + } + catch (Exception error) + { + _logger.LogError(error.Message); + return BadRequest(error.Message); + } + } + + // 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/src/EntityFramework_LoL/Sources/ApiLol/Controllers/RunesController.cs b/src/EntityFramework_LoL/Sources/ApiLol/Controllers/RunesController.cs index 17e43a7..a36c20f 100644 --- a/src/EntityFramework_LoL/Sources/ApiLol/Controllers/RunesController.cs +++ b/src/EntityFramework_LoL/Sources/ApiLol/Controllers/RunesController.cs @@ -44,7 +44,7 @@ namespace ApiLol.Controllers if (pageRequest.name == null) { dtos = (await _manager.RunesMgr.GetItems(pageRequest.index, pageRequest.count, pageRequest.orderingPropertyName, pageRequest.descending)) - .Select(x => x.ToDto()); + .Select(x => x.ToDto()); } else { @@ -160,8 +160,9 @@ namespace ApiLol.Controllers } [HttpGet("/countRunes")] - public async Task GetCountSkins() + public async Task GetCountRunes() { + _logger.LogInformation("method {Action} - RUNE call", nameof(GetCountRunes)); try { return Ok(await _manager.RunesMgr.GetNbItems()); diff --git a/src/EntityFramework_LoL/Sources/ApiLol/Controllers/SkinsController.cs b/src/EntityFramework_LoL/Sources/ApiLol/Controllers/SkinsController.cs index 1057d85..47ad829 100644 --- a/src/EntityFramework_LoL/Sources/ApiLol/Controllers/SkinsController.cs +++ b/src/EntityFramework_LoL/Sources/ApiLol/Controllers/SkinsController.cs @@ -185,6 +185,7 @@ namespace ApiLol.Controllers [HttpGet("/countSkins")] public async Task GetCountSkins() { + _logger.LogInformation("method {Action} - SKIN call", nameof(GetCountSkins)); try { return Ok(await _manager.SkinsMgr.GetNbItems()); diff --git a/src/EntityFramework_LoL/Sources/ApiLol/Controllers/v2/ChampionsController.cs b/src/EntityFramework_LoL/Sources/ApiLol/Controllers/v2/ChampionsController.cs index 130ba63..2c36a1d 100644 --- a/src/EntityFramework_LoL/Sources/ApiLol/Controllers/v2/ChampionsController.cs +++ b/src/EntityFramework_LoL/Sources/ApiLol/Controllers/v2/ChampionsController.cs @@ -206,6 +206,7 @@ namespace ApiLol.Controllers.v2 [HttpGet("/countChampions")] public async Task GetCountChampions() { + _logger.LogInformation("method {Action} - CHAMPION - V2.0 call", nameof(GetCountChampions)); try { return Ok(await _manager.ChampionsMgr.GetNbItems()); diff --git a/src/EntityFramework_LoL/Sources/ApiLol/Mapper/ChampionMapper.cs b/src/EntityFramework_LoL/Sources/ApiLol/Mapper/ChampionMapper.cs index ef89170..12334bb 100644 --- a/src/EntityFramework_LoL/Sources/ApiLol/Mapper/ChampionMapper.cs +++ b/src/EntityFramework_LoL/Sources/ApiLol/Mapper/ChampionMapper.cs @@ -15,7 +15,9 @@ namespace ApiLol.Mapper Icon = champion.Icon, Image = champion.Image.ToDto(), Skins = champion.Skins.Select(e => e.ToDto()), - Skills = champion.Skills.Select(e => e.ToDto()) + Skills = champion.Skills.Select(e => e.ToDto()), + Characteristics = champion.Characteristics.ToDictionary(c => c.Key, c => c.Value) + }; } @@ -26,11 +28,11 @@ namespace ApiLol.Mapper { champ.AddSkin(skin.ToModel(champ)); } - foreach (var skill in championDto.Skills) { champ.AddSkill(skill.ToModel()); } + champ.AddCharacteristics(championDto.Characteristics.Select(c => Tuple.Create(c.Key, c.Value)).ToArray()); return champ; } diff --git a/src/EntityFramework_LoL/Sources/ApiLol/Mapper/RunePageMapper.cs b/src/EntityFramework_LoL/Sources/ApiLol/Mapper/RunePageMapper.cs new file mode 100644 index 0000000..8501391 --- /dev/null +++ b/src/EntityFramework_LoL/Sources/ApiLol/Mapper/RunePageMapper.cs @@ -0,0 +1,31 @@ +using ApiLol.Mapper.enums; +using DTO; +using Model; + +namespace ApiLol.Mapper +{ + public static class RunePageMapper + { + public static RunePageDto ToDto(this RunePage runePage) + { + + return new RunePageDto() + { + Name = runePage.Name, + Runes = runePage.Runes.ToDictionary(c => c.Key.ToDto(), r => r.Value.ToDto()) + }; + } + + public static RunePage ToModel(this RunePageDto runePageDto) + { + + var runePage = new RunePage(runePageDto.Name); + foreach( var rune in runePageDto.Runes) + { + runePage[rune.Key.ToModel()] = rune.Value.ToModel(); + } + + return runePage; + } + } +} diff --git a/src/EntityFramework_LoL/Sources/ApiLol/Mapper/enums/CategoryMapper.cs b/src/EntityFramework_LoL/Sources/ApiLol/Mapper/enums/CategoryMapper.cs new file mode 100644 index 0000000..55dfe9f --- /dev/null +++ b/src/EntityFramework_LoL/Sources/ApiLol/Mapper/enums/CategoryMapper.cs @@ -0,0 +1,50 @@ +using DTO; +using Model; + +namespace ApiLol.Mapper.enums +{ + public static class CategoryMapper + { + public static RunePageDto.CategoryDto ToDto(this RunePage.Category category) + { + switch (category) + { + case RunePage.Category.Major: + return RunePageDto.CategoryDto.Major; + case RunePage.Category.Minor1: + return RunePageDto.CategoryDto.Minor1; + case RunePage.Category.Minor2: + return RunePageDto.CategoryDto.Minor2; + case RunePage.Category.Minor3: + return RunePageDto.CategoryDto.Minor3; + case RunePage.Category.OtherMinor1: + return RunePageDto.CategoryDto.OtherMinor1; + case RunePage.Category.OtherMinor2: + return RunePageDto.CategoryDto.OtherMinor2; + default: + return RunePageDto.CategoryDto.Major; + } + } + + public static RunePage.Category ToModel(this RunePageDto.CategoryDto category) + { + switch (category) + { + case RunePageDto.CategoryDto.Major: + return RunePage.Category.Major; + case RunePageDto.CategoryDto.Minor1: + return RunePage.Category.Minor1; + case RunePageDto.CategoryDto.Minor2: + return RunePage.Category.Minor2; + case RunePageDto.CategoryDto.Minor3: + return RunePage.Category.Minor3; + case RunePageDto.CategoryDto.OtherMinor1: + return RunePage.Category.OtherMinor1; + case RunePageDto.CategoryDto.OtherMinor2: + return RunePage.Category.OtherMinor2; + default: + return RunePage.Category.Major; + } + } + } +} diff --git a/src/EntityFramework_LoL/Sources/DTO/ChampionDto.cs b/src/EntityFramework_LoL/Sources/DTO/ChampionDto.cs index e9b002f..4bb4953 100644 --- a/src/EntityFramework_LoL/Sources/DTO/ChampionDto.cs +++ b/src/EntityFramework_LoL/Sources/DTO/ChampionDto.cs @@ -7,8 +7,9 @@ public ChampionClassDto Class { get; set; } public string Icon { get; set; } public LargeImageDto Image { get; set; } - public IEnumerable Skins { get; set; } - public IEnumerable Skills { get; set; } + public IEnumerable? Skins { get; set; } + public IEnumerable? Skills { get; set; } + public Dictionary? Characteristics { get; set; } } } \ No newline at end of file diff --git a/src/EntityFramework_LoL/Sources/DTO/RunePageDto.cs b/src/EntityFramework_LoL/Sources/DTO/RunePageDto.cs new file mode 100644 index 0000000..1056417 --- /dev/null +++ b/src/EntityFramework_LoL/Sources/DTO/RunePageDto.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DTO +{ + public partial class RunePageDto + { + public string Name { get; set; } + public Dictionary Runes { get; set; } + } + +} diff --git a/src/EntityFramework_LoL/Sources/DTO/enums/RunePageDto.CategoryDto.cs b/src/EntityFramework_LoL/Sources/DTO/enums/RunePageDto.CategoryDto.cs new file mode 100644 index 0000000..d4aa7fb --- /dev/null +++ b/src/EntityFramework_LoL/Sources/DTO/enums/RunePageDto.CategoryDto.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DTO +{ + public partial class RunePageDto + { + public enum CategoryDto + { + Major, + Minor1, + Minor2, + Minor3, + OtherMinor1, + OtherMinor2 + } + } +}