From 191b92a161c4fdad532dbf471f5c33f8f4a97a23 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 +++++++++------------- 1 file changed, 21 insertions(+), 30 deletions(-) (limited to 'BackEnd/Timeline/Services/BookmarkTimelineService.cs') 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); -- cgit v1.2.3