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 _logger; private readonly IBlogRepository _blogRepository; private readonly IDatabaseProvider _dbProvider; public BlogController(ILogger logger, IBlogRepository blogRepository, IDatabaseProvider dbProvider) { _logger = logger; _blogRepository = blogRepository; _dbProvider = dbProvider; } [HttpGet(nameof(Get) + "/{url}")] public async Task> 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> 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> 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> 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> 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> GetAllPosts() => (await _blogRepository.GetAllPostsAsync()).ToArray(); [HttpPost(nameof(DeletePost))] public async Task DeletePost([FromBody] int id) { await _blogRepository.DeletePostAsync(id); return Ok(); } [HttpPost(nameof(PublishPost))] public async Task 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 Migrate() { var mysqlRepo = new Data.BlogRepository(_dbProvider); var existingPosts = await mysqlRepo.GetAllPostsAsync(); foreach (var post in existingPosts) await _blogRepository.SavePost(post); return Ok(); } } }