Save a new post

This commit is contained in:
Robert Marshall 2020-04-10 13:05:10 +01:00
parent 71bddddc13
commit a85c12f91c
5 changed files with 101 additions and 3 deletions

View file

@ -3,6 +3,8 @@ using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Robware.Api.Blog.Models;
using Robware.Blog;
using Robware.Data;
@ -74,8 +76,24 @@ namespace Robware.Api.Blog.Controllers {
}
}
//[HttpPost]
//Task<BlogPost> SavePost(BlogPost post);
[HttpPost(nameof(SavePost))]
public async Task<ActionResult> SavePost(BlogPostSubmission submission) {
_logger.Log(LogLevel.Information, $"{nameof(SavePost)}: {nameof(submission)}={JsonConvert.SerializeObject(submission)}");
try {
var post = submission.Id.HasValue ? await _blogRepository.GetPostByIdAsync(submission.Id.Value) : new BlogPost();
post.UpdateTitle(submission.Title);
post.UpdateDraft(submission.Content);
await _blogRepository.SavePost(post);
return Ok();
}
catch (ItemNotFoundException e) {
_logger.Log(LogLevel.Error, e.Message);
return BadRequest("Tried to update post that doesn't exist");
}
}
//[HttpGet]
//Task<IEnumerable<BlogPost>> GetAllPosts();