diff --git a/src/DataAccessLayers/BirdsiteLive.DAL.Postgres/DataAccessLayers/CachedTweetsPostgresDal.cs b/src/DataAccessLayers/BirdsiteLive.DAL.Postgres/DataAccessLayers/CachedTweetsPostgresDal.cs index 3251c41..b95d5d1 100644 --- a/src/DataAccessLayers/BirdsiteLive.DAL.Postgres/DataAccessLayers/CachedTweetsPostgresDal.cs +++ b/src/DataAccessLayers/BirdsiteLive.DAL.Postgres/DataAccessLayers/CachedTweetsPostgresDal.cs @@ -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 GetTweetAsync(long tweetId) + public async Task 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(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(result.Data); } } + + internal class SerializedTweet + { + public long Id { get; set; } + public int TwitterUserId { get; set; } + public string Data { get; set; } + } } \ No newline at end of file diff --git a/src/DataAccessLayers/BirdsiteLive.DAL/Contracts/ICachedTweetsDal.cs b/src/DataAccessLayers/BirdsiteLive.DAL/Contracts/ICachedTweetsDal.cs index d1214a5..64ffe41 100644 --- a/src/DataAccessLayers/BirdsiteLive.DAL/Contracts/ICachedTweetsDal.cs +++ b/src/DataAccessLayers/BirdsiteLive.DAL/Contracts/ICachedTweetsDal.cs @@ -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 GetTweetAsync(long tweetId); + Task CreateTweetAsync(long tweetId, int userId, CachedTweet tweet); + Task GetTweetAsync(long tweetId); Task DeleteTweetAsync(long tweetId); } } \ No newline at end of file diff --git a/src/DataAccessLayers/BirdsiteLive.DAL/Models/CachedTweet.cs b/src/DataAccessLayers/BirdsiteLive.DAL/Models/CachedTweet.cs index d1472b6..25f5120 100644 --- a/src/DataAccessLayers/BirdsiteLive.DAL/Models/CachedTweet.cs +++ b/src/DataAccessLayers/BirdsiteLive.DAL/Models/CachedTweet.cs @@ -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 Hashtags { get; } + //List Urls { get; } + //List Media { get; } + //List UserMentions { get; } + //List Retweets { get; set; } + public bool IsRetweet { get; } + public CachedTweet RetweetedTweet { get; } + public string Url { get; } } + } \ No newline at end of file diff --git a/src/Tests/BirdsiteLive.DAL.Postgres.Tests/DataAccessLayers/CachedTweetsPostgresDalTests.cs b/src/Tests/BirdsiteLive.DAL.Postgres.Tests/DataAccessLayers/CachedTweetsPostgresDalTests.cs new file mode 100644 index 0000000..021784e --- /dev/null +++ b/src/Tests/BirdsiteLive.DAL.Postgres.Tests/DataAccessLayers/CachedTweetsPostgresDalTests.cs @@ -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); + } + } +} \ No newline at end of file