diff options
author | crupest <crupest@outlook.com> | 2019-11-20 16:49:12 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2019-11-20 16:49:12 +0800 |
commit | ca87f6781a5b0e80989a66be338a699846c40f8d (patch) | |
tree | 788b8acdf1141c757cb3226d3cd5f64594386b8f | |
parent | 23b3f79f4d1b48eb4a230402b1ad88a8d5409d24 (diff) | |
download | timeline-ca87f6781a5b0e80989a66be338a699846c40f8d.tar.gz timeline-ca87f6781a5b0e80989a66be338a699846c40f8d.tar.bz2 timeline-ca87f6781a5b0e80989a66be338a699846c40f8d.zip |
Get posts should return posts ordered by time.
-rw-r--r-- | Timeline.Tests/IntegratedTests/PersonalTimelineTest.cs | 29 | ||||
-rw-r--r-- | Timeline/Services/TimelineService.cs | 2 |
2 files changed, 29 insertions, 2 deletions
diff --git a/Timeline.Tests/IntegratedTests/PersonalTimelineTest.cs b/Timeline.Tests/IntegratedTests/PersonalTimelineTest.cs index 43549d1a..483499fb 100644 --- a/Timeline.Tests/IntegratedTests/PersonalTimelineTest.cs +++ b/Timeline.Tests/IntegratedTests/PersonalTimelineTest.cs @@ -2,6 +2,7 @@ using Microsoft.AspNetCore.Mvc.Testing; using System; using System.Collections.Generic; +using System.Linq; using System.Net.Http; using System.Threading.Tasks; using Timeline.Models; @@ -461,7 +462,33 @@ namespace Timeline.Tests.IntegratedTests Time = createRes2.Time }); } - // TODO! Add post not exist tests. + } + } + + [Fact] + public async Task GetPost_Should_Ordered() + { + using var client = await CreateClientAsUser(); + + async Task<long> CreatePost(DateTime time) + { + var res = await client.PostAsJsonAsync("users/user/timeline/postop/create", + new TimelinePostCreateRequest { Content = "aaa", Time = time }); + return res.Should().HaveStatusCode(200) + .And.HaveJsonBody<TimelinePostCreateResponse>() + .Which.Id; + } + + var now = DateTime.Now; + var id0 = await CreatePost(now.AddDays(1)); + var id1 = await CreatePost(now.AddDays(-1)); + var id2 = await CreatePost(now); + + { + var res = await client.GetAsync("users/user/timeline/posts"); + res.Should().HaveStatusCode(200) + .And.HaveJsonBody<TimelinePostInfo[]>() + .Which.Select(p => p.Id).Should().Equal(id1, id2, id0); } } } diff --git a/Timeline/Services/TimelineService.cs b/Timeline/Services/TimelineService.cs index 9ae9dc1c..affcff2e 100644 --- a/Timeline/Services/TimelineService.cs +++ b/Timeline/Services/TimelineService.cs @@ -348,7 +348,7 @@ namespace Timeline.Services throw new ArgumentNullException(nameof(name));
var timelineId = await FindTimelineId(name);
- var postEntities = await Database.TimelinePosts.Where(p => p.TimelineId == timelineId && p.Content != null).ToListAsync();
+ var postEntities = await Database.TimelinePosts.OrderBy(p => p.Time).Where(p => p.TimelineId == timelineId && p.Content != null).ToListAsync();
var posts = new List<TimelinePostInfo>(await Task.WhenAll(postEntities.Select(async p => new TimelinePostInfo
{
Id = p.Id,
|