From 5351d7f782132fdfaa0de74afa07f10017c428b4 Mon Sep 17 00:00:00 2001 From: Chunyi Date: Tue, 30 Sep 2025 14:52:40 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E8=99=95=E5=AE=A4=E8=88=87?= =?UTF-8?q?=E8=BA=AB=E4=BB=BD=E7=AE=A1=E7=90=86=E6=8E=A7=E5=88=B6=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增 `DepartmentController` 和 `RoleController`,提供 API 端點以管理處室和身份資料。每個控制器實作 `GetAllAsync` 方法,能異步獲取所有資料並處理錯誤,返回適當的 HTTP 狀態碼及錯誤訊息。 --- .../Controllers/DepartmentController.cs | 49 +++++++++++++++++++ .../Controllers/RoleController.cs | 49 +++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 XisongSpaceBooking_BackEnd/Controllers/DepartmentController.cs create mode 100644 XisongSpaceBooking_BackEnd/Controllers/RoleController.cs diff --git a/XisongSpaceBooking_BackEnd/Controllers/DepartmentController.cs b/XisongSpaceBooking_BackEnd/Controllers/DepartmentController.cs new file mode 100644 index 0000000..1e9fc67 --- /dev/null +++ b/XisongSpaceBooking_BackEnd/Controllers/DepartmentController.cs @@ -0,0 +1,49 @@ +using Microsoft.AspNetCore.Mvc; +using XisongSpaceBooking_BackEnd.Models.DTOs; +using XisongSpaceBooking_BackEnd.Services; + +namespace XisongSpaceBooking_BackEnd.Controllers +{ + /// + /// 處室管理 API 控制器 + /// + [ApiController] + [Route("api/[controller]")] + public class DepartmentController : ControllerBase + { + private readonly IDepartmentService _departmentService; + + /// + /// 建構函式 + /// + /// 處室業務邏輯服務 + public DepartmentController(IDepartmentService departmentService) + { + _departmentService = departmentService ?? throw new ArgumentNullException(nameof(departmentService)); + } + + /// + /// 取得所有處室資料 + /// + /// 所有處室資料的集合 + /// 成功取得處室資料列表 + /// 伺服器內部錯誤 + [HttpGet("GetAll")] + [ProducesResponseType(typeof(IEnumerable), StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] + public async Task>> GetAllAsync() + { + try + { + var departments = await _departmentService.GetAllAsync(); + return Ok(departments); + } + catch (Exception ex) + { + // 記錄錯誤(這裡可以加入日誌記錄) + return StatusCode(StatusCodes.Status500InternalServerError, + new { message = "取得處室資料時發生錯誤", error = ex.Message }); + } + } + } +} diff --git a/XisongSpaceBooking_BackEnd/Controllers/RoleController.cs b/XisongSpaceBooking_BackEnd/Controllers/RoleController.cs new file mode 100644 index 0000000..acc1c55 --- /dev/null +++ b/XisongSpaceBooking_BackEnd/Controllers/RoleController.cs @@ -0,0 +1,49 @@ +using Microsoft.AspNetCore.Mvc; +using XisongSpaceBooking_BackEnd.Models.DTOs; +using XisongSpaceBooking_BackEnd.Services; + +namespace XisongSpaceBooking_BackEnd.Controllers +{ + /// + /// 身份管理 API 控制器 + /// + [ApiController] + [Route("api/[controller]")] + public class RoleController : ControllerBase + { + private readonly IRoleService _roleService; + + /// + /// 建構函式 + /// + /// 身份業務邏輯服務 + public RoleController(IRoleService roleService) + { + _roleService = roleService ?? throw new ArgumentNullException(nameof(roleService)); + } + + /// + /// 取得所有身份資料 + /// + /// 所有身份資料的集合 + /// 成功取得身份資料列表 + /// 伺服器內部錯誤 + [HttpGet("GetAll")] + [ProducesResponseType(typeof(IEnumerable), StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status500InternalServerError)] + public async Task>> GetAllAsync() + { + try + { + var roles = await _roleService.GetAllAsync(); + return Ok(roles); + } + catch (Exception ex) + { + // 記錄錯誤(這裡可以加入日誌記錄) + return StatusCode(StatusCodes.Status500InternalServerError, + new { message = "取得身份資料時發生錯誤", error = ex.Message }); + } + } + } +}