public async Task <Poll> CreateAsync(Poll poll) { IPollOptionsRepository pollOptionsRepository = new PollOptionsRepository(_database); var options = pollOptionsRepository.MountOptions(poll.Options, await pollOptionsRepository.SequenceOptionIdAsync()); poll.PollId = await SequencePollIdAsync(); poll.PollDescription = poll.PollDescription; poll.Options = options; await _database.GetCollection <Poll>("Poll").InsertOneAsync(poll); return(poll); }
public async Task <Poll> VoteAsync(long optionId) { IPollVotesRepository pollVotesRepository = new PollVotesRepository(_database); IPollOptionsRepository pollOptionsRepository = new PollOptionsRepository(_database); PollVotes pollVotesUpdate = new PollVotes(); pollVotesUpdate.Qty = await pollVotesRepository.IsVotedAsync(optionId) ? await pollOptionsRepository.QuantityByOptionIdAsync(optionId) + 1 : 1; var filter = Builders <Poll> .Filter.Eq("options.option_id", optionId); var update = Builders <Poll> .Update.Set("options.$.votes", pollVotesUpdate); var documentBefore = await _database.GetCollection <Poll>("Poll").FindOneAndUpdateAsync(filter, update, new FindOneAndUpdateOptions <Poll> { ReturnDocument = ReturnDocument.Before }); return(documentBefore); }