aboutsummaryrefslogtreecommitdiff
path: root/BackEnd/Timeline
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2021-01-07 20:36:57 +0800
committercrupest <crupest@outlook.com>2021-01-07 20:36:57 +0800
commit191b92a161c4fdad532dbf471f5c33f8f4a97a23 (patch)
treeed5b70602431fefbddb4b33781e778fde303b1b5 /BackEnd/Timeline
parent2ec9433d0d9547383a7431e97c0577ffcc98ea97 (diff)
downloadtimeline-191b92a161c4fdad532dbf471f5c33f8f4a97a23.tar.gz
timeline-191b92a161c4fdad532dbf471f5c33f8f4a97a23.tar.bz2
timeline-191b92a161c4fdad532dbf471f5c33f8f4a97a23.zip
refactor: Highlight and bookmark service now use timeline id.
Diffstat (limited to 'BackEnd/Timeline')
-rw-r--r--BackEnd/Timeline/Controllers/BookmarkTimelineController.cs17
-rw-r--r--BackEnd/Timeline/Controllers/HighlightTimelineController.cs17
-rw-r--r--BackEnd/Timeline/Services/BookmarkTimelineService.cs51
-rw-r--r--BackEnd/Timeline/Services/HighlightTimelineService.cs52
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<ActionResult> Put([GeneralTimelineName] string timeline)
+ public async Task<ActionResult<CommonPutResponse>> 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<ActionResult> Delete([GeneralTimelineName] string timeline)
+ public async Task<ActionResult<CommonDeleteResponse>> 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<ActionResult> Put([GeneralTimelineName] string timeline)
+ public async Task<ActionResult<CommonPutResponse>> 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<ActionResult> Delete([GeneralTimelineName] string timeline)
+ public async Task<ActionResult<CommonDeleteResponse>> 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.
/// </summary>
/// <param name="userId">User id of bookmark owner.</param>
- /// <param name="timelineName">Timeline name.</param>
- /// <exception cref="ArgumentNullException">Thrown when <paramref name="timelineName"/> is null.</exception>
- /// <exception cref="ArgumentException">Thrown when <paramref name="timelineName"/> is not a valid name.</exception>
+ /// <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 AddBookmark(long userId, string timelineName);
+ 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="timelineName">Timeline name.</param>
+ /// <param name="timelineId">Timeline id.</param>
/// <returns>True if deletion is performed. False if bookmark does not exist.</returns>
- /// <exception cref="ArgumentNullException">Thrown when <paramref name="timelineName"/> is null.</exception>
- /// <exception cref="ArgumentException">Thrown when <paramref name="timelineName"/> is not a valid name.</exception>
/// <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, string timelineName);
+ 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="timelineName">Timeline name.</param>
+ /// <param name="timelineId">Timeline name.</param>
/// <param name="newPosition">New position. Starts at 1.</param>
- /// <exception cref="ArgumentNullException">Thrown when <paramref name="timelineName"/> is null.</exception>
- /// <exception cref="ArgumentException">Thrown when <paramref name="timelineName"/> is not a valid name.</exception>
/// <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, 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<bool> 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<List<long>> 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<bool> RemoveBookmark(long userId, string timelineName)
+ public async Task<bool> 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
/// <summary>
/// Add a timeline to highlight list.
/// </summary>
- /// <param name="timelineName">The timeline name.</param>
+ /// <param name="timelineId">The timeline id.</param>
/// <param name="operatorId">The user id of operator.</param>
- /// <exception cref="ArgumentNullException">Thrown when <paramref name="timelineName"/> is null.</exception>
- /// <exception cref="ArgumentException">Thrown when <paramref name="timelineName"/> is not a valid timeline name.</exception>
- /// <exception cref="TimelineNotExistException">Thrown when timeline with given name does not exist.</exception>
+ /// <returns>True if timeline is actually added to highligh. False if it already is.</returns>
+ /// <exception cref="TimelineNotExistException">Thrown when timeline with given id does not exist.</exception>
/// <exception cref="UserNotExistException">Thrown when user with given operator id does not exist.</exception>
- Task AddHighlightTimeline(string timelineName, long? operatorId);
+ Task<bool> AddHighlightTimeline(long timelineId, long? operatorId);
/// <summary>
/// Remove a timeline from highlight list.
/// </summary>
- /// <param name="timelineName">The timeline name.</param>
+ /// <param name="timelineId">The timeline id.</param>
/// <param name="operatorId">The user id of operator.</param>
/// <returns>True if deletion is actually performed. Otherwise false (timeline was not in the list).</returns>
- /// <exception cref="ArgumentNullException">Thrown when <paramref name="timelineName"/> is null.</exception>
- /// <exception cref="ArgumentException">Thrown when <paramref name="timelineName"/> is not a valid timeline name.</exception>
- /// <exception cref="TimelineNotExistException">Thrown when timeline with given name does not exist.</exception>
+ /// <exception cref="TimelineNotExistException">Thrown when timeline with given id does not exist.</exception>
/// <exception cref="UserNotExistException">Thrown when user with given operator id does not exist.</exception>
- Task<bool> RemoveHighlightTimeline(string timelineName, long? operatorId);
+ Task<bool> RemoveHighlightTimeline(long timelineId, long? operatorId);
/// <summary>
/// Move a highlight timeline to a new position.
/// </summary>
- /// <param name="timelineName">The timeline name.</param>
+ /// <param name="timelineId">The timeline name.</param>
/// <param name="newPosition">The new position. Starts at 1.</param>
- /// <exception cref="ArgumentNullException">Thrown when <paramref name="timelineName"/> is null.</exception>
- /// <exception cref="ArgumentException">Thrown when <paramref name="timelineName"/> is not a valid timeline name.</exception>
- /// <exception cref="TimelineNotExistException">Thrown when timeline with given name does not exist.</exception>
+ /// <exception cref="TimelineNotExistException">Thrown when timeline with given id does not exist.</exception>
/// <exception cref="InvalidHighlightTimelineException">Thrown when given timeline is not a highlight timeline.</exception>
/// <remarks>
/// If <paramref name="newPosition"/> is smaller than 1. Then move the timeline to head.
/// If <paramref name="newPosition"/> is bigger than total count. Then move the timeline to tail.
/// </remarks>
- 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<bool> 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<List<long>> GetHighlightTimelines()
@@ -112,12 +106,10 @@ namespace Timeline.Services
return entities.Select(e => e.TimelineId).ToList();
}
- public async Task<bool> RemoveHighlightTimeline(string timelineName, long? operatorId)
+ public async Task<bool> 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);