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. --- .../Controllers/BookmarkTimelineController.cs | 17 ++++--- .../Controllers/HighlightTimelineController.cs | 17 ++++--- .../Timeline/Services/BookmarkTimelineService.cs | 51 +++++++++------------ .../Timeline/Services/HighlightTimelineService.cs | 52 +++++++++------------- 4 files changed, 62 insertions(+), 75 deletions(-) diff --git a/BackEnd/Timeline/Controllers/BookmarkTimelineController.cs b/BackEnd/Timeline/Controllers/BookmarkTimelineController.cs index 64cb8afa..4313115e 100644 --- a/BackEnd/Timeline/Controllers/BookmarkTimelineController.cs +++ b/BackEnd/Timeline/Controllers/BookmarkTimelineController.cs @@ -52,12 +52,13 @@ namespace Timeline.Controllers [ProducesResponseType(200)] [ProducesResponseType(400)] [ProducesResponseType(401)] - public async Task Put([GeneralTimelineName] string timeline) + public async Task> Put([GeneralTimelineName] string timeline) { try { - await _service.AddBookmark(this.GetUserId(), timeline); - return Ok(); + var timelineId = await _timelineService.GetTimelineIdByName(timeline); + var create = await _service.AddBookmark(this.GetUserId(), timelineId); + return CommonPutResponse.Create(create); } catch (TimelineNotExistException) { @@ -74,12 +75,13 @@ namespace Timeline.Controllers [ProducesResponseType(200)] [ProducesResponseType(400)] [ProducesResponseType(401)] - public async Task Delete([GeneralTimelineName] string timeline) + public async Task> Delete([GeneralTimelineName] string timeline) { try { - await _service.RemoveBookmark(this.GetUserId(), timeline); - return Ok(); + var timelineId = await _timelineService.GetTimelineIdByName(timeline); + var delete = await _service.RemoveBookmark(this.GetUserId(), timelineId); + return CommonDeleteResponse.Create(delete); } catch (TimelineNotExistException) { @@ -100,7 +102,8 @@ namespace Timeline.Controllers { try { - await _service.MoveBookmark(this.GetUserId(), request.Timeline, request.NewPosition!.Value); + var timelineId = await _timelineService.GetTimelineIdByName(request.Timeline); + await _service.MoveBookmark(this.GetUserId(), timelineId, request.NewPosition!.Value); return Ok(); } catch (TimelineNotExistException) diff --git a/BackEnd/Timeline/Controllers/HighlightTimelineController.cs b/BackEnd/Timeline/Controllers/HighlightTimelineController.cs index 685ec16f..cc19cada 100644 --- a/BackEnd/Timeline/Controllers/HighlightTimelineController.cs +++ b/BackEnd/Timeline/Controllers/HighlightTimelineController.cs @@ -51,12 +51,13 @@ namespace Timeline.Controllers [ProducesResponseType(400)] [ProducesResponseType(401)] [ProducesResponseType(403)] - public async Task Put([GeneralTimelineName] string timeline) + public async Task> Put([GeneralTimelineName] string timeline) { try { - await _service.AddHighlightTimeline(timeline, this.GetUserId()); - return Ok(); + var timelineId = await _timelineService.GetTimelineIdByName(timeline); + var create = await _service.AddHighlightTimeline(timelineId, this.GetUserId()); + return CommonPutResponse.Create(create); } catch (TimelineNotExistException) { @@ -74,12 +75,13 @@ namespace Timeline.Controllers [ProducesResponseType(400)] [ProducesResponseType(401)] [ProducesResponseType(403)] - public async Task Delete([GeneralTimelineName] string timeline) + public async Task> Delete([GeneralTimelineName] string timeline) { try { - await _service.RemoveHighlightTimeline(timeline, this.GetUserId()); - return Ok(); + var timelineId = await _timelineService.GetTimelineIdByName(timeline); + var delete = await _service.RemoveHighlightTimeline(timelineId, this.GetUserId()); + return CommonDeleteResponse.Create(delete); } catch (TimelineNotExistException) { @@ -100,7 +102,8 @@ namespace Timeline.Controllers { try { - await _service.MoveHighlightTimeline(body.Timeline, body.NewPosition!.Value); + var timelineId = await _timelineService.GetTimelineIdByName(body.Timeline); + await _service.MoveHighlightTimeline(timelineId, body.NewPosition!.Value); return Ok(); } catch (TimelineNotExistException) 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