91 lines
2.8 KiB
C#
91 lines
2.8 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using TaxBaik.Application.Services;
|
|
using TaxBaik.Application.DTOs;
|
|
|
|
namespace TaxBaik.Web.Controllers;
|
|
|
|
[ApiController]
|
|
[Route("api/[controller]")]
|
|
public class BlogController : ControllerBase
|
|
{
|
|
private readonly BlogService _blogService;
|
|
|
|
public BlogController(BlogService blogService)
|
|
{
|
|
_blogService = blogService;
|
|
}
|
|
|
|
[HttpGet]
|
|
public async Task<IActionResult> GetPublished([FromQuery] int page = 1, [FromQuery] int pageSize = 12)
|
|
{
|
|
var (items, total) = await _blogService.GetPublishedPagedAsync(page, pageSize);
|
|
return Ok(new { data = items, total, page, pageSize });
|
|
}
|
|
|
|
[HttpGet("{slug}")]
|
|
public async Task<IActionResult> GetBySlug(string slug)
|
|
{
|
|
var post = await _blogService.GetBySlugAsync(slug);
|
|
if (post == null)
|
|
return NotFound(new ProblemDetails { Title = "포스트를 찾을 수 없습니다.", Status = StatusCodes.Status404NotFound });
|
|
return Ok(post);
|
|
}
|
|
|
|
[HttpGet("admin/all")]
|
|
[Authorize]
|
|
public async Task<IActionResult> GetAll()
|
|
{
|
|
var posts = await _blogService.GetAllAsync();
|
|
return Ok(posts);
|
|
}
|
|
|
|
[HttpGet("admin")]
|
|
[Authorize]
|
|
public async Task<IActionResult> GetAdminPaged([FromQuery] int page = 1, [FromQuery] int pageSize = 20)
|
|
{
|
|
var (items, total) = await _blogService.GetAdminPagedAsync(page, pageSize);
|
|
return Ok(new { data = items, total, page, pageSize });
|
|
}
|
|
|
|
[HttpPost]
|
|
[Authorize]
|
|
public async Task<IActionResult> Create([FromBody] CreateBlogPostDto dto)
|
|
{
|
|
try
|
|
{
|
|
var result = await _blogService.CreateAsync(dto);
|
|
return CreatedAtAction(nameof(GetBySlug), new { slug = result.Slug }, result);
|
|
}
|
|
catch (ValidationException ex)
|
|
{
|
|
return BadRequest(new ProblemDetails { Title = ex.Message, Status = StatusCodes.Status400BadRequest });
|
|
}
|
|
}
|
|
|
|
[HttpPut("{id}")]
|
|
[Authorize]
|
|
public async Task<IActionResult> Update(int id, [FromBody] CreateBlogPostDto dto)
|
|
{
|
|
try
|
|
{
|
|
var result = await _blogService.UpdateAsync(id, dto);
|
|
if (result == null)
|
|
return NotFound(new ProblemDetails { Title = "포스트를 찾을 수 없습니다.", Status = StatusCodes.Status404NotFound });
|
|
return Ok(result);
|
|
}
|
|
catch (ValidationException ex)
|
|
{
|
|
return BadRequest(new ProblemDetails { Title = ex.Message, Status = StatusCodes.Status400BadRequest });
|
|
}
|
|
}
|
|
|
|
[HttpDelete("{id}")]
|
|
[Authorize]
|
|
public async Task<IActionResult> Delete(int id)
|
|
{
|
|
await _blogService.DeleteAsync(id);
|
|
return NoContent();
|
|
}
|
|
}
|