From b347fcf45abdae58352cf07e24ec907e1f77a31a Mon Sep 17 00:00:00 2001 From: crupest Date: Fri, 10 Jul 2020 15:36:51 +0800 Subject: Add deleted field. --- Timeline/Models/Http/Timeline.cs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'Timeline/Models/Http/Timeline.cs') diff --git a/Timeline/Models/Http/Timeline.cs b/Timeline/Models/Http/Timeline.cs index 80e6e69d..aad9aa7b 100644 --- a/Timeline/Models/Http/Timeline.cs +++ b/Timeline/Models/Http/Timeline.cs @@ -18,7 +18,8 @@ namespace Timeline.Models.Http public class TimelinePostInfo { public long Id { get; set; } - public TimelinePostContentInfo Content { get; set; } = default!; + public TimelinePostContentInfo? Content { get; set; } + public bool Deleted { get; set; } public DateTime Time { get; set; } public UserInfo Author { get; set; } = default!; public DateTime LastUpdated { get; set; } = default!; @@ -73,7 +74,7 @@ namespace Timeline.Models.Http } } - public class TimelinePostContentResolver : IValueResolver + public class TimelinePostContentResolver : IValueResolver { private readonly IActionContextAccessor _actionContextAccessor; private readonly IUrlHelperFactory _urlHelperFactory; @@ -84,13 +85,18 @@ namespace Timeline.Models.Http _urlHelperFactory = urlHelperFactory; } - public TimelinePostContentInfo Resolve(TimelinePost source, TimelinePostInfo destination, TimelinePostContentInfo destMember, ResolutionContext context) + public TimelinePostContentInfo? Resolve(TimelinePost source, TimelinePostInfo destination, TimelinePostContentInfo? destMember, ResolutionContext context) { var actionContext = _actionContextAccessor.AssertActionContextForUrlFill(); var urlHelper = _urlHelperFactory.GetUrlHelper(actionContext); var sourceContent = source.Content; + if (sourceContent == null) + { + return null; + } + if (sourceContent is TextTimelinePostContent textContent) { return new TimelinePostContentInfo @@ -122,7 +128,8 @@ namespace Timeline.Models.Http public TimelineInfoAutoMapperProfile() { CreateMap().ForMember(u => u._links, opt => opt.MapFrom()); - CreateMap().ForMember(p => p.Content, opt => opt.MapFrom()); + CreateMap().ForMember(p => p.Content, opt => opt.MapFrom()) + .ForMember(p => p.Deleted, opt => opt.MapFrom((source, dest) => { return source.Content == null; })); CreateMap(); } } -- cgit v1.2.3 From f93ac5c1b21f9bee827c51955bd831ce0d322aaf Mon Sep 17 00:00:00 2001 From: crupest Date: Fri, 10 Jul 2020 16:10:17 +0800 Subject: Add unit tests. --- Timeline.Tests/IntegratedTests/TimelineTest.cs | 2 ++ Timeline.Tests/Services/TimelineServiceTest.cs | 39 ++++++++++++++++++++++++++ Timeline/Models/Http/Timeline.cs | 3 +- Timeline/Models/Timeline.cs | 1 + 4 files changed, 43 insertions(+), 2 deletions(-) (limited to 'Timeline/Models/Http/Timeline.cs') diff --git a/Timeline.Tests/IntegratedTests/TimelineTest.cs b/Timeline.Tests/IntegratedTests/TimelineTest.cs index 4f21d8d1..64619864 100644 --- a/Timeline.Tests/IntegratedTests/TimelineTest.cs +++ b/Timeline.Tests/IntegratedTests/TimelineTest.cs @@ -942,6 +942,7 @@ namespace Timeline.Tests.IntegratedTests body.Should().NotBeNull(); body.Content.Should().BeEquivalentTo(TimelineHelper.TextPostContent(mockContent)); body.Author.Should().BeEquivalentTo(UserInfos[1]); + body.Deleted.Should().BeFalse(); createRes = body; } { @@ -963,6 +964,7 @@ namespace Timeline.Tests.IntegratedTests body.Content.Should().BeEquivalentTo(TimelineHelper.TextPostContent(mockContent2)); body.Author.Should().BeEquivalentTo(UserInfos[1]); body.Time.Should().BeCloseTo(mockTime2, 1000); + body.Deleted.Should().BeFalse(); createRes2 = body; } { diff --git a/Timeline.Tests/Services/TimelineServiceTest.cs b/Timeline.Tests/Services/TimelineServiceTest.cs index 7e7242a2..123c2b7f 100644 --- a/Timeline.Tests/Services/TimelineServiceTest.cs +++ b/Timeline.Tests/Services/TimelineServiceTest.cs @@ -118,5 +118,44 @@ namespace Timeline.Tests.Services posts.Should().HaveCount(3) .And.Subject.Select(p => (p.Content as TextTimelinePostContent).Text).Should().Equal(postContentList.Skip(1)); } + + [Theory] + [InlineData("@user")] + [InlineData("tl")] + public async Task GetPosts_IncludeDeleted(string timelineName) + { + var userId = await _userService.GetUserIdByUsername("user"); + + var _ = TimelineHelper.ExtractTimelineName(timelineName, out var isPersonal); + if (!isPersonal) + await _timelineService.CreateTimeline(timelineName, userId); + + var postContentList = new string[] { "a", "b", "c", "d" }; + + foreach (var content in postContentList) + { + await _timelineService.CreateTextPost(timelineName, userId, content, null); + } + + var posts = await _timelineService.GetPosts(timelineName); + posts.Should().HaveCount(4); + posts.Select(p => p.Deleted).Should().Equal(Enumerable.Repeat(false, posts.Count)); + posts.Select(p => ((TextTimelinePostContent)p.Content).Text).Should().Equal(postContentList); + + foreach (var id in new long[] { posts[0].Id, posts[2].Id }) + { + await _timelineService.DeletePost(timelineName, id); + } + + posts = await _timelineService.GetPosts(timelineName); + posts.Should().HaveCount(2); + posts.Select(p => p.Deleted).Should().Equal(Enumerable.Repeat(false, posts.Count)); + posts.Select(p => ((TextTimelinePostContent)p.Content).Text).Should().Equal(new string[] { "b", "d" }); + + posts = await _timelineService.GetPosts(timelineName, includeDeleted: true); + posts.Should().HaveCount(4); + posts.Select(p => p.Deleted).Should().Equal(new bool[] { true, false, true, false }); + posts.Where(p => !p.Deleted).Select(p => ((TextTimelinePostContent)p.Content).Text).Should().Equal(new string[] { "b", "d" }); + } } } diff --git a/Timeline/Models/Http/Timeline.cs b/Timeline/Models/Http/Timeline.cs index aad9aa7b..5404d561 100644 --- a/Timeline/Models/Http/Timeline.cs +++ b/Timeline/Models/Http/Timeline.cs @@ -128,8 +128,7 @@ namespace Timeline.Models.Http public TimelineInfoAutoMapperProfile() { CreateMap().ForMember(u => u._links, opt => opt.MapFrom()); - CreateMap().ForMember(p => p.Content, opt => opt.MapFrom()) - .ForMember(p => p.Deleted, opt => opt.MapFrom((source, dest) => { return source.Content == null; })); + CreateMap().ForMember(p => p.Content, opt => opt.MapFrom()); CreateMap(); } } diff --git a/Timeline/Models/Timeline.cs b/Timeline/Models/Timeline.cs index b1772fa7..7afb1984 100644 --- a/Timeline/Models/Timeline.cs +++ b/Timeline/Models/Timeline.cs @@ -60,6 +60,7 @@ namespace Timeline.Models public long Id { get; set; } public ITimelinePostContent? Content { get; set; } + public bool Deleted => Content == null; public DateTime Time { get; set; } public User Author { get; set; } public DateTime LastUpdated { get; set; } -- cgit v1.2.3