From b87abbb8ed0aa86a808b2f97e4d22b0ee1addd9f Mon Sep 17 00:00:00 2001 From: crupest Date: Thu, 29 Apr 2021 19:29:35 +0800 Subject: ... --- .../Services/Api/BookmarkTimelineService.cs | 79 ++-------------------- .../Services/Api/HighlightTimelineService.cs | 77 ++------------------- .../Services/Api/IBookmarkTimelineService.cs | 64 ++++++++++++++++++ .../Services/Api/IHighlightTimelineService.cs | 61 +++++++++++++++++ BackEnd/Timeline/Services/Api/ISearchService.cs | 33 +++++++++ .../Services/Api/InvalidBookmarkException.cs | 15 ++++ .../Api/InvalidHighlightTimelineException.cs | 15 ++++ BackEnd/Timeline/Services/Api/SearchResult.cs | 11 +++ BackEnd/Timeline/Services/Api/SearchResultItem.cs | 18 +++++ BackEnd/Timeline/Services/Api/SearchService.cs | 55 +-------------- BackEnd/Timeline/Services/Mapper/TimelineMapper.cs | 4 +- 11 files changed, 231 insertions(+), 201 deletions(-) create mode 100644 BackEnd/Timeline/Services/Api/IBookmarkTimelineService.cs create mode 100644 BackEnd/Timeline/Services/Api/IHighlightTimelineService.cs create mode 100644 BackEnd/Timeline/Services/Api/ISearchService.cs create mode 100644 BackEnd/Timeline/Services/Api/InvalidBookmarkException.cs create mode 100644 BackEnd/Timeline/Services/Api/InvalidHighlightTimelineException.cs create mode 100644 BackEnd/Timeline/Services/Api/SearchResult.cs create mode 100644 BackEnd/Timeline/Services/Api/SearchResultItem.cs (limited to 'BackEnd/Timeline/Services') diff --git a/BackEnd/Timeline/Services/Api/BookmarkTimelineService.cs b/BackEnd/Timeline/Services/Api/BookmarkTimelineService.cs index 37b55199..4fc20ecb 100644 --- a/BackEnd/Timeline/Services/Api/BookmarkTimelineService.cs +++ b/BackEnd/Timeline/Services/Api/BookmarkTimelineService.cs @@ -1,5 +1,4 @@ using Microsoft.EntityFrameworkCore; -using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; @@ -10,74 +9,6 @@ using Timeline.Services.User; namespace Timeline.Services.Api { - [Serializable] - public class InvalidBookmarkException : Exception - { - public InvalidBookmarkException() { } - public InvalidBookmarkException(string message) : base(message) { } - public InvalidBookmarkException(string message, Exception inner) : base(message, inner) { } - protected InvalidBookmarkException( - System.Runtime.Serialization.SerializationInfo info, - System.Runtime.Serialization.StreamingContext context) : base(info, context) { } - } - - /// - /// Service interface that manages timeline bookmarks. - /// - public interface IBookmarkTimelineService - { - /// - /// Get bookmarks of a user. - /// - /// User id of bookmark owner. - /// Id of Bookmark timelines in order. - /// 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. - /// - /// 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. - Task AddBookmark(long userId, long timelineId); - - /// - /// Remove a bookmark from a user. - /// - /// 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. - Task RemoveBookmark(long userId, long timelineId); - - /// - /// Move bookmark to a new position. - /// - /// 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 the timeline is not a bookmark. - Task MoveBookmark(long userId, long timelineId, long newPosition); - } - public class BookmarkTimelineService : IBookmarkTimelineService { private readonly DatabaseContext _database; @@ -91,7 +22,7 @@ namespace Timeline.Services.Api _timelineService = timelineService; } - public async Task AddBookmark(long userId, long timelineId) + public async Task AddBookmarkAsync(long userId, long timelineId) { if (!await _userService.CheckUserExistenceAsync(userId)) throw new UserNotExistException(userId); @@ -113,7 +44,7 @@ namespace Timeline.Services.Api return true; } - public async Task> GetBookmarks(long userId) + public async Task> GetBookmarksAsync(long userId) { if (!await _userService.CheckUserExistenceAsync(userId)) throw new UserNotExistException(userId); @@ -123,7 +54,7 @@ namespace Timeline.Services.Api return entities.Select(e => e.TimelineId).ToList(); } - public async Task IsBookmark(long userId, long timelineId, bool checkUserExistence = true, bool checkTimelineExistence = true) + public async Task IsBookmarkAsync(long userId, long timelineId, bool checkUserExistence = true, bool checkTimelineExistence = true) { if (checkUserExistence && !await _userService.CheckUserExistenceAsync(userId)) throw new UserNotExistException(userId); @@ -134,7 +65,7 @@ namespace Timeline.Services.Api return await _database.BookmarkTimelines.AnyAsync(b => b.TimelineId == timelineId && b.UserId == userId); } - public async Task MoveBookmark(long userId, long timelineId, long newPosition) + public async Task MoveBookmarkAsync(long userId, long timelineId, long newPosition) { if (!await _userService.CheckUserExistenceAsync(userId)) throw new UserNotExistException(userId); @@ -176,7 +107,7 @@ namespace Timeline.Services.Api await transaction.CommitAsync(); } - public async Task RemoveBookmark(long userId, long timelineId) + public async Task RemoveBookmarkAsync(long userId, long timelineId) { if (!await _userService.CheckUserExistenceAsync(userId)) throw new UserNotExistException(userId); diff --git a/BackEnd/Timeline/Services/Api/HighlightTimelineService.cs b/BackEnd/Timeline/Services/Api/HighlightTimelineService.cs index 8224f1fe..a9d831ab 100644 --- a/BackEnd/Timeline/Services/Api/HighlightTimelineService.cs +++ b/BackEnd/Timeline/Services/Api/HighlightTimelineService.cs @@ -1,5 +1,4 @@ using Microsoft.EntityFrameworkCore; -using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; @@ -9,72 +8,6 @@ using Timeline.Services.User; namespace Timeline.Services.Api { - - [Serializable] - public class InvalidHighlightTimelineException : Exception - { - public InvalidHighlightTimelineException() { } - public InvalidHighlightTimelineException(string message) : base(message) { } - public InvalidHighlightTimelineException(string message, Exception inner) : base(message, inner) { } - protected InvalidHighlightTimelineException( - System.Runtime.Serialization.SerializationInfo info, - System.Runtime.Serialization.StreamingContext context) : base(info, context) { } - } - - /// - /// Service that controls highlight timeline. - /// - public interface IHighlightTimelineService - { - /// - /// Get all highlight timelines in order. - /// - /// 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. - /// - /// 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. - Task AddHighlightTimeline(long timelineId, long? operatorId); - - /// - /// Remove a timeline from highlight list. - /// - /// 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. - Task RemoveHighlightTimeline(long timelineId, long? operatorId); - - /// - /// Move a highlight timeline to a new position. - /// - /// The timeline name. - /// The new position. Starts at 1. - /// 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(long timelineId, long newPosition); - } - public class HighlightTimelineService : IHighlightTimelineService { private readonly DatabaseContext _database; @@ -90,7 +23,7 @@ namespace Timeline.Services.Api _clock = clock; } - public async Task AddHighlightTimeline(long timelineId, long? operatorId) + public async Task AddHighlightTimelineAsync(long timelineId, long? operatorId) { if (!await _timelineService.CheckTimelineExistenceAsync(timelineId)) throw new TimelineNotExistException(timelineId); @@ -109,14 +42,14 @@ namespace Timeline.Services.Api return true; } - public async Task> GetHighlightTimelines() + public async Task> GetHighlightTimelinesAsync() { var entities = await _database.HighlightTimelines.OrderBy(t => t.Order).Select(t => new { t.TimelineId }).ToListAsync(); return entities.Select(e => e.TimelineId).ToList(); } - public async Task RemoveHighlightTimeline(long timelineId, long? operatorId) + public async Task RemoveHighlightTimelineAsync(long timelineId, long? operatorId) { if (!await _timelineService.CheckTimelineExistenceAsync(timelineId)) throw new TimelineNotExistException(timelineId); @@ -144,7 +77,7 @@ namespace Timeline.Services.Api return true; } - public async Task MoveHighlightTimeline(long timelineId, long newPosition) + public async Task MoveHighlightTimelineAsync(long timelineId, long newPosition) { if (!await _timelineService.CheckTimelineExistenceAsync(timelineId)) throw new TimelineNotExistException(timelineId); @@ -183,7 +116,7 @@ namespace Timeline.Services.Api await transaction.CommitAsync(); } - public async Task IsHighlightTimeline(long timelineId, bool checkTimelineExistence = true) + public async Task IsHighlightTimelineAsync(long timelineId, bool checkTimelineExistence = true) { if (checkTimelineExistence && !await _timelineService.CheckTimelineExistenceAsync(timelineId)) throw new TimelineNotExistException(timelineId); diff --git a/BackEnd/Timeline/Services/Api/IBookmarkTimelineService.cs b/BackEnd/Timeline/Services/Api/IBookmarkTimelineService.cs new file mode 100644 index 00000000..18feee54 --- /dev/null +++ b/BackEnd/Timeline/Services/Api/IBookmarkTimelineService.cs @@ -0,0 +1,64 @@ +using System.Collections.Generic; +using System.Threading.Tasks; +using Timeline.Services.Timeline; +using Timeline.Services.User; + +namespace Timeline.Services.Api +{ + /// + /// Service interface that manages timeline bookmarks. + /// + public interface IBookmarkTimelineService + { + /// + /// Get bookmarks of a user. + /// + /// User id of bookmark owner. + /// Id of Bookmark timelines in order. + /// Thrown when user does not exist. + Task> GetBookmarksAsync(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 IsBookmarkAsync(long userId, long timelineId, bool checkUserExistence = true, bool checkTimelineExistence = true); + + /// + /// Add a bookmark to tail to a user. + /// + /// 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. + Task AddBookmarkAsync(long userId, long timelineId); + + /// + /// Remove a bookmark from a user. + /// + /// 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. + Task RemoveBookmarkAsync(long userId, long timelineId); + + /// + /// Move bookmark to a new position. + /// + /// 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 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 new file mode 100644 index 00000000..4f14d19b --- /dev/null +++ b/BackEnd/Timeline/Services/Api/IHighlightTimelineService.cs @@ -0,0 +1,61 @@ +using System.Collections.Generic; +using System.Threading.Tasks; +using Timeline.Services.Timeline; +using Timeline.Services.User; + +namespace Timeline.Services.Api +{ + /// + /// Service that controls highlight timeline. + /// + public interface IHighlightTimelineService + { + /// + /// Get all highlight timelines in order. + /// + /// Id list of all highlight timelines. + Task> GetHighlightTimelinesAsync(); + + /// + /// 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 IsHighlightTimelineAsync(long timelineId, bool checkTimelineExistence = true); + + /// + /// Add a timeline to highlight list. + /// + /// 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. + Task AddHighlightTimelineAsync(long timelineId, long? operatorId); + + /// + /// Remove a timeline from highlight list. + /// + /// 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. + Task RemoveHighlightTimelineAsync(long timelineId, long? operatorId); + + /// + /// Move a highlight timeline to a new position. + /// + /// The timeline name. + /// The new position. Starts at 1. + /// 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 MoveHighlightTimelineAsync(long timelineId, long newPosition); + } +} diff --git a/BackEnd/Timeline/Services/Api/ISearchService.cs b/BackEnd/Timeline/Services/Api/ISearchService.cs new file mode 100644 index 00000000..d8b4bb44 --- /dev/null +++ b/BackEnd/Timeline/Services/Api/ISearchService.cs @@ -0,0 +1,33 @@ +using System; +using System.Threading.Tasks; +using Timeline.Entities; + +namespace Timeline.Services.Api +{ + public interface ISearchService + { + /// + /// Search timelines whose name or title contains query string. + /// + /// String to contain. + /// Search results. + /// Thrown when is null. + /// Thrown when is empty. + /// + /// Implementation should promise high score is at first. + /// + Task> SearchTimelineAsync(string query); + + /// + /// Search users whose username or nickname contains query string. + /// + /// String to contain. + /// Search results. + /// Thrown when is null. + /// Thrown when is empty. + /// + /// Implementation should promise high score is at first. + /// + Task> SearchUserAsync(string query); + } +} diff --git a/BackEnd/Timeline/Services/Api/InvalidBookmarkException.cs b/BackEnd/Timeline/Services/Api/InvalidBookmarkException.cs new file mode 100644 index 00000000..39572b38 --- /dev/null +++ b/BackEnd/Timeline/Services/Api/InvalidBookmarkException.cs @@ -0,0 +1,15 @@ +using System; + +namespace Timeline.Services.Api +{ + [Serializable] + public class InvalidBookmarkException : Exception + { + public InvalidBookmarkException() { } + public InvalidBookmarkException(string message) : base(message) { } + public InvalidBookmarkException(string message, Exception inner) : base(message, inner) { } + protected InvalidBookmarkException( + System.Runtime.Serialization.SerializationInfo info, + System.Runtime.Serialization.StreamingContext context) : base(info, context) { } + } +} diff --git a/BackEnd/Timeline/Services/Api/InvalidHighlightTimelineException.cs b/BackEnd/Timeline/Services/Api/InvalidHighlightTimelineException.cs new file mode 100644 index 00000000..13b04a6b --- /dev/null +++ b/BackEnd/Timeline/Services/Api/InvalidHighlightTimelineException.cs @@ -0,0 +1,15 @@ +using System; + +namespace Timeline.Services.Api +{ + [Serializable] + public class InvalidHighlightTimelineException : Exception + { + public InvalidHighlightTimelineException() { } + public InvalidHighlightTimelineException(string message) : base(message) { } + public InvalidHighlightTimelineException(string message, Exception inner) : base(message, inner) { } + protected InvalidHighlightTimelineException( + System.Runtime.Serialization.SerializationInfo info, + System.Runtime.Serialization.StreamingContext context) : base(info, context) { } + } +} diff --git a/BackEnd/Timeline/Services/Api/SearchResult.cs b/BackEnd/Timeline/Services/Api/SearchResult.cs new file mode 100644 index 00000000..7c95ae5d --- /dev/null +++ b/BackEnd/Timeline/Services/Api/SearchResult.cs @@ -0,0 +1,11 @@ +using System.Collections.Generic; + +namespace Timeline.Services.Api +{ + public class SearchResult + { +#pragma warning disable CA2227 // Collection properties should be read only + public List> Items { get; set; } = new(); +#pragma warning restore CA2227 // Collection properties should be read only + } +} diff --git a/BackEnd/Timeline/Services/Api/SearchResultItem.cs b/BackEnd/Timeline/Services/Api/SearchResultItem.cs new file mode 100644 index 00000000..ac40281f --- /dev/null +++ b/BackEnd/Timeline/Services/Api/SearchResultItem.cs @@ -0,0 +1,18 @@ +namespace Timeline.Services.Api +{ + public class SearchResultItem + { + public SearchResultItem(TItem item, int score) + { + Item = item; + Score = score; + } + + public TItem Item { get; set; } = default!; + + /// + /// Bigger is better. + /// + public int Score { get; set; } + } +} diff --git a/BackEnd/Timeline/Services/Api/SearchService.cs b/BackEnd/Timeline/Services/Api/SearchService.cs index eec5001f..037f0490 100644 --- a/BackEnd/Timeline/Services/Api/SearchService.cs +++ b/BackEnd/Timeline/Services/Api/SearchService.cs @@ -1,62 +1,11 @@ using Microsoft.EntityFrameworkCore; using System; -using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Timeline.Entities; namespace Timeline.Services.Api { - public class SearchResultItem - { - public SearchResultItem(TItem item, int score) - { - Item = item; - Score = score; - } - - public TItem Item { get; set; } = default!; - - /// - /// Bigger is better. - /// - public int Score { get; set; } - } - - public class SearchResult - { -#pragma warning disable CA2227 // Collection properties should be read only - public List> Items { get; set; } = new(); -#pragma warning restore CA2227 // Collection properties should be read only - } - - public interface ISearchService - { - /// - /// Search timelines whose name or title contains query string. - /// - /// String to contain. - /// Search results. - /// Thrown when is null. - /// Thrown when is empty. - /// - /// Implementation should promise high score is at first. - /// - Task> SearchTimeline(string query); - - /// - /// Search users whose username or nickname contains query string. - /// - /// String to contain. - /// Search results. - /// Thrown when is null. - /// Thrown when is empty. - /// - /// Implementation should promise high score is at first. - /// - Task> SearchUser(string query); - } - public class SearchService : ISearchService { private readonly DatabaseContext _database; @@ -66,7 +15,7 @@ namespace Timeline.Services.Api _database = database; } - public async Task> SearchTimeline(string query) + public async Task> SearchTimelineAsync(string query) { if (query is null) throw new ArgumentNullException(nameof(query)); @@ -83,7 +32,7 @@ namespace Timeline.Services.Api return searchResult; } - public async Task> SearchUser(string query) + public async Task> SearchUserAsync(string query) { if (query is null) throw new ArgumentNullException(nameof(query)); diff --git a/BackEnd/Timeline/Services/Mapper/TimelineMapper.cs b/BackEnd/Timeline/Services/Mapper/TimelineMapper.cs index 5ee90a8f..d3159423 100644 --- a/BackEnd/Timeline/Services/Mapper/TimelineMapper.cs +++ b/BackEnd/Timeline/Services/Mapper/TimelineMapper.cs @@ -85,8 +85,8 @@ namespace Timeline.Services.Mapper color: entity.Color, 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), + isHighlight: await _highlightTimelineService.IsHighlightTimelineAsync(entity.Id), + isBookmark: userId is not null && await _bookmarkTimelineService.IsBookmarkAsync(userId.Value, entity.Id, false, false), manageable: manageable, postable: postable, links: new HttpTimelineLinks( -- cgit v1.2.3