From 60cf54d80e62ea9180660e5a951c569f90bc4636 Mon Sep 17 00:00:00 2001 From: crupest Date: Thu, 26 Nov 2020 20:02:03 +0800 Subject: feat: Add highlight timeline entity and service. --- .../Timeline/Services/HighlightTimelineService.cs | 112 +++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 BackEnd/Timeline/Services/HighlightTimelineService.cs (limited to 'BackEnd/Timeline/Services/HighlightTimelineService.cs') diff --git a/BackEnd/Timeline/Services/HighlightTimelineService.cs b/BackEnd/Timeline/Services/HighlightTimelineService.cs new file mode 100644 index 00000000..7528d9b0 --- /dev/null +++ b/BackEnd/Timeline/Services/HighlightTimelineService.cs @@ -0,0 +1,112 @@ +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Timeline.Entities; +using Timeline.Services.Exceptions; + +namespace Timeline.Services +{ + public interface IHighlightTimelineService + { + /// + /// Get all highlight timelines. + /// + /// A list of all highlight timelines. + Task> GetHighlightTimelines(); + + /// + /// Add a timeline to highlight list. + /// + /// The timeline name. + /// 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. + /// Thrown when user with given operator id does not exist. + Task AddHighlightTimeline(string timelineName, long? operatorId); + + /// + /// Remove a timeline from highlight list. + /// + /// The timeline name. + /// 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 user with given operator id does not exist. + Task RemoveHighlightTimeline(string timelineName, long? operatorId); + } + + public class HighlightTimelineService : IHighlightTimelineService + { + private readonly DatabaseContext _database; + private readonly IUserService _userService; + private readonly ITimelineService _timelineService; + + public HighlightTimelineService(DatabaseContext database, IUserService userService, ITimelineService timelineService) + { + _database = database; + _userService = userService; + _timelineService = timelineService; + } + + public async Task AddHighlightTimeline(string timelineName, long? operatorId) + { + if (timelineName == null) + throw new ArgumentNullException(nameof(timelineName)); + + var timelineId = await _timelineService.GetTimelineIdByName(timelineName); + + if (operatorId.HasValue && !await _userService.CheckUserExistence(operatorId.Value)) + { + throw new UserNotExistException(null, operatorId.Value, "User with given operator id does not exist.", null); + } + + var alreadyIs = await _database.HighlightTimelines.AnyAsync(t => t.TimelineId == timelineId); + + if (alreadyIs) return; + + _database.HighlightTimelines.Add(new HighlightTimelineEntity { TimelineId = timelineId, OperatorId = operatorId }); + await _database.SaveChangesAsync(); + } + + public async Task> GetHighlightTimelines() + { + var entities = await _database.HighlightTimelines.Select(t => new { t.Id }).ToListAsync(); + + var result = new List(); + + foreach (var entity in entities) + { + result.Add(await _timelineService.GetTimelineById(entity.Id)); + } + + return result; + } + + public async Task RemoveHighlightTimeline(string timelineName, long? operatorId) + { + if (timelineName == null) + throw new ArgumentNullException(nameof(timelineName)); + + var timelineId = await _timelineService.GetTimelineIdByName(timelineName); + + if (operatorId.HasValue && !await _userService.CheckUserExistence(operatorId.Value)) + { + throw new UserNotExistException(null, operatorId.Value, "User with given operator id does not exist.", null); + } + + var entity = await _database.HighlightTimelines.SingleOrDefaultAsync(t => t.TimelineId == timelineId); + + if (entity == null) return false; + + _database.HighlightTimelines.Remove(entity); + await _database.SaveChangesAsync(); + + return true; + } + } +} -- cgit v1.2.3