private async Task JoinRunInternalAsync(long userId, Run run, long optionId, CallbackData callbackData) { BaseCommand command; if (run.Ended) { throw new SlackTeaTimeException(ErrorStrings.JoinRun_RunEnded()); } //check if we need to join or update var existingOrder = await _mediator.Send(new GetUserOrderQuery(run.Id, userId)).ConfigureAwait(false); if (existingOrder == null) { command = new CreateOrderCommand( id: await _idGenerator.GenerateAsync().ConfigureAwait(false), runId: run.Id, userId: userId, optionId: optionId); } else { //we need to update the existing order command = new UpdateOrderOptionCommand(existingOrder.Id, userId, optionId); } command.AddCallbackState(callbackData); await _mediator.Send(command).ConfigureAwait(false); }
public async Task <Unit> Handle(UpdateOrderOptionCommand request, CancellationToken cancellationToken) { var existing = await _orderRepository.GetAsync(request.OrderId).ConfigureAwait(false); if (existing.OptionId == request.OptionId) { return(Unit.Value); } //store the previous id for the event var previousOptionId = existing.OptionId; //update existing.OptionId = request.OptionId; await _orderRepository.UpdateAsync(existing).ConfigureAwait(false); //create event var evt = _mapper.Map <UpdateOrderOptionCommand, OrderOptionChangedEvent>(request); evt.PreviousOptionId = previousOptionId; await _eventPublisher.Publish(evt).ConfigureAwait(false); return(Unit.Value); }