临时提交2
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using DMS.Application.DTOs;
|
||||
using DMS.Core.Enums;
|
||||
using DMS.Core.Models;
|
||||
|
||||
namespace DMS.Application.Interfaces;
|
||||
|
||||
@@ -33,7 +34,7 @@ public interface IDeviceAppService
|
||||
/// <summary>
|
||||
/// 异步删除一个设备。
|
||||
/// </summary>
|
||||
Task DeleteDeviceAsync(int id);
|
||||
Task DeleteDeviceAsync(Device device);
|
||||
|
||||
/// <summary>
|
||||
/// 异步切换设备的激活状态。
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
using AutoMapper;
|
||||
using DMS.Core.Interfaces;
|
||||
using DMS.Core.Models;
|
||||
using DMS.Application.DTOs;
|
||||
using DMS.Application.Interfaces;
|
||||
using DMS.Core.Enums;
|
||||
using DMS.Core.Interfaces;
|
||||
|
||||
namespace DMS.Application.Services;
|
||||
|
||||
@@ -49,7 +49,7 @@ public class DeviceAppService : IDeviceAppService
|
||||
{
|
||||
try
|
||||
{
|
||||
_repoManager.BeginTransaction();
|
||||
_repoManager.BeginTranAsync();
|
||||
|
||||
var device = _mapper.Map<Device>(dto.Device);
|
||||
device.IsActive = true; // 默认激活
|
||||
@@ -101,9 +101,9 @@ public class DeviceAppService : IDeviceAppService
|
||||
/// <summary>
|
||||
/// 异步删除一个设备。
|
||||
/// </summary>
|
||||
public async Task DeleteDeviceAsync(int id)
|
||||
public async Task DeleteDeviceAsync(Device device)
|
||||
{
|
||||
await _repoManager.Devices.DeleteAsync(id);
|
||||
await _repoManager.Devices.DeleteAsync(device);
|
||||
await _repoManager.CommitAsync();
|
||||
}
|
||||
|
||||
@@ -127,7 +127,7 @@ public class DeviceAppService : IDeviceAppService
|
||||
/// </summary>
|
||||
public async Task<List<DeviceDto>> GetDevicesByProtocolAsync(ProtocolType protocol)
|
||||
{
|
||||
var devices = await _repoManager.Devices.GetActiveDevicesWithDetailsAsync(protocol);
|
||||
var devices = await _repoManager.Devices.GetAllAsync();
|
||||
return _mapper.Map<List<DeviceDto>>(devices);
|
||||
}
|
||||
}
|
||||
@@ -34,27 +34,54 @@ public class MenuService : IMenuService
|
||||
|
||||
public async Task<int> CreateMenuAsync(MenuBeanDto menuDto)
|
||||
{
|
||||
var menu = _mapper.Map<MenuBean>(menuDto);
|
||||
await _repoManager.Menus.AddAsync(menu);
|
||||
await _repoManager.CommitAsync();
|
||||
return menu.Id;
|
||||
try
|
||||
{
|
||||
_repoManager.BeginTranAsync();
|
||||
var menu = _mapper.Map<MenuBean>(menuDto);
|
||||
await _repoManager.Menus.AddAsync(menu);
|
||||
await _repoManager.CommitAsync();
|
||||
return menu.Id;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await _repoManager.RollbackAsync();
|
||||
throw new ApplicationException("创建菜单时发生错误,操作已回滚。", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task UpdateMenuAsync(MenuBeanDto menuDto)
|
||||
{
|
||||
var menu = await _repoManager.Menus.GetByIdAsync(menuDto.Id);
|
||||
if (menu == null)
|
||||
try
|
||||
{
|
||||
throw new ApplicationException($"Menu with ID {menuDto.Id} not found.");
|
||||
_repoManager.BeginTranAsync();
|
||||
var menu = await _repoManager.Menus.GetByIdAsync(menuDto.Id);
|
||||
if (menu == null)
|
||||
{
|
||||
throw new ApplicationException($"Menu with ID {menuDto.Id} not found.");
|
||||
}
|
||||
_mapper.Map(menuDto, menu);
|
||||
await _repoManager.Menus.UpdateAsync(menu);
|
||||
await _repoManager.CommitAsync();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await _repoManager.RollbackAsync();
|
||||
throw new ApplicationException("更新菜单时发生错误,操作已回滚。", ex);
|
||||
}
|
||||
_mapper.Map(menuDto, menu);
|
||||
await _repoManager.Menus.UpdateAsync(menu);
|
||||
await _repoManager.CommitAsync();
|
||||
}
|
||||
|
||||
public async Task DeleteMenuAsync(int id)
|
||||
{
|
||||
await _repoManager.Menus.DeleteAsync(id);
|
||||
await _repoManager.CommitAsync();
|
||||
try
|
||||
{
|
||||
_repoManager.BeginTranAsync();
|
||||
await _repoManager.Menus.DeleteAsync(id);
|
||||
await _repoManager.CommitAsync();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await _repoManager.RollbackAsync();
|
||||
throw new ApplicationException("删除菜单时发生错误,操作已回滚。", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -34,27 +34,54 @@ public class MqttAliasAppService : IMqttAliasAppService
|
||||
|
||||
public async Task<int> CreateMqttAliasAsync(VariableMqttAliasDto mqttAliasDto)
|
||||
{
|
||||
var mqttAlias = _mapper.Map<VariableMqttAlias>(mqttAliasDto);
|
||||
await _repoManager.VariableMqttAliases.AddAsync(mqttAlias);
|
||||
await _repoManager.CommitAsync();
|
||||
return mqttAlias.Id;
|
||||
try
|
||||
{
|
||||
_repoManager.BeginTranAsync();
|
||||
var mqttAlias = _mapper.Map<VariableMqttAlias>(mqttAliasDto);
|
||||
await _repoManager.VariableMqttAliases.AddAsync(mqttAlias);
|
||||
await _repoManager.CommitAsync();
|
||||
return mqttAlias.Id;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await _repoManager.RollbackAsync();
|
||||
throw new ApplicationException("创建MQTT别名时发生错误,操作已回滚。", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task UpdateMqttAliasAsync(VariableMqttAliasDto mqttAliasDto)
|
||||
{
|
||||
var mqttAlias = await _repoManager.VariableMqttAliases.GetByIdAsync(mqttAliasDto.Id);
|
||||
if (mqttAlias == null)
|
||||
try
|
||||
{
|
||||
throw new ApplicationException($"MQTT Alias with ID {mqttAliasDto.Id} not found.");
|
||||
_repoManager.BeginTranAsync();
|
||||
var mqttAlias = await _repoManager.VariableMqttAliases.GetByIdAsync(mqttAliasDto.Id);
|
||||
if (mqttAlias == null)
|
||||
{
|
||||
throw new ApplicationException($"MQTT Alias with ID {mqttAliasDto.Id} not found.");
|
||||
}
|
||||
_mapper.Map(mqttAliasDto, mqttAlias);
|
||||
await _repoManager.VariableMqttAliases.UpdateAsync(mqttAlias);
|
||||
await _repoManager.CommitAsync();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await _repoManager.RollbackAsync();
|
||||
throw new ApplicationException("更新MQTT别名时发生错误,操作已回滚。", ex);
|
||||
}
|
||||
_mapper.Map(mqttAliasDto, mqttAlias);
|
||||
await _repoManager.VariableMqttAliases.UpdateAsync(mqttAlias);
|
||||
await _repoManager.CommitAsync();
|
||||
}
|
||||
|
||||
public async Task DeleteMqttAliasAsync(int id)
|
||||
{
|
||||
await _repoManager.VariableMqttAliases.DeleteAsync(id);
|
||||
await _repoManager.CommitAsync();
|
||||
try
|
||||
{
|
||||
_repoManager.BeginTranAsync();
|
||||
await _repoManager.VariableMqttAliases.DeleteAsync(id);
|
||||
await _repoManager.CommitAsync();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await _repoManager.RollbackAsync();
|
||||
throw new ApplicationException("删除MQTT别名时发生错误,操作已回滚。", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -34,27 +34,54 @@ public class MqttAppService : IMqttAppService
|
||||
|
||||
public async Task<int> CreateMqttServerAsync(MqttServerDto mqttServerDto)
|
||||
{
|
||||
var mqttServer = _mapper.Map<MqttServer>(mqttServerDto);
|
||||
await _repoManager.MqttServers.AddAsync(mqttServer);
|
||||
await _repoManager.CommitAsync();
|
||||
return mqttServer.Id;
|
||||
try
|
||||
{
|
||||
_repoManager.BeginTranAsync();
|
||||
var mqttServer = _mapper.Map<MqttServer>(mqttServerDto);
|
||||
await _repoManager.MqttServers.AddAsync(mqttServer);
|
||||
await _repoManager.CommitAsync();
|
||||
return mqttServer.Id;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await _repoManager.RollbackAsync();
|
||||
throw new ApplicationException("创建MQTT服务器时发生错误,操作已回滚。", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task UpdateMqttServerAsync(MqttServerDto mqttServerDto)
|
||||
{
|
||||
var mqttServer = await _repoManager.MqttServers.GetByIdAsync(mqttServerDto.Id);
|
||||
if (mqttServer == null)
|
||||
try
|
||||
{
|
||||
throw new ApplicationException($"MQTT Server with ID {mqttServerDto.Id} not found.");
|
||||
_repoManager.BeginTranAsync();
|
||||
var mqttServer = await _repoManager.MqttServers.GetByIdAsync(mqttServerDto.Id);
|
||||
if (mqttServer == null)
|
||||
{
|
||||
throw new ApplicationException($"MQTT Server with ID {mqttServerDto.Id} not found.");
|
||||
}
|
||||
_mapper.Map(mqttServerDto, mqttServer);
|
||||
await _repoManager.MqttServers.UpdateAsync(mqttServer);
|
||||
await _repoManager.CommitAsync();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await _repoManager.RollbackAsync();
|
||||
throw new ApplicationException("更新MQTT服务器时发生错误,操作已回滚。", ex);
|
||||
}
|
||||
_mapper.Map(mqttServerDto, mqttServer);
|
||||
await _repoManager.MqttServers.UpdateAsync(mqttServer);
|
||||
await _repoManager.CommitAsync();
|
||||
}
|
||||
|
||||
public async Task DeleteMqttServerAsync(int id)
|
||||
{
|
||||
await _repoManager.MqttServers.DeleteAsync(id);
|
||||
await _repoManager.CommitAsync();
|
||||
try
|
||||
{
|
||||
_repoManager.BeginTranAsync();
|
||||
await _repoManager.MqttServers.DeleteAsync(id);
|
||||
await _repoManager.CommitAsync();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await _repoManager.RollbackAsync();
|
||||
throw new ApplicationException("删除MQTT服务器时发生错误,操作已回滚。", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -34,27 +34,54 @@ public class VariableAppService : IVariableAppService
|
||||
|
||||
public async Task<int> CreateVariableAsync(VariableDto variableDto)
|
||||
{
|
||||
var variable = _mapper.Map<Variable>(variableDto);
|
||||
await _repoManager.Variables.AddAsync(variable);
|
||||
await _repoManager.CommitAsync();
|
||||
return variable.Id;
|
||||
try
|
||||
{
|
||||
_repoManager.BeginTranAsync();
|
||||
var variable = _mapper.Map<Variable>(variableDto);
|
||||
await _repoManager.Variables.AddAsync(variable);
|
||||
await _repoManager.CommitAsync();
|
||||
return variable.Id;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await _repoManager.RollbackAsync();
|
||||
throw new ApplicationException("创建变量时发生错误,操作已回滚。", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task UpdateVariableAsync(VariableDto variableDto)
|
||||
{
|
||||
var variable = await _repoManager.Variables.GetByIdAsync(variableDto.Id);
|
||||
if (variable == null)
|
||||
try
|
||||
{
|
||||
throw new ApplicationException($"Variable with ID {variableDto.Id} not found.");
|
||||
_repoManager.BeginTranAsync();
|
||||
var variable = await _repoManager.Variables.GetByIdAsync(variableDto.Id);
|
||||
if (variable == null)
|
||||
{
|
||||
throw new ApplicationException($"Variable with ID {variableDto.Id} not found.");
|
||||
}
|
||||
_mapper.Map(variableDto, variable);
|
||||
await _repoManager.Variables.UpdateAsync(variable);
|
||||
await _repoManager.CommitAsync();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await _repoManager.RollbackAsync();
|
||||
throw new ApplicationException("更新变量时发生错误,操作已回滚。", ex);
|
||||
}
|
||||
_mapper.Map(variableDto, variable);
|
||||
await _repoManager.Variables.UpdateAsync(variable);
|
||||
await _repoManager.CommitAsync();
|
||||
}
|
||||
|
||||
public async Task DeleteVariableAsync(int id)
|
||||
{
|
||||
await _repoManager.Variables.DeleteAsync(id);
|
||||
await _repoManager.CommitAsync();
|
||||
try
|
||||
{
|
||||
_repoManager.BeginTranAsync();
|
||||
await _repoManager.Variables.DeleteAsync(id);
|
||||
await _repoManager.CommitAsync();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await _repoManager.RollbackAsync();
|
||||
throw new ApplicationException("删除变量时发生错误,操作已回滚。", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user