新增 Role 與 Department 資料存取介面及實作
在 `Program.cs` 中,新增對 `IRoleRepository` 和 `IDepartmentRepository` 的依賴注入。 實作 `IDepartmentRepository` 和 `IRoleRepository` 介面,並提供 `GetAllAsync` 方法以非同步取得所有處室及身份資料。 相關命名空間也已引入以支援這些變更。
This commit is contained in:
parent
797e6b5f3c
commit
9090c6a1b7
@ -1,6 +1,7 @@
|
|||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.OpenApi.Models;
|
using Microsoft.OpenApi.Models;
|
||||||
using XisongSpaceBooking_BackEnd.Configurations;
|
using XisongSpaceBooking_BackEnd.Configurations;
|
||||||
|
using XisongSpaceBooking_BackEnd.Repositories;
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
@ -12,6 +13,10 @@ builder.Services.AddAutoMapper(cfg =>
|
|||||||
{
|
{
|
||||||
}, typeof(AutoMappingProfile).Assembly);
|
}, typeof(AutoMappingProfile).Assembly);
|
||||||
|
|
||||||
|
// µù¥U Repository ªA°È
|
||||||
|
builder.Services.AddScoped<IRoleRepository, RoleRepository>();
|
||||||
|
builder.Services.AddScoped<IDepartmentRepository, DepartmentRepository>();
|
||||||
|
|
||||||
// °t¸m Entity Framework »P³s½u¦À
|
// °t¸m Entity Framework »P³s½u¦À
|
||||||
builder.Services.AddDbContextPool<SpaceBookingDbContext>(options =>
|
builder.Services.AddDbContextPool<SpaceBookingDbContext>(options =>
|
||||||
{
|
{
|
||||||
|
|||||||
@ -0,0 +1,34 @@
|
|||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Models.Entities;
|
||||||
|
using XisongSpaceBooking_BackEnd.Configurations;
|
||||||
|
|
||||||
|
namespace XisongSpaceBooking_BackEnd.Repositories
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 處室資料存取實作
|
||||||
|
/// </summary>
|
||||||
|
public class DepartmentRepository : IDepartmentRepository
|
||||||
|
{
|
||||||
|
private readonly SpaceBookingDbContext _context;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 建構函式
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="context">資料庫內容</param>
|
||||||
|
public DepartmentRepository(SpaceBookingDbContext context)
|
||||||
|
{
|
||||||
|
_context = context;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 取得所有處室資料
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>所有處室資料的集合</returns>
|
||||||
|
public async Task<IEnumerable<DepartmentEntity>> GetAllAsync()
|
||||||
|
{
|
||||||
|
return await _context.Departments
|
||||||
|
.AsNoTracking()
|
||||||
|
.ToListAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,16 @@
|
|||||||
|
using Models.Entities;
|
||||||
|
|
||||||
|
namespace XisongSpaceBooking_BackEnd.Repositories
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 處室資料存取介面
|
||||||
|
/// </summary>
|
||||||
|
public interface IDepartmentRepository
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 取得所有處室資料
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>所有處室資料的集合</returns>
|
||||||
|
Task<IEnumerable<DepartmentEntity>> GetAllAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
16
XisongSpaceBooking_BackEnd/Repositories/IRoleRepository.cs
Normal file
16
XisongSpaceBooking_BackEnd/Repositories/IRoleRepository.cs
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
using Models.Entities;
|
||||||
|
|
||||||
|
namespace XisongSpaceBooking_BackEnd.Repositories
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 身份資料存取介面
|
||||||
|
/// </summary>
|
||||||
|
public interface IRoleRepository
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 取得所有身份資料
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>所有身份資料的集合</returns>
|
||||||
|
Task<IEnumerable<RoleEntity>> GetAllAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
34
XisongSpaceBooking_BackEnd/Repositories/RoleRepository.cs
Normal file
34
XisongSpaceBooking_BackEnd/Repositories/RoleRepository.cs
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Models.Entities;
|
||||||
|
using XisongSpaceBooking_BackEnd.Configurations;
|
||||||
|
|
||||||
|
namespace XisongSpaceBooking_BackEnd.Repositories
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 身份資料存取實作
|
||||||
|
/// </summary>
|
||||||
|
public class RoleRepository : IRoleRepository
|
||||||
|
{
|
||||||
|
private readonly SpaceBookingDbContext _context;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 建構函式
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="context">資料庫內容</param>
|
||||||
|
public RoleRepository(SpaceBookingDbContext context)
|
||||||
|
{
|
||||||
|
_context = context;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 取得所有身份資料
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>所有身份資料的集合</returns>
|
||||||
|
public async Task<IEnumerable<RoleEntity>> GetAllAsync()
|
||||||
|
{
|
||||||
|
return await _context.Roles
|
||||||
|
.AsNoTracking()
|
||||||
|
.ToListAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user