feat: collections controller, healthcheck

This commit is contained in:
2026-09-28 02:18:57 +04:00
parent 5ca59a6ad8
commit 5238bfd103
11 changed files with 273 additions and 1 deletions
@@ -0,0 +1,8 @@
using MediatR;
namespace MemeVault.Application.Commands.DeleteCollection;
public sealed record DeleteCollectionCommand(
Guid CollectionId,
long RequestingTelegramId
) : IRequest<bool>;
@@ -0,0 +1,22 @@
using MediatR;
using MemeVault.Application.Common;
using MemeVault.Domain.Repositories;
namespace MemeVault.Application.Commands.DeleteCollection;
public sealed class DeleteCollectionCommandHandler(
ICollectionRepository collections,
IUnitOfWork unitOfWork)
: IRequestHandler<DeleteCollectionCommand, bool>
{
public async Task<bool> Handle(DeleteCollectionCommand request, CancellationToken ct)
{
var collection = await collections.GetByIdAsync(request.CollectionId, ct);
if (collection is null || collection.OwnerTelegramId != request.RequestingTelegramId)
return false;
await collections.RemoveAsync(collection, ct);
await unitOfWork.SaveChangesAsync(ct);
return true;
}
}
@@ -0,0 +1,15 @@
using MediatR;
namespace MemeVault.Application.Commands.ManageCollectionItem;
public sealed record AddItemToCollectionCommand(
Guid CollectionId,
Guid MediaItemId,
long RequestingTelegramId
) : IRequest<bool>;
public sealed record RemoveItemFromCollectionCommand(
Guid CollectionId,
Guid MediaItemId,
long RequestingTelegramId
) : IRequest<bool>;
@@ -0,0 +1,37 @@
using MediatR;
using MemeVault.Application.Common;
using MemeVault.Domain.Repositories;
namespace MemeVault.Application.Commands.ManageCollectionItem;
public sealed class AddItemToCollectionCommandHandler(
ICollectionRepository collections,
IUnitOfWork unitOfWork)
: IRequestHandler<AddItemToCollectionCommand, bool>
{
public async Task<bool> Handle(AddItemToCollectionCommand request, CancellationToken ct)
{
var collection = await collections.GetByIdAsync(request.CollectionId, ct);
if (collection is null) return false;
collection.AddItem(request.MediaItemId, request.RequestingTelegramId);
await unitOfWork.SaveChangesAsync(ct);
return true;
}
}
public sealed class RemoveItemFromCollectionCommandHandler(
ICollectionRepository collections,
IUnitOfWork unitOfWork)
: IRequestHandler<RemoveItemFromCollectionCommand, bool>
{
public async Task<bool> Handle(RemoveItemFromCollectionCommand request, CancellationToken ct)
{
var collection = await collections.GetByIdAsync(request.CollectionId, ct);
if (collection is null) return false;
collection.RemoveItem(request.MediaItemId, request.RequestingTelegramId);
await unitOfWork.SaveChangesAsync(ct);
return true;
}
}
@@ -0,0 +1,10 @@
using MediatR;
namespace MemeVault.Application.Commands.UpdateCollection;
public sealed record UpdateCollectionCommand(
Guid CollectionId,
long RequestingTelegramId,
string Name,
string? Description
) : IRequest<bool>;
@@ -0,0 +1,22 @@
using MediatR;
using MemeVault.Application.Common;
using MemeVault.Domain.Repositories;
namespace MemeVault.Application.Commands.UpdateCollection;
public sealed class UpdateCollectionCommandHandler(
ICollectionRepository collections,
IUnitOfWork unitOfWork)
: IRequestHandler<UpdateCollectionCommand, bool>
{
public async Task<bool> Handle(UpdateCollectionCommand request, CancellationToken ct)
{
var collection = await collections.GetByIdAsync(request.CollectionId, ct);
if (collection is null || collection.OwnerTelegramId != request.RequestingTelegramId)
return false;
collection.Rename(request.Name, request.RequestingTelegramId);
await unitOfWork.SaveChangesAsync(ct);
return true;
}
}
@@ -0,0 +1,17 @@
using MediatR;
using MemeVault.Domain.Enums;
namespace MemeVault.Application.Queries.GetUserCollections;
public sealed record GetUserCollectionsQuery(long OwnerTelegramId)
: IRequest<IReadOnlyList<CollectionDto>>;
public sealed record CollectionDto(
Guid Id,
string Name,
string? Description,
CollectionVisibility Visibility,
string? ShareToken,
int ItemCount,
DateTimeOffset CreatedAt
);
@@ -0,0 +1,25 @@
using MediatR;
using MemeVault.Domain.Repositories;
namespace MemeVault.Application.Queries.GetUserCollections;
public sealed class GetUserCollectionsQueryHandler(ICollectionRepository collections)
: IRequestHandler<GetUserCollectionsQuery, IReadOnlyList<CollectionDto>>
{
public async Task<IReadOnlyList<CollectionDto>> Handle(
GetUserCollectionsQuery request,
CancellationToken ct)
{
var items = await collections.GetByOwnerAsync(request.OwnerTelegramId, ct);
return items.Select(c => new CollectionDto(
c.Id,
c.Name,
c.Description,
c.Visibility,
c.ShareToken,
c.MediaItemIds.Count,
c.CreatedAt
)).ToList();
}
}