59 lines
1.5 KiB
C#
59 lines
1.5 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using MilliSim.Common.Dtos;
|
|
using MilliSim.Common.Models;
|
|
using MilliSim.Services;
|
|
|
|
namespace MilliSim.Controllers;
|
|
|
|
[ApiController]
|
|
[Route("api/categories")]
|
|
public class CategoriesController : ControllerBase
|
|
{
|
|
private readonly IProjectService _projectService;
|
|
|
|
public CategoriesController(IProjectService projectService)
|
|
{
|
|
_projectService = projectService;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 分类列表
|
|
/// </summary>
|
|
[HttpGet]
|
|
public async Task<ApiResponse<List<CategoryDto>>> GetCategories()
|
|
{
|
|
return await _projectService.GetCategoriesAsync();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 创建分类
|
|
/// </summary>
|
|
[HttpPost]
|
|
[Authorize(Policy = "Admin")]
|
|
public async Task<ApiResponse<CategoryDto>> CreateCategory([FromBody] CreateCategoryRequest request)
|
|
{
|
|
return await _projectService.CreateCategoryAsync(request);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 更新分类
|
|
/// </summary>
|
|
[HttpPost("{id:long}")]
|
|
[Authorize(Policy = "Admin")]
|
|
public async Task<ApiResponse<CategoryDto>> UpdateCategory(long id, [FromBody] CreateCategoryRequest request)
|
|
{
|
|
return await _projectService.UpdateCategoryAsync(id, request);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除分类
|
|
/// </summary>
|
|
[HttpPost("{id:long}/delete")]
|
|
[Authorize(Policy = "Admin")]
|
|
public async Task<ApiResponse> DeleteCategory(long id)
|
|
{
|
|
return await _projectService.DeleteCategoryAsync(id);
|
|
}
|
|
}
|