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
{
[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.
///
/// 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);
///
/// Move a highlight timeline to a new position.
///
/// The timeline name.
/// The new position. Starts at 1.
/// Thrown when is null.
/// Thrown when is not a valid timeline name.
/// Thrown when timeline with given name 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(string timelineName, long newPosition);
}
public class HighlightTimelineService : IHighlightTimelineService
{
private readonly DatabaseContext _database;
private readonly IBasicUserService _userService;
private readonly ITimelineService _timelineService;
private readonly IClock _clock;
public HighlightTimelineService(DatabaseContext database, IBasicUserService userService, ITimelineService timelineService, IClock clock)
{
_database = database;
_userService = userService;
_timelineService = timelineService;
_clock = clock;
}
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, AddTime = _clock.GetCurrentTime(), Order = await _database.HighlightTimelines.CountAsync() + 1 });
await _database.SaveChangesAsync();
}
public async Task> GetHighlightTimelines()
{
var entities = await _database.HighlightTimelines.OrderBy(t => t.Order).Select(t => new { t.TimelineId }).ToListAsync();
var result = new List();
foreach (var entity in entities)
{
result.Add(await _timelineService.GetTimelineById(entity.TimelineId));
}
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;
await using var transaction = await _database.Database.BeginTransactionAsync();
var order = entity.Order;
_database.HighlightTimelines.Remove(entity);
await _database.SaveChangesAsync();
await _database.Database.ExecuteSqlRawAsync("UPDATE highlight_timelines SET `order` = `order` - 1 WHERE `order` > {0}", order);
await transaction.CommitAsync();
return true;
}
public async Task MoveHighlightTimeline(string timelineName, long newPosition)
{
if (timelineName == null)
throw new ArgumentNullException(nameof(timelineName));
var timelineId = await _timelineService.GetTimelineIdByName(timelineName);
var entity = await _database.HighlightTimelines.SingleOrDefaultAsync(t => t.TimelineId == timelineId);
if (entity == null) throw new InvalidHighlightTimelineException("You can't move a non-highlight timeline.");
var oldPosition = entity.Order;
if (newPosition < 1)
{
newPosition = 1;
}
else
{
var totalCount = await _database.HighlightTimelines.CountAsync();
if (newPosition > totalCount) newPosition = totalCount;
}
if (oldPosition == newPosition) return;
await using var transaction = await _database.Database.BeginTransactionAsync();
if (newPosition > oldPosition)
{
await _database.Database.ExecuteSqlRawAsync("UPDATE highlight_timelines SET `order` = `order` - 1 WHERE `order` BETWEEN {0} AND {1}", oldPosition + 1, newPosition);
await _database.Database.ExecuteSqlRawAsync("UPDATE highlight_timelines SET `order` = {0} WHERE id = {1}", newPosition, entity.Id);
}
else
{
await _database.Database.ExecuteSqlRawAsync("UPDATE highlight_timelines SET `order` = `order` + 1 WHERE `order` BETWEEN {0} AND {1}", newPosition, oldPosition - 1);
await _database.Database.ExecuteSqlRawAsync("UPDATE highlight_timelines SET `order` = {0} WHERE id = {1}", newPosition, entity.Id);
}
await transaction.CommitAsync();
}
}
}