Exemple #1
0
        public async Task <string> MapProductJson(string customerCode, int productId, string category, int?mapperId, int?templateId, bool forceDownload, SiteMode siteMode)
        {
            if (!mapperId.HasValue)
            {
                PdfTemplate pdfTemplate;
                if (templateId.HasValue)
                {
                    pdfTemplate = await _dpcApiClient.GetProduct <PdfTemplate>(customerCode, templateId.Value, siteMode);
                }
                else
                {
                    pdfTemplate = await _pdfTemplateSelector.GetPdfTemplateForProduct(customerCode, productId, category, siteMode);
                }
                mapperId = pdfTemplate.PdfScriptMapper.Id;
            }

            var mapper = await _dpcApiClient.GetProduct <PdfScriptMapper>(customerCode, mapperId.Value, true, siteMode);

//            var mapper = await _dpcApiDbApiClient.GetPdfScriptMapper(mapperId.Value);


            var productBase = await _dpcApiClient.GetProduct <DpcProductBase>(customerCode, productId, false, siteMode, new[] { "Id", "UpdateDate" });

            if (productBase == null)
            {
                throw new GetProductJsonException();
            }
            var productDownloadUrl = _dpcApiClient.GetProductJsonDownloadUrl(customerCode, productId, true, siteMode);

            var request = new PreviewJsonRequest
            {
                TariffData = new GenerateHtmlFileInfo
                {
                    Id            = productId,
                    Timestamp     = ConvertToTimestamp(productBase.UpdateDate),
                    ForceDownload = forceDownload,
                    DownloadUrl   = productDownloadUrl,
                    SiteMode      = siteMode.ToString()
                },

                MapperData = new GenerateHtmlFileInfo
                {
                    Id            = mapperId.Value,
                    Timestamp     = ConvertToTimestamp(mapper.Timestamp),
                    ForceDownload = forceDownload,
                    DownloadUrl   = $"{_settings.DpcStaticFilesScheme}:{mapper.PdfScriptMapperFile.AbsoluteUrl}",
                    SiteMode      = "db"
                },
            };

            var response = await MakeRequest(request);

            if (response.Success)
            {
                return(response.Json);
            }

            throw new ProductMappingException(response.Error?.Message ?? "Unknown error while mapping product");
        }
Exemple #2
0
        public async Task <PdfGenerationSettings> GetDefaultSettings(string customerCode, SiteMode siteMode)
        {
            var settingsArray = await _dpcApiClient.GetProduct <PdfGenerationSettingsProduct[]>(customerCode, "pdfgenerationsettings",
                                                                                                new NameValueCollection
            {
                { "IsDefault", "true" }
            }, true, siteMode);

            var settings = settingsArray?.FirstOrDefault();

            return(string.IsNullOrWhiteSpace(settings?.ValueJson)
                ? null
                : JsonConvert.DeserializeObject <PdfGenerationSettings>(settings.ValueJson));
        }
        public async Task <string> GenerateHtml(string customerCode, int productId, string category, int?templateId,
                                                int?regionId, SiteMode siteMode, bool forceDownload)
        {
            PdfTemplate pdfTemplate;

            if (templateId.HasValue)
            {
                pdfTemplate = await _client.GetProduct <PdfTemplate>(customerCode, templateId.Value, siteMode);
            }
            else
            {
                pdfTemplate =
                    await _pdfTemplateSelector.GetPdfTemplateForProduct(customerCode, productId, category, siteMode);
            }

            if (pdfTemplate == null)
            {
                throw new TemplateNotFoundException();
            }

            var productBase = await _client.GetProduct <DpcProductBase>(customerCode, productId, false, siteMode,
                                                                        new[] { "Id", "UpdateDate" });

            if (productBase == null)
            {
                throw new GetProductJsonException();
            }
            var productDownloadUrl = _client.GetProductJsonDownloadUrl(customerCode, productId, true, siteMode);
            var request            = new GenerateHtmlRequest
            {
                TariffData = new GenerateHtmlFileInfo
                {
                    Id            = productId,
                    Timestamp     = ConvertToTimestamp(productBase.UpdateDate),
                    DownloadUrl   = productDownloadUrl,
                    ForceDownload = forceDownload,
                    SiteMode      = siteMode.ToString()
                },
                MapperData = new GenerateHtmlFileInfo
                {
                    Id          = pdfTemplate.PdfScriptMapper.Id,
                    Timestamp   = ConvertToTimestamp(pdfTemplate.PdfScriptMapper.Timestamp),
                    DownloadUrl =
                        $"{_settings.DpcStaticFilesScheme}:{pdfTemplate.PdfScriptMapper.PdfScriptMapperFile.AbsoluteUrl}",
                    ForceDownload = forceDownload,
                    SiteMode      = siteMode.ToString()
                },
                TemplateData = new GenerateHtmlFileInfo
                {
                    Id            = pdfTemplate.Id,
                    Timestamp     = ConvertToTimestamp(pdfTemplate.UpdateDate),
                    DownloadUrl   = $"{_settings.DpcStaticFilesScheme}:{pdfTemplate.PdfTemplateFile.AbsoluteUrl}",
                    ForceDownload = forceDownload,
                    SiteMode      = siteMode.ToString()
                },
                TemplateEngine = pdfTemplate.PdfTemplateEngine
            };
            var response = await MakeGenerateRequest(request);

            if (response.Success && !string.IsNullOrWhiteSpace(response.RelativePath))
            {
                var generatedHtml = await GetHtml(response.RelativePath);

                var replacedHtml =
                    await _regionTagsReplacer.ReplaceTags(customerCode, generatedHtml, productId, siteMode, regionId);

                return(replacedHtml);
            }

            throw new HtmlGenerationException(response.Error?.Message ?? "Unknown error while generating html");
        }