82 lines
2.8 KiB
C#
82 lines
2.8 KiB
C#
using AutoMapper;
|
||
using DMS.Application.Interfaces;
|
||
using DMS.Core.Interfaces;
|
||
using DMS.Core.Models;
|
||
|
||
namespace DMS.Application.Services.Database;
|
||
|
||
/// <summary>
|
||
/// 菜单应用服务,负责处理菜单相关的业务逻辑。
|
||
/// 实现 <see cref="IMenuAppService"/> 接口。
|
||
/// </summary>
|
||
public class MenuAppService : IMenuAppService
|
||
{
|
||
private readonly IRepositoryManager _repoManager;
|
||
private readonly IMapper _mapper;
|
||
|
||
/// <summary>
|
||
/// 构造函数,通过依赖注入获取仓储管理器和AutoMapper实例。
|
||
/// </summary>
|
||
/// <param name="repoManager">仓储管理器实例。</param>
|
||
/// <param name="mapper">AutoMapper 实例。</param>
|
||
public MenuAppService(IRepositoryManager repoManager, IMapper mapper)
|
||
{
|
||
_repoManager = repoManager;
|
||
_mapper = mapper;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 异步根据ID获取菜单。
|
||
/// </summary>
|
||
/// <param name="id">菜单ID。</param>
|
||
/// <returns>菜单对象。</returns>
|
||
public async Task<MenuBean> GetMenuByIdAsync(int id)
|
||
{
|
||
var menu = await _repoManager.Menus.GetByIdAsync(id);
|
||
return _mapper.Map<MenuBean>(menu);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 异步获取所有菜单列表。
|
||
/// </summary>
|
||
/// <returns>菜单列表。</returns>
|
||
public async Task<List<MenuBean>> GetAllMenusAsync()
|
||
{
|
||
var menus = await _repoManager.Menus.GetAllAsync();
|
||
return _mapper.Map<List<MenuBean>>(menus);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 异步创建一个新菜单(事务性操作)。
|
||
/// </summary>
|
||
/// <param name="menu">要创建的菜单。</param>
|
||
/// <returns>新创建菜单的ID。</returns>
|
||
/// <exception cref="ApplicationException">如果创建菜单时发生错误。</exception>
|
||
public async Task<MenuBean> AddAsync(MenuBean menu)
|
||
{
|
||
return await _repoManager.Menus.AddAsync(menu);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 异步更新一个已存在的菜单(事务性操作)。
|
||
/// </summary>
|
||
/// <param name="menu">要更新的菜单。</param>
|
||
/// <returns>受影响的行数。</returns>
|
||
/// <exception cref="ApplicationException">如果找不到菜单或更新菜单时发生错误。</exception>
|
||
public async Task<int> UpdateAsync(MenuBean menu)
|
||
{
|
||
return await _repoManager.Menus.UpdateAsync(menu);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 异步删除一个菜单(事务性操作)。
|
||
/// </summary>
|
||
/// <returns>如果删除成功则为 true,否则为 false。</returns>
|
||
/// <exception cref="InvalidOperationException">如果删除菜单失败。</exception>
|
||
/// <exception cref="ApplicationException">如果删除菜单时发生其他错误。</exception>
|
||
public async Task<bool> DeleteAsync(MenuBean menu)
|
||
{
|
||
var delRes = await _repoManager.Menus.DeleteAsync(menu);
|
||
return delRes > 0;
|
||
}
|
||
} |