diff --git a/Sources/.editorconfig b/Sources/.editorconfig new file mode 100644 index 0000000..73bb42e --- /dev/null +++ b/Sources/.editorconfig @@ -0,0 +1,4 @@ +[*.cs] + +# CS8604: Possible null reference argument. +dotnet_diagnostic.CS8604.severity = none diff --git a/Sources/API_LoL/API_LoL.csproj b/Sources/API_LoL/API_LoL.csproj index db2c1c8..91c61d0 100644 --- a/Sources/API_LoL/API_LoL.csproj +++ b/Sources/API_LoL/API_LoL.csproj @@ -1,4 +1,4 @@ - + net6.0 @@ -11,6 +11,7 @@ + diff --git a/Sources/API_LoL/Controllers/ChampionsController.cs b/Sources/API_LoL/Controllers/ChampionsController.cs index 96adf52..2c35997 100644 --- a/Sources/API_LoL/Controllers/ChampionsController.cs +++ b/Sources/API_LoL/Controllers/ChampionsController.cs @@ -8,6 +8,8 @@ using System.Drawing; using System; using API_LoL.Mapper; using System.Xml.Linq; +using Microsoft.EntityFrameworkCore.Metadata.Internal; +using System.Reflection.PortableExecutable; // For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860 @@ -21,18 +23,24 @@ namespace API_LoL.Controllers { public ChampionsController(IDataManager Manager) { + this._logger = LoggerFactory.Create(builder => builder.AddConsole()).CreateLogger(); this.ChampionsManager = Manager.ChampionsMgr; this.SkinsManager = Manager.SkinsMgr; } + private IChampionsManager ChampionsManager; private ISkinsManager SkinsManager; + //private StubData stubData; + private ILogger logger; + private readonly ILogger _logger; // GET api//5 [HttpGet("count")] public async Task GetCount() { + _logger.LogInformation(message: "count returned", "Get", "/Champion/Count", 200, DateTime.Now); return Ok(ChampionsManager.GetNbItems()); } @@ -41,6 +49,7 @@ namespace API_LoL.Controllers { if (size - index > 10) { + _logger.LogError(message : "Oversized","Get","/Champion",400,"name : "+name,"skill : "+skill,"characteristics : "+characteristic,"index : "+index,"size : "+size,DateTime.Now); return BadRequest(); } if (!string.IsNullOrEmpty(name)) @@ -48,53 +57,78 @@ namespace API_LoL.Controllers var list = await ChampionsManager.GetItemsByName(name, index, size); if (list.Count() != 0) { + _logger.LogInformation(message: "Champion returned by name", "Get", "/Champion", 200, "name : " + name, "skill : " + skill, "characteristic : " + characteristic, "index : " + index, "size : " + size, DateTime.Now); return Ok(list.Select(champion => champion?.ToDTO())); } - else { return NoContent(); } + else { + _logger.LogInformation(message: "No Champion found by name", "Get", "/Champion", 204, "name : " + name, "skill : " + skill, "characteristic : " + characteristic, "index : " + index, "size : " + size, DateTime.Now); + return NoContent(); } } else if(!string.IsNullOrEmpty(skill)) { var list = await ChampionsManager.GetItemsBySkill(skill, index, size); if (list.Count() != 0) { + _logger.LogInformation(message: "Champion returned by skill", "Get", "/Champion", 200, "name : " + name, "skill : " + skill, "characteristic : " + characteristic, "index : " + index, "size : " + size, DateTime.Now); return Ok(list.Select(champion => champion?.ToDTO())); } - else { return NoContent(); } + else { + _logger.LogInformation(message: "No Champion found by skill", "Get", "/Champion", 204, "name : " + name, "skill : " + skill, "characteristic : " + characteristic, "index : " + index, "size : " + size, DateTime.Now); + return NoContent(); } } else if(!string.IsNullOrEmpty (characteristic)) { var list = await ChampionsManager.GetItems(index, size); if (list.Count() != 0) { + _logger.LogInformation(message: "Champion returned by characteristic", "Get", "/Champion", 200, "name : " + name, "skill : " + skill, "characteristic : " + characteristic, "index : " + index, "size : " + size, DateTime.Now); return Ok(list.Select(champion => champion?.ToDTO())); } - else { return NoContent(); } + else { + _logger.LogInformation(message: "No Champion found by char", "Get", "/Champion", 204, "name : " + name, "skill : " + skill, "characteristic : " + characteristic, "index : " + index, "size : " + size, DateTime.Now); + return NoContent(); } } else { var list = await ChampionsManager.GetItems(index, size); if (list.Count() != 0) { + _logger.LogInformation(message: "Champion returned default", "Get", "/Champion", 200, "name : " + name, "skill : " + skill, "characteristic : " + characteristic, "index : " + index, "size : " + size, DateTime.Now); return Ok(list.Select(champion => champion?.ToDTO())); } - else { return NoContent(); } + else { + _logger.LogInformation(message: "No Champion found Default", "Get", "/Champion", 204, "name : " + name,"skill : " + skill,"characteristic : "+ characteristic,"index : "+index,"size : "+size, DateTime.Now); + return NoContent(); + } } } [HttpGet("name")] public async Task GetByName(String name) { - if (string.IsNullOrEmpty(name)) return BadRequest(); + if (string.IsNullOrEmpty(name)) + { + _logger.LogError(message: "No paramater given", "Get", "/Champion/Name", 400, "name : " + name, DateTime.Now); + return BadRequest(); + } var list = await ChampionsManager.GetItemsByName(name, 0, 1); if (list.Count() == 1) { + _logger.LogInformation(message: "Champion found", "Get", "/Champion/Name", 20, "name : " + name, DateTime.Now); return Ok(list.Select(champion => champion?.ToDTO()).First()); } - else { return NoContent(); } + else { + _logger.LogInformation(message: "No Champion found", "Get", "/Champion/Name", 204, "name : " + name, DateTime.Now); + return NoContent(); + } } [HttpGet("name/skins")] public async Task GetSkinsByName(String name) { - if (string.IsNullOrEmpty(name)) return BadRequest(); + if (string.IsNullOrEmpty(name)) + { + _logger.LogError(message: "No paramater given", "Get", "/Champion/Name/Skins", 400, "name : " + name, DateTime.Now); + return BadRequest(); + } var list = await ChampionsManager.GetItemsByName(name, 0, 1); if (list.Count() == 1) { @@ -104,9 +138,16 @@ namespace API_LoL.Controllers var skins = await SkinsManager.GetItemsByChampion(list.First(), 0, nb); return Ok(skins.Select(skin => skin?.ToDTO())); } - else { return NoContent(); } + else { + _logger.LogInformation(message: "No Skin found found", "Get", "/Champion/Name/Skins", 204, "name : " + name, DateTime.Now); + return NoContent(); } } - else { return NoContent(); } + else { + _logger.LogInformation(message: "No Champion found", "Get", "/Champion/Name/Skins", 204, "name : " + name, DateTime.Now); + + return NoContent(); + } + } //[HttpGet("name/skills")] @@ -134,16 +175,23 @@ namespace API_LoL.Controllers { if (champion == null) { + _logger.LogError(message: "Null paramater given", "Post", "/Champion", 422, "champion : " + champion.toString, DateTime.Now); return UnprocessableEntity(); } else { - var champ = await ChampionsManager.GetItemsByName(champion.Name, 0, 1); - if(champ.Count() != 0 && champ.FirstOrDefault().Name == champion.Name) + var list = await ChampionsManager.GetItemsByName(champion.Name, 0, 1); + Champion champ = list.FirstOrDefault(); + if (champ != null) { - return Conflict(champion); + if(champ.Name == champion.Name) + { + _logger.LogError(message: "Champion with this id already exists", "Post", "/Champion", 409, "champion : " + champion.toString, DateTime.Now); + return Conflict(champion); + } } await ChampionsManager.AddItem(champion.ToChampion()); + _logger.LogInformation(message: "Champion created", "Post", "Champion/Name", 201, "champion : " + champion.toString, DateTime.Now); return CreatedAtAction("Post",champion); } @@ -153,16 +201,26 @@ namespace API_LoL.Controllers [HttpPut("name")] public async Task Put(string name, ChampionDTO championDTO) { - if(string.IsNullOrEmpty(name)) + if (string.IsNullOrEmpty(name)) + { + _logger.LogError(message: "Null paramater given for Name", "Put", "/Champion/Name", 400,"name : "+name, "champion : " + championDTO.toString, DateTime.Now); return BadRequest(); + } if(championDTO == null) + { + _logger.LogError(message: "Null paramater given for Champion", "Put", "/Champion/Name", 422, "name : " + name, "champion : " + championDTO.toString, DateTime.Now); return UnprocessableEntity(); + } var list = await ChampionsManager.GetItemsByName(name, 0, 1); if (list.Count() == 1) { + _logger.LogInformation(message: "Champion updated", "Put", "Champion/Name", 200, "name : " + name, "champion : " + championDTO.toString, DateTime.Now); return Ok(ChampionsManager.UpdateItem(list.First(), championDTO.ToChampion())); } - else { return NoContent(); } + else { + _logger.LogInformation(message: "No champion Found", "Put", "/Champion/Name", 204, "name : " + name, "champion : " + championDTO.toString, DateTime.Now); + return NoContent(); + } } // DELETE api//5 @@ -170,11 +228,17 @@ namespace API_LoL.Controllers public async Task Delete(string name) { if (string.IsNullOrEmpty(name)) - return BadRequest(); + { + _logger.LogError(message: "Null paramater given for Name", "Delete", "/Champion/Name", 400, "name : " + name, DateTime.Now); + return BadRequest(); + } var list = await ChampionsManager.GetItemsByName(name, 0, 1); if(list.Count() == 1){ + _logger.LogInformation(message: "Champion Deleted", "Delete", "/Champion/Name", 200, "name : " + name, DateTime.Now); return Ok(await ChampionsManager.DeleteItem(list.First())); - }else { return NoContent(); } + }else { + _logger.LogInformation(message: "No champion Found", "Delete", "/Champion/Name", 204, "name : " + name, DateTime.Now); + return NoContent(); } } } } diff --git a/Sources/API_LoL/Program.cs b/Sources/API_LoL/Program.cs index f1b44d0..78d467f 100644 --- a/Sources/API_LoL/Program.cs +++ b/Sources/API_LoL/Program.cs @@ -3,6 +3,7 @@ using EntityFramework; using EntityFramework.Manager; using Microsoft.AspNetCore.Mvc.ApiExplorer; using Microsoft.AspNetCore.Mvc.Versioning; +using Microsoft.Extensions.Logging; using Model; using StubLib; diff --git a/Sources/API_LoL/champion.db-shm b/Sources/API_LoL/champion.db-shm new file mode 100644 index 0000000..39d4916 Binary files /dev/null and b/Sources/API_LoL/champion.db-shm differ diff --git a/Sources/API_LoL/champion.db-wal b/Sources/API_LoL/champion.db-wal new file mode 100644 index 0000000..932d20c Binary files /dev/null and b/Sources/API_LoL/champion.db-wal differ diff --git a/Sources/Api_UT/ChampionControllerTest.cs b/Sources/Api_UT/ChampionControllerTest.cs index 09c7875..0a93acf 100644 --- a/Sources/Api_UT/ChampionControllerTest.cs +++ b/Sources/Api_UT/ChampionControllerTest.cs @@ -3,6 +3,7 @@ using DTO; using FluentAssertions; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore.Query; +using Microsoft.Extensions.Logging; using Model; using StubLib; using System.Collections; diff --git a/Sources/EF_UT/EFDataManagerChampionTest.cs b/Sources/EF_UT/EFDataManagerChampionTest.cs index 07e3ca2..486a8be 100644 --- a/Sources/EF_UT/EFDataManagerChampionTest.cs +++ b/Sources/EF_UT/EFDataManagerChampionTest.cs @@ -47,7 +47,7 @@ namespace EF_UT var ak = (await championsManager.GetItemsByName("A", 0, 1)).First(); Assert.IsNotNull(ak); - //Assert.AreEqual("Akali", ak.Name); + Assert.AreEqual("Aatrox", ak.Name); } } }