Refactor error handling in Bumper, Media, Group, and Junction components to include contextual information
Updated error definitions in BumperErrors, MediaErrors, GroupErrors, and TemplateErrors to accept a parameter for contextual information, enhancing the clarity of error messages. Refactored related command handlers to utilize these updated error messages, ensuring that users receive specific details about the usage context when attempting to delete resources. Adjusted unit tests to verify the correctness of the new error handling logic.
This commit is contained in:
+46
-1
@@ -34,10 +34,55 @@ public sealed class DeleteGroupCommandHandler(IAppDbContext dbContext)
|
||||
t => t.FallbackGroupId == group.Id,
|
||||
cancellationToken
|
||||
);
|
||||
|
||||
// Сам факт занятости и адрес — разные вопросы: адрес собирается по цепочке слой → шаблон →
|
||||
// канал, и если она неполна, имени не будет, а запрещать удаление всё равно надо.
|
||||
if (used)
|
||||
return Result.Failure(GroupErrors.InUse);
|
||||
return Result.Failure(
|
||||
GroupErrors.InUse(await UsedByAsync(group.Id, cancellationToken))
|
||||
);
|
||||
|
||||
dbContext.Groups.Remove(group);
|
||||
return Result.Success();
|
||||
}
|
||||
|
||||
/// <summary>Кто держит группу: слоты каналов, стыки и аварийная группа шаблона.</summary>
|
||||
private async Task<string> UsedByAsync(Guid groupId, CancellationToken cancellationToken)
|
||||
{
|
||||
var slots = await (
|
||||
from slot in dbContext.Slots
|
||||
join layer in dbContext.GridLayers on slot.LayerId equals layer.Id
|
||||
join template in dbContext.ScheduleTemplates on layer.TemplateId equals template.Id
|
||||
join channel in dbContext.Channels on template.ChannelId equals channel.Id
|
||||
where slot.GroupId == groupId
|
||||
select channel.Name
|
||||
)
|
||||
.Distinct()
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
var junctions = await (
|
||||
from element in dbContext.JunctionElements
|
||||
join junction in dbContext.JunctionTemplates
|
||||
on element.JunctionTemplateId equals junction.Id
|
||||
where element.GroupId == groupId
|
||||
select junction.Name
|
||||
)
|
||||
.Distinct()
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
var fallback = await (
|
||||
from template in dbContext.ScheduleTemplates
|
||||
join channel in dbContext.Channels on template.ChannelId equals channel.Id
|
||||
where template.FallbackGroupId == groupId
|
||||
select channel.Name
|
||||
)
|
||||
.Distinct()
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
return UsageText.Join(
|
||||
UsageText.Part("слоты каналов", slots),
|
||||
UsageText.Part("врезки стыков", junctions),
|
||||
UsageText.Part("аварийная группа каналов", fallback)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user