Files
mixtape/zero.Core/Media/MediaStore.cs
T

42 lines
1003 B
C#
Raw Normal View History

using FluentValidation;
namespace zero.Media;
2021-11-24 14:37:32 +01:00
2021-11-25 15:38:36 +01:00
public class MediaStore : TreeEntityStore<Media>, IMediaStore
2021-11-24 14:37:32 +01:00
{
2021-12-29 01:25:35 +01:00
public MediaStore(IStoreContext context) : base(context) { }
2021-11-25 15:38:36 +01:00
/// <summary>
/// A media (either file or folder) can only be saved for the following circumstances:
/// 1. The parent is null (=root)
/// 2. The parent is a folder
/// </summary>
public override async Task<bool> IsAllowedAsChild(Media model, string parentId)
{
if (parentId.IsNullOrEmpty())
{
return true;
}
Media parent = await Load(parentId);
2021-12-16 16:59:27 +01:00
return parent != null && parent.IsFolder;
2021-11-25 15:38:36 +01:00
}
/// <inheritdoc />
protected override void ValidationRules(ZeroValidator<Media> validator)
{
validator.RuleFor(x => x.Name).Length(2, 120);
validator.RuleFor(x => x.IsActive).Equal(true);
2021-12-16 16:59:27 +01:00
validator.When(x => x.IsFolder, () =>
{
validator.RuleFor(x => x.ParentId).Exists(Context.Store);
});
}
2021-11-24 14:37:32 +01:00
}
2021-11-25 15:38:36 +01:00
public interface IMediaStore : ITreeEntityStore<Media>
2021-11-24 14:37:32 +01:00
{
}