mirror of https://github.com/abpframework/abp.git
committed by
GitHub
34 changed files with 2458 additions and 77 deletions
File diff suppressed because it is too large
@ -0,0 +1,25 @@ |
|||
using Microsoft.EntityFrameworkCore.Migrations; |
|||
|
|||
namespace Volo.CmsKit.Migrations |
|||
{ |
|||
public partial class Added_EntityType_to_Media : Migration |
|||
{ |
|||
protected override void Up(MigrationBuilder migrationBuilder) |
|||
{ |
|||
migrationBuilder.AddColumn<string>( |
|||
name: "EntityType", |
|||
table: "CmsMediaDescriptors", |
|||
type: "nvarchar(64)", |
|||
maxLength: 64, |
|||
nullable: false, |
|||
defaultValue: ""); |
|||
} |
|||
|
|||
protected override void Down(MigrationBuilder migrationBuilder) |
|||
{ |
|||
migrationBuilder.DropColumn( |
|||
name: "EntityType", |
|||
table: "CmsMediaDescriptors"); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,4 @@ |
|||
using System.Runtime.CompilerServices; |
|||
|
|||
[assembly: InternalsVisibleTo("Volo.CmsKit.Domain.Tests", AllInternalsVisible = true)] |
|||
[assembly: InternalsVisibleTo("Volo.CmsKit.TestBase", AllInternalsVisible = true)] |
|||
@ -0,0 +1,15 @@ |
|||
using JetBrains.Annotations; |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace Volo.CmsKit.MediaDescriptors |
|||
{ |
|||
public class CmsKitMediaOptions |
|||
{ |
|||
[NotNull] |
|||
public List<MediaDescriptorDefinition> EntityTypes { get; } = new(); |
|||
} |
|||
} |
|||
@ -0,0 +1,47 @@ |
|||
using JetBrains.Annotations; |
|||
using Microsoft.Extensions.Options; |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.CmsKit.MediaDescriptors |
|||
{ |
|||
public class DefaultMediaDescriptorDefinitionStore : IMediaDescriptorDefinitionStore, ITransientDependency |
|||
{ |
|||
protected CmsKitMediaOptions Options { get; } |
|||
|
|||
public DefaultMediaDescriptorDefinitionStore(IOptions<CmsKitMediaOptions> options) |
|||
{ |
|||
Options = options.Value; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets single <see cref="MediaDescriptorDefinition"/> by entityType.
|
|||
/// </summary>
|
|||
/// <param name="entityType">EntityType to get definition.</param>
|
|||
/// <exception cref="EntityCantHaveMediaException">Thrown when EntityType is not configured as taggable.</exception>
|
|||
/// <exception cref="InvalidOperationException">More than one element satisfies the condition in predicate.</exception>
|
|||
public virtual Task<MediaDescriptorDefinition> GetDefinitionAsync([NotNull] string entityType) |
|||
{ |
|||
Check.NotNullOrWhiteSpace(entityType, nameof(entityType)); |
|||
|
|||
var result = Options.EntityTypes.SingleOrDefault(x => x.EntityType == entityType) ?? |
|||
throw new EntityCantHaveMediaException(entityType); |
|||
|
|||
return Task.FromResult(result); |
|||
} |
|||
|
|||
public virtual Task<bool> IsDefinedAsync([NotNull] string entityType) |
|||
{ |
|||
Check.NotNullOrEmpty(entityType, nameof(entityType)); |
|||
|
|||
var isDefined = Options.EntityTypes.Any(a => a.EntityType == entityType); |
|||
|
|||
return Task.FromResult(isDefined); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,21 @@ |
|||
using System.Runtime.Serialization; |
|||
using Volo.Abp; |
|||
|
|||
namespace Volo.CmsKit.MediaDescriptors |
|||
{ |
|||
public class EntityCantHaveMediaException : BusinessException |
|||
{ |
|||
public EntityCantHaveMediaException(SerializationInfo serializationInfo, StreamingContext context) : base(serializationInfo, context) |
|||
{ |
|||
} |
|||
|
|||
public EntityCantHaveMediaException(string entityType) |
|||
{ |
|||
EntityType = entityType; |
|||
Code = CmsKitErrorCodes.MediaDescriptors.EntityTypeDoesntExist; |
|||
WithData(nameof(entityType), EntityType); |
|||
} |
|||
|
|||
public string EntityType { get; } |
|||
} |
|||
} |
|||
@ -0,0 +1,12 @@ |
|||
using JetBrains.Annotations; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace Volo.CmsKit.MediaDescriptors |
|||
{ |
|||
public interface IMediaDescriptorDefinitionStore |
|||
{ |
|||
Task<bool> IsDefinedAsync([NotNull] string entityType); |
|||
|
|||
Task<MediaDescriptorDefinition> GetDefinitionAsync([NotNull] string entityType); |
|||
} |
|||
} |
|||
@ -1,9 +1,9 @@ |
|||
using System.IO; |
|||
using System.Linq; |
|||
|
|||
namespace Volo.CmsKit.MediaDescriptors.Extensions |
|||
namespace Volo.CmsKit.MediaDescriptors |
|||
{ |
|||
public static class MediaDescriptorCheck |
|||
public static class MediaDescriptorChecks |
|||
{ |
|||
public static bool IsValidMediaFileName(string name) |
|||
{ |
|||
@ -0,0 +1,20 @@ |
|||
using JetBrains.Annotations; |
|||
using System; |
|||
using System.Collections.Generic; |
|||
|
|||
namespace Volo.CmsKit.MediaDescriptors |
|||
{ |
|||
public class MediaDescriptorDefinition : PolicySpecifiedDefinition |
|||
{ |
|||
public MediaDescriptorDefinition( |
|||
[NotNull] string entityType, |
|||
IEnumerable<string> createPolicies = null, |
|||
IEnumerable<string> updatePolicies = null, |
|||
IEnumerable<string> deletePolicies = null) : base(entityType, |
|||
createPolicies, |
|||
updatePolicies, |
|||
deletePolicies) |
|||
{ |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,31 @@ |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.Domain.Services; |
|||
|
|||
namespace Volo.CmsKit.MediaDescriptors |
|||
{ |
|||
public class MediaDescriptorManager : DomainService |
|||
{ |
|||
protected IMediaDescriptorDefinitionStore MediaDescriptorDefinitionStore { get; } |
|||
|
|||
public MediaDescriptorManager(IMediaDescriptorDefinitionStore mediaDescriptorDefinitionStore) |
|||
{ |
|||
MediaDescriptorDefinitionStore = mediaDescriptorDefinitionStore; |
|||
} |
|||
|
|||
public virtual async Task<MediaDescriptor> CreateAsync(string entityType, string name, string mimeType, long size) |
|||
{ |
|||
if(!await MediaDescriptorDefinitionStore.IsDefinedAsync(entityType)) |
|||
{ |
|||
throw new EntityCantHaveMediaException(entityType); |
|||
} |
|||
|
|||
return new MediaDescriptor( |
|||
GuidGenerator.Create(), |
|||
entityType, |
|||
name, |
|||
mimeType, |
|||
size, |
|||
CurrentTenant.Id); |
|||
} |
|||
} |
|||
} |
|||
@ -1,30 +1,56 @@ |
|||
using JetBrains.Annotations; |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using Volo.Abp; |
|||
|
|||
namespace Volo.CmsKit |
|||
{ |
|||
public abstract class PolicySpecifiedDefinition |
|||
public abstract class PolicySpecifiedDefinition : IEquatable<PolicySpecifiedDefinition> |
|||
{ |
|||
protected PolicySpecifiedDefinition() |
|||
{ |
|||
} |
|||
|
|||
public PolicySpecifiedDefinition( |
|||
[CanBeNull] string createPolicy = null, |
|||
[CanBeNull] string updatePolicy = null, |
|||
[CanBeNull] string deletePolicy = null) |
|||
[NotNull] string entityType, |
|||
IEnumerable<string> createPolicies = null, |
|||
IEnumerable<string> updatePolicies = null, |
|||
IEnumerable<string> deletePolicies = null) |
|||
{ |
|||
CreatePolicy = createPolicy; |
|||
DeletePolicy = deletePolicy; |
|||
UpdatePolicy = updatePolicy; |
|||
EntityType = Check.NotNullOrEmpty(entityType, nameof(entityType)); |
|||
|
|||
if (createPolicies != null) |
|||
{ |
|||
CreatePolicies = CreatePolicies.Concat(createPolicies).ToList(); |
|||
} |
|||
|
|||
if (updatePolicies != null) |
|||
{ |
|||
UpdatePolicies = UpdatePolicies.Concat(updatePolicies).ToList(); |
|||
} |
|||
|
|||
if (deletePolicies != null) |
|||
{ |
|||
DeletePolicies = DeletePolicies.Concat(deletePolicies).ToList(); |
|||
} |
|||
} |
|||
|
|||
[CanBeNull] |
|||
public virtual string CreatePolicy { get; set; } |
|||
[NotNull] |
|||
public string EntityType { get; set; } |
|||
|
|||
[NotNull] |
|||
public virtual ICollection<string> CreatePolicies { get; } = new List<string>(); |
|||
|
|||
[CanBeNull] |
|||
public virtual string UpdatePolicy { get; set; } |
|||
[NotNull] |
|||
public virtual ICollection<string> UpdatePolicies { get; } = new List<string>(); |
|||
|
|||
[CanBeNull] |
|||
public virtual string DeletePolicy { get; set; } |
|||
[NotNull] |
|||
public virtual ICollection<string> DeletePolicies { get; } = new List<string>(); |
|||
|
|||
public bool Equals(PolicySpecifiedDefinition other) |
|||
{ |
|||
return other?.EntityType == EntityType; |
|||
} |
|||
} |
|||
} |
|||
|
|||
@ -0,0 +1,44 @@ |
|||
using Shouldly; |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
using Volo.CmsKit.Blogs; |
|||
using Xunit; |
|||
|
|||
namespace Volo.CmsKit.MediaDescriptors |
|||
{ |
|||
public class MediaDescriptorManager_Test : CmsKitDomainTestBase |
|||
{ |
|||
private readonly MediaDescriptorManager manager; |
|||
private readonly CmsKitTestData testData; |
|||
|
|||
public MediaDescriptorManager_Test() |
|||
{ |
|||
manager = GetRequiredService<MediaDescriptorManager>(); |
|||
testData = GetRequiredService<CmsKitTestData>(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task CreateAsync_ShouldWorkProperly_WithDefinedEntityType() |
|||
{ |
|||
var created = await manager.CreateAsync(testData.Media_1_EntityType, "MyAwesomeImage.png", "image/png", 128000); |
|||
|
|||
created.ShouldNotBeNull(); |
|||
created.Id.ShouldNotBe(Guid.Empty); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task CreateAsync_ShouldThrowException_WithUndefinedEntityType() |
|||
{ |
|||
var undefinedEntityType = "My.Any.EntityType"; |
|||
|
|||
var exception = await Should.ThrowAsync<EntityCantHaveMediaException>(async () => |
|||
await manager.CreateAsync(undefinedEntityType, "import.json", "application/json", 256000)); |
|||
|
|||
exception.ShouldNotBeNull(); |
|||
exception.EntityType.ShouldBe(undefinedEntityType); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue