Ajout des controllers (endpoints)

master
Thomas Chazot 2 years ago
parent c6e7171d0a
commit f37063b548

@ -7,7 +7,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" /> <PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup> </ItemGroup>
</Project> </Project>

@ -0,0 +1,15 @@
using System;
namespace ApiLol
{
public class ChampionDTO
{
public int Id { get; set; }
public string Name { get; set; }
public string Icon { get; set; }
public string Bio { get; set; }
}
}

@ -0,0 +1,98 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
namespace ApiLol.Controllers
{
[Route("api/[controller]")]
public class ChampionController : ControllerBase
{
private readonly ILogger<ChampionController> _logger;
public ChampionController(ILogger<ChampionController> logger)
{
_logger = logger;
}
// GET: api/champion
[HttpGet]
public ActionResult<IEnumerable<ChampionDTO>> Get()
{
List<ChampionDTO> champions = new List<ChampionDTO>();
champions.Add(new ChampionDTO
{
Id = 1,
Name = "Aurel",
Bio = "Trop joli",
Icon = "Moi"
});
champions.Add(new ChampionDTO
{
Id = 2,
Name = "Mathilde",
Bio = "Elle est reloue",
Icon = "Macheval"
});
return Ok(champions);
}
// GET api/values/5
[HttpGet("{id}")]
public ActionResult<ChampionDTO?> Get(int id)
{
List<ChampionDTO> champions = new List<ChampionDTO>();
champions.Add(new ChampionDTO
{
Id = 1,
Name = "Aurel",
Bio = "Trop joli",
Icon = "Moi"
});
champions.Add(new ChampionDTO
{
Id = 2,
Name = "Mathilde",
Bio = "Elle est reloue",
Icon = "Macheval"
});
ChampionDTO? champion = champions.SingleOrDefault((ChampionDTO arg) => arg.Id == id);
if (champion == null)
{
return BadRequest();
}
return Ok(champion);
}
// POST api/values
[HttpPost]
public void Post([FromBody]int id, string name, string bio, string icon)
{
if (string.IsNullOrWhiteSpace(name) || string.IsNullOrWhiteSpace(bio) || string.IsNullOrWhiteSpace(icon)){
BadRequest();
}
else
{
Ok();
}
}
// PUT api/values/5
[HttpPut("{id}")]
public void Put(int id, [FromBody]string value)
{
}
// DELETE api/values/5
[HttpDelete("{id}")]
public void Delete(int id)
{
}
}
}

@ -19,7 +19,7 @@ public class WeatherForecastController : ControllerBase
} }
[HttpGet(Name = "GetWeatherForecast")] [HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get() public IEnumerable<WeatherForecast> Get()
{ {
return Enumerable.Range(1, 5).Select(index => new WeatherForecast return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{ {

@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<None Remove="Microsoft.AspNetCore.Http" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Http" Version="2.2.2" />
</ItemGroup>
</Project>

@ -0,0 +1,12 @@
// See https://aka.ms/new-console-template for more information
using static System.Net.WebRequestMethods;
HttpClient http = new HttpClient();
//http.BaseAddress = new Uri("https://localhost:7006/");
//Console.WriteLine(await http.GetStringAsync("https://localhost:7006/api/Champion"));
Console.WriteLine(await http.GetStringAsync("https://localhost:7006/api/Champion/1"));

@ -23,6 +23,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DataManagers", "DataManager
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ApiLol", "ApiLol\ApiLol.csproj", "{51E7E6BF-95E8-4B0E-BB03-330893931C17}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ApiLol", "ApiLol\ApiLol.csproj", "{51E7E6BF-95E8-4B0E-BB03-330893931C17}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApi", "ConsoleApi\ConsoleApi.csproj", "{A3656054-1143-4235-A511-03CBD3BBCE8E}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@ -61,6 +63,10 @@ Global
{51E7E6BF-95E8-4B0E-BB03-330893931C17}.Debug|Any CPU.Build.0 = Debug|Any CPU {51E7E6BF-95E8-4B0E-BB03-330893931C17}.Debug|Any CPU.Build.0 = Debug|Any CPU
{51E7E6BF-95E8-4B0E-BB03-330893931C17}.Release|Any CPU.ActiveCfg = Release|Any CPU {51E7E6BF-95E8-4B0E-BB03-330893931C17}.Release|Any CPU.ActiveCfg = Release|Any CPU
{51E7E6BF-95E8-4B0E-BB03-330893931C17}.Release|Any CPU.Build.0 = Release|Any CPU {51E7E6BF-95E8-4B0E-BB03-330893931C17}.Release|Any CPU.Build.0 = Release|Any CPU
{A3656054-1143-4235-A511-03CBD3BBCE8E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A3656054-1143-4235-A511-03CBD3BBCE8E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A3656054-1143-4235-A511-03CBD3BBCE8E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A3656054-1143-4235-A511-03CBD3BBCE8E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

Loading…
Cancel
Save