// Quick fix for user entity manipulation outside of this db context // TODO: Fix properly static public async Task <User> GetUserEntityAsync(this BotDatabaseContext context, IUser user) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (user == null) { throw new ArgumentNullException(nameof(user)); } // Update cached displayname var displayName = user.Username; var result = await Patiently.HandleDbConcurrency(async() => { var userEntity = await context.Users.AsQueryable().FirstOrDefaultAsync(o => o.Id == user.Id); if (userEntity != null && userEntity.Name != displayName) { userEntity.Name = displayName; await context.SaveChangesAsync(); } return(userEntity); }); return(result); }
static public async Task UpdateCommandConsentAsync(this BotDatabaseContext context, IUser user, Func <CommandConsent, CommandConsent> consentUpdaterFunc) { if (context is null) { throw new ArgumentNullException(nameof(context)); } if (user is null) { throw new ArgumentNullException(nameof(user)); } await Patiently.HandleDbConcurrency(async() => { var userEntity = await GetOrCreateUserEntityAsync(context, user); userEntity.CommandConsents = consentUpdaterFunc(userEntity.CommandConsents); await context.SaveChangesAsync(); }); }
public async Task SetKisNicknameAsync(IUser user, [Remainder][Name("přezdívka")] string nickname) { if (nickname != null && await DbContext.Users.AnyAsync(o => o.KisNickname == nickname)) { await ReplyAsync(KisSettings.Messages["NonUniqueNick"]); return; } await Patiently.HandleDbConcurrency(async() => { var userEntity = await DbContext.GetOrCreateUserEntityAsync(user); userEntity.KisNickname = nickname; await DbContext.SaveChangesAsync(); await Context.Message.AddReactionAsync(ReactionSettings.Checkmark); }); }
public async Task SetKisNicknameAsync([Remainder][Name("přezdívka")] string nickname) { if (await DbContext.Users.AnyAsync(o => o.KisNickname == nickname)) { await ReplyAsync(Configuration["Kis:Messages:NonUniqueNick"]); return; } await Patiently.HandleDbConcurrency(async() => { var user = await DbContext.GetOrCreateUserEntityAsync(Context.User); if (!string.IsNullOrEmpty(user.KisNickname)) { await ReplyAsync(KisSettings.Messages["AlreadySet"]); return; } user.KisNickname = nickname; await DbContext.SaveChangesAsync(); await Context.Message.AddReactionAsync(ReactionSettings.Checkmark); }); }