using DeliverysetService.Library.Models.ValueObjects;
using DeliverysetService.Library.Persistence;
using DeliverysetService.Library.Persistence.Models;
using DeliverysetService.Library.Persistence.Models.Json;
using DeliverysetService.Library.Services;
using DeliverysetService.Library.Services.ChangesCoordinator;
using DeliverysetService.Library.Services.DataServices;
using FluentValidation;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
public class DeliverysetSaverService : IDeliverysetSaverService
{
private readonly IApplicationDbContext _context;
private readonly IDeliverysetVersionInfoByIdRetrieve _deliverysetVersionInfoByIdRetrieve;
private readonly IAssetStatusMapRetriever _assetStatusMapRetriever;
private readonly ILogger<DeliverysetSaverService> _logger;
private readonly IDeliverysetService _deliverySetService;
private readonly IValidator<Deliveryset> _deliverySetValidator;
private readonly IDeliverysetFileChangesCoordinator _deliverySetFileChangesCoordinator;
public DeliverysetSaverService(IApplicationDbContext context, IDeliverysetVersionInfoByIdRetrieve deliverysetVersionInfoByIdRetrieve, IAssetStatusMapRetriever assetStatusMapRetriever, ILogger<DeliverysetSaverService> logger, IDeliverysetService deliverySetService, IValidator<Deliveryset> deliverySetValidator, IDeliverysetFileChangesCoordinator deliverySetFileChangesCoordinator)
{
_context = context;
_deliverysetVersionInfoByIdRetrieve = deliverysetVersionInfoByIdRetrieve;
_assetStatusMapRetriever = assetStatusMapRetriever;
_logger = logger;
_deliverySetService = deliverySetService;
_deliverySetValidator = deliverySetValidator;
_deliverySetFileChangesCoordinator = deliverySetFileChangesCoordinator;
}
public async Task<bool> SaveDeliveryset(Deliveryset? deliverySet, bool isDeletedFlag = false,
CancellationToken cancellationToken = default)
{
if (deliverySet == null) return false;
var validationResult = await _deliverySetValidator.ValidateAsync(deliverySet, cancellationToken);
if (!validationResult.IsValid)
{
_logger.LogWarning("Deliveryset validation failed, reason: {reason}", validationResult.ToString());
return false;
}
var id = new SomeIdValueObject(deliverySet.Tags.MhtId);
var contentId = (await _context.Contents.SingleOrDefaultAsync(content =>
content.ProductionNr == id.ProductionNr && content.ProgramNr == id.ProgramNr
&& content.Version == id.Version && content.Format == id.Format, cancellationToken: cancellationToken))?.Id;
var (someTrafficId, _) = id;
if (!contentId.HasValue)
{
_logger.LogWarning(
"Content for id:{id} was not found when saving delivery set", someTrafficId);
return false;
}
var deliverySetVersion =
await _deliverysetVersionInfoByIdRetrieve.GetVersionInfo(contentId.Value, cancellationToken);
var deliverySetChangesInfo = await _deliverySetFileChangesCoordinator.GetDeliverysetChangesInfo(deliverySetVersion, deliverySet, cancellationToken);
if (!deliverySetChangesInfo.HasChanges)
{
return false;
}
var contentDeliveryset = new ContentDeliveryset
{
ContentDeliverysetMetadata = new ContentDeliverysetMetadata()
{
AssetStatusId = _assetStatusMapRetriever.GetAssetStatusMap()["some_code"].Id
},
Deliveryset = deliverySet,
ContentId = contentId,
Version = deliverySetVersion.NextVersion,
DeliverysetId = deliverySet.Id,
SubtitleFiles = deliverySetChangesInfo.SubtitleFiles.ToList(),
VideoFiles = deliverySetChangesInfo.VideoFiles.ToList(),
IsDeleted = isDeletedFlag
};
await _deliverySetService.Save(contentDeliveryset, cancellationToken);
return true;
}
}
using DeliverysetService.Library.Enums;
using DeliverysetService.Library.Models;
using DeliverysetService.Library.Persistence;
using DeliverysetService.Library.Persistence.Models;
using DeliverysetService.Library.Persistence.Models.Json;
using DeliverysetService.Library.Services.DeliverysetFileCopyHandlerStrategy;
using DeliverysetService.Library.Services.DeliverysetFileDetectChangesHandlerStrategy;
using Microsoft.EntityFrameworkCore;
public class DeliverysetFileChangesCoordinator : IDeliverysetFileChangesCoordinator
{
private readonly IApplicationDbContext _context;
private readonly IDeliverysetFileCopyHandlerFactory _deliverySetFileCopyHandlerFactory;
private readonly IDeliverysetFileDetectChangesHandlerFactory _detectChangesHandlerFactory;
private IEnumerable<VideoFiles> _videoFiles = Array.Empty<VideoFiles>();
private IEnumerable<SubtitleFiles> _subtitleFiles = Array.Empty<SubtitleFiles>();
private bool _anyChanges;
public DeliverysetFileChangesCoordinator(IApplicationDbContext context, IDeliverysetFileCopyHandlerFactory deliverySetFileCopyHandlerFactory, IDeliverysetFileDetectChangesHandlerFactory detectChangesHandlerFactory)
{
_context = context;
_deliverySetFileCopyHandlerFactory = deliverySetFileCopyHandlerFactory;
_detectChangesHandlerFactory = detectChangesHandlerFactory;
}
public async Task<DeliverysetChangesInfo> GetDeliverysetChangesInfo(DeliverysetVersionInfo deliverySetVersionInfo, Deliveryset newDeliveryset, CancellationToken cancellationToken = default)
{
if (deliverySetVersionInfo.CurrentVersion == null)
{
return new DeliverysetChangesInfo(true, Array.Empty<VideoFiles>(), Array.Empty<SubtitleFiles>());
}
var currentNewestDeliveryset =
await _context.ContentDeliveryset.AsNoTracking().Where(d => d.Id == deliverySetVersionInfo.CurrentVersion.Id)
.Select(t => new
{
t.Deliveryset,
t.VideoFiles,
t.SubtitleFiles,
t.Version
}).SingleAsync(cancellationToken: cancellationToken);
foreach (var deliverySetChange in Enum.GetValues<DeliverysetChange>())
{
var changesHandler = _detectChangesHandlerFactory.GetDeliverysetFileCopyHandler(deliverySetChange);
var changesResult = changesHandler.GetDeliverysetChangesResult(currentNewestDeliveryset.Deliveryset, newDeliveryset);
if (!changesResult.IsAllAreEqual)
{
_anyChanges = true;
}
if (changesResult.IsAllAreEqual && deliverySetChange == DeliverysetChange.Video)
{
var copyHandler = _deliverySetFileCopyHandlerFactory.GetDeliverysetFileCopyHandler<VideoFiles>(DeliverysetFileCopyHandler.Video);
_videoFiles = copyHandler.GetFilesToCopy(currentNewestDeliveryset.VideoFiles);
}
if (deliverySetChange == DeliverysetChange.Subtitle)
_subtitleFiles = GetSubtitlesToCopy(changesResult, currentNewestDeliveryset.SubtitleFiles);
}
return new DeliverysetChangesInfo(_anyChanges, _videoFiles, _subtitleFiles);
}
private IEnumerable<SubtitleFiles> GetSubtitlesToCopy(DeliverysetChangesResult changesResult, List<SubtitleFiles> currentDeliverysetSubFiles)
{
var copy = _deliverySetFileCopyHandlerFactory.GetDeliverysetFileCopyHandler<SubtitleFiles>(DeliverysetFileCopyHandler.Sub);
bool FilterNotChangedSubtitleTypes(SubtitleFiles t) => changesResult.NotChangedByKeyList.Contains(t.SubtitleType);
var subtitleFilesList = changesResult.IsAllAreEqual
? currentDeliverysetSubFiles
: currentDeliverysetSubFiles.Where(FilterNotChangedSubtitleTypes).ToList();
var subtitleFilesEnumerable = copy.GetFilesToCopy(subtitleFilesList);
return subtitleFilesEnumerable;
}
}
using DeliverysetService.Library.Enums;
public class DeliverysetFileDetectChangesHandlerFactory : IDeliverysetFileDetectChangesHandlerFactory
{
private readonly IDictionary<DeliverysetChange, IDeliverysetFileDetectChangesHandler> _map;
public DeliverysetFileDetectChangesHandlerFactory(IDictionary<DeliverysetChange, IDeliverysetFileDetectChangesHandler> map)
{
_map = map;
}
public IDeliverysetFileDetectChangesHandler GetDeliverysetFileCopyHandler(DeliverysetChange deliverySetChange)
{
return _map[deliverySetChange];
}
}
Interface IDeliverysetFileDetectChangesHandlerIDeliverysetFileDetectChangesHandler
:
using DeliverysetService.Library.Models;
using DeliverysetService.Library.Persistence.Models.Json;
public interface IDeliverysetFileDetectChangesHandler
{
DeliverysetChangesResult GetDeliverysetChangesResult(Deliveryset currentDeliveryset, Deliveryset newDeliveryset);
}
using DeliverysetService.Library.Models;
using DeliverysetService.Library.Persistence.Models.Json;
using DeliverysetService.Library.Services.Comparers;
public class DeliverysetAssetsDetectChangesHandler : IDeliverysetFileDetectChangesHandler
{
public DeliverysetChangesResult GetDeliverysetChangesResult(Deliveryset currentDeliveryset, Deliveryset newDeliveryset)
{
return DeliverysetComparer.Init(newDeliveryset, currentDeliveryset).AssetsChanges();
}
}
using DeliverysetService.Library.Enums;
using DeliverysetService.Library.Persistence.Models.Common.Interfaces;
public class DeliverysetFileCopyHandlerFactory : IDeliverysetFileCopyHandlerFactory
{
private readonly IDictionary<DeliverysetFileCopyHandler, IDeliverysetFileCopyHandler> _map;
public DeliverysetFileCopyHandlerFactory(IDictionary<DeliverysetFileCopyHandler, IDeliverysetFileCopyHandler> map)
{
_map = map;
}
public IDeliverysetFileCopyHandler<T> GetDeliverysetFileCopyHandler<T>(DeliverysetFileCopyHandler copyMethod) where T : IFileEntity
{
var deliverySetFileCopyHandler = _map[copyMethod];
return (IDeliverysetFileCopyHandler<T>)deliverySetFileCopyHandler;
}
}
Interfaces IDeliverysetFileCopyHandlerInterface IDeliverysetFileCopyHandler
:
using DeliverysetService.Library.Persistence.Models.Common.Interfaces;
public interface IDeliverysetFileCopyHandler
{
}
public interface IDeliverysetFileCopyHandler<T> : IDeliverysetFileCopyHandler where T : IFileEntity
{
IEnumerable<T> GetFilesToCopy(List<T> entities);
}
using DeliverysetService.Library.Persistence.Models;
public class DeliverysetSubtitleFilesCopyHandler : IDeliverysetFileCopyHandler<SubtitleFiles>
{
public IEnumerable<SubtitleFiles> GetFilesToCopy(List<SubtitleFiles>? entities)
{
if (entities == null) yield break;
foreach (var entity in entities)
{
yield return new SubtitleFiles()
{
FileId = entity.FileId,
Status = entity.Status,
SubtitleType = entity.SubtitleType
};
}
}
}
using DeliverysetService.Library.Enums;
using DeliverysetService.Library.Persistence.Models.Json;
using DeliverysetService.Library.Validators;
using FluentValidation;
using Microsoft.Extensions.DependencyInjection;
using System.Reflection;
using DeliverysetService.Library.Persistence;
using DeliverysetService.Library.Services.DataServices;
using Microsoft.EntityFrameworkCore;
using DeliverysetService.Library.Services.DeliverysetFileCopyHandlerStrategy;
using DeliverysetService.Library.Services.DeliverysetFileDetectChangesHandlerStrategy;
using DeliverysetService.Library.Services.ChangesCoordinator;
using DeliverysetService.Library.Services;
public static class ConfigureServices
{
public static IServiceCollection AddLibraryServices(this IServiceCollection services)
{
var dummyConnString = "Server=127.0.0.1;Port=5432;Database=myDataBase;User Id=myUsername;Password=myPassword";
services.AddDbContext<ApplicationDbContext>(options =>
options.UseNpgsql(dummyConnString));
services.AddValidatorsFromAssembly(Assembly.GetExecutingAssembly());
services.AddScoped<IApplicationDbContext>(provider => provider.GetRequiredService<ApplicationDbContext>());
services.AddScoped<AuditableEntitySaveChangesInterceptor>();
services.AddDeliverysetServices();
return services;
}
public static IServiceCollection AddDeliverysetServices(this IServiceCollection services)
{
services.AddScoped<IValidator<Deliveryset>, DeliverysetValidator>();
services.AddScoped<IDeliverysetSaverService, DeliverysetSaverService>();
services.AddScoped<IAssetStatusMapRetriever, AssetStatusMapRetriever>();
services.AddScoped<IDeliverysetVersionInfoByIdRetrieve, DeliverysetVersionInfoByIdRetrieve>();
services.AddScoped<IDeliverysetService, Services.DataServices.DeliverysetService>();
services.AddScoped<IDeliverysetFileChangesCoordinator, DeliverysetFileChangesCoordinator>();
services.AddSingleton<IDeliverysetFileCopyHandlerFactory, DeliverysetFileCopyHandlerFactory>(_ =>
{
var deliverySetFileCopyHandlers =
new Dictionary<DeliverysetFileCopyHandler, IDeliverysetFileCopyHandler>
{
{ DeliverysetFileCopyHandler.Video, new DeliverysetVideoFilesCopyHandler() },
{ DeliverysetFileCopyHandler.Sub, new DeliverysetSubtitleFilesCopyHandler() }
};
return new DeliverysetFileCopyHandlerFactory(deliverySetFileCopyHandlers);
});
services.AddSingleton<IDeliverysetFileDetectChangesHandlerFactory, DeliverysetFileDetectChangesHandlerFactory>(_ =>
{
var deliverySetFileDetectChangesHandlers =
new Dictionary<DeliverysetChange, IDeliverysetFileDetectChangesHandler>
{
{ DeliverysetChange.Video, new DeliverysetVideoFilesDetectChangesHandler() },
{ DeliverysetChange.Asset, new DeliverysetAssetsDetectChangesHandler() },
{ DeliverysetChange.Subtitle, new DeliverysetSubtitleFilesDetectChangesHandler() }
};
return new DeliverysetFileDetectChangesHandlerFactory(deliverySetFileDetectChangesHandlers);
});
return services;
}
}
}