From 1a44a615ad79054218d493f8bc4bf8563201002b Mon Sep 17 00:00:00 2001 From: crupest Date: Thu, 7 Jan 2021 20:48:47 +0800 Subject: feat: Add check existence feature to bookmark and highlight service. --- .../Timeline/Services/BookmarkTimelineService.cs | 23 ++++++++++++++++++++++ .../Timeline/Services/HighlightTimelineService.cs | 17 ++++++++++++++++ 2 files changed, 40 insertions(+) (limited to 'BackEnd/Timeline') diff --git a/BackEnd/Timeline/Services/BookmarkTimelineService.cs b/BackEnd/Timeline/Services/BookmarkTimelineService.cs index 4c8bfdae..4930686e 100644 --- a/BackEnd/Timeline/Services/BookmarkTimelineService.cs +++ b/BackEnd/Timeline/Services/BookmarkTimelineService.cs @@ -33,6 +33,18 @@ namespace Timeline.Services /// Thrown when user does not exist. Task> GetBookmarks(long userId); + /// + /// Check if a timeline is a bookmark. + /// + /// The user id. + /// Timeline id. + /// If true it will throw when user does not exist. + /// If true it will throw when timeline does not exist. + /// True if timeline is a bookmark. Otherwise false. + /// Throw if user does not exist and is true. + /// Thrown if timeline does not exist and is true. + Task IsBookmark(long userId, long timelineId, bool checkUserExistence = true, bool checkTimelineExistence = true); + /// /// Add a bookmark to tail to a user. /// @@ -110,6 +122,17 @@ namespace Timeline.Services return entities.Select(e => e.TimelineId).ToList(); } + public async Task IsBookmark(long userId, long timelineId, bool checkUserExistence = true, bool checkTimelineExistence = true) + { + if (checkUserExistence && !await _userService.CheckUserExistence(userId)) + throw new UserNotExistException(userId); + + if (checkTimelineExistence && !await _timelineService.CheckExistence(timelineId)) + throw new TimelineNotExistException(timelineId); + + return await _database.BookmarkTimelines.AnyAsync(b => b.TimelineId == timelineId && b.UserId == userId); + } + public async Task MoveBookmark(long userId, long timelineId, long newPosition) { if (!await _userService.CheckUserExistence(userId)) diff --git a/BackEnd/Timeline/Services/HighlightTimelineService.cs b/BackEnd/Timeline/Services/HighlightTimelineService.cs index bf0aac91..557478c7 100644 --- a/BackEnd/Timeline/Services/HighlightTimelineService.cs +++ b/BackEnd/Timeline/Services/HighlightTimelineService.cs @@ -31,6 +31,15 @@ namespace Timeline.Services /// Id list of all highlight timelines. Task> GetHighlightTimelines(); + /// + /// Check if a timeline is highlight timeline. + /// + /// Timeline id. + /// If true it will throw if timeline does not exist. + /// True if timeline is highlight. Otherwise false. + /// Thrown when timeline does not exist and is true. + Task IsHighlightTimeline(long timelineId, bool checkTimelineExistence = true); + /// /// Add a timeline to highlight list. /// @@ -172,5 +181,13 @@ namespace Timeline.Services await transaction.CommitAsync(); } + + public async Task IsHighlightTimeline(long timelineId, bool checkTimelineExistence = true) + { + if (checkTimelineExistence && !await _timelineService.CheckExistence(timelineId)) + throw new TimelineNotExistException(timelineId); + + return await _database.HighlightTimelines.AnyAsync(t => t.TimelineId == timelineId); + } } } -- cgit v1.2.3 From 04186d5f1091266b85758d4b4255c6a7c1b498f6 Mon Sep 17 00:00:00 2001 From: crupest Date: Thu, 7 Jan 2021 22:10:58 +0800 Subject: feat: Timeline info contains bookmark and highlight flag. --- .../IntegratedTests/BookmarkTimelineTest.cs | 38 ++++++++++++++++++++++ .../IntegratedTests/HighlightTimelineTest.cs | 33 +++++++++++++++++++ .../Controllers/BookmarkTimelineController.cs | 2 +- .../Controllers/HighlightTimelineController.cs | 2 +- BackEnd/Timeline/Controllers/TimelineController.cs | 10 +++--- BackEnd/Timeline/Models/Http/Timeline.cs | 8 ++++- BackEnd/Timeline/Models/Mapper/TimelineMapper.cs | 14 +++++--- 7 files changed, 95 insertions(+), 12 deletions(-) (limited to 'BackEnd/Timeline') diff --git a/BackEnd/Timeline.Tests/IntegratedTests/BookmarkTimelineTest.cs b/BackEnd/Timeline.Tests/IntegratedTests/BookmarkTimelineTest.cs index e6ae178f..99cf6d3a 100644 --- a/BackEnd/Timeline.Tests/IntegratedTests/BookmarkTimelineTest.cs +++ b/BackEnd/Timeline.Tests/IntegratedTests/BookmarkTimelineTest.cs @@ -83,5 +83,43 @@ namespace Timeline.Tests.IntegratedTests h.Should().BeEmpty(); } } + + [Fact] + public async Task TimelineGet_IsBookmarkField_ShouldWork() + { + using var client = await CreateClientAsUser(); + await client.TestPostAsync("timelines", new TimelineCreateRequest { Name = "t" }); + + { + var t = await client.TestGetAsync("timelines/t"); + t.IsBookmark.Should().BeFalse(); + } + + await client.TestPutAsync("bookmarks/t"); + + { + var t = await client.TestGetAsync("timelines/t"); + t.IsBookmark.Should().BeTrue(); + } + + { + var client1 = await CreateDefaultClient(); + var t = await client1.TestGetAsync("timelines/t"); + t.IsBookmark.Should().BeFalse(); + } + + { + var client1 = await CreateClientAsAdministrator(); + var t = await client1.TestGetAsync("timelines/t"); + t.IsBookmark.Should().BeFalse(); + } + + await client.TestDeleteAsync("bookmarks/t"); + + { + var t = await client.TestGetAsync("timelines/t"); + t.IsBookmark.Should().BeFalse(); + } + } } } diff --git a/BackEnd/Timeline.Tests/IntegratedTests/HighlightTimelineTest.cs b/BackEnd/Timeline.Tests/IntegratedTests/HighlightTimelineTest.cs index a3f2855e..440759f4 100644 --- a/BackEnd/Timeline.Tests/IntegratedTests/HighlightTimelineTest.cs +++ b/BackEnd/Timeline.Tests/IntegratedTests/HighlightTimelineTest.cs @@ -86,5 +86,38 @@ namespace Timeline.Tests.IntegratedTests h.Should().BeEmpty(); } } + + [Fact] + public async Task TimelineGet_IsHighlighField_Should_Work() + { + using var client = await CreateClientAsAdministrator(); + await client.TestPostAsync("timelines", new TimelineCreateRequest { Name = "t" }); + + { + var t = await client.TestGetAsync("timelines/t"); + t.IsHighlight.Should().BeFalse(); + } + + await client.TestPutAsync("highlights/t"); + + { + var t = await client.TestGetAsync("timelines/t"); + t.IsHighlight.Should().BeTrue(); + } + + { + var client1 = await CreateDefaultClient(); + var t = await client1.TestGetAsync("timelines/t"); + t.IsHighlight.Should().BeTrue(); + } + + await client.TestDeleteAsync("highlights/t"); + + { + var t = await client.TestGetAsync("timelines/t"); + t.IsHighlight.Should().BeFalse(); + } + + } } } diff --git a/BackEnd/Timeline/Controllers/BookmarkTimelineController.cs b/BackEnd/Timeline/Controllers/BookmarkTimelineController.cs index 4313115e..16793de6 100644 --- a/BackEnd/Timeline/Controllers/BookmarkTimelineController.cs +++ b/BackEnd/Timeline/Controllers/BookmarkTimelineController.cs @@ -40,7 +40,7 @@ namespace Timeline.Controllers { var ids = await _service.GetBookmarks(this.GetUserId()); var timelines = await _timelineService.GetTimelineList(ids); - return await _timelineMapper.MapToHttp(timelines, Url); + return await _timelineMapper.MapToHttp(timelines, Url, this.GetOptionalUserId()); } /// diff --git a/BackEnd/Timeline/Controllers/HighlightTimelineController.cs b/BackEnd/Timeline/Controllers/HighlightTimelineController.cs index cc19cada..ea012f76 100644 --- a/BackEnd/Timeline/Controllers/HighlightTimelineController.cs +++ b/BackEnd/Timeline/Controllers/HighlightTimelineController.cs @@ -38,7 +38,7 @@ namespace Timeline.Controllers { var ids = await _service.GetHighlightTimelines(); var timelines = await _timelineService.GetTimelineList(ids); - return await _timelineMapper.MapToHttp(timelines, Url); + return await _timelineMapper.MapToHttp(timelines, Url, this.GetOptionalUserId()); } /// diff --git a/BackEnd/Timeline/Controllers/TimelineController.cs b/BackEnd/Timeline/Controllers/TimelineController.cs index efc49952..b2e37b15 100644 --- a/BackEnd/Timeline/Controllers/TimelineController.cs +++ b/BackEnd/Timeline/Controllers/TimelineController.cs @@ -109,7 +109,7 @@ namespace Timeline.Controllers } var timelines = await _service.GetTimelines(relationship, visibilityFilter); - var result = await _timelineMapper.MapToHttp(timelines, Url); + var result = await _timelineMapper.MapToHttp(timelines, Url, this.GetOptionalUserId()); return result; } @@ -168,7 +168,7 @@ namespace Timeline.Controllers else { var t = await _service.GetTimeline(timelineId); - var result = await _timelineMapper.MapToHttp(t, Url); + var result = await _timelineMapper.MapToHttp(t, Url, this.GetOptionalUserId()); return result; } } @@ -363,7 +363,7 @@ namespace Timeline.Controllers } await _service.ChangeProperty(timelineId, _mapper.Map(body)); var t = await _service.GetTimeline(timelineId); - var result = await _timelineMapper.MapToHttp(t, Url); + var result = await _timelineMapper.MapToHttp(t, Url, this.GetOptionalUserId()); return result; } @@ -448,7 +448,7 @@ namespace Timeline.Controllers try { var timeline = await _service.CreateTimeline(body.Name, userId); - var result = await _timelineMapper.MapToHttp(timeline, Url); + var result = await _timelineMapper.MapToHttp(timeline, Url, this.GetOptionalUserId()); return result; } catch (EntityAlreadyExistException e) when (e.EntityName == EntityNames.Timeline) @@ -507,7 +507,7 @@ namespace Timeline.Controllers { await _service.ChangeTimelineName(timelineId, body.NewName); var timeline = await _service.GetTimeline(timelineId); - return await _timelineMapper.MapToHttp(timeline, Url); + return await _timelineMapper.MapToHttp(timeline, Url, this.GetOptionalUserId()); } catch (EntityAlreadyExistException) { diff --git a/BackEnd/Timeline/Models/Http/Timeline.cs b/BackEnd/Timeline/Models/Http/Timeline.cs index 06fa4e5a..5e5889f6 100644 --- a/BackEnd/Timeline/Models/Http/Timeline.cs +++ b/BackEnd/Timeline/Models/Http/Timeline.cs @@ -86,7 +86,7 @@ namespace Timeline.Models.Http { public HttpTimeline() { } - public HttpTimeline(string uniqueId, string title, string name, DateTime nameLastModifed, string description, HttpUser owner, TimelineVisibility visibility, List members, DateTime createTime, DateTime lastModified, HttpTimelineLinks links) + public HttpTimeline(string uniqueId, string title, string name, DateTime nameLastModifed, string description, HttpUser owner, TimelineVisibility visibility, List members, DateTime createTime, DateTime lastModified, bool isHighlight, bool isBookmark, HttpTimelineLinks links) { UniqueId = uniqueId; Title = title; @@ -98,6 +98,8 @@ namespace Timeline.Models.Http Members = members; CreateTime = createTime; LastModified = lastModified; + IsHighlight = isHighlight; + IsBookmark = isBookmark; _links = links; } @@ -144,6 +146,10 @@ namespace Timeline.Models.Http /// public DateTime LastModified { get; set; } = default!; + public bool IsHighlight { get; set; } + + public bool IsBookmark { get; set; } + #pragma warning disable CA1707 // Identifiers should not contain underscores /// /// Related links. diff --git a/BackEnd/Timeline/Models/Mapper/TimelineMapper.cs b/BackEnd/Timeline/Models/Mapper/TimelineMapper.cs index 14ca8fe9..95418573 100644 --- a/BackEnd/Timeline/Models/Mapper/TimelineMapper.cs +++ b/BackEnd/Timeline/Models/Mapper/TimelineMapper.cs @@ -15,14 +15,18 @@ namespace Timeline.Models.Mapper { private readonly DatabaseContext _database; private readonly UserMapper _userMapper; + private readonly IHighlightTimelineService _highlightTimelineService; + private readonly IBookmarkTimelineService _bookmarkTimelineService; - public TimelineMapper(DatabaseContext database, UserMapper userMapper) + public TimelineMapper(DatabaseContext database, UserMapper userMapper, IHighlightTimelineService highlightTimelineService, IBookmarkTimelineService bookmarkTimelineService) { _database = database; _userMapper = userMapper; + _highlightTimelineService = highlightTimelineService; + _bookmarkTimelineService = bookmarkTimelineService; } - public async Task MapToHttp(TimelineEntity entity, IUrlHelper urlHelper) + public async Task MapToHttp(TimelineEntity entity, IUrlHelper urlHelper, long? userId) { await _database.Entry(entity).Reference(e => e.Owner).LoadAsync(); await _database.Entry(entity).Collection(e => e.Members).Query().Include(m => m.User).LoadAsync(); @@ -40,6 +44,8 @@ namespace Timeline.Models.Mapper members: await _userMapper.MapToHttp(entity.Members.Select(m => m.User).ToList(), urlHelper), createTime: entity.CreateTime, lastModified: entity.LastModified, + isHighlight: await _highlightTimelineService.IsHighlightTimeline(entity.Id), + isBookmark: userId is not null && await _bookmarkTimelineService.IsBookmark(userId.Value, entity.Id, false, false), links: new HttpTimelineLinks( self: urlHelper.ActionLink(nameof(TimelineController.TimelineGet), nameof(TimelineController)[0..^nameof(Controller).Length], new { timeline = timelineName }), posts: urlHelper.ActionLink(nameof(TimelineController.PostListGet), nameof(TimelineController)[0..^nameof(Controller).Length], new { timeline = timelineName }) @@ -47,12 +53,12 @@ namespace Timeline.Models.Mapper ); } - public async Task> MapToHttp(List entities, IUrlHelper urlHelper) + public async Task> MapToHttp(List entities, IUrlHelper urlHelper, long? userId) { var result = new List(); foreach (var entity in entities) { - result.Add(await MapToHttp(entity, urlHelper)); + result.Add(await MapToHttp(entity, urlHelper, userId)); } return result; } -- cgit v1.2.3