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>, IDistributedEventHandler>, ITransientDependency { private readonly IRepository _todoSummaryRepository; public TodoEventHandler(IRepository todoSummaryRepository) { _todoSummaryRepository = todoSummaryRepository; } [UnitOfWork] public virtual async Task HandleEventAsync(EntityCreatedEto 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) { todoSummary = await _todoSummaryRepository.InsertAsync(new TodoSummary(dateTime)); } else { todoSummary.Increase(); await _todoSummaryRepository.UpdateAsync(todoSummary); } Console.WriteLine("Increased total count: " + todoSummary); } public async Task HandleEventAsync(EntityDeletedEto 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); } } } }