diff --git a/src/main/java/group/goforward/ballistic/imports/MerchantFeedImportService.java b/src/main/java/group/goforward/ballistic/imports/MerchantFeedImportService.java index cf069bd..d3aae5d 100644 --- a/src/main/java/group/goforward/ballistic/imports/MerchantFeedImportService.java +++ b/src/main/java/group/goforward/ballistic/imports/MerchantFeedImportService.java @@ -1,5 +1,9 @@ package group.goforward.ballistic.imports; public interface MerchantFeedImportService { + + /** + * Import the feed for a given merchant id. + */ void importMerchantFeed(Integer merchantId); } \ No newline at end of file diff --git a/src/main/java/group/goforward/ballistic/imports/MerchantFeedImportServiceImpl.java b/src/main/java/group/goforward/ballistic/imports/MerchantFeedImportServiceImpl.java index eb7e88f..97a8622 100644 --- a/src/main/java/group/goforward/ballistic/imports/MerchantFeedImportServiceImpl.java +++ b/src/main/java/group/goforward/ballistic/imports/MerchantFeedImportServiceImpl.java @@ -1,16 +1,108 @@ package group.goforward.ballistic.imports; +import group.goforward.ballistic.model.Brand; +import group.goforward.ballistic.model.Merchant; +import group.goforward.ballistic.model.Product; +import group.goforward.ballistic.repos.BrandRepository; +import group.goforward.ballistic.repos.MerchantRepository; +import group.goforward.ballistic.repos.ProductRepository; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; + @Service @Transactional public class MerchantFeedImportServiceImpl implements MerchantFeedImportService { + private final MerchantRepository merchantRepository; + private final BrandRepository brandRepository; + private final ProductRepository productRepository; + + public MerchantFeedImportServiceImpl(MerchantRepository merchantRepository, + BrandRepository brandRepository, + ProductRepository productRepository) { + this.merchantRepository = merchantRepository; + this.brandRepository = brandRepository; + this.productRepository = productRepository; + } + @Override public void importMerchantFeed(Integer merchantId) { - // TODO: real import logic will be re-added. - // This stub exists to fix the repository/package mixup and get the project compiling again. - throw new UnsupportedOperationException("Merchant feed import not yet implemented after refactor."); + System.out.println("IMPORT >>> importMerchantFeed(" + merchantId + ")"); + + Merchant merchant = merchantRepository.findById(merchantId) + .orElseThrow(() -> new IllegalArgumentException("Merchant not found: " + merchantId)); + + // For now, just pick a brand to prove inserts work. + Brand brand = brandRepository.findByNameIgnoreCase("Aero Precision") + .orElseThrow(() -> new IllegalStateException("Brand 'Aero Precision' not found")); + + // Fake a single row – we’ll swap this for real CSV parsing once the plumbing works + MerchantFeedRow row = new MerchantFeedRow( + "TEST-SKU-001", + "APPG100002", + brand.getName(), + "Test Product From Import", + null, null, null, null, null, + null, null, null, null, null, + null, null, + null, null, null, null, null, null, null, null + ); + + Product p = createProduct(brand, row); + System.out.println("IMPORT >>> created product id=" + p.getId() + + ", name=" + p.getName() + + ", merchant=" + merchant.getName()); + } + + private Product createProduct(Brand brand, MerchantFeedRow row) { + System.out.println("IMPORT >>> createProduct brand=" + brand.getName() + + ", sku=" + row.sku() + + ", productName=" + row.productName()); + + Product p = new Product(); + p.setBrand(brand); + + String name = row.productName(); + if (name == null || name.isBlank()) { + name = row.sku(); + } + if (name == null || name.isBlank()) { + name = "Unknown Product"; + } + + // Set required fields: name and slug + p.setName(name); + + // Generate a simple slug from the name (fallback to SKU if needed) + String baseForSlug = name; + if (baseForSlug == null || baseForSlug.isBlank()) { + baseForSlug = row.sku(); + } + if (baseForSlug == null || baseForSlug.isBlank()) { + baseForSlug = "product-" + System.currentTimeMillis(); + } + + String slug = baseForSlug + .toLowerCase() + .replaceAll("[^a-z0-9]+", "-") + .replaceAll("(^-|-$)", ""); + + if (slug.isBlank()) { + slug = "product-" + System.currentTimeMillis(); + } + + p.setSlug(slug); + + if (p.getPlatform() == null || p.getPlatform().isBlank()) { + p.setPlatform("AR-15"); + } + + if (p.getPartRole() == null || p.getPartRole().isBlank()) { + p.setPartRole("unknown"); + } + + + return productRepository.save(p); } } \ No newline at end of file diff --git a/src/main/java/group/goforward/ballistic/imports/MerchantFeedRow.java b/src/main/java/group/goforward/ballistic/imports/MerchantFeedRow.java new file mode 100644 index 0000000..0cc0c42 --- /dev/null +++ b/src/main/java/group/goforward/ballistic/imports/MerchantFeedRow.java @@ -0,0 +1,30 @@ +package group.goforward.ballistic.imports; + +import java.math.BigDecimal; + +public record MerchantFeedRow( + String sku, + String manufacturerId, + String brandName, + String productName, + String longDescription, + String shortDescription, + String department, + String category, + String subCategory, + String thumbUrl, + String imageUrl, + String buyLink, + String keywords, + String reviews, + BigDecimal retailPrice, + BigDecimal salePrice, + String brandPageLink, + String brandLogoImage, + String productPageViewTracking, + String variantsXml, + String mediumImageUrl, + String productContentWidget, + String googleCategorization, + String itemBasedCommission +) {} \ No newline at end of file diff --git a/src/main/java/group/goforward/ballistic/repos/ProductRepository.java b/src/main/java/group/goforward/ballistic/repos/ProductRepository.java index 5812b90..b91bed1 100644 --- a/src/main/java/group/goforward/ballistic/repos/ProductRepository.java +++ b/src/main/java/group/goforward/ballistic/repos/ProductRepository.java @@ -1,11 +1,17 @@ +package group.goforward.ballistic.repos; + import group.goforward.ballistic.model.Product; import group.goforward.ballistic.model.Brand; import org.springframework.data.jpa.repository.JpaRepository; + import java.util.Optional; import java.util.UUID; public interface ProductRepository extends JpaRepository { + Optional findByUuid(UUID uuid); + Optional findByBrandAndMpn(Brand brand, String mpn); + Optional findByBrandAndUpc(Brand brand, String upc); } \ No newline at end of file