Initial commit
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
using MemeVault.Domain.Events;
|
||||
|
||||
namespace MemeVault.Domain.Aggregates.BotUser;
|
||||
|
||||
/// <summary>
|
||||
/// Aggregate Root: пользователь Telegram-бота.
|
||||
/// </summary>
|
||||
public sealed class BotUser
|
||||
{
|
||||
private readonly List<IDomainEvent> _domainEvents = [];
|
||||
|
||||
public Guid Id { get; private set; }
|
||||
public long TelegramId { get; private set; }
|
||||
public string Username { get; private set; } = string.Empty;
|
||||
public string? FirstName { get; private set; }
|
||||
public DateTimeOffset CreatedAt { get; private set; }
|
||||
public DateTimeOffset LastSeenAt { get; private set; }
|
||||
|
||||
public IReadOnlyList<IDomainEvent> DomainEvents => _domainEvents.AsReadOnly();
|
||||
|
||||
// EF Core constructor
|
||||
private BotUser() { }
|
||||
|
||||
private BotUser(Guid id, long telegramId, string username, string? firstName)
|
||||
{
|
||||
Id = id;
|
||||
TelegramId = telegramId;
|
||||
Username = username;
|
||||
FirstName = firstName;
|
||||
CreatedAt = DateTimeOffset.UtcNow;
|
||||
LastSeenAt = DateTimeOffset.UtcNow;
|
||||
}
|
||||
|
||||
public static BotUser Create(long telegramId, string username, string? firstName)
|
||||
{
|
||||
var user = new BotUser(Guid.NewGuid(), telegramId, username, firstName);
|
||||
user._domainEvents.Add(new BotUserCreatedEvent(user.Id, telegramId));
|
||||
return user;
|
||||
}
|
||||
|
||||
public void RecordActivity()
|
||||
{
|
||||
LastSeenAt = DateTimeOffset.UtcNow;
|
||||
}
|
||||
|
||||
public void ClearDomainEvents() => _domainEvents.Clear();
|
||||
}
|
||||
@@ -0,0 +1,169 @@
|
||||
using MemeVault.Domain.Enums;
|
||||
using MemeVault.Domain.Events;
|
||||
using MemeVault.Domain.Exceptions;
|
||||
|
||||
namespace MemeVault.Domain.Aggregates.Collection;
|
||||
|
||||
/// <summary>
|
||||
/// Aggregate Root: коллекция медиа-элементов с ACL-моделью доступа.
|
||||
/// </summary>
|
||||
public sealed class Collection
|
||||
{
|
||||
private readonly List<Guid> _mediaItemIds = [];
|
||||
private readonly List<CollectionMember> _members = [];
|
||||
private readonly List<IDomainEvent> _domainEvents = [];
|
||||
|
||||
public Guid Id { get; private set; }
|
||||
public long OwnerTelegramId { get; private set; }
|
||||
public string Name { get; private set; } = string.Empty;
|
||||
public string? Description { get; private set; }
|
||||
public CollectionVisibility Visibility { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Токен для доступа по ссылке (для Visibility = LinkOnly и Public).
|
||||
/// </summary>
|
||||
public string? ShareToken { get; private set; }
|
||||
|
||||
public IReadOnlyList<Guid> MediaItemIds => _mediaItemIds.AsReadOnly();
|
||||
public IReadOnlyList<CollectionMember> Members => _members.AsReadOnly();
|
||||
public DateTimeOffset CreatedAt { get; private set; }
|
||||
public DateTimeOffset? UpdatedAt { get; private set; }
|
||||
|
||||
public IReadOnlyList<IDomainEvent> DomainEvents => _domainEvents.AsReadOnly();
|
||||
|
||||
// EF Core constructor
|
||||
private Collection() { }
|
||||
|
||||
private Collection(Guid id, long ownerTelegramId, string name, string? description)
|
||||
{
|
||||
Id = id;
|
||||
OwnerTelegramId = ownerTelegramId;
|
||||
Name = name;
|
||||
Description = description;
|
||||
Visibility = CollectionVisibility.Private;
|
||||
CreatedAt = DateTimeOffset.UtcNow;
|
||||
|
||||
// Владелец автоматически становится членом с ролью Owner
|
||||
_members.Add(new CollectionMember(id, ownerTelegramId, CollectionRole.Owner));
|
||||
}
|
||||
|
||||
public static Collection Create(long ownerTelegramId, string name, string? description = null)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(name))
|
||||
throw new DomainException("Collection name cannot be empty.");
|
||||
|
||||
if (name.Length > 100)
|
||||
throw new DomainException("Collection name must be 100 characters or less.");
|
||||
|
||||
return new Collection(Guid.NewGuid(), ownerTelegramId, name, description);
|
||||
}
|
||||
|
||||
// ── Управление элементами ──────────────────────────────────────────────────
|
||||
|
||||
public void AddItem(Guid mediaItemId, long requestingTelegramId)
|
||||
{
|
||||
EnsureRole(requestingTelegramId, CollectionRole.Manager);
|
||||
|
||||
if (_mediaItemIds.Contains(mediaItemId)) return;
|
||||
if (_mediaItemIds.Count >= 500)
|
||||
throw new DomainException("Collection cannot have more than 500 items.");
|
||||
|
||||
_mediaItemIds.Add(mediaItemId);
|
||||
UpdatedAt = DateTimeOffset.UtcNow;
|
||||
}
|
||||
|
||||
public void RemoveItem(Guid mediaItemId, long requestingTelegramId)
|
||||
{
|
||||
EnsureRole(requestingTelegramId, CollectionRole.Manager);
|
||||
_mediaItemIds.Remove(mediaItemId);
|
||||
UpdatedAt = DateTimeOffset.UtcNow;
|
||||
}
|
||||
|
||||
// ── Управление участниками ─────────────────────────────────────────────────
|
||||
|
||||
public void AddMember(long telegramId, CollectionRole role, long requestingTelegramId)
|
||||
{
|
||||
EnsureRole(requestingTelegramId, CollectionRole.Owner);
|
||||
|
||||
if (role == CollectionRole.Owner)
|
||||
throw new DomainException("Cannot assign Owner role. Transfer ownership instead.");
|
||||
|
||||
var existing = _members.FirstOrDefault(m => m.TelegramId == telegramId);
|
||||
if (existing is not null)
|
||||
{
|
||||
existing.ChangeRole(role);
|
||||
}
|
||||
else
|
||||
{
|
||||
_members.Add(new CollectionMember(Id, telegramId, role));
|
||||
}
|
||||
|
||||
UpdatedAt = DateTimeOffset.UtcNow;
|
||||
}
|
||||
|
||||
public void RemoveMember(long telegramId, long requestingTelegramId)
|
||||
{
|
||||
EnsureRole(requestingTelegramId, CollectionRole.Owner);
|
||||
|
||||
if (telegramId == OwnerTelegramId)
|
||||
throw new DomainException("Cannot remove the owner from the collection.");
|
||||
|
||||
var member = _members.FirstOrDefault(m => m.TelegramId == telegramId)
|
||||
?? throw new DomainException("Member not found.");
|
||||
|
||||
_members.Remove(member);
|
||||
UpdatedAt = DateTimeOffset.UtcNow;
|
||||
}
|
||||
|
||||
// ── Видимость и шаринг ────────────────────────────────────────────────────
|
||||
|
||||
public void SetVisibility(CollectionVisibility visibility, long requestingTelegramId)
|
||||
{
|
||||
EnsureRole(requestingTelegramId, CollectionRole.Owner);
|
||||
|
||||
Visibility = visibility;
|
||||
|
||||
if (visibility is CollectionVisibility.LinkOnly or CollectionVisibility.Public)
|
||||
{
|
||||
ShareToken ??= GenerateToken();
|
||||
}
|
||||
|
||||
UpdatedAt = DateTimeOffset.UtcNow;
|
||||
}
|
||||
|
||||
public void Rename(string newName, long requestingTelegramId)
|
||||
{
|
||||
EnsureRole(requestingTelegramId, CollectionRole.Manager);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(newName))
|
||||
throw new DomainException("Collection name cannot be empty.");
|
||||
|
||||
Name = newName.Trim();
|
||||
UpdatedAt = DateTimeOffset.UtcNow;
|
||||
}
|
||||
|
||||
// ── Доступ ────────────────────────────────────────────────────────────────
|
||||
|
||||
public bool CanView(long telegramId)
|
||||
{
|
||||
if (Visibility == CollectionVisibility.Public) return true;
|
||||
return _members.Any(m => m.TelegramId == telegramId);
|
||||
}
|
||||
|
||||
public CollectionRole? GetRole(long telegramId) =>
|
||||
_members.FirstOrDefault(m => m.TelegramId == telegramId)?.Role;
|
||||
|
||||
private void EnsureRole(long telegramId, CollectionRole minimumRole)
|
||||
{
|
||||
var role = GetRole(telegramId);
|
||||
if (role is null || role < minimumRole)
|
||||
throw new DomainException(
|
||||
$"Action requires at least {minimumRole} role.");
|
||||
}
|
||||
|
||||
private static string GenerateToken() =>
|
||||
Convert.ToBase64String(Guid.NewGuid().ToByteArray())
|
||||
.Replace("/", "_").Replace("+", "-").TrimEnd('=');
|
||||
|
||||
public void ClearDomainEvents() => _domainEvents.Clear();
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
using MemeVault.Domain.Enums;
|
||||
|
||||
namespace MemeVault.Domain.Aggregates.Collection;
|
||||
|
||||
/// <summary>
|
||||
/// Участник коллекции с определённой ролью.
|
||||
/// Является сущностью внутри агрегата Collection.
|
||||
/// </summary>
|
||||
public sealed class CollectionMember
|
||||
{
|
||||
public Guid Id { get; private set; }
|
||||
public Guid CollectionId { get; private set; }
|
||||
public long TelegramId { get; private set; }
|
||||
public CollectionRole Role { get; private set; }
|
||||
public DateTimeOffset AddedAt { get; private set; }
|
||||
|
||||
// EF Core constructor
|
||||
private CollectionMember() { }
|
||||
|
||||
internal CollectionMember(Guid collectionId, long telegramId, CollectionRole role)
|
||||
{
|
||||
Id = Guid.NewGuid();
|
||||
CollectionId = collectionId;
|
||||
TelegramId = telegramId;
|
||||
Role = role;
|
||||
AddedAt = DateTimeOffset.UtcNow;
|
||||
}
|
||||
|
||||
internal void ChangeRole(CollectionRole newRole) => Role = newRole;
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
using MemeVault.Domain.Enums;
|
||||
using MemeVault.Domain.Events;
|
||||
using MemeVault.Domain.Exceptions;
|
||||
using MemeVault.Domain.ValueObjects;
|
||||
|
||||
namespace MemeVault.Domain.Aggregates.MediaItem;
|
||||
|
||||
/// <summary>
|
||||
/// Aggregate Root: сохранённый пользователем медиа-элемент.
|
||||
/// Поддерживает одиночные файлы и группы (media_group_id из Telegram).
|
||||
/// Источник файла: Telegram file_id или S3 object key.
|
||||
/// </summary>
|
||||
public sealed class MediaItem
|
||||
{
|
||||
// Not readonly — EF Core needs to set this via reflection when materialising from DB
|
||||
private List<Tag> _tags = [];
|
||||
private readonly List<IDomainEvent> _domainEvents = [];
|
||||
|
||||
public Guid Id { get; private set; }
|
||||
public long OwnerTelegramId { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Источник файла (Telegram или S3). Null для текстового контента.
|
||||
/// </summary>
|
||||
public MediaSource? Source { get; private set; }
|
||||
|
||||
public MediaType Type { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Подпись к медиа или сам текст (для Type == Text).
|
||||
/// </summary>
|
||||
public string? Caption { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Идентификатор группы сообщений Telegram (album).
|
||||
/// Несколько MediaItem с одинаковым MediaGroupId принадлежат одному альбому.
|
||||
/// </summary>
|
||||
public string? MediaGroupId { get; private set; }
|
||||
|
||||
public IReadOnlyList<Tag> Tags => _tags.AsReadOnly();
|
||||
public DateTimeOffset CreatedAt { get; private set; }
|
||||
public DateTimeOffset? UpdatedAt { get; private set; }
|
||||
|
||||
public IReadOnlyList<IDomainEvent> DomainEvents => _domainEvents.AsReadOnly();
|
||||
|
||||
// EF Core constructor
|
||||
private MediaItem() { }
|
||||
|
||||
private MediaItem(
|
||||
Guid id,
|
||||
long ownerTelegramId,
|
||||
MediaSource? source,
|
||||
MediaType type,
|
||||
string? caption,
|
||||
string? mediaGroupId,
|
||||
IEnumerable<Tag> tags)
|
||||
{
|
||||
Id = id;
|
||||
OwnerTelegramId = ownerTelegramId;
|
||||
Source = source;
|
||||
Type = type;
|
||||
Caption = caption;
|
||||
MediaGroupId = mediaGroupId;
|
||||
CreatedAt = DateTimeOffset.UtcNow;
|
||||
_tags.AddRange(tags);
|
||||
}
|
||||
|
||||
public static MediaItem Create(
|
||||
long ownerTelegramId,
|
||||
MediaSource? source,
|
||||
MediaType type,
|
||||
string? caption,
|
||||
string? mediaGroupId = null,
|
||||
IEnumerable<Tag>? tags = null)
|
||||
{
|
||||
if (type == MediaType.Text && string.IsNullOrWhiteSpace(caption))
|
||||
throw new DomainException("Text media must have non-empty caption.");
|
||||
|
||||
if (type != MediaType.Text && source is null)
|
||||
throw new DomainException("Non-text media must have a source (Telegram fileId or S3 key).");
|
||||
|
||||
var item = new MediaItem(
|
||||
Guid.NewGuid(),
|
||||
ownerTelegramId,
|
||||
source,
|
||||
type,
|
||||
caption,
|
||||
mediaGroupId,
|
||||
tags ?? []);
|
||||
|
||||
item._domainEvents.Add(new MediaItemSavedEvent(item.Id, ownerTelegramId, type));
|
||||
return item;
|
||||
}
|
||||
|
||||
public void UpdateCaption(string? caption)
|
||||
{
|
||||
Caption = caption;
|
||||
UpdatedAt = DateTimeOffset.UtcNow;
|
||||
}
|
||||
|
||||
public void AddTag(Tag tag)
|
||||
{
|
||||
if (_tags.Contains(tag)) return;
|
||||
if (_tags.Count >= 20)
|
||||
throw new DomainException("Cannot have more than 20 tags per item.");
|
||||
|
||||
_tags.Add(tag);
|
||||
UpdatedAt = DateTimeOffset.UtcNow;
|
||||
}
|
||||
|
||||
public void RemoveTag(Tag tag)
|
||||
{
|
||||
_tags.Remove(tag);
|
||||
UpdatedAt = DateTimeOffset.UtcNow;
|
||||
}
|
||||
|
||||
public void ClearDomainEvents() => _domainEvents.Clear();
|
||||
}
|
||||
Reference in New Issue
Block a user