Initial commit
This commit is contained in:
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user