public async Task <bool> Handle(CompleteTodoCommand request, CancellationToken cancellationToken)
        {
            var todo = await _context.Todoes.FindAsync(request.Id);

            todo.Completed = true;
            _context.Todoes.Update(todo);

            var summary = await _context.TodoSummaries.Where(x => x.Date == DateTime.Today).FirstOrDefaultAsync();

            if (summary == null)
            {
                summary = new TodoSummary {
                    Date = DateTime.Today, CompletedCount = 1
                };
                _context.TodoSummaries.Add(summary);
            }
            else
            {
                summary.CompletedCount += 1;
                _context.TodoSummaries.Update(summary);
            }

            await _context.SaveChangesAsync();

            //notify query service to update
            Console.WriteLine("Publish command from CompleteTodo");
            _eventbus.Publish(new TodoIntegrationEvent()
            {
                Todo = todo, Summary = summary
            });
            return(true);
        }
        public async Task <Guid> Handle(AddTodoCommand request, CancellationToken cancellationToken)
        {
            var todo = new Todo {
                Name = request.Name
            };

            _context.Todoes.Add(todo);
            var summary = await _context.TodoSummaries.Where(x => x.Date == DateTime.Today).FirstOrDefaultAsync();

            if (summary == null)
            {
                summary = new TodoSummary {
                    Date = DateTime.Today, AddedCount = 1
                };
                _context.TodoSummaries.Add(summary);
            }
            else
            {
                summary.AddedCount += 1;
                _context.TodoSummaries.Update(summary);
            }
            await _context.SaveChangesAsync();

            //notify query service to update
            _logger.LogDebug("Publish command from AddTodo");
            _eventbus.Publish(new TodoIntegrationEvent()
            {
                Todo = todo, Summary = summary
            });
            return(todo.Id);
        }
Example #3
0
        public async Task <bool> Handle(CompleteTodoCommand request, CancellationToken cancellationToken)
        {
            var todo = await _context.Todoes.FindAsync(request.Id);

            todo.Completed = true;
            _context.Todoes.Update(todo);

            var summary = await _context.TodoSummaries.Where(x => x.Date == DateTime.Today).FirstOrDefaultAsync();

            if (summary == null)
            {
                summary = new TodoSummary {
                    Date = DateTime.Today, CompletedCount = 1
                };
                _context.TodoSummaries.Add(summary);
            }
            else
            {
                summary.CompletedCount += 1;
                _context.TodoSummaries.Update(summary);
            }

            await _context.SaveChangesAsync();

            return(true);
        }
Example #4
0
        public async Task <Guid> Handle(AddTodoCommand request, CancellationToken cancellationToken)
        {
            var todo = new Todo {
                Name = request.Name
            };

            _context.Todoes.Add(todo);
            var summary = await _context.TodoSummaries.Where(x => x.Date == DateTime.Today).FirstOrDefaultAsync();

            if (summary == null)
            {
                summary = new TodoSummary {
                    Date = DateTime.Today, AddedCount = 1
                };
                _context.TodoSummaries.Add(summary);
            }
            else
            {
                summary.AddedCount += 1;
                _context.TodoSummaries.Update(summary);
            }
            await _context.SaveChangesAsync();

            return(todo.Id);
        }