Browse Source

Implemented EF Core integration for reactions

pull/4776/head
Halil İbrahim Kalkan 6 years ago
parent
commit
fc3473f85d
  1. 1079
      modules/cms-kit/host/Volo.CmsKit.Web.Unified/Migrations/20200715191733_Added_UserReactions.Designer.cs
  2. 43
      modules/cms-kit/host/Volo.CmsKit.Web.Unified/Migrations/20200715191733_Added_UserReactions.cs
  3. 38
      modules/cms-kit/host/Volo.CmsKit.Web.Unified/Migrations/UnifiedDbContextModelSnapshot.cs
  4. 4
      modules/cms-kit/host/Volo.CmsKit.Web.Unified/Pages/Index.cshtml.cs
  5. 10
      modules/cms-kit/src/Volo.CmsKit.Domain.Shared/Volo/CmsKit/Reactions/UserReactionConsts.cs
  6. 2
      modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/CmsKitDbProperties.cs
  7. 2
      modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Reactions/IUserReactionRepository.cs
  8. 9
      modules/cms-kit/src/Volo.CmsKit.EntityFrameworkCore/Volo/CmsKit/EntityFrameworkCore/CmsKitDbContext.cs
  9. 28
      modules/cms-kit/src/Volo.CmsKit.EntityFrameworkCore/Volo/CmsKit/EntityFrameworkCore/CmsKitDbContextModelCreatingExtensions.cs
  10. 10
      modules/cms-kit/src/Volo.CmsKit.EntityFrameworkCore/Volo/CmsKit/EntityFrameworkCore/ICmsKitDbContext.cs
  11. 42
      modules/cms-kit/src/Volo.CmsKit.EntityFrameworkCore/Volo/CmsKit/Reactions/EfCoreUserReactionRepository.cs
  12. 15
      modules/cms-kit/src/Volo.CmsKit.Public.Application/Volo/CmsKit/Reactions/ReactionPublicAppService.cs
  13. 5
      modules/cms-kit/src/Volo.CmsKit.Public.Web/Pages/CmsKit/Shared/Components/ReactionSelection/Default.cshtml

1079
modules/cms-kit/host/Volo.CmsKit.Web.Unified/Migrations/20200715191733_Added_UserReactions.Designer.cs

File diff suppressed because it is too large

43
modules/cms-kit/host/Volo.CmsKit.Web.Unified/Migrations/20200715191733_Added_UserReactions.cs

@ -0,0 +1,43 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
namespace Volo.CmsKit.Migrations
{
public partial class Added_UserReactions : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "CmsUserReactions",
columns: table => new
{
Id = table.Column<Guid>(nullable: false),
EntityType = table.Column<string>(maxLength: 64, nullable: false),
EntityId = table.Column<string>(maxLength: 64, nullable: false),
ReactionName = table.Column<string>(maxLength: 32, nullable: false),
CreationTime = table.Column<DateTime>(nullable: false),
UserId = table.Column<Guid>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_CmsUserReactions", x => x.Id);
});
migrationBuilder.CreateIndex(
name: "IX_CmsUserReactions_EntityType_EntityId",
table: "CmsUserReactions",
columns: new[] { "EntityType", "EntityId" });
migrationBuilder.CreateIndex(
name: "IX_CmsUserReactions_UserId_EntityType_EntityId_ReactionName",
table: "CmsUserReactions",
columns: new[] { "UserId", "EntityType", "EntityId", "ReactionName" });
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "CmsUserReactions");
}
}
}

38
modules/cms-kit/host/Volo.CmsKit.Web.Unified/Migrations/UnifiedDbContextModelSnapshot.cs

