using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Timeline.Entities; using Timeline.Models; 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 IBasicUserService _userService; private readonly ITimelineService _timelineService; public HighlightTimelineService(DatabaseContext database, IBasicUserService 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; } } }