feat: new exceptions

This commit is contained in:
2026-10-07 02:59:23 +04:00
parent 274e60a433
commit e7e7bf4db7
13 changed files with 74 additions and 36 deletions
@@ -20,7 +20,7 @@ public sealed class DeleteCollectionCommandHandler(
var user = await users.GetByTelegramIdAsync(request.RequestingTelegramId, ct);
if (user?.InboxCollectionId == collection.Id)
throw new DomainException("Cannot delete the Inbox collection.");
throw new InboxProtectionException("Cannot delete the Inbox collection.");
collection.Delete(request.RequestingTelegramId);
await collections.RemoveAsync(collection, ct);
@@ -22,7 +22,7 @@ public sealed class SetCollectionInboxCommandHandler(
if (user is null) return false;
if (user.InboxCollectionId == target.Id)
throw new DomainException("Collection is already the Inbox.");
throw new InboxProtectionException("Collection is already the Inbox.");
user.SetInboxCollection(target.Id);
await users.UpdateAsync(user, ct);
@@ -1,13 +1,11 @@
using MediatR;
using MemeVault.Application.Common;
using MemeVault.Domain.Exceptions;
using MemeVault.Domain.Repositories;
namespace MemeVault.Application.Commands.UpdateCollection;
public sealed class UpdateCollectionCommandHandler(
ICollectionRepository collections,
IBotUserRepository users,
IUnitOfWork unitOfWork)
: IRequestHandler<UpdateCollectionCommand, bool>
{
@@ -15,14 +13,10 @@ public sealed class UpdateCollectionCommandHandler(
{
var collection = await collections.GetByIdAsync(request.CollectionId, ct);
// Если коллекции нет или пользователь — не участник, возвращаем 404.
// Если роль ниже Manager — domain.Rename() бросит DomainException.
// Если роль ниже Manager — domain.Rename() бросит InsufficientPermissionException.
if (collection is null || collection.GetRole(request.RequestingTelegramId) is null)
return false;
var user = await users.GetByTelegramIdAsync(request.RequestingTelegramId, ct);
if (user?.InboxCollectionId == collection.Id)
throw new DomainException("Cannot rename the Inbox collection.");
collection.Rename(request.Name, request.RequestingTelegramId);
await unitOfWork.SaveChangesAsync(ct);
return true;
@@ -13,14 +13,17 @@ public sealed class GetMediaDownloadUrlQueryHandler
private static readonly TimeSpan UrlExpiry = TimeSpan.FromMinutes(15);
private readonly IMediaItemRepository _mediaItems;
private readonly ITelegramFileCacheService _cache;
private readonly ITelegramFileCacheService _telegramCache;
private readonly IFileStorage _fileStorage;
public GetMediaDownloadUrlQueryHandler(
IMediaItemRepository mediaItems,
ITelegramFileCacheService cache)
ITelegramFileCacheService telegramCache,
IFileStorage fileStorage)
{
_mediaItems = mediaItems;
_cache = cache;
_mediaItems = mediaItems;
_telegramCache = telegramCache;
_fileStorage = fileStorage;
}
public async Task<string> Handle(GetMediaDownloadUrlQuery request, CancellationToken ct)
@@ -29,18 +32,18 @@ public sealed class GetMediaDownloadUrlQueryHandler
?? throw new DomainException("Media item not found.");
if (item.OwnerTelegramId != request.RequestingUserTelegramId)
throw new DomainException("Access denied.");
throw new InsufficientPermissionException("Access denied.");
if (item.Type == MediaType.Text || item.Source is null)
throw new DomainException("This media item has no downloadable file.");
if (item.Source.Origin == MediaOrigin.S3)
{
// Already stored in S3 — just generate a presigned URL directly.
return await _cache.GetCachedUrlAsync(item.Source.Reference!, UrlExpiry, ct);
// File is already stored in S3 — generate presigned URL directly.
return await _fileStorage.GetDownloadUrlAsync(item.Source.Reference!, UrlExpiry, ct);
}
// Telegram source — go through cache (lock → S3 check → fetch → upload → presign).
return await _cache.GetCachedUrlAsync(item.Source.Reference!, UrlExpiry, ct);
// Telegram source — fetch, cache in S3, and return presigned URL.
return await _telegramCache.GetCachedUrlAsync(item.Source.Reference!, UrlExpiry, ct);
}
}