@ -4,8 +4,8 @@ using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using Volo.CmsKit.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;
using Volo.CmsKit.EntityFrameworkCore;
namespace Volo.CmsKit.Migrations
{
@ -912,6 +912,42 @@ namespace Volo.CmsKit.Migrations
b.ToTable("AbpTenantConnectionStrings");
});
modelBuilder.Entity("Volo.CmsKit.Reactions.UserReaction", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uniqueidentifier");
b.Property<DateTime>("CreationTime")
.HasColumnType("datetime2");
b.Property<string>("EntityId")
.IsRequired()
.HasColumnType("nvarchar(64)")
.HasMaxLength(64);
b.Property<string>("EntityType")
.IsRequired()
.HasColumnType("nvarchar(64)")
.HasMaxLength(64);
b.Property<string>("ReactionName")
.IsRequired()
.HasColumnType("nvarchar(32)")
.HasMaxLength(32);
b.Property<Guid>("UserId")
.HasColumnType("uniqueidentifier");
b.HasKey("Id");
b.HasIndex("EntityType", "EntityId");
b.HasIndex("UserId", "EntityType", "EntityId", "ReactionName");
b.ToTable("CmsUserReactions");
});
modelBuilder.Entity("Volo.Abp.AuditLogging.AuditLogAction", b =>
{
b.HasOne("Volo.Abp.AuditLogging.AuditLog", null)

4
modules/cms-kit/host/Volo.CmsKit.Web.Unified/Pages/Index.cshtml.cs

@ -1,11 +1,13 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace Volo.CmsKit.Pages
{
[Authorize]
public class IndexModel : PageModel
{
public void OnGet()
{
}
}
}
}

10
modules/cms-kit/src/Volo.CmsKit.Domain.Shared/Volo/CmsKit/Reactions/UserReactionConsts.cs

@ -0,0 +1,10 @@
namespace Volo.CmsKit.Reactions
{
public static class UserReactionConsts
{
public static int EntityTypeLength { get; set; } = 64;
public static int EntityIdLength { get; set; } = 64;
public static int ReactionNameLength { get; set; } = 32;
}
}

2
modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/CmsKitDbProperties.cs

@ -2,7 +2,7 @@
{
public static class CmsKitDbProperties
{
public static string DbTablePrefix { get; set; } = "CmsKit";
public static string DbTablePrefix { get; set; } = "Cms";
public static string DbSchema { get; set; } = null;

2
modules/cms-kit/src/Volo.CmsKit.Domain/Volo/CmsKit/Reactions/IUserReactionRepository.cs

@ -16,6 +16,6 @@ namespace Volo.CmsKit.Reactions
Task<List<UserReaction>> GetListForUserAsync(Guid userId, string entityType, string entityId);
Task<List<ReactionSummaryQueryResultItem>> GetSummariesAsync(string inputEntityType, string inputEntityId);
Task<List<ReactionSummaryQueryResultItem>> GetSummariesAsync(string entityType, string entityId);
}
}

9
modules/cms-kit/src/Volo.CmsKit.EntityFrameworkCore/Volo/CmsKit/EntityFrameworkCore/CmsKitDbContext.cs

@ -1,17 +1,16 @@
using Microsoft.EntityFrameworkCore;
using Volo.Abp.Data;
using Volo.Abp.EntityFrameworkCore;
using Volo.CmsKit.Reactions;
namespace Volo.CmsKit.EntityFrameworkCore
{
[ConnectionStringName(CmsKitDbProperties.ConnectionStringName)]
public class CmsKitDbContext : AbpDbContext<CmsKitDbContext>, ICmsKitDbContext
{
/* Add DbSet for each Aggregate Root here. Example:
* public DbSet<Question> Questions { get; set; }
*/
public DbSet<UserReaction> UserReactions { get; set; }
public CmsKitDbContext(DbContextOptions<CmsKitDbContext> options)
public CmsKitDbContext(DbContextOptions<CmsKitDbContext> options)
: base(options)
{
@ -24,4 +23,4 @@ namespace Volo.CmsKit.EntityFrameworkCore
builder.ConfigureCmsKit();
}
}
}
}

28
modules/cms-kit/src/Volo.CmsKit.EntityFrameworkCore/Volo/CmsKit/EntityFrameworkCore/CmsKitDbContextModelCreatingExtensions.cs

@ -1,6 +1,8 @@
using System;
using Microsoft.EntityFrameworkCore;
using Volo.Abp;
using Volo.Abp.EntityFrameworkCore.Modeling;
using Volo.CmsKit.Reactions;
namespace Volo.CmsKit.EntityFrameworkCore
{
@ -19,25 +21,19 @@ namespace Volo.CmsKit.EntityFrameworkCore
optionsAction?.Invoke(options);
/* Configure all entities here. Example:
builder.Entity<Question>(b =>
builder.Entity<UserReaction>(b =>
{
//Configure table & schema name
b.ToTable(options.TablePrefix + "Questions", options.Schema);
b.ToTable(options.TablePrefix + "UserReactions", options.Schema);
b.ConfigureByConvention();
//Properties
b.Property(q => q.Title).IsRequired().HasMaxLength(QuestionConsts.MaxTitleLength);
//Relations
b.HasMany(question => question.Tags).WithOne().HasForeignKey(qt => qt.QuestionId);
//Indexes
b.HasIndex(q => q.CreationTime);
b.Property(x => x.EntityType).IsRequired().HasMaxLength(UserReactionConsts.EntityTypeLength);
b.Property(x => x.EntityId).IsRequired().HasMaxLength(UserReactionConsts.EntityIdLength);
b.Property(x => x.ReactionName).IsRequired().HasMaxLength(UserReactionConsts.ReactionNameLength);
b.Property(x => x.CreationTime);
b.HasIndex(x => new { x.EntityType, x.EntityId });
b.HasIndex(x => new { x.UserId, x.EntityType, x.EntityId, x.ReactionName });
});
*/
}
}
}
}

10
modules/cms-kit/src/Volo.CmsKit.EntityFrameworkCore/Volo/CmsKit/EntityFrameworkCore/ICmsKitDbContext.cs

