Browse Source

Refactor the ReactionManager.

pull/4809/head
Halil İbrahim Kalkan 6 years ago
parent
commit
e6ba0af734
  1. 4
      modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Reactions/DefaultReactionDefinitionStore.cs
  2. 7
      modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Reactions/IReactionDefinitionStore.cs
  3. 81
      modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Reactions/ReactionManager.cs
  4. 34
      modules/cms-kit/src/Volo.CmsKit.Public.Application/Volo/CmsKit/Reactions/ReactionPublicAppService.cs

4
modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Reactions/DefaultReactionDefinitionStore.cs

@ -15,12 +15,12 @@ namespace Volo.CmsKit.Reactions
Options = options.Value; Options = options.Value;
} }
public virtual Task<List<ReactionDefinition>> GetAvailableReactionsAsync(string entityType) public virtual Task<List<ReactionDefinition>> GetReactionsAsync(string entityType = null)
{ {
return Task.FromResult(Options.Reactions.Values.ToList()); return Task.FromResult(Options.Reactions.Values.ToList());
} }
public Task<ReactionDefinition> GetReactionOrNullAsync(string reactionName, string entityType) public Task<ReactionDefinition> GetReactionOrNullAsync(string reactionName, string entityType = null)
{ {
return Task.FromResult(Options.Reactions.GetOrDefault(reactionName)); return Task.FromResult(Options.Reactions.GetOrDefault(reactionName));
} }

7
modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Reactions/IReactionDefinitionStore.cs

@ -1,5 +1,4 @@
using System; using System.Collections.Generic;
using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using JetBrains.Annotations; using JetBrains.Annotations;
@ -7,8 +6,8 @@ namespace Volo.CmsKit.Reactions
{ {
public interface IReactionDefinitionStore public interface IReactionDefinitionStore
{ {
Task<List<ReactionDefinition>> GetAvailableReactionsAsync([CanBeNull] string entityType); Task<List<ReactionDefinition>> GetReactionsAsync([CanBeNull] string entityType = null);
Task<ReactionDefinition> GetReactionOrNullAsync([NotNull] string reactionName, [CanBeNull] string entityType); Task<ReactionDefinition> GetReactionOrNullAsync([NotNull] string reactionName, [CanBeNull] string entityType = null);
} }
} }

