Add new functions for ChampionsController and create Skill Dto with mapper
continuous-integration/drone/push Build is passing Details

Logs_Version_Filtrage_Pagination
Emre KARTAL 2 years ago
parent afbd78428b
commit c88eac1354

@ -23,12 +23,14 @@ namespace ApiLol.Controllers
// GET: api/<ValuesController> // GET: api/<ValuesController>
[HttpGet] [HttpGet]
public async Task<IActionResult> Get([FromQuery] PageRequest pageRequest) public async Task<IActionResult> Get([FromQuery] PageRequest pageRequest)
{
try
{ {
int nbTotal = await _manager.ChampionsMgr.GetNbItems(); int nbTotal = await _manager.ChampionsMgr.GetNbItems();
if (pageRequest.count + pageRequest.index > nbTotal) if (pageRequest.count + pageRequest.index > nbTotal)
{ {
_logger.LogWarning($"too many, maximum {nbTotal}"); _logger.LogWarning($"too many, maximum {nbTotal}");
return BadRequest("Champion limit exceed"); return BadRequest($"Champion limit exceed, max {nbTotal}");
} }
_logger.LogInformation($"method Get call"); _logger.LogInformation($"method Get call");
@ -36,10 +38,18 @@ namespace ApiLol.Controllers
.Select(x => x.ToDto()); .Select(x => x.ToDto());
return Ok(dtos); return Ok(dtos);
} }
catch (Exception e)
{
return BadRequest(e.Message);
}
}
// GET api/<ValuesController>/5 // GET api/<ValuesController>/5
[HttpGet("{name}")] [HttpGet("{name}")]
public async Task<IActionResult> Get(string name) public async Task<IActionResult> Get(string name)
{
try
{ {
_logger.LogInformation($"method GetByName call with {name}"); _logger.LogInformation($"method GetByName call with {name}");
var dtos = (await _manager.ChampionsMgr.GetItemsByName(name, 0, await _manager.ChampionsMgr.GetNbItems())) var dtos = (await _manager.ChampionsMgr.GetItemsByName(name, 0, await _manager.ChampionsMgr.GetNbItems()))
@ -51,10 +61,18 @@ namespace ApiLol.Controllers
} }
return Ok(dtos); return Ok(dtos);
} }
catch (Exception e)
{
return BadRequest(e.Message);
}
}
// POST api/<ValuesController> // POST api/<ValuesController>
[HttpPost] [HttpPost]
public async Task<IActionResult> Post([FromBody] ChampionDto champion) public async Task<IActionResult> Post([FromBody] ChampionDto champion)
{
try
{ {
_logger.LogInformation($"method Post call"); _logger.LogInformation($"method Post call");
var dtos = (await _manager.ChampionsMgr.GetItemsByName(champion.Name, 0, await _manager.ChampionsMgr.GetNbItems())); var dtos = (await _manager.ChampionsMgr.GetItemsByName(champion.Name, 0, await _manager.ChampionsMgr.GetNbItems()));
@ -65,10 +83,18 @@ namespace ApiLol.Controllers
return CreatedAtAction(nameof(Get), return CreatedAtAction(nameof(Get),
(await _manager.ChampionsMgr.AddItem(champion.ToModel())).ToDto()); (await _manager.ChampionsMgr.AddItem(champion.ToModel())).ToDto());
} }
catch (Exception e)
{
return BadRequest(e.Message);
}
}
// PUT api/<ValuesController>/5 // PUT api/<ValuesController>/5
[HttpPut("{name}")] [HttpPut("{name}")]
public async Task<IActionResult> Put(string name, [FromBody] ChampionDto champion) public async Task<IActionResult> Put(string name, [FromBody] ChampionDto champion)
{
try
{ {
_logger.LogInformation($"method Put call with {name}"); _logger.LogInformation($"method Put call with {name}");
var dtos = (await _manager.ChampionsMgr.GetItemsByName(name, 0, await _manager.ChampionsMgr.GetNbItems())); var dtos = (await _manager.ChampionsMgr.GetItemsByName(name, 0, await _manager.ChampionsMgr.GetNbItems()));
@ -87,10 +113,53 @@ namespace ApiLol.Controllers
} }
return Ok(await _manager.ChampionsMgr.UpdateItem(dtos.First(), champion.ToModel())); return Ok(await _manager.ChampionsMgr.UpdateItem(dtos.First(), champion.ToModel()));
} }
catch (Exception e)
{
return BadRequest(e.Message);
}
}
[HttpGet("/{name}/skins")]
public async Task<ActionResult<Skin>> GetChampionsSkins(string name)
{
try
{
var champions = await _manager.ChampionsMgr.GetItemsByName(name, 0, await _manager.ChampionsMgr.GetNbItems());
//skinsDTO
IEnumerable<SkinDto> res = champions.First().Skins.Select(e => e.ToDto());
return Ok(res);
}
catch (Exception e)
{
return BadRequest(e.Message);
}
}
[HttpGet("/{name}/skills")]
public async Task<ActionResult<Skin>> GetChampionsSkills(string name)
{
try
{
var champions = await _manager.ChampionsMgr.GetItemsByName(name, 0, await _manager.ChampionsMgr.GetNbItems());
//SkillDTO
IEnumerable<SkillDto> res = champions.First().Skills.Select(e => e.ToDto());
return Ok(res);
}
catch (Exception e)
{
return BadRequest(e.Message);
}
}
// DELETE api/<ValuesController>/5 // DELETE api/<ValuesController>/5
[HttpDelete("{name}")] [HttpDelete("{name}")]
public async Task<IActionResult> Delete(string name) public async Task<IActionResult> Delete(string name)
{
try
{ {
_logger.LogInformation($"method Delete call with {name}"); _logger.LogInformation($"method Delete call with {name}");
var dtos = (await _manager.ChampionsMgr.GetItemsByName(name, 0, await _manager.ChampionsMgr.GetNbItems())); var dtos = (await _manager.ChampionsMgr.GetItemsByName(name, 0, await _manager.ChampionsMgr.GetNbItems()));
@ -101,5 +170,10 @@ namespace ApiLol.Controllers
} }
return Ok(await _manager.ChampionsMgr.DeleteItem(dtos.First())); return Ok(await _manager.ChampionsMgr.DeleteItem(dtos.First()));
} }
catch (Exception e)
{
return BadRequest(e.Message);
}
}
} }
} }

@ -14,13 +14,14 @@ namespace ApiLol.Mapper
Class = champion.Class.ToDto(), Class = champion.Class.ToDto(),
Icon = champion.Icon, Icon = champion.Icon,
Image = champion.Image.ToDto(), Image = champion.Image.ToDto(),
Skins = champion.Skins.Select(e => e.ToDto()) Skins = champion.Skins.Select(e => e.ToDto()),
Skills = champion.Skills.Select(e => e.ToDto())
}; };
} }
public static Champion ToModel(this ChampionDto championDto) public static Champion ToModel(this ChampionDto championDto)
{ {
return new Champion(championDto.Name, championDto.Class.ToModel(), championDto.Icon, championDto.Image.Base64,championDto.Bio); return new Champion(championDto.Name, championDto.Class.ToModel(), championDto.Icon, championDto.Image.Base64, championDto.Bio);
} }
} }

@ -11,15 +11,13 @@ namespace ApiLol.Mapper
{ {
Name = skill.Name, Name = skill.Name,
Description = skill.Description, Description = skill.Description,
Type = skill.Type.ToDto()
}; };
} }
/* public static Skill ToModel(this SkillDto skillDto) public static Skill ToModel(this SkillDto skillDto)
{ {
return new Skill(skill.Name) return new Skill(skillDto.Name, skillDto.Type.ToModel(), skillDto.Description);
{ }
Description = skill.Description
};
}*/
} }
} }

@ -1,8 +1,7 @@
using DTO; using DTO;
using DTO.enums;
using Model; using Model;
namespace ApiLol.Mapper.enums namespace ApiLol.Mapper
{ {
public static class SkillTypeMapper public static class SkillTypeMapper
{ {

@ -18,10 +18,10 @@ namespace Client
httpClient.BaseAddress = new Uri("https://localhost:7252;http://localhost:5252"); httpClient.BaseAddress = new Uri("https://localhost:7252;http://localhost:5252");
} }
public async Task<IEnumerable<ChampionDto>> GetChampion() public async Task<IEnumerable<ChampionDto>> GetChampion(int index, int count)
{ {
var champions = await _httpClient.GetFromJsonAsync<IEnumerable<ChampionDto>>(ApiChampions); var url = $"{ApiChampions}?index={index}&count={count}";
return champions; return await _httpClient.GetFromJsonAsync<IEnumerable<ChampionDto>>(url);
} }
public async void Add(ChampionDto champion) public async void Add(ChampionDto champion)
{ {

@ -1,2 +1,38 @@
// See https://aka.ms/new-console-template for more information // See https://aka.ms/new-console-template for more information
using Client;
using DTO;
Console.WriteLine("Hello, World!"); Console.WriteLine("Hello, World!");
var championClient = new ChampionHttpClient(new HttpClient());
// Get all champions
var champions = await championClient.GetChampion(0,6);
Console.WriteLine("All champions:");
foreach (var champion in champions)
{
Console.WriteLine($"{champion.Name} ({champion.Bio})");
}
/*// Add a new champion
var newChampion = new ChampionDto { Name = "Akali", Role = "Assassin" };
championClient.Add(newChampion);
// Delete a champion
var championToDelete = champions.FirstOrDefault(c => c.Name == "Riven");
if (championToDelete != null)
{
championClient.Delete(championToDelete);
Console.WriteLine($"{championToDelete.Name} deleted.");
}
// Update a champion
var championToUpdate = champions.FirstOrDefault(c => c.Name == "Ashe");
if (championToUpdate != null)
{
championToUpdate.Role = "Marksman";
championClient.Update(championToUpdate);
Console.WriteLine($"{championToUpdate.Name} updated.");
}
*/

@ -8,6 +8,7 @@
public string Icon { get; set; } public string Icon { get; set; }
public LargeImageDto Image { get; set; } public LargeImageDto Image { get; set; }
public IEnumerable<SkinDto> Skins { get; set; } public IEnumerable<SkinDto> Skins { get; set; }
public IEnumerable<SkillDto> Skills { get; set; }
} }
} }

@ -10,5 +10,6 @@ namespace DTO
{ {
public string Name { get; set; } public string Name { get; set; }
public string Description { get; set; } public string Description { get; set; }
public SkillTypeDto Type { get; set; }
} }
} }

@ -4,7 +4,7 @@ using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace DTO.enums namespace DTO
{ {
public enum SkillTypeDto public enum SkillTypeDto
{ {

Loading…
Cancel
Save