@ -1,13 +1,13 @@
using Volo.Abp.Data;
using Microsoft.EntityFrameworkCore;
using Volo.Abp.Data;
using Volo.Abp.EntityFrameworkCore;
using Volo.CmsKit.Reactions;
namespace Volo.CmsKit.EntityFrameworkCore
{
[ConnectionStringName(CmsKitDbProperties.ConnectionStringName)]
public interface ICmsKitDbContext : IEfCoreDbContext
{
/* Add DbSet for each Aggregate Root here. Example:
* DbSet<Question> Questions { get; }
*/
DbSet<UserReaction> UserReactions { get; }
}
}
}

42
modules/cms-kit/src/Volo.CmsKit.EntityFrameworkCore/Volo/CmsKit/Reactions/EfCoreUserReactionRepository.cs

@ -1,32 +1,60 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Volo.Abp.Domain.Repositories.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;
using Volo.CmsKit.EntityFrameworkCore;
namespace Volo.CmsKit.Reactions
{
public class EfCoreUserReactionRepository : EfCoreRepository<ICmsKitDbContext, UserReaction, Guid>, IUserReactionRepository
public class EfCoreUserReactionRepository : EfCoreRepository<ICmsKitDbContext, UserReaction, Guid>,
IUserReactionRepository
{
public EfCoreUserReactionRepository(IDbContextProvider<ICmsKitDbContext> dbContextProvider)
: base(dbContextProvider)
{
}
public Task<UserReaction> FindAsync(Guid userId, string entityType, string entityId, string reactionName)
public async Task<UserReaction> FindAsync(
Guid userId,
string entityType,
string entityId,
string reactionName)
{
throw new NotImplementedException();
return await DbSet
.Where(x =>
x.UserId == userId &&
x.EntityType == entityType &&
x.EntityId == entityId &&
x.ReactionName == reactionName)
.FirstOrDefaultAsync();
}
public Task<List<UserReaction>> GetListForUserAsync(Guid userId, string entityType, string entityId)
public async Task<List<UserReaction>> GetListForUserAsync(Guid userId, string entityType, string entityId)
{
throw new NotImplementedException();
return await DbSet
.Where(x =>
x.UserId == userId &&
x.EntityType == entityType &&
x.EntityId == entityId)
.ToListAsync();
}
public Task<List<ReactionSummaryQueryResultItem>> GetSummariesAsync(string inputEntityType, string inputEntityId)
public async Task<List<ReactionSummaryQueryResultItem>> GetSummariesAsync(string entityType, string entityId)
{
throw new NotImplementedException();
return await DbSet
.Where(x =>
x.EntityType == entityType &&
x.EntityId == entityId)
.GroupBy(x => x.ReactionName)
.Select(g => new ReactionSummaryQueryResultItem
{
ReactionName = g.Key,
Count = g.Count()
})
.ToListAsync();
}
}
}

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

@ -1,6 +1,8 @@
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading.Tasks;
using System.Xml.Schema;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Users;
@ -76,8 +78,15 @@ namespace Volo.CmsKit.Reactions
.GetAvailableReactionsAsync(
input.EntityType
);
var summaries =
(await ReactionManager.GetSummariesAsync(input.EntityType, input.EntityId))
.ToDictionary(x => x.Reaction.Name, x => x.Count);
//var summaries = await ReactionManager.GetSummariesAsync(input.EntityType, input.EntityId);
var userReactions = await ReactionManager.GetUserReactionsAsync(
CurrentUser.GetId(),
input.EntityType,
input.EntityId
);
var reactionDtos = new List<ReactionWithSelectionDto>();
@ -87,8 +96,8 @@ namespace Volo.CmsKit.Reactions
new ReactionWithSelectionDto
{
Reaction = ConvertToReactionDto(reactionDefinition),
Count = 0,
IsSelectedByCurrentUser = false
Count = summaries.GetOrDefault(reactionDefinition.Name),
IsSelectedByCurrentUser = userReactions.Any(x => x.Name == reactionDefinition.Name)
}
);
}

5
modules/cms-kit/src/Volo.CmsKit.Public.Web/Pages/CmsKit/Shared/Components/ReactionSelection/Default.cshtml

@ -4,13 +4,14 @@
Pick a reaction:
@foreach (var reaction in Model.Reactions)
{
<cms-icon name="@reaction.Icon" highlight="@reaction.IsSelectedByCurrentUser"/>
<span class="mr-2"><cms-icon name="@reaction.Icon" highlight="@reaction.IsSelectedByCurrentUser"/></span>
}
</div>
<div class="cms-reaction-selection-reactions">
Current reactions:
@foreach (var reaction in Model.Reactions.Where(r => r.Count > 0))
{
<cms-icon name="@reaction.Icon" highlight="@reaction.IsSelectedByCurrentUser"/><span> - @reaction.Count |</span>
<span class="mr-2"><cms-icon name="@reaction.Icon" highlight="@reaction.IsSelectedByCurrentUser"/><span> @reaction.Count</span></span>
}
</div>
</span>

Loading…
Cancel
Save