基本修改完毕开始进行单元测试

This commit is contained in:
2025-07-19 14:36:34 +08:00
parent ca67d2e6df
commit aaf8bdf08a
23 changed files with 843 additions and 353 deletions

View File

@@ -12,14 +12,12 @@ namespace DMS.Infrastructure.Repositories;
/// <summary>
/// Mqtt仓储类用于操作DbMqtt实体
/// </summary>
public class MqttRepository : BaseRepository<DbMqtt, Mqtt>
public class MqttRepository : BaseRepository<DbMqtt>
{
private readonly MenuRepository _menuRepository;
public MqttRepository(MenuRepository menuRepository, IMapper mapper, ITransaction transaction)
: base(mapper, transaction)
public MqttRepository(SqlSugarDbContext dbContext)
: base(dbContext)
{
_menuRepository = menuRepository;
}
/// <summary>
@@ -27,7 +25,7 @@ public class MqttRepository : BaseRepository<DbMqtt, Mqtt>
/// </summary>
/// <param name="id">主键ID</param>
/// <returns></returns>
public async Task<Mqtt> GetByIdAsync(int id)
public override async Task<DbMqtt> GetByIdAsync(int id)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
@@ -36,14 +34,14 @@ public class MqttRepository : BaseRepository<DbMqtt, Mqtt>
.SingleAsync();
stopwatch.Stop();
NlogHelper.Info($"根据ID '{id}' 获取Mqtt配置耗时{stopwatch.ElapsedMilliseconds}ms");
return _mapper.Map<Mqtt>(result);
return result;
}
/// <summary>
/// 获取所有Mqtt配置
/// </summary>
/// <returns></returns>
public async Task<List<Mqtt>> GetAllAsync()
public override async Task<List<DbMqtt>> GetAllAsync()
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
@@ -53,115 +51,8 @@ public class MqttRepository : BaseRepository<DbMqtt, Mqtt>
.ToListAsync();
stopwatch.Stop();
NlogHelper.Info($"获取所有Mqtt配置耗时{stopwatch.ElapsedMilliseconds}ms");
return result.Select(m => _mapper.Map<Mqtt>(m))
.ToList();
return result;
}
/// <summary>
/// 新增Mqtt配置
/// </summary>
/// <param name="mqtt">Mqtt实体</param>
/// <returns></returns>
public async Task<int> AddAsync(Mqtt mqtt)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
await Db.BeginTranAsync();
try
{
var result = await Db.Insertable(_mapper.Map<DbMqtt>(mqtt))
.ExecuteReturnIdentityAsync();
var mqttMenu = await _menuRepository.GetMainMenuByNameAsync("Mqtt服务器");
// AddAsync menu entry
var menu = new MenuBean()
{
Name = mqtt.Name,
// Icon = SegoeFluentIcons.Wifi.Glyph,
Type = MenuType.MqttMenu,
DataId = result,
ParentId = mqttMenu.Id,
};
await _menuRepository.AddAsync(menu, Db);
await Db.CommitTranAsync();
stopwatch.Stop();
NlogHelper.Info($"新增Mqtt配置 '{mqtt.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
}
catch (Exception ex)
{
await Db.RollbackTranAsync();
NlogHelper.Error($"添加MQTT配置 {{mqtt.Name}} 失败", ex);
throw;
}
}
/// <summary>
/// 更新Mqtt配置
/// </summary>
/// <param name="mqtt">Mqtt实体</param>
/// <returns></returns>
public async Task<int> UpdateAsync(Mqtt mqtt)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
await Db.BeginTranAsync();
try
{
var result = await Db.Updateable(_mapper.Map<DbMqtt>(mqtt))
.ExecuteCommandAsync();
// Update menu entry
var menu = await _menuRepository.GetMenuByDataIdAsync(mqtt.Id, MenuType.MqttMenu);
if (menu != null)
{
menu.Name = mqtt.Name;
await _menuRepository.UpdateAsync(menu, Db);
}
await Db.CommitTranAsync();
stopwatch.Stop();
NlogHelper.Info($"更新Mqtt配置 '{mqtt.Name}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
}
catch (Exception ex)
{
await Db.RollbackTranAsync();
NlogHelper.Error($"更新MQTT配置 {{mqtt.Name}} 失败", ex);
throw;
}
}
/// <summary>
/// 根据ID删除Mqtt配置
/// </summary>
/// <param name="mqtt">Mqtt实体</param>
/// <returns></returns>
public async Task<int> DeleteAsync(Mqtt mqtt)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
await Db.BeginTranAsync();
try
{
var result = await Db.Deleteable<DbMqtt>()
.In(mqtt.Id)
.ExecuteCommandAsync();
// DeleteAsync menu entry
var menu = await _menuRepository.GetMenuByDataIdAsync(mqtt.Id, MenuType.MqttMenu);
if (menu != null)
{
await _menuRepository.DeleteAsync(menu, Db);
}
await Db.CommitTranAsync();
stopwatch.Stop();
NlogHelper.Info($"删除Mqtt配置ID '{mqtt.Id}' 耗时:{stopwatch.ElapsedMilliseconds}ms");
return result;
}
catch (Exception ex)
{
await Db.RollbackTranAsync();
NlogHelper.Error($"删除MQTT配置 {{mqtt.Name}} 失败", ex);
throw;
}
}
}