Add tests for controller. Put in contigency for when an item isn't found.

This commit is contained in:
Robert Marshall 2020-04-10 10:01:27 +01:00
parent a22f8b125c
commit c0c1311a0a
6 changed files with 119 additions and 11 deletions

View file

@ -3,6 +3,7 @@ using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Robware.Blog;
using Robware.Data;
namespace Robware.Api.Blog.Controllers {
[ApiController]
@ -16,14 +17,20 @@ namespace Robware.Api.Blog.Controllers {
}
[HttpGet(nameof(Get) + "/{url}")]
public async Task<BlogPost> Get(string url) {
public async Task<ActionResult<BlogPost>> Get(string url) {
_logger.Log(LogLevel.Information, $"{nameof(Get)}: {nameof(url)}={url}");
if (int.TryParse(url, out int id)) {
return await _blogRepository.GetPostByIdAsync(id);
}
try {
if (int.TryParse(url, out int id)) {
return await _blogRepository.GetPostByIdAsync(id);
}
return await _blogRepository.GetPostByUrlAsync(url);
return await _blogRepository.GetPostByUrlAsync(url);
}
catch (ItemNotFoundException e) {
_logger.Log(LogLevel.Error, e.Message);
return NotFound("Could not find blog post");
}
}
//[HttpGet]