public async Task <IActionResult> Post([FromBody] AddVoteModel model)
        {
            var result = await _mediator.Send(new AddVoteCommand(model.SubjectId, model.OptionName));

            await _mediator.Publish(VoteCreated.From(result));

            var view = ViewVote.From(result);

            return(Ok(view));
        }
Пример #2
0
        public async Task <Vote> Handle(AddVoteCommand request, CancellationToken cancellationToken)
        {
            var count = await _db.HashIncrementAsync(MapKey(request.SubjectId), request.OptionName);

            var vote = new Vote(
                count: (uint)count,
                optionName: request.OptionName,
                subjectId: request.SubjectId
                );

            await _mediator.Publish(VoteCreated.From(vote));

            return(vote);
        }
        public override async Task <VoteResponse> Add(AddRequest request, ServerCallContext context)
        {
            using (var scope = _scopeFactory.CreateScope())
            {
                var mediator = scope.ServiceProvider.GetRequiredService <IMediator>();

                //TODO: refactor this command
                //It needs to be only one command doing the same
                var vote = await mediator.Send(new AddVoteCommand(request.SubjectId, request.OptionName));

                await mediator.Publish(VoteCreated.From(vote));

                return(new VoteResponse
                {
                    Count = vote.Count,
                    OptionName = vote.OptionName,
                });
            }
        }