Implemented new API endpoints for suggesting groups based on the current library, allowing admins to retrieve and create groups from suggestions. Updated the backend to include error handling for suggestions and added necessary DTOs. Enhanced the frontend with new API functions and UI components to display suggestions, improving the user experience for group management. Localization updates were made to support new features in both English and Russian.
43 lines
1.6 KiB
C#
43 lines
1.6 KiB
C#
using LiteCqrs;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using TeleWave.Application.Common.Interfaces;
|
|
using TeleWave.Application.Common.Models;
|
|
using TeleWave.Domain.Programming;
|
|
|
|
namespace TeleWave.Application.Programming.Groups.Suggest;
|
|
|
|
public sealed class CreateGroupFromSuggestionCommandHandler(
|
|
IAppDbContext dbContext,
|
|
GroupSuggestionBuilder builder,
|
|
GroupStatsService stats
|
|
) : ICommandHandler<CreateGroupFromSuggestionCommand, Result<Guid>>
|
|
{
|
|
public async Task<Result<Guid>> Handle(
|
|
CreateGroupFromSuggestionCommand command,
|
|
CancellationToken cancellationToken
|
|
)
|
|
{
|
|
var suggestions = await builder.BuildAsync(cancellationToken);
|
|
var suggestion = suggestions.FirstOrDefault(s => s.Key == command.Key);
|
|
if (suggestion is null)
|
|
return Result.Failure<Guid>(GroupErrors.SuggestionNotFound);
|
|
|
|
// Имя — ключ узнаваемости группы, и две «Мультфильм» в списке слота неразличимы.
|
|
var taken = await dbContext.Groups.AnyAsync(
|
|
g => g.Name.ToLower() == suggestion.Name.ToLower(),
|
|
cancellationToken
|
|
);
|
|
if (taken)
|
|
return Result.Failure<Guid>(GroupErrors.SuggestionAlreadyCreated);
|
|
|
|
var group = Group.Create(suggestion.Name);
|
|
group.SetFilter(suggestion.Filter?.ToJson());
|
|
foreach (var showId in suggestion.ShowIds)
|
|
group.AddElement(GroupElementKind.Show, showId);
|
|
|
|
dbContext.Groups.Add(group);
|
|
await stats.RecomputeAsync(group, cancellationToken);
|
|
return Result.Success(group.Id);
|
|
}
|
|
}
|