private async Task SaveOrUpdate(Factoid factoid, CancellationToken cancellationToken) { if (factoid.Id == 0) { await _context.Factoids.AddAsync(factoid, cancellationToken); } else { _context.Factoids.Update(factoid); } await _context.SaveChangesAsync(cancellationToken); }
// TODO [Kog 10/06/2018] : Looks like we're gonna need some repo infrastructure. private async Task SaveOrUpdate(KarmaItem karmaItem, CancellationToken cancellationToken) { if (karmaItem.Id == 0) { await _context.Karma.AddAsync(karmaItem, cancellationToken); } else { _context.Karma.Update(karmaItem); } await _context.SaveChangesAsync(cancellationToken); }
private async Task CreateUser(IrcPrivMessage request, string nickname, string email, string ircHostname, string matchType, string role, CancellationToken cancellationToken) { // If the user already exists, bail. if (null != await _userManager.FindByEmailAsync(email)) { await SendResponse(request, $"{email} already exists, bailing..."); } else { bool.TryParse(matchType, out var prefixMatch); // Create our user. Apparently the pre-built identity login methods prefer the username and email to be equal. var user = new SpikeCoreUser { UserName = email, Email = email }; await _userManager.CreateAsync(user); // Associate the proper roles with the user. var persistedUser = await _userManager.FindByEmailAsync(email); var roles = role.Equals("op", StringComparison.InvariantCultureIgnoreCase) ? OpRoles : RegularRoles; await _userManager.AddToRolesAsync(persistedUser, roles); // Associate a login with the user, so they can use the bot. await _userManager.AddLoginAsync(persistedUser, new UserLoginInfo(IrcLoginProvider, ircHostname, nickname)); // Prefix match is a custom field, so we'll update it out of band via EF directly. if (prefixMatch) { var login = _context.UserLogins.Single(record => record.LoginProvider == IrcLoginProvider && record.ProviderKey == ircHostname); login.MatchType = "StartsWith"; _context.UserLogins.Update(login); await _context.SaveChangesAsync(cancellationToken); } Log.Information("{IrcUserName} (identity: {IdentityUserName}) has just created user {AffectedUserName}", request.UserName, request.IdentityUser.UserName, email); await SendResponse(request, $"successfully created user {nickname} (email: {email}), with roles [{string.Join(", ", roles)}] (match type: {(prefixMatch ? "StartsWith" : "Literal")})"); } }