From a672e10faad434899d81ef9d0d0d5adbbc7841da Mon Sep 17 00:00:00 2001 From: crupest Date: Fri, 30 Apr 2021 16:52:55 +0800 Subject: refactor: ... --- .../Services/Api/BookmarkTimelineService.cs | 32 ++++++++-------------- .../Services/Api/HighlightTimelineService.cs | 21 ++++++-------- .../Services/Api/IBookmarkTimelineService.cs | 18 ++++++------ .../Services/Api/IHighlightTimelineService.cs | 12 ++++---- 4 files changed, 35 insertions(+), 48 deletions(-) (limited to 'BackEnd/Timeline/Services/Api') diff --git a/BackEnd/Timeline/Services/Api/BookmarkTimelineService.cs b/BackEnd/Timeline/Services/Api/BookmarkTimelineService.cs index 4fc20ecb..de70a9db 100644 --- a/BackEnd/Timeline/Services/Api/BookmarkTimelineService.cs +++ b/BackEnd/Timeline/Services/Api/BookmarkTimelineService.cs @@ -24,11 +24,8 @@ namespace Timeline.Services.Api public async Task AddBookmarkAsync(long userId, long timelineId) { - if (!await _userService.CheckUserExistenceAsync(userId)) - throw new UserNotExistException(userId); - - if (!await _timelineService.CheckTimelineExistenceAsync(timelineId)) - throw new TimelineNotExistException(timelineId); + await _userService.ThrowIfUserNotExist(userId); + await _timelineService.ThrowIfTimelineNotExist(timelineId); if (await _database.BookmarkTimelines.AnyAsync(t => t.TimelineId == timelineId && t.UserId == userId)) return false; @@ -46,8 +43,7 @@ namespace Timeline.Services.Api public async Task> GetBookmarksAsync(long userId) { - if (!await _userService.CheckUserExistenceAsync(userId)) - throw new UserNotExistException(userId); + await _userService.ThrowIfUserNotExist(userId); var entities = await _database.BookmarkTimelines.Where(t => t.UserId == userId).OrderBy(t => t.Rank).Select(t => new { t.TimelineId }).ToListAsync(); @@ -56,22 +52,19 @@ namespace Timeline.Services.Api public async Task IsBookmarkAsync(long userId, long timelineId, bool checkUserExistence = true, bool checkTimelineExistence = true) { - if (checkUserExistence && !await _userService.CheckUserExistenceAsync(userId)) - throw new UserNotExistException(userId); + if (checkUserExistence) + await _userService.ThrowIfUserNotExist(userId); - if (checkTimelineExistence && !await _timelineService.CheckTimelineExistenceAsync(timelineId)) - throw new TimelineNotExistException(timelineId); + if (checkTimelineExistence) + await _timelineService.ThrowIfTimelineNotExist(timelineId); return await _database.BookmarkTimelines.AnyAsync(b => b.TimelineId == timelineId && b.UserId == userId); } public async Task MoveBookmarkAsync(long userId, long timelineId, long newPosition) { - if (!await _userService.CheckUserExistenceAsync(userId)) - throw new UserNotExistException(userId); - - if (!await _timelineService.CheckTimelineExistenceAsync(timelineId)) - throw new TimelineNotExistException(timelineId); + await _userService.ThrowIfUserNotExist(userId); + await _timelineService.ThrowIfTimelineNotExist(timelineId); var entity = await _database.BookmarkTimelines.SingleOrDefaultAsync(t => t.TimelineId == timelineId && t.UserId == userId); @@ -109,11 +102,8 @@ namespace Timeline.Services.Api public async Task RemoveBookmarkAsync(long userId, long timelineId) { - if (!await _userService.CheckUserExistenceAsync(userId)) - throw new UserNotExistException(userId); - - if (!await _timelineService.CheckTimelineExistenceAsync(timelineId)) - throw new TimelineNotExistException(timelineId); + await _userService.ThrowIfUserNotExist(userId); + await _timelineService.ThrowIfTimelineNotExist(timelineId); var entity = await _database.BookmarkTimelines.SingleOrDefaultAsync(t => t.UserId == userId && t.TimelineId == timelineId); diff --git a/BackEnd/Timeline/Services/Api/HighlightTimelineService.cs b/BackEnd/Timeline/Services/Api/HighlightTimelineService.cs index a9d831ab..d4367e57 100644 --- a/BackEnd/Timeline/Services/Api/HighlightTimelineService.cs +++ b/BackEnd/Timeline/Services/Api/HighlightTimelineService.cs @@ -25,12 +25,11 @@ namespace Timeline.Services.Api public async Task AddHighlightTimelineAsync(long timelineId, long? operatorId) { - if (!await _timelineService.CheckTimelineExistenceAsync(timelineId)) - throw new TimelineNotExistException(timelineId); + await _timelineService.ThrowIfTimelineNotExist(timelineId); - if (operatorId.HasValue && !await _userService.CheckUserExistenceAsync(operatorId.Value)) + if (operatorId.HasValue) { - throw new UserNotExistException(null, operatorId.Value, "User with given operator id does not exist.", null); + await _userService.ThrowIfUserNotExist(operatorId.Value); } var alreadyIs = await _database.HighlightTimelines.AnyAsync(t => t.TimelineId == timelineId); @@ -51,12 +50,11 @@ namespace Timeline.Services.Api public async Task RemoveHighlightTimelineAsync(long timelineId, long? operatorId) { - if (!await _timelineService.CheckTimelineExistenceAsync(timelineId)) - throw new TimelineNotExistException(timelineId); + await _timelineService.ThrowIfTimelineNotExist(timelineId); - if (operatorId.HasValue && !await _userService.CheckUserExistenceAsync(operatorId.Value)) + if (operatorId.HasValue) { - throw new UserNotExistException(null, operatorId.Value, "User with given operator id does not exist.", null); + await _userService.ThrowIfUserNotExist(operatorId.Value); } var entity = await _database.HighlightTimelines.SingleOrDefaultAsync(t => t.TimelineId == timelineId); @@ -79,8 +77,7 @@ namespace Timeline.Services.Api public async Task MoveHighlightTimelineAsync(long timelineId, long newPosition) { - if (!await _timelineService.CheckTimelineExistenceAsync(timelineId)) - throw new TimelineNotExistException(timelineId); + await _timelineService.ThrowIfTimelineNotExist(timelineId); var entity = await _database.HighlightTimelines.SingleOrDefaultAsync(t => t.TimelineId == timelineId); @@ -118,8 +115,8 @@ namespace Timeline.Services.Api public async Task IsHighlightTimelineAsync(long timelineId, bool checkTimelineExistence = true) { - if (checkTimelineExistence && !await _timelineService.CheckTimelineExistenceAsync(timelineId)) - throw new TimelineNotExistException(timelineId); + if (checkTimelineExistence) + await _timelineService.ThrowIfTimelineNotExist(timelineId); return await _database.HighlightTimelines.AnyAsync(t => t.TimelineId == timelineId); } diff --git a/BackEnd/Timeline/Services/Api/IBookmarkTimelineService.cs b/BackEnd/Timeline/Services/Api/IBookmarkTimelineService.cs index 18feee54..63b4affa 100644 --- a/BackEnd/Timeline/Services/Api/IBookmarkTimelineService.cs +++ b/BackEnd/Timeline/Services/Api/IBookmarkTimelineService.cs @@ -15,7 +15,7 @@ namespace Timeline.Services.Api /// /// User id of bookmark owner. /// Id of Bookmark timelines in order. - /// Thrown when user does not exist. + /// Thrown when user does not exist. Task> GetBookmarksAsync(long userId); /// @@ -26,8 +26,8 @@ namespace Timeline.Services.Api /// 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. + /// Throw if user does not exist and is true. + /// Thrown if timeline does not exist and is true. Task IsBookmarkAsync(long userId, long timelineId, bool checkUserExistence = true, bool checkTimelineExistence = true); /// @@ -36,8 +36,8 @@ namespace Timeline.Services.Api /// User id of bookmark owner. /// 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. + /// Thrown when user does not exist. + /// Thrown when timeline does not exist. Task AddBookmarkAsync(long userId, long timelineId); /// @@ -46,8 +46,8 @@ namespace Timeline.Services.Api /// User id of bookmark owner. /// Timeline id. /// True if deletion is performed. False if bookmark does not exist. - /// Thrown when user does not exist. - /// Thrown when timeline does not exist. + /// Thrown when user does not exist. + /// Thrown when timeline does not exist. Task RemoveBookmarkAsync(long userId, long timelineId); /// @@ -56,8 +56,8 @@ namespace Timeline.Services.Api /// User id of bookmark owner. /// Timeline name. /// New position. Starts at 1. - /// Thrown when user does not exist. - /// Thrown when timeline does not exist. + /// Thrown when user does not exist. + /// Thrown when timeline does not exist. /// Thrown when the timeline is not a bookmark. Task MoveBookmarkAsync(long userId, long timelineId, long newPosition); } diff --git a/BackEnd/Timeline/Services/Api/IHighlightTimelineService.cs b/BackEnd/Timeline/Services/Api/IHighlightTimelineService.cs index 4f14d19b..2e488778 100644 --- a/BackEnd/Timeline/Services/Api/IHighlightTimelineService.cs +++ b/BackEnd/Timeline/Services/Api/IHighlightTimelineService.cs @@ -22,7 +22,7 @@ namespace Timeline.Services.Api /// 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. + /// Thrown when timeline does not exist and is true. Task IsHighlightTimelineAsync(long timelineId, bool checkTimelineExistence = true); /// @@ -31,8 +31,8 @@ namespace Timeline.Services.Api /// The timeline id. /// The user id of operator. /// 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. + /// Thrown when timeline with given id does not exist. + /// Thrown when user with given operator id does not exist. Task AddHighlightTimelineAsync(long timelineId, long? operatorId); /// @@ -41,8 +41,8 @@ namespace Timeline.Services.Api /// The timeline id. /// The user id of operator. /// True if deletion is actually performed. Otherwise false (timeline was not in the list). - /// Thrown when timeline with given id does not exist. - /// Thrown when user with given operator id does not exist. + /// Thrown when timeline with given id does not exist. + /// Thrown when user with given operator id does not exist. Task RemoveHighlightTimelineAsync(long timelineId, long? operatorId); /// @@ -50,7 +50,7 @@ namespace Timeline.Services.Api /// /// The timeline name. /// The new position. Starts at 1. - /// Thrown when timeline with given id 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. -- cgit v1.2.3