81
modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Reactions/ReactionManager.cs

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using JetBrains.Annotations; using JetBrains.Annotations;
using Volo.Abp; using Volo.Abp;
@ -19,80 +20,34 @@ namespace Volo.CmsKit.Reactions
ReactionDefinitionStore = reactionDefinitionStore; ReactionDefinitionStore = reactionDefinitionStore;
} }
public virtual async Task<List<ReactionDefinition>> GetAvailableReactionsAsync( public virtual async Task<List<ReactionDefinition>> GetReactionsAsync(
[CanBeNull] string entityType = null) [CanBeNull] string entityType = null)
{ {
return await ReactionDefinitionStore.GetAvailableReactionsAsync(entityType); return await ReactionDefinitionStore.GetReactionsAsync(entityType);
} }
public virtual async Task<List<ReactionSummary>> GetSummariesAsync( public virtual async Task<List<ReactionSummary>> GetSummariesAsync(
[NotNull] string entityType, [NotNull] string entityType,
[NotNull] string entityId) [NotNull] string entityId)
{ {
var queryResultItems = await UserReactionRepository.GetSummariesAsync(entityType, entityId); Check.NotNullOrWhiteSpace(entityType, nameof(entityType));
Check.NotNullOrWhiteSpace(entityId, nameof(entityId));
var summaryDtos = new List<ReactionSummary>();
foreach (var queryResultItem in queryResultItems)
{
var summary = new ReactionSummary
{
Count = queryResultItem.Count
};
//TODO: Get all definitions then filter here?
var reactionDefinition = await ReactionDefinitionStore
.GetReactionOrNullAsync(
queryResultItem.ReactionName,
entityType
);
if (reactionDefinition == null)
{
continue;
}
summary.Reaction = reactionDefinition;
summaryDtos.Add(summary); var userReactionCounts = (await UserReactionRepository.GetSummariesAsync(entityType, entityId))
} .ToDictionary(x => x.ReactionName, x => x.Count);
return summaryDtos; var reactions = await ReactionDefinitionStore
} .GetReactionsAsync(
entityType
public virtual async Task<List<ReactionDefinition>> GetUserReactionsAsync(
Guid userId,
[NotNull] string entityType,
[NotNull] string entityId)
{
var userReactions = await UserReactionRepository
.GetListForUserAsync(
userId,
entityType,
entityId
); );
var reactionDtos = new List<ReactionDefinition>(); return reactions
.Select(reaction => new ReactionSummary
foreach (var userReaction in userReactions)
{
//TODO: Get all definitions then filter here?
var reactionDefinition = await ReactionDefinitionStore
.GetReactionOrNullAsync(
userReaction.ReactionName,
userReaction.EntityType
);
if (reactionDefinition == null)
{ {
await UserReactionRepository.DeleteAsync(userReaction); Reaction = reaction,
continue; Count = userReactionCounts.GetOrDefault(reaction.Name)
} })
.ToList();
reactionDtos.Add(reactionDefinition);
}
return reactionDtos;
} }
public virtual async Task<UserReaction> CreateAsync( public virtual async Task<UserReaction> CreateAsync(
@ -129,6 +84,10 @@ namespace Volo.CmsKit.Reactions
[NotNull] string entityId, [NotNull] string entityId,
[NotNull] string reactionName) [NotNull] string reactionName)
{ {
Check.NotNullOrWhiteSpace(entityType, nameof(entityType));
Check.NotNullOrWhiteSpace(entityId, nameof(entityId));
Check.NotNullOrWhiteSpace(reactionName, nameof(reactionName));
var existingReaction = await UserReactionRepository.FindAsync(userId, entityType, entityId, reactionName); var existingReaction = await UserReactionRepository.FindAsync(userId, entityType, entityId, reactionName);
if (existingReaction == null) if (existingReaction == null)
{ {

34
modules/cms-kit/src/Volo.CmsKit.Public.Application/Volo/CmsKit/Reactions/ReactionPublicAppService.cs

@ -27,36 +27,30 @@ namespace Volo.CmsKit.Reactions
public virtual async Task<ListResultDto<ReactionWithSelectionDto>> GetForSelectionAsync(string entityType, string entityId) public virtual async Task<ListResultDto<ReactionWithSelectionDto>> GetForSelectionAsync(string entityType, string entityId)
{ {
var reactionDefinitions = await ReactionManager var summaries = await ReactionManager.GetSummariesAsync(entityType, entityId);
.GetAvailableReactionsAsync(
entityType
);
var summaries =
(await ReactionManager.GetSummariesAsync(entityType, entityId))
.ToDictionary(x => x.Reaction.Name, x => x.Count);
var userReactions = await ReactionManager.GetUserReactionsAsync( var userReactions = (await UserReactionRepository
CurrentUser.GetId(), .GetListForUserAsync(
entityType, CurrentUser.GetId(),
entityId entityType,
); entityId
)).ToDictionary(x => x.ReactionName, x => x);
var reactionDtos = new List<ReactionWithSelectionDto>(); var reactionWithSelectionDtos = new List<ReactionWithSelectionDto>();
foreach (var reactionDefinition in reactionDefinitions) foreach (var summary in summaries)
{ {
reactionDtos.Add( reactionWithSelectionDtos.Add(
new ReactionWithSelectionDto new ReactionWithSelectionDto
{ {
Reaction = ConvertToReactionDto(reactionDefinition), Reaction = ConvertToReactionDto(summary.Reaction),
Count = summaries.GetOrDefault(reactionDefinition.Name), Count = summary.Count,
IsSelectedByCurrentUser = userReactions.Any(x => x.Name == reactionDefinition.Name) IsSelectedByCurrentUser = userReactions.ContainsKey(summary.Reaction.Name)
} }
); );
} }
return new ListResultDto<ReactionWithSelectionDto>(reactionDtos); return new ListResultDto<ReactionWithSelectionDto>(reactionWithSelectionDtos);
} }
public virtual async Task CreateAsync(CreateReactionDto input) public virtual async Task CreateAsync(CreateReactionDto input)

Loading…
Cancel
Save