From 80df6116ab57a3f9ea569614430edbb258a6c9f8 Mon Sep 17 00:00:00 2001 From: Harkamal Randhawa Date: Fri, 17 Apr 2026 14:18:00 -0600 Subject: [PATCH] consolidate shared constants --- .../com/petshop/backend/service/CartService.java | 3 ++- .../com/petshop/backend/service/PetService.java | 15 +++------------ .../petshop/backend/service/ProductService.java | 15 +++------------ .../com/petshop/backend/service/SaleService.java | 5 +++-- .../petshop/backend/util/BusinessConstants.java | 11 +++++++++++ 5 files changed, 22 insertions(+), 27 deletions(-) create mode 100644 backend/src/main/java/com/petshop/backend/util/BusinessConstants.java diff --git a/backend/src/main/java/com/petshop/backend/service/CartService.java b/backend/src/main/java/com/petshop/backend/service/CartService.java index 9e11ac13..47e411e6 100644 --- a/backend/src/main/java/com/petshop/backend/service/CartService.java +++ b/backend/src/main/java/com/petshop/backend/service/CartService.java @@ -7,6 +7,7 @@ import com.petshop.backend.entity.*; import com.petshop.backend.exception.BusinessException; import com.petshop.backend.exception.ResourceNotFoundException; import com.petshop.backend.repository.*; +import com.petshop.backend.util.BusinessConstants; import com.stripe.Stripe; import com.stripe.exception.StripeException; import com.stripe.model.PaymentIntent; @@ -24,7 +25,7 @@ import java.util.List; @Service public class CartService { - private static final int LOYALTY_POINTS_PER_DOLLAR = 20; + private static final int LOYALTY_POINTS_PER_DOLLAR = BusinessConstants.LOYALTY_POINTS_PER_DOLLAR; private final CartRepository cartRepository; private final CartItemRepository cartItemRepository; diff --git a/backend/src/main/java/com/petshop/backend/service/PetService.java b/backend/src/main/java/com/petshop/backend/service/PetService.java index 98b63cd8..2f90011a 100644 --- a/backend/src/main/java/com/petshop/backend/service/PetService.java +++ b/backend/src/main/java/com/petshop/backend/service/PetService.java @@ -1,6 +1,7 @@ package com.petshop.backend.service; import com.petshop.backend.dto.common.BulkDeleteRequest; +import com.petshop.backend.util.ImageValidationUtil; import com.petshop.backend.util.StringUtils; import com.petshop.backend.dto.pet.MyPetRequest; import com.petshop.backend.dto.pet.MyPetResponse; @@ -31,7 +32,7 @@ import org.springframework.web.multipart.MultipartFile; import java.io.IOException; import java.util.List; -import java.util.Locale; + @Service public class PetService { @@ -284,17 +285,7 @@ public class PetService { if (file == null || file.isEmpty()) { throw new BusinessException("Please select an image to upload"); } - if (file.getSize() > 5 * 1024 * 1024) { - throw new BusinessException("Image file size must be less than 5MB"); - } - String contentType = file.getContentType(); - if (contentType == null) { - throw new BusinessException("Only JPG, PNG, and GIF images are allowed"); - } - String normalized = contentType.toLowerCase(Locale.ROOT); - if (!normalized.equals("image/jpeg") && !normalized.equals("image/png") && !normalized.equals("image/gif")) { - throw new BusinessException("Only JPG, PNG, and GIF images are allowed"); - } + ImageValidationUtil.validate(file); } private Pet findOwnedPet(Long ownerUserId, Long petId) { diff --git a/backend/src/main/java/com/petshop/backend/service/ProductService.java b/backend/src/main/java/com/petshop/backend/service/ProductService.java index 9f2a9069..fc292f36 100644 --- a/backend/src/main/java/com/petshop/backend/service/ProductService.java +++ b/backend/src/main/java/com/petshop/backend/service/ProductService.java @@ -1,6 +1,7 @@ package com.petshop.backend.service; import com.petshop.backend.dto.common.BulkDeleteRequest; +import com.petshop.backend.util.ImageValidationUtil; import com.petshop.backend.util.StringUtils; import com.petshop.backend.dto.product.ProductRequest; import com.petshop.backend.dto.product.ProductResponse; @@ -21,7 +22,7 @@ import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; import java.io.IOException; -import java.util.Locale; + @Service public class ProductService { @@ -138,17 +139,7 @@ public class ProductService { if (file == null || file.isEmpty()) { throw new BusinessException("Please select an image to upload"); } - if (file.getSize() > 5 * 1024 * 1024) { - throw new BusinessException("Image file size must be less than 5MB"); - } - String contentType = file.getContentType(); - if (contentType == null) { - throw new BusinessException("Only JPG, PNG, and GIF images are allowed"); - } - String normalized = contentType.toLowerCase(Locale.ROOT); - if (!normalized.equals("image/jpeg") && !normalized.equals("image/png") && !normalized.equals("image/gif")) { - throw new BusinessException("Only JPG, PNG, and GIF images are allowed"); - } + ImageValidationUtil.validate(file); } private void deleteStoredImageIfPresent(String storedImagePath) { diff --git a/backend/src/main/java/com/petshop/backend/service/SaleService.java b/backend/src/main/java/com/petshop/backend/service/SaleService.java index f69ef9e1..adf6ab3b 100644 --- a/backend/src/main/java/com/petshop/backend/service/SaleService.java +++ b/backend/src/main/java/com/petshop/backend/service/SaleService.java @@ -8,6 +8,7 @@ import com.petshop.backend.exception.ResourceNotFoundException; import com.petshop.backend.event.SaleReceiptEvent; import com.petshop.backend.repository.*; import com.petshop.backend.util.AuthenticationHelper; +import com.petshop.backend.util.BusinessConstants; import com.petshop.backend.util.StringUtils; import org.springframework.context.ApplicationEventPublisher; import org.springframework.data.domain.Page; @@ -24,8 +25,8 @@ import java.util.List; @Service public class SaleService { - private static final BigDecimal EMPLOYEE_DISCOUNT_PERCENT = new BigDecimal("0.10"); - private static final int LOYALTY_POINTS_PER_DOLLAR = 20; + private static final BigDecimal EMPLOYEE_DISCOUNT_PERCENT = BusinessConstants.EMPLOYEE_DISCOUNT_PERCENT; + private static final int LOYALTY_POINTS_PER_DOLLAR = BusinessConstants.LOYALTY_POINTS_PER_DOLLAR; private final SaleRepository saleRepository; private final ProductRepository productRepository; diff --git a/backend/src/main/java/com/petshop/backend/util/BusinessConstants.java b/backend/src/main/java/com/petshop/backend/util/BusinessConstants.java new file mode 100644 index 00000000..0be56b8a --- /dev/null +++ b/backend/src/main/java/com/petshop/backend/util/BusinessConstants.java @@ -0,0 +1,11 @@ +package com.petshop.backend.util; + +import java.math.BigDecimal; + +public final class BusinessConstants { + + private BusinessConstants() {} + + public static final int LOYALTY_POINTS_PER_DOLLAR = 20; + public static final BigDecimal EMPLOYEE_DISCOUNT_PERCENT = new BigDecimal("0.10"); +}