using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; using Microsoft.EntityFrameworkCore; using Newtonsoft.Json; using PartSource.Data.Contexts; using PartSource.Data.Models; using PartSource.Data.Nexpart; using PartSource.Services; using Ratermania.Automation.Interfaces; using Ratermania.Shopify; using Ratermania.Shopify.Resources; namespace PartSource.Automation.Jobs.POC { public class AlsoGetImageUrls : IAutomationJob { private readonly NexpartService _nexpartService; private readonly PartSourceContext _partSourceContext; private readonly FitmentContext _fitmentContext; public AlsoGetImageUrls(NexpartService nexpartService, PartSourceContext partSourceContext, FitmentContext fitmentContext) { _nexpartService = nexpartService; _partSourceContext = partSourceContext; _fitmentContext = fitmentContext; } public async Task Run(CancellationToken token, params string[] arguments) { IList rows = new List { "\"Line Code\", \"Part Number\", \"Image URL(s)\"" }; IList parts = await _fitmentContext.Parts.ToListAsync(); string oldLineCode = string.Empty; IList mappings = new List(); foreach (Data.Models.Part part in parts) { if (part.LineCode != oldLineCode) { mappings = await _fitmentContext.DcfMappings.Where(d => d.LineCode == part.LineCode).ToListAsync(); } ; foreach (DcfMapping mapping in mappings) { SmartPageDataSearch dataSearch = new SmartPageDataSearch { Items = new Item[] { new Item { MfrCode = mapping.WhiCode, PartNumber = part.PartNumber } }, DataOption = new[] { "ALL" } }; SmartPageDataSearchResponse response = await _nexpartService.SendRequest(dataSearch); if (response.ResponseBody.Item?.Length > 0) { List urls = new List(); if (!string.IsNullOrEmpty(response.ResponseBody.Item[0].PrimaryImg?.ImgUrl)) { urls.Add(response.ResponseBody.Item[0].PrimaryImg?.ImgUrl); }; if (response.ResponseBody.Item[0].AddImgs?.AddImg?.Length > 0) { urls.AddRange(response.ResponseBody.Item[0].AddImgs.AddImg.Select(i => i.AddImgUrl)); } if (urls.Count > 0) { rows.Add($"\"{part.LineCode}\", \"{part.PartNumber}\", \"{string.Join(";", urls)}\""); } } } } await File.WriteAllLinesAsync("C:\\users\\Tommy\\desktop\\WHI Images.csv", rows); } } }