implementing TwitterDal
This commit is contained in:
parent
774c304ff7
commit
b23a710533
8 changed files with 258 additions and 27 deletions
|
@ -0,0 +1,34 @@
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using BirdsiteLive.DAL.Contracts;
|
||||||
|
using BirdsiteLive.DAL.Postgres.DataAccessLayers.Base;
|
||||||
|
using BirdsiteLive.DAL.Postgres.Settings;
|
||||||
|
using BirdsiteLive.DAL.Postgres.Tools;
|
||||||
|
using Tweetinvi.Models;
|
||||||
|
|
||||||
|
namespace BirdsiteLive.DAL.Postgres.DataAccessLayers
|
||||||
|
{
|
||||||
|
public class CachedTweetsPostgresDal : PostgresBase, ICachedTweetsDal
|
||||||
|
{
|
||||||
|
#region Ctor
|
||||||
|
public CachedTweetsPostgresDal(PostgresSettings settings, PostgresTools tools) : base(settings)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
public Task AddTweetAsync(long tweetId, int userId, ITweet tweet)
|
||||||
|
{
|
||||||
|
throw new System.NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task<ITweet> GetTweetAsync(long tweetId)
|
||||||
|
{
|
||||||
|
throw new System.NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task DeleteTweetAsync(long tweetId)
|
||||||
|
{
|
||||||
|
throw new System.NotImplementedException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,9 +1,77 @@
|
||||||
using BirdsiteLive.DAL.Contracts;
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using BirdsiteLive.DAL.Contracts;
|
||||||
|
using BirdsiteLive.DAL.Models;
|
||||||
|
using BirdsiteLive.DAL.Postgres.DataAccessLayers.Base;
|
||||||
|
using BirdsiteLive.DAL.Postgres.Settings;
|
||||||
|
using BirdsiteLive.DAL.Postgres.Tools;
|
||||||
|
using Dapper;
|
||||||
|
using Npgsql;
|
||||||
|
|
||||||
namespace BirdsiteLive.DAL.Postgres.DataAccessLayers
|
namespace BirdsiteLive.DAL.Postgres.DataAccessLayers
|
||||||
{
|
{
|
||||||
public class TwitterUserPostgresDal : ITwitterUserDal
|
public class TwitterUserPostgresDal : PostgresBase, ITwitterUserDal
|
||||||
|
{
|
||||||
|
#region Ctor
|
||||||
|
public TwitterUserPostgresDal(PostgresSettings settings) : base(settings)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
public async Task CreateTwitterUserAsync(string acct, long lastTweetPostedId)
|
||||||
|
{
|
||||||
|
using (var dbConnection = Connection)
|
||||||
|
{
|
||||||
|
dbConnection.Open();
|
||||||
|
|
||||||
|
await dbConnection.ExecuteAsync(
|
||||||
|
$"INSERT INTO {_settings.TwitterUserTableName} (acct,lastTweetPostedId,lastTweetSynchronizedForAllFollowersId) VALUES(@acct,@lastTweetPostedId,@lastTweetSynchronizedForAllFollowersId)",
|
||||||
|
new { acct = acct, lastTweetPostedId = lastTweetPostedId, lastTweetSynchronizedForAllFollowersId = lastTweetPostedId });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task<SyncTwitterUser> GetTwitterUserAsync(string acct)
|
||||||
|
{
|
||||||
|
var query = $"SELECT * FROM {_settings.TwitterUserTableName} WHERE acct = @acct";
|
||||||
|
|
||||||
|
using (var dbConnection = Connection)
|
||||||
|
{
|
||||||
|
dbConnection.Open();
|
||||||
|
|
||||||
|
var result = (await dbConnection.QueryAsync<SyncTwitterUser>(query, new { acct = acct })).FirstOrDefault();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task<SyncTwitterUser[]> GetAllTwitterUsersAsync()
|
||||||
|
{
|
||||||
|
throw new System.NotImplementedException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task UpdateTwitterUserAsync(int id, long lastTweetPostedId, long lastTweetSynchronizedForAllFollowersId)
|
||||||
|
{
|
||||||
|
if(id == default) throw new ArgumentException("id");
|
||||||
|
if(lastTweetPostedId == default) throw new ArgumentException("lastTweetPostedId");
|
||||||
|
if(lastTweetSynchronizedForAllFollowersId == default) throw new ArgumentException("lastTweetSynchronizedForAllFollowersId");
|
||||||
|
|
||||||
|
var query = $"UPDATE {_settings.TwitterUserTableName} SET lastTweetPostedId = {lastTweetPostedId}, lastTweetSynchronizedForAllFollowersId = {lastTweetSynchronizedForAllFollowersId} WHERE id = {id}";
|
||||||
|
|
||||||
|
using (var dbConnection = Connection)
|
||||||
|
using (var cmd = new NpgsqlCommand(query, dbConnection))
|
||||||
|
{
|
||||||
|
dbConnection.Open();
|
||||||
|
await cmd.ExecuteNonQueryAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task DeleteTwitterUserAsync(string acct)
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
throw new System.NotImplementedException();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -4,4 +4,8 @@
|
||||||
<TargetFramework>netstandard2.0</TargetFramework>
|
<TargetFramework>netstandard2.0</TargetFramework>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="TweetinviAPI" Version="4.0.3" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
@ -0,0 +1,12 @@
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Tweetinvi.Models;
|
||||||
|
|
||||||
|
namespace BirdsiteLive.DAL.Contracts
|
||||||
|
{
|
||||||
|
public interface ICachedTweetsDal
|
||||||
|
{
|
||||||
|
Task AddTweetAsync(long tweetId, int userId, ITweet tweet);
|
||||||
|
Task<ITweet> GetTweetAsync(long tweetId);
|
||||||
|
Task DeleteTweetAsync(long tweetId);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,7 +1,14 @@
|
||||||
namespace BirdsiteLive.DAL.Contracts
|
using System.Threading.Tasks;
|
||||||
|
using BirdsiteLive.DAL.Models;
|
||||||
|
|
||||||
|
namespace BirdsiteLive.DAL.Contracts
|
||||||
{
|
{
|
||||||
public interface ITwitterUserDal
|
public interface ITwitterUserDal
|
||||||
{
|
{
|
||||||
|
Task CreateTwitterUserAsync(string acct, long lastTweetPostedId);
|
||||||
|
Task<SyncTwitterUser> GetTwitterUserAsync(string acct);
|
||||||
|
Task<SyncTwitterUser[]> GetAllTwitterUsersAsync();
|
||||||
|
Task UpdateTwitterUserAsync(int id, long lastTweetPostedId, long lastTweetSynchronizedForAllFollowersId);
|
||||||
|
Task DeleteTwitterUserAsync(string acct);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -0,0 +1,27 @@
|
||||||
|
using BirdsiteLive.DAL.Postgres.Settings;
|
||||||
|
using BirdsiteLive.DAL.Postgres.Tools;
|
||||||
|
using BirdsiteLive.DAL.Tools;
|
||||||
|
|
||||||
|
namespace BirdsiteLive.DAL.Postgres.Tests.DataAccessLayers.Base
|
||||||
|
{
|
||||||
|
public class PostgresTestingBase
|
||||||
|
{
|
||||||
|
protected readonly PostgresSettings _settings;
|
||||||
|
protected readonly PostgresTools _tools;
|
||||||
|
|
||||||
|
#region Ctor
|
||||||
|
public PostgresTestingBase()
|
||||||
|
{
|
||||||
|
_settings = new PostgresSettings
|
||||||
|
{
|
||||||
|
ConnString = "Host=127.0.0.1;Username=postgres;Password=mysecretpassword;Database=mytestdb",
|
||||||
|
DbVersionTableName = "DbVersionTableName" + RandomGenerator.GetString(4),
|
||||||
|
CachedTweetsTableName = "CachedTweetsTableName" + RandomGenerator.GetString(4),
|
||||||
|
FollowersTableName = "FollowersTableName" + RandomGenerator.GetString(4),
|
||||||
|
TwitterUserTableName = "TwitterUserTableName" + RandomGenerator.GetString(4),
|
||||||
|
};
|
||||||
|
_tools = new PostgresTools(_settings);
|
||||||
|
}
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,34 +1,14 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using BirdsiteLive.DAL.Postgres.DataAccessLayers;
|
using BirdsiteLive.DAL.Postgres.DataAccessLayers;
|
||||||
using BirdsiteLive.DAL.Postgres.Settings;
|
using BirdsiteLive.DAL.Postgres.Tests.DataAccessLayers.Base;
|
||||||
using BirdsiteLive.DAL.Postgres.Tools;
|
|
||||||
using BirdsiteLive.DAL.Tools;
|
|
||||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||||
|
|
||||||
namespace BirdsiteLive.DAL.Postgres.Tests.DataAccessLayers
|
namespace BirdsiteLive.DAL.Postgres.Tests.DataAccessLayers
|
||||||
{
|
{
|
||||||
[TestClass]
|
[TestClass]
|
||||||
public class DbInitializerPostgresDalTests
|
public class DbInitializerPostgresDalTests : PostgresTestingBase
|
||||||
{
|
{
|
||||||
private readonly PostgresSettings _settings;
|
|
||||||
private readonly PostgresTools _tools;
|
|
||||||
|
|
||||||
#region Ctor
|
|
||||||
public DbInitializerPostgresDalTests()
|
|
||||||
{
|
|
||||||
_settings = new PostgresSettings
|
|
||||||
{
|
|
||||||
ConnString = "Host=127.0.0.1;Username=postgres;Password=mysecretpassword;Database=mytestdb",
|
|
||||||
DbVersionTableName = "DbVersionTableName" + RandomGenerator.GetString(4),
|
|
||||||
CachedTweetsTableName = "CachedTweetsTableName" + RandomGenerator.GetString(4),
|
|
||||||
FollowersTableName = "FollowersTableName" + RandomGenerator.GetString(4),
|
|
||||||
TwitterUserTableName = "TwitterUserTableName" + RandomGenerator.GetString(4),
|
|
||||||
};
|
|
||||||
_tools = new PostgresTools(_settings);
|
|
||||||
}
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
[TestCleanup]
|
[TestCleanup]
|
||||||
public async Task CleanUp()
|
public async Task CleanUp()
|
||||||
{
|
{
|
||||||
|
|
|
@ -0,0 +1,99 @@
|
||||||
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using System.Xml;
|
||||||
|
using BirdsiteLive.DAL.Postgres.DataAccessLayers;
|
||||||
|
using BirdsiteLive.DAL.Postgres.Tests.DataAccessLayers.Base;
|
||||||
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||||
|
|
||||||
|
namespace BirdsiteLive.DAL.Postgres.Tests.DataAccessLayers
|
||||||
|
{
|
||||||
|
[TestClass]
|
||||||
|
public class TwitterUserPostgresDalTests : PostgresTestingBase
|
||||||
|
{
|
||||||
|
[TestInitialize]
|
||||||
|
public async Task TestInit()
|
||||||
|
{
|
||||||
|
var dal = new DbInitializerPostgresDal(_settings, _tools);
|
||||||
|
await dal.InitDbAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestCleanup]
|
||||||
|
public async Task CleanUp()
|
||||||
|
{
|
||||||
|
var dal = new DbInitializerPostgresDal(_settings, _tools);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await dal.DeleteAllAsync();
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Console.WriteLine(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task GetTwitterUserAsync_NoUser()
|
||||||
|
{
|
||||||
|
var dal = new TwitterUserPostgresDal(_settings);
|
||||||
|
var result = await dal.GetTwitterUserAsync("dontexist");
|
||||||
|
Assert.IsNull(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task CreateAndGetUser()
|
||||||
|
{
|
||||||
|
var acct = "myid";
|
||||||
|
var lastTweetId = 1548L;
|
||||||
|
|
||||||
|
var dal = new TwitterUserPostgresDal(_settings);
|
||||||
|
|
||||||
|
await dal.CreateTwitterUserAsync(acct, lastTweetId);
|
||||||
|
var result = await dal.GetTwitterUserAsync(acct);
|
||||||
|
|
||||||
|
Assert.AreEqual(acct, result.Acct);
|
||||||
|
Assert.AreEqual(lastTweetId, result.LastTweetPostedId);
|
||||||
|
Assert.AreEqual(lastTweetId, result.LastTweetSynchronizedForAllFollowersId);
|
||||||
|
Assert.IsTrue(result.Id > 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task CreateUpdateAndGetUser()
|
||||||
|
{
|
||||||
|
var acct = "myid";
|
||||||
|
var lastTweetId = 1548L;
|
||||||
|
|
||||||
|
var dal = new TwitterUserPostgresDal(_settings);
|
||||||
|
|
||||||
|
await dal.CreateTwitterUserAsync(acct, lastTweetId);
|
||||||
|
var result = await dal.GetTwitterUserAsync(acct);
|
||||||
|
|
||||||
|
|
||||||
|
var updatedLastTweetId = 1600L;
|
||||||
|
var updatedLastSyncId = 1550L;
|
||||||
|
await dal.UpdateTwitterUserAsync(result.Id, updatedLastTweetId, updatedLastSyncId);
|
||||||
|
|
||||||
|
result = await dal.GetTwitterUserAsync(acct);
|
||||||
|
|
||||||
|
Assert.AreEqual(acct, result.Acct);
|
||||||
|
Assert.AreEqual(updatedLastTweetId, result.LastTweetPostedId);
|
||||||
|
Assert.AreEqual(updatedLastSyncId, result.LastTweetSynchronizedForAllFollowersId);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task CreateAndDeleteUser()
|
||||||
|
{
|
||||||
|
var acct = "myid";
|
||||||
|
var lastTweetId = 1548L;
|
||||||
|
|
||||||
|
var dal = new TwitterUserPostgresDal(_settings);
|
||||||
|
|
||||||
|
await dal.CreateTwitterUserAsync(acct, lastTweetId);
|
||||||
|
var result = await dal.GetTwitterUserAsync(acct);
|
||||||
|
Assert.IsNotNull(result);
|
||||||
|
|
||||||
|
await dal.DeleteTwitterUserAsync(acct);
|
||||||
|
result = await dal.GetTwitterUserAsync(acct);
|
||||||
|
Assert.IsNull(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue