mirror of https://github.com/abpframework/abp.git
committed by
GitHub
30 changed files with 318 additions and 96 deletions
@ -1,17 +1,7 @@ |
|||
using JetBrains.Annotations; |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
using Volo.CmsKit.Comments; |
|||
|
|||
namespace Volo.CmsKit.Comments |
|||
namespace Volo.CmsKit.Comments |
|||
{ |
|||
public interface ICommentEntityTypeDefinitionStore |
|||
public interface ICommentEntityTypeDefinitionStore : IEntityTypeDefinitionStore<CommentEntityTypeDefinition> |
|||
{ |
|||
Task<CommentEntityTypeDefinition> GetDefinitionAsync([NotNull] string entityType); |
|||
|
|||
Task<bool> IsDefinedAsync([NotNull] string entityType); |
|||
} |
|||
} |
|||
|
|||
@ -0,0 +1,22 @@ |
|||
using JetBrains.Annotations; |
|||
using System; |
|||
using Volo.Abp; |
|||
|
|||
namespace Volo.CmsKit |
|||
{ |
|||
public abstract class EntityTypeDefinition : IEquatable<EntityTypeDefinition> |
|||
{ |
|||
public EntityTypeDefinition([NotNull] string entityType) |
|||
{ |
|||
EntityType = Check.NotNullOrEmpty(entityType, nameof(entityType)); |
|||
} |
|||
|
|||
[NotNull] |
|||
public string EntityType { get; protected set; } |
|||
|
|||
public bool Equals(EntityTypeDefinition other) |
|||
{ |
|||
return EntityType == other?.EntityType; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,14 @@ |
|||
using JetBrains.Annotations; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.DependencyInjection; |
|||
|
|||
namespace Volo.CmsKit |
|||
{ |
|||
public interface IEntityTypeDefinitionStore<TPolicyDefinition> : ITransientDependency |
|||
where TPolicyDefinition : EntityTypeDefinition |
|||
{ |
|||
Task<TPolicyDefinition> GetAsync([NotNull] string entityType); |
|||
|
|||
Task<bool> IsDefinedAsync([NotNull] string entityType); |
|||
} |
|||
} |
|||
@ -1,12 +1,7 @@ |
|||
using JetBrains.Annotations; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace Volo.CmsKit.MediaDescriptors |
|||
namespace Volo.CmsKit.MediaDescriptors |
|||
{ |
|||
public interface IMediaDescriptorDefinitionStore |
|||
public interface IMediaDescriptorDefinitionStore : IEntityTypeDefinitionStore<MediaDescriptorDefinition> |
|||
{ |
|||
Task<bool> IsDefinedAsync([NotNull] string entityType); |
|||
|
|||
Task<MediaDescriptorDefinition> GetDefinitionAsync([NotNull] string entityType); |
|||
} |
|||
} |
|||
|
|||
@ -0,0 +1,11 @@ |
|||
using JetBrains.Annotations; |
|||
using System.Collections.Generic; |
|||
|
|||
namespace Volo.CmsKit.Ratings |
|||
{ |
|||
public class CmsKitRatingOptions |
|||
{ |
|||
[NotNull] |
|||
public List<RatingEntityTypeDefinition> EntityTypes { get; } = new (); |
|||
} |
|||
} |
|||
@ -0,0 +1,38 @@ |
|||
using JetBrains.Annotations; |
|||
using Microsoft.Extensions.Options; |
|||
using System; |
|||
using System.Linq; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp; |
|||
|
|||
namespace Volo.CmsKit.Ratings |
|||
{ |
|||
public class DefaultRatingEntityTypeDefinitionStore : IRatingEntityTypeDefinitionStore |
|||
{ |
|||
protected CmsKitRatingOptions Options { get; } |
|||
|
|||
public DefaultRatingEntityTypeDefinitionStore(IOptions<CmsKitRatingOptions> options) |
|||
{ |
|||
Options = options.Value; |
|||
} |
|||
|
|||
public virtual Task<RatingEntityTypeDefinition> GetAsync([NotNull] string entityType) |
|||
{ |
|||
Check.NotNullOrWhiteSpace(entityType, nameof(entityType)); |
|||
|
|||
var definition = Options.EntityTypes.SingleOrDefault(x => x.EntityType.Equals(entityType, StringComparison.InvariantCultureIgnoreCase)) ?? |
|||
throw new EntityCantHaveRatingException(entityType); |
|||
|
|||
return Task.FromResult(definition); |
|||
} |
|||
|
|||
public virtual Task<bool> IsDefinedAsync([NotNull] string entityType) |
|||
{ |
|||
Check.NotNullOrWhiteSpace(entityType, nameof(entityType)); |
|||
|
|||
var isDefined = Options.EntityTypes.Any(x => x.EntityType.Equals(entityType, StringComparison.InvariantCultureIgnoreCase)); |
|||
|
|||
return Task.FromResult(isDefined); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,22 @@ |
|||
using JetBrains.Annotations; |
|||
using System.Runtime.Serialization; |
|||
using Volo.Abp; |
|||
|
|||
namespace Volo.CmsKit.Ratings |
|||
{ |
|||
public class EntityCantHaveRatingException : BusinessException |
|||
{ |
|||
public EntityCantHaveRatingException(SerializationInfo serializationInfo, StreamingContext context) : base(serializationInfo, context) |
|||
{ |
|||
} |
|||
|
|||
public EntityCantHaveRatingException([NotNull] string entityType) |
|||
{ |
|||
Code = CmsKitErrorCodes.Ratings.EntityCantHaveRating; |
|||
EntityType = Check.NotNullOrEmpty(entityType, nameof(entityType)); |
|||
WithData(nameof(EntityType), EntityType); |
|||
} |
|||
|
|||
public string EntityType { get; } |
|||
} |
|||
} |
|||
@ -0,0 +1,9 @@ |
|||
using Volo.CmsKit.Ratings; |
|||
|
|||
namespace Volo.CmsKit.Ratings |
|||
{ |
|||
public interface IRatingEntityTypeDefinitionStore : IEntityTypeDefinitionStore<RatingEntityTypeDefinition> |
|||
{ |
|||
|
|||
} |
|||
} |
|||
@ -0,0 +1,12 @@ |
|||
using JetBrains.Annotations; |
|||
|
|||
namespace Volo.CmsKit.Ratings |
|||
{ |
|||
public class RatingEntityTypeDefinition : EntityTypeDefinition |
|||
{ |
|||
public RatingEntityTypeDefinition( |
|||
[NotNull] string entityType) : base(entityType) |
|||
{ |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,52 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.Domain.Services; |
|||
using Volo.CmsKit.Users; |
|||
|
|||
namespace Volo.CmsKit.Ratings |
|||
{ |
|||
public class RatingManager : DomainService |
|||
{ |
|||
protected IRatingRepository RatingRepository { get; } |
|||
protected IRatingEntityTypeDefinitionStore RatingDefinitionStore { get; } |
|||
|
|||
public RatingManager( |
|||
IRatingRepository ratingRepository, |
|||
IRatingEntityTypeDefinitionStore ratingDefinitionStore) |
|||
{ |
|||
RatingRepository = ratingRepository; |
|||
RatingDefinitionStore = ratingDefinitionStore; |
|||
} |
|||
|
|||
public async Task<Rating> SetStarAsync(CmsUser user, string entityType, string entityId, short starCount) |
|||
{ |
|||
var currentUserRating = await RatingRepository.GetCurrentUserRatingAsync(entityType, entityId, user.Id); |
|||
|
|||
if (currentUserRating != null) |
|||
{ |
|||
currentUserRating.SetStarCount(starCount); |
|||
|
|||
return await RatingRepository.UpdateAsync(currentUserRating); |
|||
} |
|||
|
|||
if (!await RatingDefinitionStore.IsDefinedAsync(entityType)) |
|||
{ |
|||
throw new EntityCantHaveRatingException(entityType); |
|||
} |
|||
|
|||
return await RatingRepository.InsertAsync( |
|||
new Rating( |
|||
GuidGenerator.Create(), |
|||
entityType, |
|||
entityId, |
|||
starCount, |
|||
user.Id, |
|||
CurrentTenant.Id |
|||
) |
|||
); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,66 @@ |
|||
using Shouldly; |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
using Volo.CmsKit.Blogs; |
|||
using Volo.CmsKit.Users; |
|||
using Xunit; |
|||
|
|||
namespace Volo.CmsKit.Ratings |
|||
{ |
|||
public class RatingManager_Test : CmsKitDomainTestBase |
|||
{ |
|||
private readonly CmsKitTestData _cmsKitTestData; |
|||
private readonly RatingManager _ratingManager; |
|||
private readonly ICmsUserRepository _userRepository; |
|||
|
|||
public RatingManager_Test() |
|||
{ |
|||
_cmsKitTestData = GetRequiredService<CmsKitTestData>(); |
|||
_ratingManager = GetRequiredService<RatingManager>(); |
|||
_userRepository = GetRequiredService<ICmsUserRepository>(); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task SetStarAsync_ShouldCreate_WhenFirstCall() |
|||
{ |
|||
var user = await _userRepository.GetAsync(_cmsKitTestData.User1Id); |
|||
short starCount = 4; |
|||
|
|||
var rating = await _ratingManager.SetStarAsync(user, _cmsKitTestData.EntityType1, _cmsKitTestData.BlogPost_1_Id.ToString(), starCount); |
|||
|
|||
rating.ShouldNotBeNull(); |
|||
rating.Id.ShouldNotBe(Guid.Empty); |
|||
rating.StarCount.ShouldBe(starCount); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task SetStarAsync_ShouldUpdate_WithExistingRating() |
|||
{ |
|||
var user = await _userRepository.GetAsync(_cmsKitTestData.User1Id); |
|||
short starCount = 2; |
|||
|
|||
var rating = await _ratingManager.SetStarAsync(user, _cmsKitTestData.EntityType1, _cmsKitTestData.EntityId1, starCount); |
|||
|
|||
rating.ShouldNotBeNull(); |
|||
rating.Id.ShouldNotBe(Guid.Empty); |
|||
rating.StarCount.ShouldBe(starCount); |
|||
} |
|||
|
|||
[Fact] |
|||
public async Task SetStarAsync_ShouldThrowException_WithNotConfiguredentityType() |
|||
{ |
|||
var user = await _userRepository.GetAsync(_cmsKitTestData.User1Id); |
|||
var notConfiguredEntityType = "AnyOtherEntityType"; |
|||
short starCount = 3; |
|||
|
|||
var exception = await Should.ThrowAsync<EntityCantHaveRatingException>(async () => |
|||
await _ratingManager.SetStarAsync(user, notConfiguredEntityType, "1", starCount)); |
|||
|
|||
exception.ShouldNotBeNull(); |
|||
exception.EntityType.ShouldBe(notConfiguredEntityType); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue