backend DRY/KISS cleanup #324

Merged
RecentRunner merged 14 commits from refactor/backend-cleanup into main 2026-04-18 08:23:37 -06:00
5 changed files with 22 additions and 27 deletions
Showing only changes of commit a86aa91c16 - Show all commits

View File

@@ -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;

View File

@@ -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) {

View File

@@ -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) {

View File

@@ -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;

View File

@@ -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");
}