コード例 #1
0
        public async Task <bool> Handle(DeleteItemCommand command, CancellationToken cancellationToken)
        {
            var items      = _dbContext.Set <Item>();
            var itemFromDb = await items.SingleAsync(x => x.ItemId == command.ItemId, cancellationToken : cancellationToken);

            _dbContext.Items.Remove(itemFromDb);

            var result = await _dbContext.SaveChangesAsync(cancellationToken);

            await _itemCreatedNotificationHandler.Handle(new ItemDeletedMessage
            {
                ItemId = command.ItemId
            },
                                                         cancellationToken);

            return(result > 0);
        }
コード例 #2
0
        public async Task <ItemDto> Handle(AddItemCommand command, CancellationToken cancellationToken)
        {
            var itemFromCommand = _mapper.Map <Item>(command);

            var items      = _dbContext.Set <Item>();
            var itemFromDb = await items.SingleOrDefaultAsync(x => x.ItemId == command.ItemId, cancellationToken : cancellationToken);

            if (itemFromDb != null)
            {
                _mapper.Map(itemFromCommand, itemFromDb);
                items.Update(itemFromDb);
            }
            else
            {
                await _dbContext.Items.AddAsync(itemFromCommand, cancellationToken);
            }

            await _dbContext.SaveChangesAsync(cancellationToken);

            await _itemCreatedNotificationHandler.Handle(new ItemCreatedMessage(), cancellationToken);

            return(_mapper.Map <ItemDto>(itemFromCommand));
        }