public int GetOrAdd(IDbConnection connection, string authId, IUserLocalStorageCache cache) { _checkAndInit(connection, cache); int val; _storage.TryGetValue(authId, out val); if (val != 0) { return(val); } var cacheUser = cache.LocalOperation(connection, col => col.FirstOrDefault(i => i.AuthId == authId)); if (cacheUser != null && cacheUser.Id != 0) { return(_storage.AddOrUpdateSimple(cacheUser.AuthId, cacheUser.Id)); } var repo = cache.GetRepository(); var tableName = repo.SchemeTableName; var sql = $"SELECT TOP(1) * FROM {tableName} WHERE authId=@authId"; var user = repo.Provider.Text <user>(connection, sql, new { authId }).ToList(); if (user.Any()) { var u = user.First(); cache.UpdateLocalItem(connection, repo.ConvertToWorkModel(u)); return(_storage.AddOrUpdateSimple(u.authId, u.Id)); } return(0); }
public int GetOrAdd(IDbConnection connection, string userName, IUserLocalStorageCache cache) { _checkAndInit(connection, cache); int val; _storage.TryGetValue(userName, out val); if (val != 0) { return(val); } var cacheUser = cache.LocalOperation(connection, col => col.FirstOrDefault(i => i.Nickname == userName)); if (cacheUser != null && cacheUser.Id != 0) { return(_storage.AddOrUpdateSimple(cacheUser.Nickname, cacheUser.Id)); } var repo = cache.GetRepository(); var tableName = repo.SchemeTableName; var sql = $"SELECT TOP(1) * FROM {tableName} WHERE nickname=@userName"; var user = repo.Provider.Text <user>(connection, sql, new { userName }).Single(); cache.UpdateLocalItem(connection, repo.ConvertToWorkModel(user)); return(_storage.AddOrUpdateSimple(user.authId, user.Id)); }