refactor:完成重构设备的添加,删除,更新。
This commit is contained in:
@@ -1,8 +1,7 @@
|
||||
using AutoMapper;
|
||||
using DMS.Application.Interfaces;
|
||||
using DMS.Core.Interfaces;
|
||||
using DMS.Core.Models;
|
||||
using DMS.Application.Interfaces.Database;
|
||||
using DMS.Application.Interfaces;
|
||||
|
||||
namespace DMS.Application.Services.Database;
|
||||
|
||||
@@ -53,20 +52,9 @@ public class MenuAppService : IMenuAppService
|
||||
/// <param name="menu">要创建的菜单。</param>
|
||||
/// <returns>新创建菜单的ID。</returns>
|
||||
/// <exception cref="ApplicationException">如果创建菜单时发生错误。</exception>
|
||||
public async Task<int> CreateMenuAsync(MenuBean menu)
|
||||
public async Task<MenuBean> AddAsync(MenuBean menu)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _repoManager.BeginTranAsync();
|
||||
await _repoManager.Menus.AddAsync(menu);
|
||||
await _repoManager.CommitAsync();
|
||||
return menu.Id;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await _repoManager.RollbackAsync();
|
||||
throw new ApplicationException("创建菜单时发生错误,操作已回滚。", ex);
|
||||
}
|
||||
return await _repoManager.Menus.AddAsync(menu);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -75,57 +63,20 @@ public class MenuAppService : IMenuAppService
|
||||
/// <param name="menu">要更新的菜单。</param>
|
||||
/// <returns>受影响的行数。</returns>
|
||||
/// <exception cref="ApplicationException">如果找不到菜单或更新菜单时发生错误。</exception>
|
||||
public async Task<int> UpdateMenuAsync(MenuBean menu)
|
||||
public async Task<int> UpdateAsync(MenuBean menu)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _repoManager.BeginTranAsync();
|
||||
var dbmenu = await _repoManager.Menus.GetByIdAsync(menu.Id);
|
||||
if (dbmenu == null)
|
||||
{
|
||||
throw new ApplicationException($"Menu with ID {menu.Id} not found.");
|
||||
}
|
||||
_mapper.Map(menu, dbmenu);
|
||||
int res = await _repoManager.Menus.UpdateAsync(dbmenu);
|
||||
await _repoManager.CommitAsync();
|
||||
return res;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await _repoManager.RollbackAsync();
|
||||
throw new ApplicationException("更新菜单时发生错误,操作已回滚。", ex);
|
||||
}
|
||||
return await _repoManager.Menus.UpdateAsync(menu);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 异步删除一个菜单(事务性操作)。
|
||||
/// </summary>
|
||||
/// <param name="id">要删除菜单的ID。</param>
|
||||
/// <returns>如果删除成功则为 true,否则为 false。</returns>
|
||||
/// <exception cref="InvalidOperationException">如果删除菜单失败。</exception>
|
||||
/// <exception cref="ApplicationException">如果删除菜单时发生其他错误。</exception>
|
||||
public async Task<bool> DeleteMenuAsync(int id)
|
||||
public async Task<bool> DeleteAsync(MenuBean menu)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _repoManager.BeginTranAsync();
|
||||
var menu = await _repoManager.Menus.GetByIdAsync(id);
|
||||
if (menu == null)
|
||||
{
|
||||
throw new InvalidOperationException($"删除菜单失败:菜单ID:{id},请检查菜单Id是否存在");
|
||||
}
|
||||
var delRes = await _repoManager.Menus.DeleteAsync(menu);
|
||||
if (delRes == 0)
|
||||
{
|
||||
throw new InvalidOperationException($"删除菜单失败:菜单ID:{id},请检查菜单Id是否存在");
|
||||
}
|
||||
await _repoManager.CommitAsync();
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await _repoManager.RollbackAsync();
|
||||
throw new ApplicationException("删除菜单时发生错误,操作已回滚。", ex);
|
||||
}
|
||||
var delRes = await _repoManager.Menus.DeleteAsync(menu);
|
||||
return delRes > 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user