136 lines
4.1 KiB
C#
136 lines
4.1 KiB
C#
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.Logging;
|
|
using MongoDB.Driver;
|
|
using Newtonsoft.Json;
|
|
using Robware.Api.Blog.Models;
|
|
using Robware.Blog;
|
|
using Robware.Data;
|
|
|
|
namespace Robware.Api.Blog.Controllers {
|
|
[ApiController]
|
|
public class BlogController : ControllerBase {
|
|
private const int DefaultPostLimit = 10;
|
|
|
|
private readonly ILogger<BlogController> _logger;
|
|
private readonly IBlogRepository _blogRepository;
|
|
private readonly IDatabaseProvider _dbProvider;
|
|
|
|
public BlogController(ILogger<BlogController> logger, IBlogRepository blogRepository, IDatabaseProvider dbProvider) {
|
|
_logger = logger;
|
|
_blogRepository = blogRepository;
|
|
_dbProvider = dbProvider;
|
|
}
|
|
|
|
[HttpGet(nameof(Get) + "/{url}")]
|
|
public async Task<ActionResult<BlogPost>> Get(string url) {
|
|
_logger.Log(LogLevel.Information, $"{nameof(Get)}: {nameof(url)}={url}");
|
|
|
|
try {
|
|
if (int.TryParse(url, out int id)) {
|
|
return await _blogRepository.GetPostByIdAsync(id);
|
|
}
|
|
|
|
return await _blogRepository.GetPostByUrlAsync(url);
|
|
}
|
|
catch (ItemNotFoundException e) {
|
|
_logger.Log(LogLevel.Error, e.Message);
|
|
return NotFound("Could not find blog post");
|
|
}
|
|
}
|
|
|
|
[HttpGet(nameof(GetLatestPosts))]
|
|
public async Task<ActionResult<BlogPost[]>> GetLatestPosts(int count = 0, int offset = 0) {
|
|
_logger.Log(LogLevel.Information, $"{nameof(GetLatestPosts)}: {nameof(count)}={count}, {nameof(offset)}={offset}");
|
|
|
|
if (count <= 0)
|
|
count = DefaultPostLimit;
|
|
|
|
try {
|
|
return (await _blogRepository.GetLatestPostsAsync(count, offset)).ToArray();
|
|
}
|
|
catch (ItemNotFoundException e) {
|
|
_logger.Log(LogLevel.Error, e.Message);
|
|
return NotFound("Could not get latest posts with given parameters");
|
|
}
|
|
}
|
|
|
|
[HttpGet(nameof(GetLatestPost))]
|
|
public async Task<ActionResult<BlogPost>> GetLatestPost() {
|
|
_logger.Log(LogLevel.Information, $"{nameof(GetLatestPost)}");
|
|
try {
|
|
return await _blogRepository.GetLatestPostAsync();
|
|
}
|
|
catch (ItemNotFoundException e) {
|
|
_logger.Log(LogLevel.Error, e.Message);
|
|
return NotFound("Could not find blog post");
|
|
}
|
|
}
|
|
|
|
[HttpGet(nameof(GetCount))]
|
|
public async Task<ActionResult<int>> GetCount() {
|
|
_logger.Log(LogLevel.Information, $"{nameof(GetCount)}");
|
|
try {
|
|
return await _blogRepository.GetCountAsync();
|
|
}
|
|
catch (ItemNotFoundException e) {
|
|
_logger.Log(LogLevel.Error, e.Message);
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
[HttpPost(nameof(SavePost))]
|
|
public async Task<ActionResult<BlogPost>> 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);
|
|
|
|
return await _blogRepository.SavePost(post);
|
|
}
|
|
catch (ItemNotFoundException e) {
|
|
_logger.Log(LogLevel.Error, e.Message);
|
|
return BadRequest("Tried to update post that doesn't exist");
|
|
}
|
|
}
|
|
|
|
[HttpGet(nameof(GetAllPosts))]
|
|
public async Task<ActionResult<BlogPost[]>> GetAllPosts() => (await _blogRepository.GetAllPostsAsync()).ToArray();
|
|
|
|
[HttpPost(nameof(DeletePost))]
|
|
public async Task<ActionResult> DeletePost([FromBody] int id) {
|
|
await _blogRepository.DeletePostAsync(id);
|
|
return Ok();
|
|
}
|
|
|
|
[HttpPost(nameof(PublishPost))]
|
|
public async Task<ActionResult> PublishPost([FromBody] int id) {
|
|
try {
|
|
var post = await _blogRepository.GetPostByIdAsync(id);
|
|
post.Publish();
|
|
await _blogRepository.SavePost(post);
|
|
return Ok();
|
|
}
|
|
catch (ItemNotFoundException e) {
|
|
_logger.Log(LogLevel.Error, e.Message);
|
|
return BadRequest("Tried to publish post that doesn't exist");
|
|
}
|
|
}
|
|
|
|
[HttpGet(nameof(Migrate))]
|
|
public async Task<ActionResult> Migrate() {
|
|
var mysqlRepo = new Data.BlogRepository(_dbProvider);
|
|
|
|
var existingPosts = await mysqlRepo.GetAllPostsAsync();
|
|
foreach (var post in existingPosts)
|
|
await _blogRepository.SavePost(post);
|
|
|
|
return Ok();
|
|
}
|
|
}
|
|
}
|