mirror of https://github.com/abpframework/abp.git
10 changed files with 313 additions and 2 deletions
@ -0,0 +1,95 @@ |
|||
// <auto-generated />
|
|||
using System; |
|||
using DistDemoApp; |
|||
using Microsoft.EntityFrameworkCore; |
|||
using Microsoft.EntityFrameworkCore.Infrastructure; |
|||
using Microsoft.EntityFrameworkCore.Metadata; |
|||
using Microsoft.EntityFrameworkCore.Migrations; |
|||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion; |
|||
using Volo.Abp.EntityFrameworkCore; |
|||
|
|||
namespace DistDemoApp.Migrations |
|||
{ |
|||
[DbContext(typeof(TodoDbContext))] |
|||
[Migration("20210825112717_Added_Summary_Table")] |
|||
partial class Added_Summary_Table |
|||
{ |
|||
protected override void BuildTargetModel(ModelBuilder modelBuilder) |
|||
{ |
|||
#pragma warning disable 612, 618
|
|||
modelBuilder |
|||
.HasAnnotation("_Abp_DatabaseProvider", EfCoreDatabaseProvider.SqlServer) |
|||
.HasAnnotation("Relational:MaxIdentifierLength", 128) |
|||
.HasAnnotation("ProductVersion", "5.0.9") |
|||
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); |
|||
|
|||
modelBuilder.Entity("DistDemoApp.TodoItem", b => |
|||
{ |
|||
b.Property<Guid>("Id") |
|||
.HasColumnType("uniqueidentifier"); |
|||
|
|||
b.Property<string>("ConcurrencyStamp") |
|||
.IsConcurrencyToken() |
|||
.HasMaxLength(40) |
|||
.HasColumnType("nvarchar(40)") |
|||
.HasColumnName("ConcurrencyStamp"); |
|||
|
|||
b.Property<DateTime>("CreationTime") |
|||
.HasColumnType("datetime2") |
|||
.HasColumnName("CreationTime"); |
|||
|
|||
b.Property<Guid?>("CreatorId") |
|||
.HasColumnType("uniqueidentifier") |
|||
.HasColumnName("CreatorId"); |
|||
|
|||
b.Property<string>("ExtraProperties") |
|||
.HasColumnType("nvarchar(max)") |
|||
.HasColumnName("ExtraProperties"); |
|||
|
|||
b.Property<string>("Text") |
|||
.IsRequired() |
|||
.HasMaxLength(128) |
|||
.HasColumnType("nvarchar(128)"); |
|||
|
|||
b.HasKey("Id"); |
|||
|
|||
b.ToTable("TodoItems"); |
|||
}); |
|||
|
|||
modelBuilder.Entity("DistDemoApp.TodoSummary", b => |
|||
{ |
|||
b.Property<int>("Id") |
|||
.ValueGeneratedOnAdd() |
|||
.HasColumnType("int") |
|||
.HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); |
|||
|
|||
b.Property<string>("ConcurrencyStamp") |
|||
.IsConcurrencyToken() |
|||
.HasMaxLength(40) |
|||
.HasColumnType("nvarchar(40)") |
|||
.HasColumnName("ConcurrencyStamp"); |
|||
|
|||
b.Property<byte>("Day") |
|||
.HasColumnType("tinyint"); |
|||
|
|||
b.Property<string>("ExtraProperties") |
|||
.HasColumnType("nvarchar(max)") |
|||
.HasColumnName("ExtraProperties"); |
|||
|
|||
b.Property<byte>("Month") |
|||
.HasColumnType("tinyint"); |
|||
|
|||
b.Property<int>("TotalCount") |
|||
.HasColumnType("int"); |
|||
|
|||
b.Property<int>("Year") |
|||
.HasColumnType("int"); |
|||
|
|||
b.HasKey("Id"); |
|||
|
|||
b.ToTable("TodoSummaries"); |
|||
}); |
|||
#pragma warning restore 612, 618
|
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,34 @@ |
|||
using Microsoft.EntityFrameworkCore.Migrations; |
|||
|
|||
namespace DistDemoApp.Migrations |
|||
{ |
|||
public partial class Added_Summary_Table : Migration |
|||
{ |
|||
protected override void Up(MigrationBuilder migrationBuilder) |
|||
{ |
|||
migrationBuilder.CreateTable( |
|||
name: "TodoSummaries", |
|||
columns: table => new |
|||
{ |
|||
Id = table.Column<int>(type: "int", nullable: false) |
|||
.Annotation("SqlServer:Identity", "1, 1"), |
|||
Year = table.Column<int>(type: "int", nullable: false), |
|||
Month = table.Column<byte>(type: "tinyint", nullable: false), |
|||
Day = table.Column<byte>(type: "tinyint", nullable: false), |
|||
TotalCount = table.Column<int>(type: "int", nullable: false), |
|||
ExtraProperties = table.Column<string>(type: "nvarchar(max)", nullable: true), |
|||
ConcurrencyStamp = table.Column<string>(type: "nvarchar(40)", maxLength: 40, nullable: true) |
|||
}, |
|||
constraints: table => |
|||
{ |
|||
table.PrimaryKey("PK_TodoSummaries", x => x.Id); |
|||
}); |
|||
} |
|||
|
|||
protected override void Down(MigrationBuilder migrationBuilder) |
|||
{ |
|||
migrationBuilder.DropTable( |
|||
name: "TodoSummaries"); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,63 @@ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.Domain.Entities.Events.Distributed; |
|||
using Volo.Abp.Domain.Repositories; |
|||
using Volo.Abp.EventBus.Distributed; |
|||
using Volo.Abp.Uow; |
|||
|
|||
namespace DistDemoApp |
|||
{ |
|||
public class TodoEventHandler : |
|||
IDistributedEventHandler<EntityCreatedEto<TodoItemEto>>, |
|||
IDistributedEventHandler<EntityDeletedEto<TodoItemEto>>, |
|||
ITransientDependency |
|||
{ |
|||
private readonly IRepository<TodoSummary, int> _todoSummaryRepository; |
|||
|
|||
public TodoEventHandler(IRepository<TodoSummary, int> todoSummaryRepository) |
|||
{ |
|||
_todoSummaryRepository = todoSummaryRepository; |
|||
} |
|||
|
|||
[UnitOfWork] |
|||
public virtual async Task HandleEventAsync(EntityCreatedEto<TodoItemEto> eventData) |
|||
{ |
|||
var dateTime = eventData.Entity.CreationTime; |
|||
var todoSummary = await _todoSummaryRepository.FindAsync( |
|||
x => x.Year == dateTime.Year && |
|||
x.Month == dateTime.Month && |
|||
x.Day == dateTime.Day |
|||
); |
|||
|
|||
if (todoSummary == null) |
|||
{ |
|||
await _todoSummaryRepository.InsertAsync(new TodoSummary(dateTime)); |
|||
} |
|||
else |
|||
{ |
|||
todoSummary.Increase(); |
|||
await _todoSummaryRepository.UpdateAsync(todoSummary); |
|||
} |
|||
|
|||
Console.WriteLine("Increased total count: " + todoSummary); |
|||
} |
|||
|
|||
public async Task HandleEventAsync(EntityDeletedEto<TodoItemEto> eventData) |
|||
{ |
|||
var dateTime = eventData.Entity.CreationTime; |
|||
var todoSummary = await _todoSummaryRepository.FirstOrDefaultAsync( |
|||
x => x.Year == dateTime.Year && |
|||
x.Month == dateTime.Month && |
|||
x.Day == dateTime.Day |
|||
); |
|||
|
|||
if (todoSummary != null) |
|||
{ |
|||
todoSummary.Decrease(); |
|||
await _todoSummaryRepository.UpdateAsync(todoSummary); |
|||
Console.WriteLine("Decreased total count: " + todoSummary); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,12 @@ |
|||
using System; |
|||
using Volo.Abp.EventBus; |
|||
|
|||
namespace DistDemoApp |
|||
{ |
|||
[EventName("todo-item")] |
|||
public class TodoItemEto |
|||
{ |
|||
public DateTime CreationTime { get; set; } |
|||
public string Text { get; set; } |
|||
} |
|||
} |
|||
@ -0,0 +1,24 @@ |
|||
using Volo.Abp.DependencyInjection; |
|||
using Volo.Abp.ObjectMapping; |
|||
|
|||
namespace DistDemoApp |
|||
{ |
|||
public class TodoItemObjectMapper : IObjectMapper<TodoItem, TodoItemEto>, ISingletonDependency |
|||
{ |
|||
public TodoItemEto Map(TodoItem source) |
|||
{ |
|||
return new TodoItemEto |
|||
{ |
|||
Text = source.Text, |
|||
CreationTime = source.CreationTime |
|||
}; |
|||
} |
|||
|
|||
public TodoItemEto Map(TodoItem source, TodoItemEto destination) |
|||
{ |
|||
destination.Text = source.Text; |
|||
destination.CreationTime = source.CreationTime; |
|||
return destination; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,41 @@ |
|||
using System; |
|||
using Volo.Abp.Domain.Entities; |
|||
|
|||
namespace DistDemoApp |
|||
{ |
|||
public class TodoSummary : AggregateRoot<int> |
|||
{ |
|||
public int Year { get; private set; } |
|||
public byte Month { get; private set; } |
|||
public byte Day { get; private set; } |
|||
public int TotalCount { get; private set; } |
|||
|
|||
private TodoSummary() |
|||
{ |
|||
|
|||
} |
|||
|
|||
public TodoSummary(DateTime dateTime, int initialCount = 1) |
|||
{ |
|||
Year = dateTime.Year; |
|||
Month = (byte)dateTime.Month; |
|||
Day = (byte)dateTime.Day; |
|||
TotalCount = initialCount; |
|||
} |
|||
|
|||
public void Increase(int amount = 1) |
|||
{ |
|||
TotalCount += amount; |
|||
} |
|||
|
|||
public void Decrease(int amount = 1) |
|||
{ |
|||
TotalCount -= amount; |
|||
} |
|||
|
|||
public override string ToString() |
|||
{ |
|||
return $"{base.ToString()}, {Year}-{Month:2}-{Day:2}: {TotalCount}"; |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue