public Task <ActionResult <VoteView> > AddVote([FromRoute] long contentId, [FromBody] VoteType vote) { //A nice shortcut in case users do it this way if (vote == VoteType.none) { return(DeleteVote(contentId)); } return(MatchExceptions(async() => { RateLimit(RateInteract); var uid = GetUserIdStrict(); VoteView writeVote; //Try to lookup the existing vote to update it, otherwise create a new one try { writeVote = await shortcuts.LookupVoteByContentIdAsync(uid, contentId); } catch (NotFoundException) { writeVote = new VoteView() { contentId = contentId }; } //Actually set the vote to what they wanted writeVote.vote = vote; return await CachedWriter.WriteAsync(writeVote, uid); //message used for activity and such })); }
public async Task LookupVoteByContentId_Simple(long uid, long cid) { //Shouldn't exist at first... we hope? await Assert.ThrowsAnyAsync <NotFoundException>(() => service.LookupVoteByContentIdAsync(uid, cid)); var vote = new VoteView() { contentId = cid, vote = VoteType.ok }; var writtenVote = await writer.WriteAsync(vote, uid); Assert.Equal(vote.vote, writtenVote.vote); Assert.Equal(vote.contentId, writtenVote.contentId); //now go look it up var lookupVote = await service.LookupVoteByContentIdAsync(uid, cid); Assert.Equal(uid, lookupVote.userId); Assert.Equal(cid, lookupVote.contentId); Assert.Equal(vote.vote, lookupVote.vote); }