2026-06-06 21:30:41 +01:00
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2023-03-19 17:57:55 +00:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2026-08-03 01:32:59 +01:00
|
|
|
using Yavsc.Blogspot;
|
2019-01-01 16:28:47 +00:00
|
|
|
using Yavsc.Models.Blog;
|
2026-06-10 00:23:17 +01:00
|
|
|
using Yavsc.Server.Exceptions;
|
2025-07-15 19:43:41 +01:00
|
|
|
using Yavsc.Server.Helpers;
|
2026-06-20 18:43:01 +01:00
|
|
|
using static Yavsc.Blogs.Constants;
|
2019-01-01 16:28:47 +00:00
|
|
|
|
2026-06-10 16:59:23 +01:00
|
|
|
namespace Yavsc.Blogs.Controllers
|
2019-01-01 16:28:47 +00:00
|
|
|
{
|
2026-06-06 21:30:41 +01:00
|
|
|
[Authorize("BlogScope")]
|
2019-01-01 16:28:47 +00:00
|
|
|
[Produces("application/json")]
|
2026-06-20 18:43:01 +01:00
|
|
|
[Route(APIPrefix + "/blog")]
|
2019-01-01 16:28:47 +00:00
|
|
|
public class BlogApiController : Controller
|
|
|
|
|
{
|
2026-06-10 00:23:17 +01:00
|
|
|
private readonly BlogSpotService blogSpotService;
|
2019-01-01 16:28:47 +00:00
|
|
|
|
2026-06-10 00:23:17 +01:00
|
|
|
public BlogApiController(BlogSpotService blogSpotService)
|
2019-01-01 16:28:47 +00:00
|
|
|
{
|
2026-06-10 00:23:17 +01:00
|
|
|
this.blogSpotService = blogSpotService;
|
2019-01-01 16:28:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GET: api/BlogApi
|
|
|
|
|
[HttpGet]
|
2026-08-05 21:11:22 +01:00
|
|
|
public async Task<IEnumerable<IBlogPost>> GetBlogspot(int start = 0, int take = 25)
|
2019-01-01 16:28:47 +00:00
|
|
|
{
|
2026-08-05 21:11:22 +01:00
|
|
|
return await blogSpotService.Index(User, null, start, take);
|
2019-01-01 16:28:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// GET: api/BlogApi/5
|
|
|
|
|
[HttpGet("{id}", Name = "GetBlog")]
|
2026-06-10 00:23:17 +01:00
|
|
|
public async Task<IActionResult> GetBlog([FromRoute] long id)
|
2019-01-01 16:28:47 +00:00
|
|
|
{
|
|
|
|
|
if (!ModelState.IsValid)
|
|
|
|
|
{
|
2023-03-19 17:57:55 +00:00
|
|
|
return BadRequest(ModelState);
|
2019-01-01 16:28:47 +00:00
|
|
|
}
|
|
|
|
|
|
2026-06-10 00:23:17 +01:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var blog = await blogSpotService.Details(User, id);
|
|
|
|
|
if (blog == null)
|
|
|
|
|
{
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
2019-01-01 16:28:47 +00:00
|
|
|
|
2026-06-10 00:23:17 +01:00
|
|
|
return Ok(blog);
|
|
|
|
|
}
|
|
|
|
|
catch (AuthorizationFailureException)
|
2019-01-01 16:28:47 +00:00
|
|
|
{
|
2026-06-10 00:23:17 +01:00
|
|
|
return Challenge();
|
2019-01-01 16:28:47 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// PUT: api/BlogApi/5
|
|
|
|
|
[HttpPut("{id}")]
|
2026-06-10 00:23:17 +01:00
|
|
|
public async Task<IActionResult> PutBlog(long id, [FromBody] BlogPost blog)
|
2019-01-01 16:28:47 +00:00
|
|
|
{
|
|
|
|
|
if (!ModelState.IsValid)
|
|
|
|
|
{
|
2023-03-19 17:57:55 +00:00
|
|
|
return BadRequest(ModelState);
|
2019-01-01 16:28:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (id != blog.Id)
|
|
|
|
|
{
|
2023-03-19 17:57:55 +00:00
|
|
|
return BadRequest();
|
2019-01-01 16:28:47 +00:00
|
|
|
}
|
|
|
|
|
|
2026-06-10 00:23:17 +01:00
|
|
|
var existing = await blogSpotService.GetBlogPostAsync(id);
|
|
|
|
|
if (existing == null)
|
|
|
|
|
{
|
|
|
|
|
return NotFound();
|
|
|
|
|
}
|
2019-01-01 16:28:47 +00:00
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2026-06-10 00:23:17 +01:00
|
|
|
await blogSpotService.Modify(User, blog);
|
2019-01-01 16:28:47 +00:00
|
|
|
}
|
2026-06-10 00:23:17 +01:00
|
|
|
catch (AuthorizationFailureException)
|
2019-01-01 16:28:47 +00:00
|
|
|
{
|
2026-06-10 00:23:17 +01:00
|
|
|
return Challenge();
|
2019-01-01 16:28:47 +00:00
|
|
|
}
|
|
|
|
|
|
2023-03-19 17:57:55 +00:00
|
|
|
return new StatusCodeResult(StatusCodes.Status204NoContent);
|
2019-01-01 16:28:47 +00:00
|
|
|
}
|
|
|
|
|
|
2026-07-12 01:17:52 +01:00
|
|
|
// POST: api/v1/blog
|
2019-01-01 16:28:47 +00:00
|
|
|
[HttpPost]
|
|
|
|
|
public IActionResult PostBlog([FromBody] Models.Blog.BlogPost blog)
|
|
|
|
|
{
|
|
|
|
|
if (!ModelState.IsValid)
|
|
|
|
|
{
|
2023-03-19 17:57:55 +00:00
|
|
|
return BadRequest(ModelState);
|
2019-01-01 16:28:47 +00:00
|
|
|
}
|
|
|
|
|
|
2026-07-06 22:03:41 +01:00
|
|
|
// The BlogSpotService.Create() signature requires an
|
|
|
|
|
// IFormFileCollection for file uploads. Reading
|
|
|
|
|
// Request.Form.Files when the request is a plain JSON
|
|
|
|
|
// body (e.g. from PostIt) throws
|
|
|
|
|
// "This request does not have a Content-Type header.
|
|
|
|
|
// Forms are available from requests with bodies like
|
|
|
|
|
// POSTs and a form Content-Type of either
|
|
|
|
|
// application/x-www-form-urlencoded or
|
|
|
|
|
// multipart/form-data."
|
|
|
|
|
//
|
|
|
|
|
// Two valid use cases for this endpoint:
|
|
|
|
|
// 1. JSON body only (no files) — PostIt path.
|
|
|
|
|
// 2. multipart/form-data with a 'blog' field + 0..N
|
|
|
|
|
// files — future browser / server-rendered path.
|
|
|
|
|
//
|
|
|
|
|
// Branch on HasFormContentType: pass the form files when
|
|
|
|
|
// present, pass an empty collection otherwise. The
|
|
|
|
|
// FileSystem branch in BlogSpotService.Create then
|
|
|
|
|
// short-circuits to "no files to handle".
|
|
|
|
|
var files = Request.HasFormContentType
|
|
|
|
|
? Request.Form.Files
|
|
|
|
|
: (IFormFileCollection)new FormFileCollection();
|
2026-08-02 23:02:54 +01:00
|
|
|
var uid = User.GetUserId();
|
|
|
|
|
var post = blogSpotService.Create(uid, blog, files);
|
2026-06-10 00:23:17 +01:00
|
|
|
return CreatedAtRoute("GetBlog", new { id = post.Id }, post);
|
2019-01-01 16:28:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// DELETE: api/BlogApi/5
|
|
|
|
|
[HttpDelete("{id}")]
|
2026-06-10 00:23:17 +01:00
|
|
|
public async Task<IActionResult> DeleteBlog(long id)
|
2019-01-01 16:28:47 +00:00
|
|
|
{
|
|
|
|
|
if (!ModelState.IsValid)
|
|
|
|
|
{
|
2023-03-19 17:57:55 +00:00
|
|
|
return BadRequest(ModelState);
|
2019-01-01 16:28:47 +00:00
|
|
|
}
|
|
|
|
|
|
2026-06-10 00:23:17 +01:00
|
|
|
var blog = await blogSpotService.GetBlogPostAsync(id);
|
2019-01-01 16:28:47 +00:00
|
|
|
if (blog == null)
|
|
|
|
|
{
|
2023-03-19 17:57:55 +00:00
|
|
|
return NotFound();
|
2019-01-01 16:28:47 +00:00
|
|
|
}
|
|
|
|
|
|
2026-06-10 00:23:17 +01:00
|
|
|
await blogSpotService.Delete(User, id);
|
2019-01-01 16:28:47 +00:00
|
|
|
return Ok(blog);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Dispose(bool disposing)
|
|
|
|
|
{
|
|
|
|
|
base.Dispose(disposing);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-18 09:42:50 +01:00
|
|
|
}
|