完成设备删除单元测试
This commit is contained in:
@@ -36,6 +36,11 @@ public interface IDeviceAppService
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
Task DeleteDeviceAsync(Device device);
|
Task DeleteDeviceAsync(Device device);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 异步删除一个设备。
|
||||||
|
/// </summary>
|
||||||
|
Task<bool> DeleteDeviceByIdAsync(int deviceId);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 异步切换设备的激活状态。
|
/// 异步切换设备的激活状态。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -98,7 +98,8 @@ public class DeviceService : IDeviceAppService
|
|||||||
var addVariableTableMenu = await _repoManager.Menus.AddAsync(menu);
|
var addVariableTableMenu = await _repoManager.Menus.AddAsync(menu);
|
||||||
if (addVariableTableMenu == null || addVariableTableMenu.Id == 0)
|
if (addVariableTableMenu == null || addVariableTableMenu.Id == 0)
|
||||||
{
|
{
|
||||||
throw new InvalidOperationException($"添加设备变量表菜单失败,变量表:{variableTable.Name},变量表菜单:{menu.Header}");
|
throw new InvalidOperationException(
|
||||||
|
$"添加设备变量表菜单失败,变量表:{variableTable.Name},变量表菜单:{menu.Header}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -136,8 +137,34 @@ public class DeviceService : IDeviceAppService
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public async Task DeleteDeviceAsync(Device device)
|
public async Task DeleteDeviceAsync(Device device)
|
||||||
{
|
{
|
||||||
await _repoManager.Devices.DeleteAsync(device);
|
await DeleteDeviceByIdAsync(device.Id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<bool> DeleteDeviceByIdAsync(int deviceId)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await _repoManager.BeginTranAsync();
|
||||||
|
var delRes = await _repoManager.Devices.DeleteByIdAsync(deviceId);
|
||||||
|
if (delRes == 0)
|
||||||
|
{
|
||||||
|
throw new InvalidOperationException($"删除设备失败:设备ID:{deviceId},请检查设备Id是否存在");
|
||||||
|
}
|
||||||
|
|
||||||
|
await _repoManager.VariableTables.DeleteByDeviceIdAsync(deviceId);
|
||||||
|
|
||||||
|
await _repoManager.Menus.DeleteMenuTreeByTargetIdAsync(MenuType.DeviceMenu,deviceId);
|
||||||
|
|
||||||
await _repoManager.CommitAsync();
|
await _repoManager.CommitAsync();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
await _repoManager.RollbackAsync();
|
||||||
|
// 可以在此记录日志
|
||||||
|
throw new ApplicationException($"删除设备时发生错误,操作已回滚,错误信息:{ex.Message}", ex);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -75,7 +75,7 @@ public class MenuService : IMenuService
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
_repoManager.BeginTranAsync();
|
_repoManager.BeginTranAsync();
|
||||||
await _repoManager.Menus.DeleteAsync(id);
|
await _repoManager.Menus.DeleteByIdAsync(id);
|
||||||
await _repoManager.CommitAsync();
|
await _repoManager.CommitAsync();
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
|||||||
@@ -75,7 +75,7 @@ public class MqttAliasAppService : IMqttAliasAppService
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
_repoManager.BeginTranAsync();
|
_repoManager.BeginTranAsync();
|
||||||
await _repoManager.VariableMqttAliases.DeleteAsync(id);
|
await _repoManager.VariableMqttAliases.DeleteByIdAsync(id);
|
||||||
await _repoManager.CommitAsync();
|
await _repoManager.CommitAsync();
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
|||||||
@@ -75,7 +75,7 @@ public class MqttAppService : IMqttAppService
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
_repoManager.BeginTranAsync();
|
_repoManager.BeginTranAsync();
|
||||||
await _repoManager.MqttServers.DeleteAsync(id);
|
await _repoManager.MqttServers.DeleteByIdAsync(id);
|
||||||
await _repoManager.CommitAsync();
|
await _repoManager.CommitAsync();
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
|||||||
@@ -75,7 +75,7 @@ public class VariableAppService : IVariableAppService
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
_repoManager.BeginTranAsync();
|
_repoManager.BeginTranAsync();
|
||||||
await _repoManager.Variables.DeleteAsync(id);
|
await _repoManager.Variables.DeleteByIdAsync(id);
|
||||||
await _repoManager.CommitAsync();
|
await _repoManager.CommitAsync();
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ public interface IBaseRepository<T> where T : class
|
|||||||
/// 异步根据ID删除一个实体。
|
/// 异步根据ID删除一个实体。
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="id">要删除的实体的主键ID。</param>
|
/// <param name="id">要删除的实体的主键ID。</param>
|
||||||
Task<int> DeleteAsync(int id);
|
Task<int> DeleteByIdAsync(int id);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 从数据库获取数据。
|
/// 从数据库获取数据。
|
||||||
|
|||||||
@@ -1,10 +1,12 @@
|
|||||||
|
|
||||||
|
using DMS.Core.Enums;
|
||||||
using DMS.Core.Models;
|
using DMS.Core.Models;
|
||||||
|
|
||||||
namespace DMS.Core.Interfaces.Repositories
|
namespace DMS.Core.Interfaces.Repositories
|
||||||
{
|
{
|
||||||
public interface IMenuRepository:IBaseRepository<MenuBean>
|
public interface IMenuRepository:IBaseRepository<MenuBean>
|
||||||
{
|
{
|
||||||
|
Task<int> DeleteMenuTreeByIdAsync(int id);
|
||||||
|
Task<int> DeleteMenuTreeByTargetIdAsync(MenuType menuType, int targetId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -5,7 +5,6 @@ namespace DMS.Core.Interfaces.Repositories
|
|||||||
{
|
{
|
||||||
public interface IVariableTableRepository:IBaseRepository<VariableTable>
|
public interface IVariableTableRepository:IBaseRepository<VariableTable>
|
||||||
{
|
{
|
||||||
|
Task<int> DeleteByDeviceIdAsync(int deviceId);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -38,4 +38,14 @@ public class DeviceServiceTest : BaseServiceTest // 继承基类
|
|||||||
// Assert
|
// Assert
|
||||||
Assert.NotEqual(0, addedDeviceId);
|
Assert.NotEqual(0, addedDeviceId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public async Task DeleteDeviceAsyncTest()
|
||||||
|
{
|
||||||
|
// Act
|
||||||
|
var isSuccess = await _deviceService.DeleteDeviceByIdAsync(4);
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
Assert.Equal(isSuccess,true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -41,7 +41,7 @@ public class DeviceRepository : BaseRepository<DbDevice>, IDeviceRepository
|
|||||||
|
|
||||||
public async Task<int> DeleteAsync(Core.Models.Device model) => await base.DeleteAsync(_mapper.Map<DbDevice>(model));
|
public async Task<int> DeleteAsync(Core.Models.Device model) => await base.DeleteAsync(_mapper.Map<DbDevice>(model));
|
||||||
|
|
||||||
public async Task<int> DeleteAsync(int id)
|
public async Task<int> DeleteByIdAsync(int id)
|
||||||
{
|
{
|
||||||
var stopwatch = new Stopwatch();
|
var stopwatch = new Stopwatch();
|
||||||
stopwatch.Start();
|
stopwatch.Start();
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using AutoMapper;
|
using AutoMapper;
|
||||||
|
using DMS.Core.Enums;
|
||||||
using DMS.Core.Helper;
|
using DMS.Core.Helper;
|
||||||
using DMS.Core.Interfaces.Repositories;
|
using DMS.Core.Interfaces.Repositories;
|
||||||
using DMS.Core.Models;
|
using DMS.Core.Models;
|
||||||
@@ -54,7 +55,7 @@ public class MenuRepository : BaseRepository<DbMenu>, IMenuRepository
|
|||||||
|
|
||||||
public async Task<int> DeleteAsync(MenuBean entity) => await base.DeleteAsync(_mapper.Map<DbMenu>(entity));
|
public async Task<int> DeleteAsync(MenuBean entity) => await base.DeleteAsync(_mapper.Map<DbMenu>(entity));
|
||||||
|
|
||||||
public async Task<int> DeleteAsync(int id)
|
public async Task<int> DeleteByIdAsync(int id)
|
||||||
{
|
{
|
||||||
var stopwatch = new Stopwatch();
|
var stopwatch = new Stopwatch();
|
||||||
stopwatch.Start();
|
stopwatch.Start();
|
||||||
@@ -65,10 +66,44 @@ public class MenuRepository : BaseRepository<DbMenu>, IMenuRepository
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<int> DeleteMenuTreeByIdAsync(int id)
|
||||||
|
{
|
||||||
|
var stopwatch = new Stopwatch();
|
||||||
|
stopwatch.Start();
|
||||||
|
int delConut = 0;
|
||||||
|
var childList = await Db.Queryable<DbMenu>()
|
||||||
|
.ToChildListAsync(c => c.ParentId, id);
|
||||||
|
delConut = await Db.Deleteable<DbMenu>(childList)
|
||||||
|
.ExecuteCommandAsync();
|
||||||
|
delConut += await Db.Deleteable<DbMenu>()
|
||||||
|
.Where(m => m.Id == id)
|
||||||
|
.ExecuteCommandAsync();
|
||||||
|
stopwatch.Stop();
|
||||||
|
NlogHelper.Info($"Delete {typeof(DbMenu)},ID={id},耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||||
|
return delConut;
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<int> DeleteMenuTreeByTargetIdAsync(MenuType menuType, int targetId)
|
||||||
|
{
|
||||||
|
var stopwatch = new Stopwatch();
|
||||||
|
stopwatch.Start();
|
||||||
|
var menu = await Db.Queryable<DbMenu>().FirstAsync(m => m.MenuType == menuType && m.TargetId == targetId);
|
||||||
|
if (menu == null) return 0;
|
||||||
|
var childList = await Db.Queryable<DbMenu>()
|
||||||
|
.ToChildListAsync(c => c.ParentId, menu.Id);
|
||||||
|
var delConut = await Db.Deleteable<DbMenu>(childList)
|
||||||
|
.ExecuteCommandAsync();
|
||||||
|
delConut += await Db.Deleteable<DbMenu>()
|
||||||
|
.Where(m => m.Id == menu.Id)
|
||||||
|
.ExecuteCommandAsync();
|
||||||
|
stopwatch.Stop();
|
||||||
|
NlogHelper.Info($"Delete {typeof(DbMenu)},TargetId={targetId},耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||||
|
return delConut;
|
||||||
|
}
|
||||||
|
|
||||||
public new async Task<List<MenuBean>> TakeAsync(int number)
|
public new async Task<List<MenuBean>> TakeAsync(int number)
|
||||||
{
|
{
|
||||||
var dbList = await base.TakeAsync(number);
|
var dbList = await base.TakeAsync(number);
|
||||||
return _mapper.Map<List<MenuBean>>(dbList);
|
return _mapper.Map<List<MenuBean>>(dbList);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -43,7 +43,7 @@ public class MqttServerRepository : BaseRepository<DbMqttServer>, IMqttServerRep
|
|||||||
|
|
||||||
public async Task<int> DeleteAsync(MqttServer entity) => await base.DeleteAsync(_mapper.Map<DbMqttServer>(entity));
|
public async Task<int> DeleteAsync(MqttServer entity) => await base.DeleteAsync(_mapper.Map<DbMqttServer>(entity));
|
||||||
|
|
||||||
public async Task<int> DeleteAsync(int id)
|
public async Task<int> DeleteByIdAsync(int id)
|
||||||
{
|
{
|
||||||
var stopwatch = new Stopwatch();
|
var stopwatch = new Stopwatch();
|
||||||
stopwatch.Start();
|
stopwatch.Start();
|
||||||
|
|||||||
@@ -54,7 +54,7 @@ public class UserRepository : BaseRepository<DbUser>, IUserRepository
|
|||||||
|
|
||||||
public async Task<int> DeleteAsync(User entity) => await base.DeleteAsync(_mapper.Map<DbUser>(entity));
|
public async Task<int> DeleteAsync(User entity) => await base.DeleteAsync(_mapper.Map<DbUser>(entity));
|
||||||
|
|
||||||
public async Task<int> DeleteAsync(int id)
|
public async Task<int> DeleteByIdAsync(int id)
|
||||||
{
|
{
|
||||||
var stopwatch = new Stopwatch();
|
var stopwatch = new Stopwatch();
|
||||||
stopwatch.Start();
|
stopwatch.Start();
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ public class VariableHistoryRepository : BaseRepository<DbVariableHistory>, IVar
|
|||||||
|
|
||||||
public async Task<int> DeleteAsync(VariableHistory entity) => await base.DeleteAsync(_mapper.Map<DbVariableHistory>(entity));
|
public async Task<int> DeleteAsync(VariableHistory entity) => await base.DeleteAsync(_mapper.Map<DbVariableHistory>(entity));
|
||||||
|
|
||||||
public async Task<int> DeleteAsync(int id)
|
public async Task<int> DeleteByIdAsync(int id)
|
||||||
{
|
{
|
||||||
var stopwatch = new Stopwatch();
|
var stopwatch = new Stopwatch();
|
||||||
stopwatch.Start();
|
stopwatch.Start();
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ public class VariableMqttAliasRepository : BaseRepository<DbVariableMqttAlias>,
|
|||||||
|
|
||||||
public async Task<int> DeleteAsync(VariableMqttAlias entity) => await base.DeleteAsync(_mapper.Map<DbVariableMqttAlias>(entity));
|
public async Task<int> DeleteAsync(VariableMqttAlias entity) => await base.DeleteAsync(_mapper.Map<DbVariableMqttAlias>(entity));
|
||||||
|
|
||||||
public async Task<int> DeleteAsync(int id)
|
public async Task<int> DeleteByIdAsync(int id)
|
||||||
{
|
{
|
||||||
var stopwatch = new Stopwatch();
|
var stopwatch = new Stopwatch();
|
||||||
stopwatch.Start();
|
stopwatch.Start();
|
||||||
|
|||||||
@@ -127,7 +127,7 @@ public class VariableRepository : BaseRepository<DbVariable>, IVariableRepositor
|
|||||||
public async Task<int> DeleteAsync(Variable entity) => await base.DeleteAsync(_mapper.Map<DbVariable>(entity));
|
public async Task<int> DeleteAsync(Variable entity) => await base.DeleteAsync(_mapper.Map<DbVariable>(entity));
|
||||||
|
|
||||||
|
|
||||||
public async Task<int> DeleteAsync(int id)
|
public async Task<int> DeleteByIdAsync(int id)
|
||||||
{
|
{
|
||||||
var stopwatch = new Stopwatch();
|
var stopwatch = new Stopwatch();
|
||||||
stopwatch.Start();
|
stopwatch.Start();
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ public class VariableTableRepository : BaseRepository<DbVariableTable>, IVariabl
|
|||||||
|
|
||||||
public async Task<int> DeleteAsync(VariableTable entity) => await base.DeleteAsync(_mapper.Map<DbVariableTable>(entity));
|
public async Task<int> DeleteAsync(VariableTable entity) => await base.DeleteAsync(_mapper.Map<DbVariableTable>(entity));
|
||||||
|
|
||||||
public async Task<int> DeleteAsync(int id)
|
public async Task<int> DeleteByIdAsync(int id)
|
||||||
{
|
{
|
||||||
var stopwatch = new Stopwatch();
|
var stopwatch = new Stopwatch();
|
||||||
stopwatch.Start();
|
stopwatch.Start();
|
||||||
@@ -63,4 +63,16 @@ public class VariableTableRepository : BaseRepository<DbVariableTable>, IVariabl
|
|||||||
return _mapper.Map<List<VariableTable>>(dbList);
|
return _mapper.Map<List<VariableTable>>(dbList);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<int> DeleteByDeviceIdAsync(int deviceId)
|
||||||
|
{
|
||||||
|
var stopwatch = new Stopwatch();
|
||||||
|
stopwatch.Start();
|
||||||
|
var result = await Db.Deleteable<DbVariableTable>()
|
||||||
|
.Where(it => it.DeviceId == deviceId)
|
||||||
|
.ExecuteCommandAsync();
|
||||||
|
stopwatch.Stop();
|
||||||
|
NlogHelper.Info($"Delete VariableTable by DeviceId={deviceId}, 耗时:{stopwatch.ElapsedMilliseconds}ms");
|
||||||
|
return result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user