完成新建变量表的功能测试
This commit is contained in:
@@ -10,7 +10,7 @@ namespace DMS.Application.Services;
|
||||
/// <summary>
|
||||
/// 实现设备管理的应用服务。
|
||||
/// </summary>
|
||||
public class DeviceService : IDeviceAppService
|
||||
public class DeviceAppService : IDeviceAppService
|
||||
{
|
||||
private readonly IRepositoryManager _repoManager;
|
||||
private readonly IMapper _mapper;
|
||||
@@ -18,7 +18,7 @@ public class DeviceService : IDeviceAppService
|
||||
/// <summary>
|
||||
/// 构造函数,通过依赖注入获取仓储管理器和AutoMapper实例。
|
||||
/// </summary>
|
||||
public DeviceService(IRepositoryManager repoManager, IMapper mapper)
|
||||
public DeviceAppService(IRepositoryManager repoManager, IMapper mapper)
|
||||
{
|
||||
_repoManager = repoManager;
|
||||
_mapper = mapper;
|
||||
90
DMS.Application/Services/VariableTableAppService.cs
Normal file
90
DMS.Application/Services/VariableTableAppService.cs
Normal file
@@ -0,0 +1,90 @@
|
||||
using AutoMapper;
|
||||
using DMS.Application.DTOs;
|
||||
using DMS.Application.Interfaces;
|
||||
using DMS.Core.Interfaces;
|
||||
using DMS.Core.Models;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using DMS.Core.Enums;
|
||||
|
||||
namespace DMS.Application.Services
|
||||
{
|
||||
public class VariableTableAppService : IVariableTableAppService
|
||||
{
|
||||
private readonly IRepositoryManager _repositoryManager;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
public VariableTableAppService(IRepositoryManager repositoryManager, IMapper mapper)
|
||||
{
|
||||
_repositoryManager = repositoryManager;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
public async Task<VariableTableDto> GetVariableTableByIdAsync(int id)
|
||||
{
|
||||
var variableTable = await _repositoryManager.VariableTables.GetByIdAsync(id);
|
||||
return _mapper.Map<VariableTableDto>(variableTable);
|
||||
}
|
||||
|
||||
public async Task<List<VariableTableDto>> GetAllVariableTablesAsync()
|
||||
{
|
||||
var variableTables = await _repositoryManager.VariableTables.GetAllAsync();
|
||||
return _mapper.Map<List<VariableTableDto>>(variableTables);
|
||||
}
|
||||
|
||||
public async Task<VariableTableDto> CreateVariableTableAsync(CreateVariableTableWithMenuDto createDto)
|
||||
{
|
||||
await _repositoryManager.BeginTranAsync();
|
||||
try
|
||||
{
|
||||
var variableTable = _mapper.Map<VariableTable>(createDto.VariableTable);
|
||||
variableTable.DeviceId = createDto.DeviceId;
|
||||
|
||||
var createdVariableTable = await _repositoryManager.VariableTables.AddAsync(variableTable);
|
||||
if (createdVariableTable.Id == 0)
|
||||
{
|
||||
throw new ApplicationException($"添加变量表失败,设备ID:{createDto.DeviceId},请检查。");
|
||||
}
|
||||
|
||||
if (createDto.Menu!=null)
|
||||
{
|
||||
var deviceMenu
|
||||
= await _repositoryManager.Menus.GetMenuByTargetIdAsync(
|
||||
MenuType.DeviceMenu, createDto.DeviceId);
|
||||
if (deviceMenu == null)
|
||||
{
|
||||
throw new ApplicationException($"添加变量表菜单时,找不到设备ID:{createDto.DeviceId},请检查。");
|
||||
}
|
||||
|
||||
var menu = _mapper.Map<MenuBean>(createDto.Menu);
|
||||
menu.ParentId = deviceMenu.Id;
|
||||
menu.TargetId = createdVariableTable.Id;
|
||||
menu.MenuType = MenuType.VariableTableMenu;
|
||||
await _repositoryManager.Menus.AddAsync(menu);
|
||||
}
|
||||
|
||||
|
||||
|
||||
await _repositoryManager.CommitAsync();
|
||||
|
||||
return _mapper.Map<VariableTableDto>(createdVariableTable);
|
||||
}
|
||||
catch
|
||||
{
|
||||
await _repositoryManager.RollbackAsync();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task UpdateVariableTableAsync(VariableTableDto variableTableDto)
|
||||
{
|
||||
var variableTable = _mapper.Map<VariableTable>(variableTableDto);
|
||||
await _repositoryManager.VariableTables.UpdateAsync(variableTable);
|
||||
}
|
||||
|
||||
public async Task DeleteVariableTableAsync(int id)
|
||||
{
|
||||
await _repositoryManager.VariableTables.DeleteByIdAsync(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user