Add project files.
This commit is contained in:
45
Controllers/PokemonController.cs
Normal file
45
Controllers/PokemonController.cs
Normal file
@ -0,0 +1,45 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using PokeApiNet;
|
||||
using Pokespearean.Models.Generic;
|
||||
using Pokespearean.Models.Pokemon;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Pokespearean.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("[controller]")]
|
||||
public class PokemonController : ControllerBase
|
||||
{
|
||||
private readonly ILogger<PokemonController> _logger;
|
||||
|
||||
public PokemonController(ILogger<PokemonController> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
[Route("{pokemon}")]
|
||||
public async Task<IActionResult> Get([FromRoute] string pokemon = "ho-oh")
|
||||
{
|
||||
var result = new WebResult();
|
||||
try
|
||||
{
|
||||
var client = new PokeApiClient();
|
||||
|
||||
var pokedex = await client.GetResourceAsync<Pokedex>(pokemon);
|
||||
|
||||
return Ok(new PokeResult
|
||||
{
|
||||
Name = pokedex.Name,
|
||||
Description = pokedex.Descriptions.FirstOrDefault(d => d.Language.Name == "en").Description
|
||||
});
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return BadRequest(result.Invalidate(ex.Message, ex));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user