public async Task Execute(PangulDbContext db, UpdateQuestionMeta command) { command.Validate(); var meta = await db.QuestionMeta.FirstOrDefaultAsync(i => i.UserId == command.UserContext.User.UserId && i.QuestionId == command.Derived.QuestionId && i.RowVersion == command.Derived.RowVersion); // Maybe just inserted? if (meta == null) { meta = db.QuestionMeta.Local.AsQueryable().FirstOrDefault(i => i.UserId == command.UserContext.User.UserId && i.QuestionId == command.Derived.QuestionId); } if (meta == null) { throw new PangulCommandFailedException(CommandFailureType.MissingData, $"No such QuestionMeta ({command.QuestionId})"); } // Update properties meta.RowVersion = command.Derived.RowVersion; meta.Votes = command.Votes; meta.Star = command.Star; }
public async Task <QuestionMetaInternalModel> UpdateQuestionMetadata(PangulDbContext db, UserContext user, UpdateQuestionMeta model) { model.UserContext = user; var meta = await _internalMetaService.RequireQuestionMetaForUser(db, user, model.QuestionId); var delta = model.Votes - meta.Votes; await _updateQuestionMeta.Execute(db, model); if (delta != 0) { await _internalMetaService.UpdateQuestionGlobalMetadata(db, user, new UpdateQuestionGlobalMeta() { UserContext = user, QuestionId = model.QuestionId, Votes = delta, }); } return(await GetQuestionMetadata(db, user, model.QuestionId)); }