完成设备删除单元测试

This commit is contained in:
2025-07-24 18:09:46 +08:00
parent ac38128e4d
commit 7b9c91eee1
18 changed files with 119 additions and 29 deletions

View File

@@ -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(int id)
public async Task<int> DeleteByIdAsync(int id)
{
var stopwatch = new Stopwatch();
stopwatch.Start();

View File

@@ -1,5 +1,6 @@
using System.Diagnostics;
using AutoMapper;
using DMS.Core.Enums;
using DMS.Core.Helper;
using DMS.Core.Interfaces.Repositories;
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(int id)
public async Task<int> DeleteByIdAsync(int id)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
@@ -64,11 +65,45 @@ public class MenuRepository : BaseRepository<DbMenu>, IMenuRepository
NlogHelper.Info($"Delete {typeof(DbMenu)},ID={id},耗时:{stopwatch.ElapsedMilliseconds}ms");
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)
{
var dbList = await base.TakeAsync(number);
return _mapper.Map<List<MenuBean>>(dbList);
}
}
}

View File

@@ -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(int id)
public async Task<int> DeleteByIdAsync(int id)
{
var stopwatch = new Stopwatch();
stopwatch.Start();

View File

@@ -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(int id)
public async Task<int> DeleteByIdAsync(int id)
{
var stopwatch = new Stopwatch();
stopwatch.Start();

View File

@@ -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(int id)
public async Task<int> DeleteByIdAsync(int id)
{
var stopwatch = new Stopwatch();
stopwatch.Start();

View File

@@ -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(int id)
public async Task<int> DeleteByIdAsync(int id)
{
var stopwatch = new Stopwatch();
stopwatch.Start();

View File

@@ -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(int id)
public async Task<int> DeleteByIdAsync(int id)
{
var stopwatch = new Stopwatch();
stopwatch.Start();

View File

@@ -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(int id)
public async Task<int> DeleteByIdAsync(int id)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
@@ -63,4 +63,16 @@ public class VariableTableRepository : BaseRepository<DbVariableTable>, IVariabl
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;
}
}