diff --git a/Sources/APILOL/Controllers/ChampionsController.cs b/Sources/APILOL/Controllers/ChampionsController.cs index c5d795c..d5579a0 100644 --- a/Sources/APILOL/Controllers/ChampionsController.cs +++ b/Sources/APILOL/Controllers/ChampionsController.cs @@ -19,24 +19,27 @@ namespace APILOL.Controllers public async Task Get() { var champions = await dataManager.GetItems(0, await dataManager.GetNbItems()); - return Ok(new { result = champions.Select(c => c.ToDto())}); + IEnumerable items = champions.Select(c => c.ToDto()); + return Ok(items); } // GET api//5 [HttpGet("{name}")] - public IActionResult Get(string name) + public async Task Get(string name) { if (dataManager.GetNbItemsByName(name) != null) { - return Ok(dataManager.GetItemsByName(name, 0, dataManager.GetNbItemsByName(name))); + return Ok(dataManager.GetItemsByName(name, 0, await dataManager.GetNbItems())); } return NotFound(); } // POST api/ [HttpPost] - public void Post([FromBody] ChampionDTO championDTO) + public async Task Post([FromBody] ChampionDTO championDTO) { + + return CreatedAtAction(nameof(Get),(await dataManager.AddItem(championDTO.ToModel())).ToDto); } // PUT api//5 diff --git a/Sources/APILOL/Mapper/ChampionMapper.cs b/Sources/APILOL/Mapper/ChampionMapper.cs index 33bde68..883052c 100644 --- a/Sources/APILOL/Mapper/ChampionMapper.cs +++ b/Sources/APILOL/Mapper/ChampionMapper.cs @@ -10,7 +10,13 @@ namespace APILOL.Mapper return new ChampionDTO() { Name = champion.Name, + Bio = champion.Bio, }; } + + public static Champion ToModel(this ChampionDTO champion) + { + return new Champion(champion.Name); + } } } diff --git a/Sources/DTO/ChampionDTO.cs b/Sources/DTO/ChampionDTO.cs index 5ef392e..2a8f880 100644 --- a/Sources/DTO/ChampionDTO.cs +++ b/Sources/DTO/ChampionDTO.cs @@ -3,5 +3,6 @@ public class ChampionDTO { public string Name { get; set; } + public string Bio { get; set; } } } \ No newline at end of file diff --git a/Sources/LeagueOfLegends.sln b/Sources/LeagueOfLegends.sln index 19c01c7..67afe5a 100644 --- a/Sources/LeagueOfLegends.sln +++ b/Sources/LeagueOfLegends.sln @@ -19,7 +19,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "APILOL", "APILOL\APILOL.csp EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DTO", "DTO\DTO.csproj", "{1434DEF6-0575-4C3D-BF14-AF29A444BC74}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EntityFrameworkLOL", "EntityFrameworkLOL\EntityFrameworkLOL.csproj", "{61D807B0-FA1A-439D-9810-9F31A0C47034}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EntityFrameworkLOL", "EntityFrameworkLOL\EntityFrameworkLOL.csproj", "{61D807B0-FA1A-439D-9810-9F31A0C47034}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestUnitaire", "TestUnitaire\TestUnitaire.csproj", "{D24FBC48-F9C3-45CA-8D51-A851559C307F}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -55,6 +57,10 @@ Global {61D807B0-FA1A-439D-9810-9F31A0C47034}.Debug|Any CPU.Build.0 = Debug|Any CPU {61D807B0-FA1A-439D-9810-9F31A0C47034}.Release|Any CPU.ActiveCfg = Release|Any CPU {61D807B0-FA1A-439D-9810-9F31A0C47034}.Release|Any CPU.Build.0 = Release|Any CPU + {D24FBC48-F9C3-45CA-8D51-A851559C307F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D24FBC48-F9C3-45CA-8D51-A851559C307F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D24FBC48-F9C3-45CA-8D51-A851559C307F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D24FBC48-F9C3-45CA-8D51-A851559C307F}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -62,6 +68,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} + {D24FBC48-F9C3-45CA-8D51-A851559C307F} = {C76D0C23-1FFA-4963-93CD-E12BD643F030} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {92F3083D-793F-4552-8A9A-0AD6534159C9} diff --git a/Sources/Shared/IGenericDataManager.cs b/Sources/Shared/IGenericDataManager.cs index 7dfbe88..a1a66f9 100644 --- a/Sources/Shared/IGenericDataManager.cs +++ b/Sources/Shared/IGenericDataManager.cs @@ -2,7 +2,8 @@ public interface IGenericDataManager { Task GetNbItems(); - Task> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false); + + Task> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false); Task GetNbItemsByName(string substring); Task> GetItemsByName(string substring, int index, int count, string? orderingPropertyName = null, bool descending = false); Task UpdateItem(T oldItem, T newItem); diff --git a/Sources/StubLib/Extensions.cs b/Sources/StubLib/Extensions.cs index ef2e82b..1b447a2 100644 --- a/Sources/StubLib/Extensions.cs +++ b/Sources/StubLib/Extensions.cs @@ -60,6 +60,7 @@ namespace StubLib collection.Add(newItem!); return Task.FromResult(newItem); } + } } diff --git a/Sources/StubLib/StubData.Champions.cs b/Sources/StubLib/StubData.Champions.cs index 8df8028..6d99c73 100644 --- a/Sources/StubLib/StubData.Champions.cs +++ b/Sources/StubLib/StubData.Champions.cs @@ -25,12 +25,18 @@ namespace StubLib public Task AddItem(Champion? item) => parent.champions.AddItem(item); + + public Task DeleteItem(Champion? item) => parent.champions.DeleteItem(item); public Task GetNbItems() => Task.FromResult(parent.champions.Count); + public Champion GetLast() + => parent.champions.Last(); + + public Task> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false) => parent.champions.GetItemsWithFilterAndOrdering( c => true, diff --git a/Sources/TestUnitaire/TestUnitaire.csproj b/Sources/TestUnitaire/TestUnitaire.csproj new file mode 100644 index 0000000..a4ba0cb --- /dev/null +++ b/Sources/TestUnitaire/TestUnitaire.csproj @@ -0,0 +1,23 @@ + + + + net6.0 + enable + enable + + false + + + + + + + + + + + + + + + diff --git a/Sources/TestUnitaire/UnitTestChampion.cs b/Sources/TestUnitaire/UnitTestChampion.cs new file mode 100644 index 0000000..3714acf --- /dev/null +++ b/Sources/TestUnitaire/UnitTestChampion.cs @@ -0,0 +1,62 @@ +using APILOL.Controllers; +using APILOL.Mapper; +using DTO; +using Microsoft.AspNetCore.Mvc; +using StubLib; + +namespace TestUnitaire +{ + [TestClass] + public class UnitTestChampion + { + private readonly ChampionsController controller; + private readonly StubData stub; + public UnitTestChampion() + { + stub = new StubData(); + controller = new ChampionsController(); + } + + [TestMethod] + public async Task TestGet() + { + var champions = await controller.Get(); + + var resultObject = champions as OkObjectResult; + Assert.IsNotNull(resultObject); + + var resultType = resultObject?.Value as IEnumerable; + Assert.IsNotNull(resultType); + + Assert.AreEqual(resultType.Count(), await stub.ChampionsMgr.GetNbItems()); + } + + [TestMethod] + public async Task TestPost() + { + var ChampionDtoToTest = new ChampionDTO { Bio= "This champion is a legendary Fox", Name="Foxane"}; + + var champions = await controller.Post(ChampionDtoToTest); + var resultObject = champions as OkObjectResult; + Assert.IsNotNull(resultObject); + + var resultType = resultObject?.Value as IEnumerable; + Assert.IsNotNull(resultType); + + Assert.AreEqual(resultType.Last(), stub.ChampionsMgr.GetItems(-1, await stub.ChampionsMgr.GetNbItems())); + } + + + [TestMethod] + public void TestPut() + { + } + + [TestMethod] + public void TestDelete() + { + } + + + } +} \ No newline at end of file diff --git a/Sources/TestUnitaire/Usings.cs b/Sources/TestUnitaire/Usings.cs new file mode 100644 index 0000000..ab67c7e --- /dev/null +++ b/Sources/TestUnitaire/Usings.cs @@ -0,0 +1 @@ +global using Microsoft.VisualStudio.TestTools.UnitTesting; \ No newline at end of file