Ejemplo n.º 1
0
        public HandlerRoutes(
            IAiringService airingSvc,
            IDestinationService destinationSvc,
            IPathingService pathingSvc,
            IFileService fileSvc,
            IHandlerHistoryService handlerHistorySvc,
            IQueueService queueSvc,
            IReportingService reporterSvc,
            EncodingFileContentValidator encodingFileValidator,
            Serilog.ILogger logger
            )
            : base("v1")
        {
            _airingSvc             = airingSvc;
            _destinationSvc        = destinationSvc;
            _pathingSvc            = pathingSvc;
            _fileSvc               = fileSvc;
            _handlerHistorySvc     = handlerHistorySvc;
            _queueSvc              = queueSvc;
            _encodingFileValidator = encodingFileValidator;
            _reporterSvc           = reporterSvc;
            _logger = logger;


            /// Persist Encoding related data through POST operation.
            /// The correlation between an existing airing and the provided payload
            /// is performed using the provided odt-media-id data point. As a result,
            /// odt-media-id is a required field in the payload.

            Post("/handler/encoding", _ =>
            {
                BLFileModel.File file = default(BLFileModel.File);

                try
                {
                    // Perform preliminary authorization
                    this.RequiresAuthentication();
                    this.RequiresClaims(c => c.Type == HttpMethod.Post.Verb());

                    // Deserealize JSON request to DataContracts.
                    EncodingFileContentViewModel encodingPayLoad = this.Bind <EncodingFileContentViewModel>();

                    // Retrieve encoding destination details
                    DF_ENCOM_DESTINATION_CODE = _destinationSvc.GetByDestinationNames(new List <String> {
                        "ENCOM"
                    }).First().ExternalId;

                    // Persist encoding raw JSON data before proceeding
                    this.Request.Body.Seek(0, SeekOrigin.Begin);
                    _handlerHistorySvc.Save(this.Request.Body.AsString(), encodingPayLoad.MediaId);

                    // Validate provided data contract. If validation errors are found
                    // then inform user, else continue
                    var validationResult = _encodingFileValidator.Validate(encodingPayLoad);
                    if (!validationResult.IsValid)
                    {
                        // Report error status (for encoding destination) to monitoring system
                        ReportStatusToMonitoringSystem(encodingPayLoad.MediaId, "Ingestion of encoding data failed due to validation errors in payload", DF_ERROR_STATUS_CODE, DF_ENCOM_DESTINATION_CODE);

                        // Return status
                        return(Negotiate.WithModel(validationResult.Errors.Select(c => c.ErrorMessage))
                               .WithStatusCode(HttpStatusCode.BadRequest));
                    }


                    ApplyPathTranslationInformation(encodingPayLoad);

                    // Translate DataContracts to Business Models
                    file = encodingPayLoad.ToBusinessModel <EncodingFileContentViewModel, BLFileModel.File>();

                    // Perform CRUD
                    _fileSvc.PersistVideoFile(file);

                    // Send notification to all subscribed consumers
                    SendNotification(file);

                    // Report completed status (for Encoding destination) to monitoring system
                    ReportStatusToMonitoringSystem(file.MediaId, "Successfully ingested encoding data", DF_COMPLETED_STATUS_CODE, DF_ENCOM_DESTINATION_CODE);

                    // Return
                    return(Response.AsJson(new
                    {
                        result = "success"
                    }, HttpStatusCode.OK));
                }
                catch (Exception ex)
                {
                    // Log error
                    logger.Error(ex, ex.Message);
                    throw;
                }
            });
        }