aboutsummaryrefslogtreecommitdiff
path: root/BackEnd/Timeline/Services/Api/BookmarkTimelineService.cs
diff options
context:
space:
mode:
Diffstat (limited to 'BackEnd/Timeline/Services/Api/BookmarkTimelineService.cs')
-rw-r--r--BackEnd/Timeline/Services/Api/BookmarkTimelineService.cs111
1 files changed, 16 insertions, 95 deletions
diff --git a/BackEnd/Timeline/Services/Api/BookmarkTimelineService.cs b/BackEnd/Timeline/Services/Api/BookmarkTimelineService.cs
index 37b55199..de70a9db 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) { }
- }
-
- /// <summary>
- /// Service interface that manages timeline bookmarks.
- /// </summary>
- public interface IBookmarkTimelineService
- {
- /// <summary>
- /// Get bookmarks of a user.
- /// </summary>
- /// <param name="userId">User id of bookmark owner.</param>
- /// <returns>Id of Bookmark timelines in order.</returns>
- /// <exception cref="UserNotExistException">Thrown when user does not exist.</exception>
- Task<List<long>> GetBookmarks(long userId);
-
- /// <summary>
- /// Check if a timeline is a bookmark.
- /// </summary>
- /// <param name="userId">The user id.</param>
- /// <param name="timelineId">Timeline id.</param>
- /// <param name="checkUserExistence">If true it will throw when user does not exist.</param>
- /// <param name="checkTimelineExistence">If true it will throw when timeline does not exist.</param>
- /// <returns>True if timeline is a bookmark. Otherwise false.</returns>
- /// <exception cref="UserNotExistException">Throw if user does not exist and <paramref name="checkUserExistence"/> is true.</exception>
- /// <exception cref="TimelineNotExistException">Thrown if timeline does not exist and <paramref name="checkTimelineExistence"/> is true.</exception>
- Task<bool> IsBookmark(long userId, long timelineId, bool checkUserExistence = true, bool checkTimelineExistence = true);
-
- /// <summary>
- /// Add a bookmark to tail to a user.
- /// </summary>
- /// <param name="userId">User id of bookmark owner.</param>
- /// <param name="timelineId">Timeline id.</param>
- /// <returns>True if timeline is added to bookmark. False if it already is.</returns>
- /// <exception cref="UserNotExistException">Thrown when user does not exist.</exception>
- /// <exception cref="TimelineNotExistException">Thrown when timeline does not exist.</exception>
- Task<bool> AddBookmark(long userId, long timelineId);
-
- /// <summary>
- /// Remove a bookmark from a user.
- /// </summary>
- /// <param name="userId">User id of bookmark owner.</param>
- /// <param name="timelineId">Timeline id.</param>
- /// <returns>True if deletion is performed. False if bookmark does not exist.</returns>
- /// <exception cref="UserNotExistException">Thrown when user does not exist.</exception>
- /// <exception cref="TimelineNotExistException">Thrown when timeline does not exist.</exception>
- Task<bool> RemoveBookmark(long userId, long timelineId);
-
- /// <summary>
- /// Move bookmark to a new position.
- /// </summary>
- /// <param name="userId">User id of bookmark owner.</param>
- /// <param name="timelineId">Timeline name.</param>
- /// <param name="newPosition">New position. Starts at 1.</param>
- /// <exception cref="UserNotExistException">Thrown when user does not exist.</exception>
- /// <exception cref="TimelineNotExistException">Thrown when timeline does not exist.</exception>
- /// <exception cref="InvalidBookmarkException">Thrown when the timeline is not a bookmark.</exception>
- Task MoveBookmark(long userId, long timelineId, long newPosition);
- }
-
public class BookmarkTimelineService : IBookmarkTimelineService
{
private readonly DatabaseContext _database;
@@ -91,13 +22,10 @@ namespace Timeline.Services.Api
_timelineService = timelineService;
}
- public async Task<bool> AddBookmark(long userId, long timelineId)
+ public async Task<bool> 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;
@@ -113,34 +41,30 @@ namespace Timeline.Services.Api
return true;
}
- public async Task<List<long>> GetBookmarks(long userId)
+ public async Task<List<long>> 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();
return entities.Select(e => e.TimelineId).ToList();
}
- public async Task<bool> IsBookmark(long userId, long timelineId, bool checkUserExistence = true, bool checkTimelineExistence = true)
+ public async Task<bool> 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 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);
-
- 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);
@@ -176,13 +100,10 @@ namespace Timeline.Services.Api
await transaction.CommitAsync();
}
- public async Task<bool> RemoveBookmark(long userId, long timelineId)
+ public async Task<bool> 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);