Backend API 已經可以接收前端的呼叫需求,HR API Service 取得人事資料的部分也完成了。接下來要透過 Backend API 進行整合,並匯整成前端所需的物件格式,讓前端能讀取並呈現在介面中。本篇我們持續完成下圖黃色區塊。
圖12-1:後端在取得身份後,讀取人事資料,並完成資料整理,回應至前端站台。
namespace Core.Interface
{
public interface IHRApiClient
{
Task<Employee> GetEmployeeByEmailOrEmplidAsync(string email, string emplid);
}
}
GetEmployeeByEmailOrEmplidAsync
內容SendRequestAsync
為另外開發用來呼叫 API 的 helper,可透過 Swagger 與 NSwag 工具自動產生。public virtual System.Threading.Tasks.Task<Employee> GetEmployeeByEmailOrEmplidAsync(string email, string emplid)
{
return GetEmployeeByEmailOrEmplidAsync(email, emplid, System.Threading.CancellationToken.None);
}
public virtual async System.Threading.Tasks.Task<Employee> GetEmployeeByEmailOrEmplidAsync(string email, string emplid, System.Threading.CancellationToken cancellationToken)
{
var queryParams = new Dictionary<string, string>
{
{ "email", email },
{ "emplid", emplid }
};
var url = BuildUrl("/Employee", queryParams);
return await SendRequestAsync<Employee>(HttpMethod.Get, url, null, cancellationToken);
}
namespace Core.Models.Dto
{
public class UserInfoDto
{
public required string id{ get; set; }
public required string username { get; set; }
public required string EmailAddress { get; set; }
public string? deptName { get; set; }
public string? deptId { get; set; }
public string? deptName { get; set; }
public string? compName { get; set; }
public string? email { get; set; }
}
}
IHRApiClient.GetEmployeeByEmailOrEmplidAsync
取得人事資料namespace backendAPI.Services
{
public class UserInfoService
{
private readonly IHRApiClient _apiHRClient;
public UserInfoService(IHRApiClient apiHRClient )
{
_apiHRClient = apiHRClient;
}
}
public async Task<UserInfoDto?> GetUserInfobyEmail(string email)
{
//呼叫 apiClient.GetEmployeeByEmailOrEmplidAsync 取得使用者資訊
var employee = await _apiHRClient.GetEmployeeByEmailOrEmplidAsync(email, null);
if (employee == null)
{
return null;
}
//將取得的使用者資訊轉換成 UserInfoDto
UserInfoDto userInfo = new UserInfoDto
{
id= employee.id,
username= employee.Name,
email= employee.Email,
deptName = employee.DeptName,
deptId = employee.DeptId,
deptName= employee.DeptName,
compName = employee.DeptLongName.Split('/')[0] ,
};
return userInfo;
}
}
}
namespace backendAPI.Controllers
{
[ApiController]
[Route("[controller]")]
public class UserInfoController : ControllerBase
{
private readonly UserInfoService _userInfoService;
// 建構子注入 UserInfoService
public UserInfoController(UserInfoService userInfoService)
{
_userInfoService = userInfoService;
}
[HttpGet]
[ProducesResponseType(typeof(UserInfoDto), 200)]
public async Task<IActionResult> GetLoginUserInfo()
{
var emailClaim = User.FindFirst(ClaimTypes.Upn)?.Value;
UserInfoDto? userInfo = await _userInfoService.GetUserInfobyEmail(emailClaim);
if (userInfo == null)
{
return NotFound();
}
return Ok(userInfo);
}
}
}
啟動以下服務,前端在發送 Request 時,會對接到 Backend API,並在從 HR API 取得資料後進行匯整,再回應至前端。
圖12-3:檢視回應,確認有得到資料
IHRApi.cs
:定義 IHRApiClient 介面HRApi.cs
:Service 實作 GetEmployeeByEmailOrEmplidAsync,使用 SendRequestAsync 呼叫 APIUserInfoDto.cs
:整合回傳資料,提供給前端UserInfoService.cs
:注入 IHRApiClient,整理 HR API 回傳資料並生成 DTOUserInfoController
:注入 UserInfoService,透過 GetLoginUserInfo API 回應前端今日完成了後端與人事資料的互動,開始在往前端將取得的資料於前端顯示~明天見