From e701f8bab033dd364673215e66d58a1cb9fea339 Mon Sep 17 00:00:00 2001 From: crupest Date: Thu, 7 Jan 2021 20:36:57 +0800 Subject: refactor: Highlight and bookmark service now use timeline id. --- .../Timeline/Services/BookmarkTimelineService.cs | 51 +++++++++------------ .../Timeline/Services/HighlightTimelineService.cs | 52 +++++++++------------- 2 files changed, 42 insertions(+), 61 deletions(-) (limited to 'BackEnd/Timeline/Services') diff --git a/BackEnd/Timeline/Services/BookmarkTimelineService.cs b/BackEnd/Timeline/Services/BookmarkTimelineService.cs index 380e1909..4c8bfdae 100644 --- a/BackEnd/Timeline/Services/BookmarkTimelineService.cs +++ b/BackEnd/Timeline/Services/BookmarkTimelineService.cs @@ -37,37 +37,32 @@ namespace Timeline.Services /// Add a bookmark to tail to a user. /// /// User id of bookmark owner. - /// Timeline name. - /// Thrown when is null. - /// Thrown when is not a valid name. + /// Timeline id. + /// True if timeline is added to bookmark. False if it already is. /// Thrown when user does not exist. /// Thrown when timeline does not exist. - Task AddBookmark(long userId, string timelineName); + Task AddBookmark(long userId, long timelineId); /// /// Remove a bookmark from a user. /// /// User id of bookmark owner. - /// Timeline name. + /// Timeline id. /// True if deletion is performed. False if bookmark does not exist. - /// Thrown when is null. - /// Thrown when is not a valid name. /// Thrown when user does not exist. /// Thrown when timeline does not exist. - Task RemoveBookmark(long userId, string timelineName); + Task RemoveBookmark(long userId, long timelineId); /// /// Move bookmark to a new position. /// /// User id of bookmark owner. - /// Timeline name. + /// Timeline name. /// New position. Starts at 1. - /// Thrown when is null. - /// Thrown when is not a valid name. /// Thrown when user does not exist. /// Thrown when timeline does not exist. /// Thrown when the timeline is not a bookmark. - Task MoveBookmark(long userId, string timelineName, long newPosition); + Task MoveBookmark(long userId, long timelineId, long newPosition); } public class BookmarkTimelineService : IBookmarkTimelineService @@ -83,20 +78,16 @@ namespace Timeline.Services _timelineService = timelineService; } - public async Task AddBookmark(long userId, string timelineName) + public async Task AddBookmark(long userId, long timelineId) { - if (timelineName is null) - throw new ArgumentNullException(nameof(timelineName)); - if (!await _userService.CheckUserExistence(userId)) throw new UserNotExistException(userId); - var timelineId = await _timelineService.GetTimelineIdByName(timelineName); + if (!await _timelineService.CheckExistence(timelineId)) + throw new TimelineNotExistException(timelineId); - if (await _database.BookmarkTimelines.SingleOrDefaultAsync(t => t.TimelineId == timelineId) is not null) - { - return; - } + if (await _database.BookmarkTimelines.AnyAsync(t => t.TimelineId == timelineId && t.UserId == userId)) + return false; _database.BookmarkTimelines.Add(new BookmarkTimelineEntity { @@ -106,6 +97,7 @@ namespace Timeline.Services }); await _database.SaveChangesAsync(); + return true; } public async Task> GetBookmarks(long userId) @@ -118,12 +110,13 @@ namespace Timeline.Services return entities.Select(e => e.TimelineId).ToList(); } - public async Task MoveBookmark(long userId, string timelineName, long newPosition) + public async Task MoveBookmark(long userId, long timelineId, long newPosition) { - if (timelineName == null) - throw new ArgumentNullException(nameof(timelineName)); + if (!await _userService.CheckUserExistence(userId)) + throw new UserNotExistException(userId); - var timelineId = await _timelineService.GetTimelineIdByName(timelineName); + if (!await _timelineService.CheckExistence(timelineId)) + throw new TimelineNotExistException(timelineId); var entity = await _database.BookmarkTimelines.SingleOrDefaultAsync(t => t.TimelineId == timelineId && t.UserId == userId); @@ -159,15 +152,13 @@ namespace Timeline.Services await transaction.CommitAsync(); } - public async Task RemoveBookmark(long userId, string timelineName) + public async Task RemoveBookmark(long userId, long timelineId) { - if (timelineName is null) - throw new ArgumentNullException(nameof(timelineName)); - if (!await _userService.CheckUserExistence(userId)) throw new UserNotExistException(userId); - var timelineId = await _timelineService.GetTimelineIdByName(timelineName); + if (!await _timelineService.CheckExistence(timelineId)) + throw new TimelineNotExistException(timelineId); var entity = await _database.BookmarkTimelines.SingleOrDefaultAsync(t => t.UserId == userId && t.TimelineId == timelineId); diff --git a/BackEnd/Timeline/Services/HighlightTimelineService.cs b/BackEnd/Timeline/Services/HighlightTimelineService.cs index d0a06fe7..bf0aac91 100644 --- a/BackEnd/Timeline/Services/HighlightTimelineService.cs +++ b/BackEnd/Timeline/Services/HighlightTimelineService.cs @@ -34,40 +34,35 @@ namespace Timeline.Services /// /// Add a timeline to highlight list. /// - /// The timeline name. + /// The timeline id. /// The user id of operator. - /// Thrown when is null. - /// Thrown when is not a valid timeline name. - /// Thrown when timeline with given name does not exist. + /// True if timeline is actually added to highligh. False if it already is. + /// Thrown when timeline with given id does not exist. /// Thrown when user with given operator id does not exist. - Task AddHighlightTimeline(string timelineName, long? operatorId); + Task AddHighlightTimeline(long timelineId, long? operatorId); /// /// Remove a timeline from highlight list. /// - /// The timeline name. + /// The timeline id. /// The user id of operator. /// True if deletion is actually performed. Otherwise false (timeline was not in the list). - /// Thrown when is null. - /// Thrown when is not a valid timeline name. - /// Thrown when timeline with given name does not exist. + /// Thrown when timeline with given id does not exist. /// Thrown when user with given operator id does not exist. - Task RemoveHighlightTimeline(string timelineName, long? operatorId); + Task RemoveHighlightTimeline(long timelineId, long? operatorId); /// /// Move a highlight timeline to a new position. /// - /// The timeline name. + /// The timeline name. /// The new position. Starts at 1. - /// Thrown when is null. - /// Thrown when is not a valid timeline name. - /// Thrown when timeline with given name does not exist. + /// Thrown when timeline with given id does not exist. /// Thrown when given timeline is not a highlight timeline. /// /// If is smaller than 1. Then move the timeline to head. /// If is bigger than total count. Then move the timeline to tail. /// - Task MoveHighlightTimeline(string timelineName, long newPosition); + Task MoveHighlightTimeline(long timelineId, long newPosition); } public class HighlightTimelineService : IHighlightTimelineService @@ -85,12 +80,10 @@ namespace Timeline.Services _clock = clock; } - public async Task AddHighlightTimeline(string timelineName, long? operatorId) + public async Task AddHighlightTimeline(long timelineId, long? operatorId) { - if (timelineName == null) - throw new ArgumentNullException(nameof(timelineName)); - - var timelineId = await _timelineService.GetTimelineIdByName(timelineName); + if (!await _timelineService.CheckExistence(timelineId)) + throw new TimelineNotExistException(timelineId); if (operatorId.HasValue && !await _userService.CheckUserExistence(operatorId.Value)) { @@ -99,10 +92,11 @@ namespace Timeline.Services var alreadyIs = await _database.HighlightTimelines.AnyAsync(t => t.TimelineId == timelineId); - if (alreadyIs) return; + if (alreadyIs) return false; _database.HighlightTimelines.Add(new HighlightTimelineEntity { TimelineId = timelineId, OperatorId = operatorId, AddTime = _clock.GetCurrentTime(), Order = await _database.HighlightTimelines.CountAsync() + 1 }); await _database.SaveChangesAsync(); + return true; } public async Task> GetHighlightTimelines() @@ -112,12 +106,10 @@ namespace Timeline.Services return entities.Select(e => e.TimelineId).ToList(); } - public async Task RemoveHighlightTimeline(string timelineName, long? operatorId) + public async Task RemoveHighlightTimeline(long timelineId, long? operatorId) { - if (timelineName == null) - throw new ArgumentNullException(nameof(timelineName)); - - var timelineId = await _timelineService.GetTimelineIdByName(timelineName); + if (!await _timelineService.CheckExistence(timelineId)) + throw new TimelineNotExistException(timelineId); if (operatorId.HasValue && !await _userService.CheckUserExistence(operatorId.Value)) { @@ -142,12 +134,10 @@ namespace Timeline.Services return true; } - public async Task MoveHighlightTimeline(string timelineName, long newPosition) + public async Task MoveHighlightTimeline(long timelineId, long newPosition) { - if (timelineName == null) - throw new ArgumentNullException(nameof(timelineName)); - - var timelineId = await _timelineService.GetTimelineIdByName(timelineName); + if (!await _timelineService.CheckExistence(timelineId)) + throw new TimelineNotExistException(timelineId); var entity = await _database.HighlightTimelines.SingleOrDefaultAsync(t => t.TimelineId == timelineId); -- cgit v1.2.3