Implementation of CachedTweetsDal + Tests

This commit is contained in:
Nicolas Constant 2020-07-06 02:12:51 -04:00
parent 490c68ccc5
commit aa07ee880e
No known key found for this signature in database
GPG key ID: 1E9F677FB01A5688
4 changed files with 172 additions and 14 deletions

View file

@ -1,8 +1,13 @@
using System.Threading.Tasks;
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 Newtonsoft.Json;
using Tweetinvi.Models;
namespace BirdsiteLive.DAL.Postgres.DataAccessLayers
@ -10,25 +15,69 @@ namespace BirdsiteLive.DAL.Postgres.DataAccessLayers
public class CachedTweetsPostgresDal : PostgresBase, ICachedTweetsDal
{
#region Ctor
public CachedTweetsPostgresDal(PostgresSettings settings, PostgresTools tools) : base(settings)
public CachedTweetsPostgresDal(PostgresSettings settings) : base(settings)
{
}
#endregion
public Task AddTweetAsync(long tweetId, int userId, ITweet tweet)
public async Task CreateTweetAsync(long tweetId, int userId, CachedTweet tweet)
{
throw new System.NotImplementedException();
if(tweetId == default) throw new ArgumentException("tweetId");
if(userId == default) throw new ArgumentException("userId");
var serializedData = JsonConvert.SerializeObject(tweet);
using (var dbConnection = Connection)
{
dbConnection.Open();
await dbConnection.ExecuteAsync(
$"INSERT INTO {_settings.CachedTweetsTableName} (id,twitterUserId,data) VALUES(@id,@twitterUserId,CAST(@data as json))",
new { id = tweetId, twitterUserId = userId, data = serializedData });
}
}
public Task<ITweet> GetTweetAsync(long tweetId)
public async Task<CachedTweet> GetTweetAsync(long tweetId)
{
throw new System.NotImplementedException();
if (tweetId == default) throw new ArgumentException("tweetId");
var query = $"SELECT * FROM {_settings.CachedTweetsTableName} WHERE id = @id";
using (var dbConnection = Connection)
{
dbConnection.Open();
var result = (await dbConnection.QueryAsync<SerializedTweet>(query, new { id = tweetId })).FirstOrDefault();
return Convert(result);
}
}
public Task DeleteTweetAsync(long tweetId)
public async Task DeleteTweetAsync(long tweetId)
{
throw new System.NotImplementedException();
if (tweetId == default) throw new ArgumentException("tweetId");
var query = $"DELETE FROM {_settings.CachedTweetsTableName} WHERE id = @id";
using (var dbConnection = Connection)
{
dbConnection.Open();
await dbConnection.QueryAsync(query, new { id = tweetId });
}
}
private CachedTweet Convert(SerializedTweet result)
{
if (result == null || string.IsNullOrWhiteSpace(result.Data)) return null;
return JsonConvert.DeserializeObject<CachedTweet>(result.Data);
}
}
internal class SerializedTweet
{
public long Id { get; set; }
public int TwitterUserId { get; set; }
public string Data { get; set; }
}
}

View file

@ -1,12 +1,13 @@
using System.Threading.Tasks;
using BirdsiteLive.DAL.Models;
using Tweetinvi.Models;
namespace BirdsiteLive.DAL.Contracts
{
public interface ICachedTweetsDal
{
Task AddTweetAsync(long tweetId, int userId, ITweet tweet);
Task<ITweet> GetTweetAsync(long tweetId);
Task CreateTweetAsync(long tweetId, int userId, CachedTweet tweet);
Task<CachedTweet> GetTweetAsync(long tweetId);
Task DeleteTweetAsync(long tweetId);
}
}

View file

@ -1,10 +1,31 @@
namespace BirdsiteLive.DAL.Models
using System;
namespace BirdsiteLive.DAL.Models
{
public class CachedTweet
{
public int UserId { get; set; }
public long Id { get; set; }
public long TwitterUserId { get; set; }
public string TweetData { get; set; }
public DateTime CreatedAt { get; set; }
public string Text { get; set; }
public string FullText { get; set; }
public long? InReplyToStatusId { get; set; }
public string InReplyToStatusIdStr { get; set; }
public long? InReplyToUserId { get; set; }
public string InReplyToUserIdStr { get; set; }
public string InReplyToScreenName { get; set; }
// List<IHashtagEntity> Hashtags { get; }
//List<IUrlEntity> Urls { get; }
//List<IMediaEntity> Media { get; }
//List<IUserMentionEntity> UserMentions { get; }
//List<ITweet> Retweets { get; set; }
public bool IsRetweet { get; }
public CachedTweet RetweetedTweet { get; }
public string Url { get; }
}
}

View file

@ -0,0 +1,87 @@
using System;
using System.Threading.Tasks;
using BirdsiteLive.DAL.Models;
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 CachedTweetsPostgresDalTests : 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 CreateAndGet()
{
var id = 152L;
var userId = 15;
var tweet = new CachedTweet
{
UserId = userId,
Id = id,
Text = "text data",
FullText = "full text data",
CreatedAt = DateTime.UtcNow
};
var dal = new CachedTweetsPostgresDal(_settings);
await dal.CreateTweetAsync(id, userId, tweet);
var result = await dal.GetTweetAsync(id);
Assert.IsNotNull(result);
Assert.AreEqual(id, result.Id);
Assert.AreEqual(tweet.Text, result.Text);
Assert.AreEqual(tweet.FullText, result.FullText);
Assert.AreEqual(tweet.CreatedAt, result.CreatedAt);
}
[TestMethod]
public async Task CreateAndDelete()
{
var id = 152L;
var userId = 15;
var tweet = new CachedTweet
{
UserId = userId,
Id = id,
Text = "text data",
FullText = "full text data",
CreatedAt = DateTime.UtcNow
};
var dal = new CachedTweetsPostgresDal(_settings);
await dal.CreateTweetAsync(id, userId, tweet);
var result = await dal.GetTweetAsync(id);
Assert.IsNotNull(result);
await dal.DeleteTweetAsync(id);
result = await dal.GetTweetAsync(id);
Assert.IsNull(result);
}
}
}