新增 Service 層以處理業務邏輯

在 `Program.cs` 中註冊 `IRoleService` 和 `IDepartmentService` 的相依性注入。
新增 `DepartmentService` 和 `RoleService` 類別,實作相應的介面,並提供取得所有處室及身份資料的功能,使用 AutoMapper 進行物件映射。
定義 `IDepartmentService` 和 `IRoleService` 介面,包含 `GetAllAsync` 方法以取得資料 DTO 集合。
This commit is contained in:
Chen, Chun-Yi 2025-09-30 14:29:42 +08:00
parent 9090c6a1b7
commit b3edd27cb8
5 changed files with 109 additions and 0 deletions

View File

@ -2,6 +2,7 @@ using Microsoft.EntityFrameworkCore;
using Microsoft.OpenApi.Models;
using XisongSpaceBooking_BackEnd.Configurations;
using XisongSpaceBooking_BackEnd.Repositories;
using XisongSpaceBooking_BackEnd.Services;
var builder = WebApplication.CreateBuilder(args);
@ -17,6 +18,10 @@ builder.Services.AddAutoMapper(cfg =>
builder.Services.AddScoped<IRoleRepository, RoleRepository>();
builder.Services.AddScoped<IDepartmentRepository, DepartmentRepository>();
// µù¥U Service ªA°È
builder.Services.AddScoped<IRoleService, RoleService>();
builder.Services.AddScoped<IDepartmentService, DepartmentService>();
// °t¸m Entity Framework »P³s½u¦À
builder.Services.AddDbContextPool<SpaceBookingDbContext>(options =>
{

View File

@ -0,0 +1,36 @@
using AutoMapper;
using XisongSpaceBooking_BackEnd.Models.DTOs;
using XisongSpaceBooking_BackEnd.Repositories;
namespace XisongSpaceBooking_BackEnd.Services
{
/// <summary>
/// 處室業務邏輯實作
/// </summary>
public class DepartmentService : IDepartmentService
{
private readonly IDepartmentRepository _departmentRepository;
private readonly IMapper _mapper;
/// <summary>
/// 建構函式
/// </summary>
/// <param name="departmentRepository">處室資料存取</param>
/// <param name="mapper">物件映射器</param>
public DepartmentService(IDepartmentRepository departmentRepository, IMapper mapper)
{
_departmentRepository = departmentRepository ?? throw new ArgumentNullException(nameof(departmentRepository));
_mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
}
/// <summary>
/// 取得所有處室資料
/// </summary>
/// <returns>所有處室 DTO 的集合</returns>
public async Task<IEnumerable<DepartmentDto>> GetAllAsync()
{
var departments = await _departmentRepository.GetAllAsync();
return _mapper.Map<IEnumerable<DepartmentDto>>(departments);
}
}
}

View File

@ -0,0 +1,16 @@
using XisongSpaceBooking_BackEnd.Models.DTOs;
namespace XisongSpaceBooking_BackEnd.Services
{
/// <summary>
/// 處室業務邏輯介面
/// </summary>
public interface IDepartmentService
{
/// <summary>
/// 取得所有處室資料
/// </summary>
/// <returns>所有處室 DTO 的集合</returns>
Task<IEnumerable<DepartmentDto>> GetAllAsync();
}
}

View File

@ -0,0 +1,16 @@
using XisongSpaceBooking_BackEnd.Models.DTOs;
namespace XisongSpaceBooking_BackEnd.Services
{
/// <summary>
/// 身份業務邏輯介面
/// </summary>
public interface IRoleService
{
/// <summary>
/// 取得所有身份資料
/// </summary>
/// <returns>所有身份 DTO 的集合</returns>
Task<IEnumerable<RoleDto>> GetAllAsync();
}
}

View File

@ -0,0 +1,36 @@
using AutoMapper;
using XisongSpaceBooking_BackEnd.Models.DTOs;
using XisongSpaceBooking_BackEnd.Repositories;
namespace XisongSpaceBooking_BackEnd.Services
{
/// <summary>
/// 身份業務邏輯實作
/// </summary>
public class RoleService : IRoleService
{
private readonly IRoleRepository _roleRepository;
private readonly IMapper _mapper;
/// <summary>
/// 建構函式
/// </summary>
/// <param name="roleRepository">身份資料存取</param>
/// <param name="mapper">物件映射器</param>
public RoleService(IRoleRepository roleRepository, IMapper mapper)
{
_roleRepository = roleRepository ?? throw new ArgumentNullException(nameof(roleRepository));
_mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
}
/// <summary>
/// 取得所有身份資料
/// </summary>
/// <returns>所有身份 DTO 的集合</returns>
public async Task<IEnumerable<RoleDto>> GetAllAsync()
{
var roles = await _roleRepository.GetAllAsync();
return _mapper.Map<IEnumerable<RoleDto>>(roles);
}
}
}