private async Task VotePasta(EventContext e, bool vote) { string pastaName = e.Arguments.ToString(); // TODO: add resource friendly exception for arguments if (string.IsNullOrEmpty(pastaName)) { throw new ArgumentNullException("pasta name"); } using (var context = new MikiContext()) { var pasta = await context.Pastas.FindAsync(pastaName); if (pasta == null) { e.ErrorEmbed(e.GetResource("miki_module_pasta_error_null")).Build().QueueToChannel(e.Channel); return; } long authorId = e.Author.Id.ToDbLong(); var voteObject = context.Votes.Where(q => q.Id == pastaName && q.UserId == authorId) .FirstOrDefault(); if (voteObject == null) { voteObject = new PastaVote() { Id = pastaName, UserId = e.Author.Id.ToDbLong(), PositiveVote = vote }; context.Votes.Add(voteObject); } else { voteObject.PositiveVote = vote; } await context.SaveChangesAsync(); var votecount = await pasta.GetVotesAsync(context); pasta.Score = votecount.Upvotes - votecount.Downvotes; await context.SaveChangesAsync(); Utils.SuccessEmbed(e.Channel.Id, e.GetResource("miki_module_pasta_vote_success", votecount.Upvotes - votecount.Downvotes)).QueueToChannel(e.Channel); } }
private async Task VotePasta(EventContext e, bool vote) { if (e.Arguments.Take(out string pastaName)) { using (var context = new MikiContext()) { var pasta = await context.Pastas.FindAsync(pastaName); if (pasta == null) { await e.ErrorEmbed(e.Locale.GetString("miki_module_pasta_error_null")).ToEmbed().QueueToChannelAsync(e.Channel); return; } long authorId = e.Author.Id.ToDbLong(); var voteObject = context.Votes .Where(q => q.Id == pastaName && q.UserId == authorId) .FirstOrDefault(); if (voteObject == null) { voteObject = new PastaVote() { Id = pastaName, UserId = e.Author.Id.ToDbLong(), PositiveVote = vote }; context.Votes.Add(voteObject); } else { voteObject.PositiveVote = vote; } await context.SaveChangesAsync(); var votecount = await pasta.GetVotesAsync(context); pasta.Score = votecount.Upvotes - votecount.Downvotes; await context.SaveChangesAsync(); await e.SuccessEmbed(e.Locale.GetString("miki_module_pasta_vote_success", votecount.Upvotes - votecount.Downvotes)).QueueToChannelAsync(e.Channel); } } }
private async Task VotePasta(EventContext e, bool vote) { Locale locale = Locale.GetEntity(e.Channel.Id); using (var context = new MikiContext()) { var pasta = await context.Pastas.FindAsync(e.arguments); if (pasta == null) { await Utils.ErrorEmbed(locale, e.GetResource("miki_module_pasta_error_null")).QueueToChannel(e.Channel); return; } long authorId = e.Author.Id.ToDbLong(); var voteObject = context.Votes.Where(q => q.Id == e.arguments && q.UserId == authorId) .FirstOrDefault(); if (voteObject == null) { voteObject = new PastaVote() { Id = e.arguments, UserId = e.Author.Id.ToDbLong(), PositiveVote = vote }; context.Votes.Add(voteObject); } else { voteObject.PositiveVote = vote; } await context.SaveChangesAsync(); var votecount = await pasta.GetVotesAsync(context); pasta.Score = votecount.Upvotes - votecount.Downvotes; await context.SaveChangesAsync(); await Utils.SuccessEmbed(locale, e.GetResource("miki_module_pasta_vote_success", votecount.Upvotes - votecount.Downvotes)).QueueToChannel(e.Channel); } }
private async Task VotePasta(EventContext e, bool vote) { Locale locale = Locale.GetEntity(e.Guild.Id.ToDbLong()); using (var context = MikiContext.CreateNoCache()) { context.Set <GlobalPasta>().AsNoTracking(); var pasta = await context.Pastas.FindAsync(e.arguments); if (pasta == null) { await e.Channel.SendMessage(Utils.ErrorEmbed(locale, "This pasta doesn't exist :((")); return; } long authorId = e.Author.Id.ToDbLong(); var voteObject = context.Votes.AsNoTracking().Where(q => q.Id == e.arguments && q.__UserId == authorId).FirstOrDefault(); if (voteObject == null) { voteObject = new PastaVote() { Id = e.arguments, __UserId = e.Author.Id.ToDbLong(), PositiveVote = vote }; context.Votes.Add(voteObject); } else { voteObject.PositiveVote = vote; } await context.SaveChangesAsync(); var votecount = pasta.GetVotes(context); await e.Channel.SendMessage(Utils.SuccessEmbed(locale, $"Your vote has been updated!\nCurrent Score: `{votecount.Upvotes - votecount.Downvotes}`")); } }
public async ValueTask VoteAsync(PastaVote vote) { var pasta = await GetPastaAsync(vote.Id); var currentVote = await GetVoteAsync(vote.Id, vote.UserId); if (currentVote == null) { await voteRepository.AddAsync(vote); } else { await voteRepository.EditAsync(vote); } await unit.CommitAsync(); pasta.Score = await GetScoreAsync(pasta.Id); await repository.EditAsync(pasta); await unit.CommitAsync(); }