yavsc/src/Yavsc.Blogs/Controllers/BlogApiController.cs

126 lines
3.3 KiB
C#
Raw Normal View History

2026-06-06 21:30:41 +01:00
using Microsoft.AspNetCore.Authorization;
2026-06-10 00:23:17 +01:00
using Microsoft.AspNetCore.Http;
2023-03-19 17:57:55 +00:00
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
2019-01-01 16:28:47 +00:00
using Yavsc.Models;
using Yavsc.Models.Blog;
2026-06-10 00:23:17 +01:00
using Yavsc.Server.Exceptions;
using Yavsc.Server.Helpers;
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")]
[Route("api/blog")]
2019-05-18 09:42:50 +01:00
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-06-10 00:23:17 +01:00
public async Task<IEnumerable<IBlogPost>> GetBlogspot(int start = 0, int take = 25)
2019-01-01 16:28:47 +00:00
{
2026-06-10 00:23:17 +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
}
// POST: api/BlogApi
[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-06-10 00:23:17 +01:00
var post = blogSpotService.Create(User.GetUserId(), blog, Request.Form.Files);
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
}