新增處室與身份管理控制器

新增 `DepartmentController` 和 `RoleController`,提供 API 端點以管理處室和身份資料。每個控制器實作 `GetAllAsync` 方法,能異步獲取所有資料並處理錯誤,返回適當的 HTTP 狀態碼及錯誤訊息。
This commit is contained in:
Chen, Chun-Yi 2025-09-30 14:52:40 +08:00
parent b3edd27cb8
commit 5351d7f782
2 changed files with 98 additions and 0 deletions

View File

@ -0,0 +1,49 @@
using Microsoft.AspNetCore.Mvc;
using XisongSpaceBooking_BackEnd.Models.DTOs;
using XisongSpaceBooking_BackEnd.Services;
namespace XisongSpaceBooking_BackEnd.Controllers
{
/// <summary>
/// 處室管理 API 控制器
/// </summary>
[ApiController]
[Route("api/[controller]")]
public class DepartmentController : ControllerBase
{
private readonly IDepartmentService _departmentService;
/// <summary>
/// 建構函式
/// </summary>
/// <param name="departmentService">處室業務邏輯服務</param>
public DepartmentController(IDepartmentService departmentService)
{
_departmentService = departmentService ?? throw new ArgumentNullException(nameof(departmentService));
}
/// <summary>
/// 取得所有處室資料
/// </summary>
/// <returns>所有處室資料的集合</returns>
/// <response code="200">成功取得處室資料列表</response>
/// <response code="500">伺服器內部錯誤</response>
[HttpGet("GetAll")]
[ProducesResponseType(typeof(IEnumerable<DepartmentDto>), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<ActionResult<IEnumerable<DepartmentDto>>> GetAllAsync()
{
try
{
var departments = await _departmentService.GetAllAsync();
return Ok(departments);
}
catch (Exception ex)
{
// 記錄錯誤(這裡可以加入日誌記錄)
return StatusCode(StatusCodes.Status500InternalServerError,
new { message = "取得處室資料時發生錯誤", error = ex.Message });
}
}
}
}

View File

@ -0,0 +1,49 @@
using Microsoft.AspNetCore.Mvc;
using XisongSpaceBooking_BackEnd.Models.DTOs;
using XisongSpaceBooking_BackEnd.Services;
namespace XisongSpaceBooking_BackEnd.Controllers
{
/// <summary>
/// 身份管理 API 控制器
/// </summary>
[ApiController]
[Route("api/[controller]")]
public class RoleController : ControllerBase
{
private readonly IRoleService _roleService;
/// <summary>
/// 建構函式
/// </summary>
/// <param name="roleService">身份業務邏輯服務</param>
public RoleController(IRoleService roleService)
{
_roleService = roleService ?? throw new ArgumentNullException(nameof(roleService));
}
/// <summary>
/// 取得所有身份資料
/// </summary>
/// <returns>所有身份資料的集合</returns>
/// <response code="200">成功取得身份資料列表</response>
/// <response code="500">伺服器內部錯誤</response>
[HttpGet("GetAll")]
[ProducesResponseType(typeof(IEnumerable<RoleDto>), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<ActionResult<IEnumerable<RoleDto>>> GetAllAsync()
{
try
{
var roles = await _roleService.GetAllAsync();
return Ok(roles);
}
catch (Exception ex)
{
// 記錄錯誤(這裡可以加入日誌記錄)
return StatusCode(StatusCodes.Status500InternalServerError,
new { message = "取得身份資料時發生錯誤", error = ex.Message });
}
}
}
}