feat: quotas
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
using MediatR;
|
using MediatR;
|
||||||
using MemeVault.Application.Common;
|
using MemeVault.Application.Common;
|
||||||
using MemeVault.Domain.Aggregates.Collection;
|
using MemeVault.Domain.Aggregates.Collection;
|
||||||
|
using MemeVault.Domain.Exceptions;
|
||||||
using MemeVault.Domain.Repositories;
|
using MemeVault.Domain.Repositories;
|
||||||
|
|
||||||
namespace MemeVault.Application.Commands.CreateCollection;
|
namespace MemeVault.Application.Commands.CreateCollection;
|
||||||
@@ -18,6 +19,12 @@ public sealed class CreateCollectionCommandHandler : IRequestHandler<CreateColle
|
|||||||
|
|
||||||
public async Task<Guid> Handle(CreateCollectionCommand request, CancellationToken cancellationToken)
|
public async Task<Guid> Handle(CreateCollectionCommand request, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
|
const int CollectionOwnerLimit = 5;
|
||||||
|
var owned = await _collections.GetByOwnerAsync(request.OwnerTelegramId, cancellationToken);
|
||||||
|
// Inbox не считается — пользовательские коллекции только
|
||||||
|
if (owned.Count(c => !c.IsInbox) >= CollectionOwnerLimit)
|
||||||
|
throw new CollectionQuotaExceededException(CollectionOwnerLimit);
|
||||||
|
|
||||||
var collection = Collection.Create(request.OwnerTelegramId, request.Name, request.Description);
|
var collection = Collection.Create(request.OwnerTelegramId, request.Name, request.Description);
|
||||||
await _collections.AddAsync(collection, cancellationToken);
|
await _collections.AddAsync(collection, cancellationToken);
|
||||||
await _uow.SaveChangesAsync(cancellationToken);
|
await _uow.SaveChangesAsync(cancellationToken);
|
||||||
|
|||||||
@@ -43,6 +43,11 @@ public sealed class SaveMediaCommandHandler : IRequestHandler<SaveMediaCommand,
|
|||||||
collectionId = inbox.Id;
|
collectionId = inbox.Id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const int CollectionMediaLimit = 120;
|
||||||
|
var count = await _mediaItems.CountByCollectionAsync(collectionId, cancellationToken);
|
||||||
|
if (count >= CollectionMediaLimit)
|
||||||
|
throw new MediaInCollectionQuotaExceededException(CollectionMediaLimit);
|
||||||
|
|
||||||
var item = MediaItem.Create(
|
var item = MediaItem.Create(
|
||||||
request.OwnerTelegramId,
|
request.OwnerTelegramId,
|
||||||
collectionId,
|
collectionId,
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
namespace MemeVault.Domain.Exceptions;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Пользователь достиг лимита пользовательских коллекций (Owner).
|
||||||
|
/// </summary>
|
||||||
|
public sealed class CollectionQuotaExceededException : DomainException
|
||||||
|
{
|
||||||
|
public int Limit { get; }
|
||||||
|
|
||||||
|
public CollectionQuotaExceededException(int limit)
|
||||||
|
: base($"Cannot create more than {limit} collections.")
|
||||||
|
{
|
||||||
|
Limit = limit;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
namespace MemeVault.Domain.Exceptions;
|
namespace MemeVault.Domain.Exceptions;
|
||||||
|
|
||||||
public sealed class DomainException : Exception
|
public class DomainException : Exception
|
||||||
{
|
{
|
||||||
public DomainException(string message) : base(message) { }
|
public DomainException(string message) : base(message) { }
|
||||||
public DomainException(string message, Exception inner) : base(message, inner) { }
|
public DomainException(string message, Exception inner) : base(message, inner) { }
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
namespace MemeVault.Domain.Exceptions;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Коллекция достигла лимита медиа-элементов.
|
||||||
|
/// </summary>
|
||||||
|
public sealed class MediaInCollectionQuotaExceededException : DomainException
|
||||||
|
{
|
||||||
|
public int Limit { get; }
|
||||||
|
|
||||||
|
public MediaInCollectionQuotaExceededException(int limit)
|
||||||
|
: base($"Collection has reached the limit of {limit} items.")
|
||||||
|
{
|
||||||
|
Limit = limit;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -21,6 +21,9 @@ public interface IMediaItemRepository
|
|||||||
bool includeNsfw = true,
|
bool includeNsfw = true,
|
||||||
CancellationToken ct = default);
|
CancellationToken ct = default);
|
||||||
|
|
||||||
|
/// <summary>Количество элементов в указанной коллекции.</summary>
|
||||||
|
Task<int> CountByCollectionAsync(Guid collectionId, CancellationToken ct = default);
|
||||||
|
|
||||||
/// <summary>Количество элементов в каждой из указанных коллекций за один запрос.</summary>
|
/// <summary>Количество элементов в каждой из указанных коллекций за один запрос.</summary>
|
||||||
Task<Dictionary<Guid, int>> GetCountsByCollectionIdsAsync(
|
Task<Dictionary<Guid, int>> GetCountsByCollectionIdsAsync(
|
||||||
IReadOnlyList<Guid> collectionIds, CancellationToken ct = default);
|
IReadOnlyList<Guid> collectionIds, CancellationToken ct = default);
|
||||||
|
|||||||
@@ -94,6 +94,9 @@ public sealed class MediaItemRepository : IMediaItemRepository
|
|||||||
.ToListAsync(ct);
|
.ToListAsync(ct);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Task<int> CountByCollectionAsync(Guid collectionId, CancellationToken ct) =>
|
||||||
|
_db.MediaItems.CountAsync(x => x.CollectionId == collectionId, ct);
|
||||||
|
|
||||||
public async Task<Dictionary<Guid, int>> GetCountsByCollectionIdsAsync(
|
public async Task<Dictionary<Guid, int>> GetCountsByCollectionIdsAsync(
|
||||||
IReadOnlyList<Guid> collectionIds, CancellationToken ct)
|
IReadOnlyList<Guid> collectionIds, CancellationToken ct)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user