Commit b292f7e6 authored by HankG's avatar HankG

Add ability to poll by id or social network id for posts

parent 32b47ce3
......@@ -9,11 +9,15 @@ namespace MySocialPortalLib.Repository
void AddPost(Post post);
void AddPosts(IEnumerable<Post> posts);
Post? GetById(string id);
Post? GetByOriginalNetworkId(string id);
IEnumerable<Post> GetPosts(int maxPosts);
IEnumerable<Post> GetPosts(int maxPosts, string personId = "", string networkName = "");
IEnumerable<Post> GetPosts(DateTimeOffset newestPostTime, int maxPosts, bool inclusive);
IEnumerable<Post> GetPosts(DateTimeOffset newestPostTime, int maxPosts, bool inclusive, string personId = "", string networkName = "");
IEnumerable<Post> GetPosts(DateTimeOffset oldestPostTime, DateTimeOffset newestPostTime, int maxPosts, bool inclusive);
IEnumerable<Post> GetPosts(DateTimeOffset oldestPostTime, DateTimeOffset newestPostTime, int maxPosts, bool inclusive, string personId = "", string networkName = "");
}
}
\ No newline at end of file
......@@ -67,31 +67,70 @@ namespace MySocialPortalLib.Repository
return _postsRepository.Query<Post>(_postsCollectionName).LongCount();
}
public IEnumerable<Post> GetPosts(int maxPosts = DefaultMaxPosts)
public Post? GetById(string id)
{
return GetPosts(DateTimeOffset.MinValue, DateTimeOffset.MaxValue, maxPosts, true);
if (string.IsNullOrWhiteSpace(id))
{
return null;
}
return _postsRepository.Query<Post>(_postsCollectionName)
.Where(p => p.Id == id)
.FirstOrDefault();
}
public IEnumerable<Post> GetPosts(DateTimeOffset newestPostTime, int maxPosts = DefaultMaxPosts, bool inclusive = true)
public Post GetByOriginalNetworkId(string id)
{
return GetPosts(DateTimeOffset.MinValue, newestPostTime, maxPosts, inclusive);
if (string.IsNullOrWhiteSpace(id))
{
return null;
}
return _postsRepository.Query<Post>(_postsCollectionName)
.Where(p => p.OriginalLinkUrlOrId == id)
.FirstOrDefault();
}
public IEnumerable<Post> GetPosts(DateTimeOffset oldestPostTime, DateTimeOffset newestPostTime, int maxPosts = DefaultMaxPosts, bool inclusive = true)
public IEnumerable<Post> GetPosts(int maxPosts = DefaultMaxPosts, string personId = "", string networkName = "")
{
return GetPosts(DateTimeOffset.MinValue, DateTimeOffset.MaxValue, maxPosts, true, personId, networkName);
}
public IEnumerable<Post> GetPosts(DateTimeOffset newestPostTime, int maxPosts = DefaultMaxPosts, bool inclusive = true, string personId = "", string networkName = "")
{
return GetPosts(DateTimeOffset.MinValue, newestPostTime, maxPosts, inclusive, personId, networkName);
}
public IEnumerable<Post> GetPosts(DateTimeOffset oldestPostTime, DateTimeOffset newestPostTime, int maxPosts = DefaultMaxPosts, bool inclusive = true, string personId = "", string networkName = "")
{
var baseQuery = GetDefaultQuery();
var dateFilteredQuery = inclusive ? baseQuery.Where(p => p.PostDateTime >= oldestPostTime && p.PostDateTime <= newestPostTime)
: baseQuery.Where(p => p.PostDateTime > oldestPostTime && p.PostDateTime < newestPostTime) ;
return dateFilteredQuery
.Limit(maxPosts)
.ToEnumerable();
var query = inclusive ? baseQuery.Where(p => p.PostDateTime >= oldestPostTime && p.PostDateTime <= newestPostTime)
: baseQuery.Where(p => p.PostDateTime > oldestPostTime && p.PostDateTime < newestPostTime) ;
if (!string.IsNullOrWhiteSpace(personId))
{
query = query.Where(p => p.UserId == personId);
}
if (!string.IsNullOrWhiteSpace(networkName))
{
query = query.Where(p => p.OriginalSocialMediaSystem == networkName);
}
if (query.ToList().Count > 0)
{
return query
.OrderByDescending(p => p.PostDateTime)
.Limit(maxPosts)
.ToEnumerable();
}
return query.ToEnumerable();
}
private ILiteQueryable<Post> GetDefaultQuery()
{
return _postsRepository.Query<Post>(_postsCollectionName)
.OrderByDescending(p => p.PostDateTime);
return _postsRepository.Query<Post>(_postsCollectionName);
}
}
}
\ No newline at end of file
......@@ -108,6 +108,67 @@ namespace MySocialPortalLibTest.Service
Assert.Equal(postLimit, postsResults.Count());
}
[Fact]
public void TestGetPostForAUser()
{
using var postDb = GetTempDb();
var postCount = 10;
var postLimit = 3;
var posts = GeneratePosts(postCount);
var userId = Guid.NewGuid().ToString();
postDb.AddPosts(posts);
var post = new Post
{
UserId = userId
};
postDb.AddPost(post);
var postResults = postDb.GetPosts(personId: userId);
Assert.Equal(1, postResults.Count());
Assert.Equal(post, postResults.First());
postResults = postDb.GetPosts(personId: "1234");
Assert.False(postResults.Any());
}
[Fact]
public void TestGetPostForAUserNetwork()
{
using var postDb = GetTempDb();
var postCount = 10;
var postLimit = 3;
var posts = GeneratePosts(postCount);
var userId = Guid.NewGuid().ToString();
postDb.AddPosts(posts);
var post1 = new Post
{
UserId = userId,
OriginalSocialMediaSystem = StandardSocialNetworkNames.Twitter
};
postDb.AddPost(post1);
var post2 = new Post
{
UserId = userId,
OriginalSocialMediaSystem = StandardSocialNetworkNames.Diaspora
};
postDb.AddPost(post2);
var post3 = new Post
{
UserId = Guid.NewGuid().ToString(),
OriginalSocialMediaSystem = StandardSocialNetworkNames.Diaspora
};
postDb.AddPost(post3);
var postResults = postDb.GetPosts(personId: userId, networkName: StandardSocialNetworkNames.Diaspora);
Assert.Equal(1, postResults.Count());
Assert.Equal(post2, postResults.First());
postResults = postDb.GetPosts(networkName: StandardSocialNetworkNames.Diaspora);
Assert.Equal(2, postResults.Count());
postResults = postDb.GetPosts(personId: "1234");
Assert.False(postResults.Any());
postResults = postDb.GetPosts(personId: userId, networkName: "OtherNetwork");
Assert.False(postResults.Any());
}
[Fact]
public void TestPagingFlow()
{
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment