Pokespearean/Pokespearean/Controllers/PokemonController.cs

58 lines
1.4 KiB
C#

using Microsoft.AspNetCore.Mvc;
using Pokespearean.Models.Generic;
using Pokespearean.Models.Pokemon;
using Pokespearean.Services;
using System;
using System.ComponentModel.DataAnnotations;
using System.Threading.Tasks;
namespace Pokespearean.Controllers
{
[ApiController]
[Route("[controller]")]
public class PokemonController : ControllerBase
{
private readonly IPokemonService PokemonService;
private readonly IShakespeareService ShakespeareService;
public PokemonController(IPokemonService pokemonService,
IShakespeareService shakespeareService)
{
PokemonService = pokemonService;
ShakespeareService = shakespeareService;
}
[Route("{pokemonName}")]
public async Task<IActionResult> Get([FromRoute, Required] string pokemonName)
{
var result = new WebResult();
try
{
result = await PokemonService.GetPokemonDescription(pokemonName);
if (!result.IsValid)
return BadRequest(result);
var pokemonDescription = result.Data as string;
result = await ShakespeareService.ToShakespearean(pokemonDescription);
if (!result.IsValid)
return BadRequest(result);
var shakespeareTranslation = result.Data as string;
var pokeResult = new PokeResult
{
Name = pokemonName,
Description = shakespeareTranslation
};
return Ok(pokeResult);
}
catch (Exception ex)
{
return BadRequest(result.Invalidate(ex.Message, ex));
}
}
}
}