diff --git a/Sources/API_LoL/Controllers/ChampionsController.cs b/Sources/API_LoL/Controllers/ChampionsController.cs index 1a077b8..b397831 100644 --- a/Sources/API_LoL/Controllers/ChampionsController.cs +++ b/Sources/API_LoL/Controllers/ChampionsController.cs @@ -30,6 +30,12 @@ namespace API_LoL.Controllers // GET api//5 + [HttpGet("count")] + public async Task GetCount() + { + return Ok(ChampionsManager.GetNbItems()); + } + [HttpGet] public async Task Get(string? name = null,String? skill = null, String? characteristic = null,int index = 0,int size =10) { @@ -103,8 +109,27 @@ namespace API_LoL.Controllers else { return NoContent(); } } - // POST api/ - [HttpPost] + //[HttpGet("name/skills")] + //public async Task GetSkillsByName(String name) + //{ + // if (string.IsNullOrEmpty(name)) return BadRequest(); + // var list = await ChampionsManager.GetItemsByName(name, 0, 1); + // if (list.Count() == 1) + // { + // var skins = await SkinsManager.GetItemsByChampion(list.First(), 0, await SkinsManager.GetNbItemsByChampion(list.First())); + // if (skins.Count() != 0) + // { + // return Ok(skins.Select(skin => skin?.ToDTO())); + // } + // else { return NoContent(); } + // } + // else { return NoContent(); } + //} + + + + // POST api/ + [HttpPost] public async Task Post(ChampionDTO champion) { if (champion == null) @@ -113,21 +138,43 @@ namespace API_LoL.Controllers } else { + var champ = await ChampionsManager.GetItemsByName(champion.Name, 0, 1); + if(champ.FirstOrDefault().Name == champion.Name) + { + return Conflict(champion); + } await ChampionsManager.AddItem(champion.ToChampion()); return CreatedAtAction("Post",champion); + } } // PUT api//5 - [HttpPut("{id}")] - public void Put(int id, [FromBody] string value) + [HttpPut("name")] + public async Task Put(string name, ChampionDTO championDTO) { + if(string.IsNullOrEmpty(name)) + return BadRequest(); + if(championDTO == null) + return UnprocessableEntity(); + var list = await ChampionsManager.GetItemsByName(name, 0, 1); + if (list.Count() == 1) + { + return Ok(ChampionsManager.UpdateItem(list.First(), championDTO.ToChampion())); + } + else { return NoContent(); } } // DELETE api//5 - [HttpDelete("{id}")] - public void Delete(int id) + [HttpDelete("name")] + public async Task Delete(string name) { + if (string.IsNullOrEmpty(name)) + return BadRequest(); + var list = await ChampionsManager.GetItemsByName(name, 0, 1); + if(list.Count() == 1){ + return Ok(await ChampionsManager.DeleteItem(list.First())); + }else { return NoContent(); } } } } diff --git a/Sources/API_LoL/Mapper/ChampionMapper.cs b/Sources/API_LoL/Mapper/ChampionMapper.cs index 3dd6a11..632149e 100644 --- a/Sources/API_LoL/Mapper/ChampionMapper.cs +++ b/Sources/API_LoL/Mapper/ChampionMapper.cs @@ -12,19 +12,13 @@ namespace DTO.Mapper { public static ChampionDTO ToDTO(this Champion champion) { - return new ChampionDTO(champion.Name, champion.Bio, champion.Icon,champion.Class.ToDTO().ToString()); - //return new ChampionDTO(champion.Name, champion.Bio, champion.Icon, champion.Skills); + return new ChampionDTO(champion.Name, champion.Bio, champion.Icon, champion.Class.ToDTO(), champion.Image.Base64); } public static Champion ToChampion(this ChampionDTO champion) { - Champion champ = new Champion(champion.Name, champion.Class.ToChampionClass(), champion.Icon, "", champion.Bio); + return new Champion(champion.Name, champClass: champion.Class.ToChampionClass(),icon: champion.Icon,bio: champion.Bio,image :champion.Image); - //foreach (Skill skill in champion.Skills) - //{ - // champ.AddSkill(skill); - //} - return champ; } } } diff --git a/Sources/API_LoL/Mapper/SkinMapper.cs b/Sources/API_LoL/Mapper/SkinMapper.cs index 25ac8f6..5b18c55 100644 --- a/Sources/API_LoL/Mapper/SkinMapper.cs +++ b/Sources/API_LoL/Mapper/SkinMapper.cs @@ -7,12 +7,12 @@ namespace API_LoL.Mapper { public static SkinDTO ToDTO(this Skin skin) { - return new SkinDTO(skin.Name, skin.Description, skin.Icon); + return new SkinDTO(skin.Name, skin.Description, skin.Icon,skin.Image.Base64,skin.Price); } public static Skin ToSkin(this SkinDTO skin) { - return new Skin(skin.Name, null, icon:skin.Icon) ; + return new Skin(skin.Name, null,price: skin.Price, icon:skin.Icon,image: skin.Image,description: skin.Description) ; } } } diff --git a/Sources/API_LoL/champion.db b/Sources/API_LoL/champion.db index bea5ebd..cfdeed2 100644 Binary files a/Sources/API_LoL/champion.db and b/Sources/API_LoL/champion.db differ diff --git a/Sources/API_LoL/champion.db-shm b/Sources/API_LoL/champion.db-shm index fe9ac28..bc92bf7 100644 Binary files a/Sources/API_LoL/champion.db-shm 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 index e69de29..8456db7 100644 Binary files a/Sources/API_LoL/champion.db-wal and b/Sources/API_LoL/champion.db-wal differ diff --git a/Sources/Api_UT/ChampionControllerTest.cs b/Sources/Api_UT/ChampionControllerTest.cs index a3ca675..787899f 100644 --- a/Sources/Api_UT/ChampionControllerTest.cs +++ b/Sources/Api_UT/ChampionControllerTest.cs @@ -21,7 +21,7 @@ namespace Api_UT public async Task Get_Default_OkList() { - List list = new List {new ChampionDTO("Akali","","","Assassin"), new ChampionDTO("Aatrox", "", "", "Fighter"), new ChampionDTO("Ahri", "", "", "Mage"), new ChampionDTO("Akshan", "", "", "Marksman"), new ChampionDTO("Bard", "", "","Support"), new ChampionDTO("Alistar", "", "","Tank") }; + List list = new List {new ChampionDTO("Akali","","","Assassin",""), new ChampionDTO("Aatrox", "", "", "Fighter",""), new ChampionDTO("Ahri", "", "", "Mage",""), new ChampionDTO("Akshan", "", "", "Marksman",""), new ChampionDTO("Bard", "", "","Support",""), new ChampionDTO("Alistar", "", "","Tank","") }; IActionResult a = await api.Get(); a.Should().NotBeNull(); var aObject = a as OkObjectResult; @@ -42,7 +42,7 @@ namespace Api_UT [TestMethod] public async Task Get_2First_OkListOf2() { - List list = new List { new ChampionDTO("Akali", "", "", "Assassin"), new ChampionDTO("Aatrox", "", "", "Fighter") }; + List list = new List { new ChampionDTO("Akali", "", "", "Assassin",""), new ChampionDTO("Aatrox", "", "", "Fighter","") }; IActionResult a = await api.Get(index: 0,size: 2); @@ -57,7 +57,7 @@ namespace Api_UT [TestMethod] public async Task Get_FilterAName_OkListOf5() { - List list = new List { new ChampionDTO("Akali", "", "", "Assassin"), new ChampionDTO("Akshan", "", "", "Marksman") }; + List list = new List { new ChampionDTO("Akali", "", "", "Assassin", ""), new ChampionDTO("Akshan", "", "", "Marksman", "") }; IActionResult a = await api.Get(name: "Ak"); @@ -75,9 +75,9 @@ namespace Api_UT public async Task Post_ValidChampion_Created() { ChampionsController api = new ChampionsController(new StubData()); - IActionResult a = await api.Post(new ChampionDTO("nom","bio","icon", "Assassin")); + IActionResult a = await api.Post(new ChampionDTO("nom","bio","icon", "Assassin","")); Assert.IsNotNull(a); - ChampionDTO champ = new ChampionDTO("nom", "bio", "icon","Assassin"); + ChampionDTO champ = new ChampionDTO("nom", "bio", "icon","Assassin", ""); Assert.IsTrue(champ.equals((ChampionDTO)((CreatedAtActionResult)a).Value)); } diff --git a/Sources/DTO/ChampionDTO.cs b/Sources/DTO/ChampionDTO.cs index 723d7b4..23478dd 100644 --- a/Sources/DTO/ChampionDTO.cs +++ b/Sources/DTO/ChampionDTO.cs @@ -5,17 +5,19 @@ namespace DTO { public class ChampionDTO { - public ChampionDTO(string name, string bio, string icon, string championClassDTO) + public ChampionDTO(string name, string bio, string icon, string Class, string image) { Name = name; Bio = bio; Icon = icon; - Class = championClassDTO; + this.Class = Class; + Image = image; } public string Name { get; set; } public string Bio { get; set; } public string Icon { get; set; } + public string Image { get; set; } public string Class { get; set; } public bool equals(ChampionDTO other) diff --git a/Sources/DTO/SkinDTO.cs b/Sources/DTO/SkinDTO.cs index 32696d6..eb255f6 100644 --- a/Sources/DTO/SkinDTO.cs +++ b/Sources/DTO/SkinDTO.cs @@ -13,10 +13,16 @@ namespace DTO public string Description { get; set; } public string Icon { get; set; } - public SkinDTO(string name,string description,string icon) { + public string Image { get; set; } + + public float Price { get; set; } + + public SkinDTO(string name,string description,string icon,string image,float price) { this.Name = name; this.Description = description; this.Icon = icon; + this.Image = image; + this.Price = price; } } diff --git a/Sources/EntityFramework/Manager/EFDataManager.Champions.cs b/Sources/EntityFramework/Manager/EFDataManager.Champions.cs index e784096..689959b 100644 --- a/Sources/EntityFramework/Manager/EFDataManager.Champions.cs +++ b/Sources/EntityFramework/Manager/EFDataManager.Champions.cs @@ -26,7 +26,9 @@ namespace EntityFramework.Manager { if (item != null) { + context.Add(item.ToEntity()); + context.SaveChanges(); return item; } else @@ -36,9 +38,21 @@ namespace EntityFramework.Manager } } - public Task DeleteItem(Champion? item) + public async Task DeleteItem(Champion? item) { - throw new NotImplementedException(); + using (var context = new LoLDBContextWithStub()) + { + var champ = context.Champions.Select(c => c == item.ToEntity()); + + if(champ.Count()<1) + { + return false; + } + context.Champions.Remove(item.ToEntity()); + context.SaveChanges(); + return true; + + } } public async Task> GetItems(int index, int count, string? orderingPropertyName = null, bool descending = false) @@ -115,9 +129,12 @@ namespace EntityFramework.Manager } } - public Task GetNbItems() + public async Task GetNbItems() { - throw new NotImplementedException(); + using(var context = new LoLDBContextWithStub()) + { + return context.Champions.Count(); + } } public Task GetNbItemsByCharacteristic(string charName) @@ -150,9 +167,19 @@ namespace EntityFramework.Manager throw new NotImplementedException(); } - public Task UpdateItem(Champion? oldItem, Champion? newItem) + public async Task UpdateItem(Champion? oldItem, Champion? newItem) { - throw new NotImplementedException(); + using(var context = new LoLDBContextWithStub()) + { + if (oldItem != null && newItem != null) + { + var champ = context.Champions.Where(c => c == oldItem.ToEntity()).First(); + champ = newItem.ToEntity(); + context.SaveChanges(); + return newItem; + } + else { throw new Exception(); } + } } } } diff --git a/Sources/StubLib/StubData.Champions.cs b/Sources/StubLib/StubData.Champions.cs index ad19275..90a33d9 100644 --- a/Sources/StubLib/StubData.Champions.cs +++ b/Sources/StubLib/StubData.Champions.cs @@ -8,7 +8,7 @@ namespace StubLib private List champions = new() { new Champion("Akali", ChampionClass.Assassin), - new Champion("Aatrox", ChampionClass.Fighter), + new Champion("Aatrox", ChampionClass.Fighter), new Champion("Ahri", ChampionClass.Mage), new Champion("Akshan", ChampionClass.Marksman), new Champion("Bard", ChampionClass.Support),