backend DRY/KISS cleanup #324

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

View File

@@ -21,11 +21,13 @@ public class CategoryService {
this.categoryRepository = categoryRepository;
}
@Transactional(readOnly = true)
public Page<CategoryResponse> getAllCategories(String query, String type, Pageable pageable) {
return categoryRepository.searchCategories(StringUtils.trimToNull(query), StringUtils.trimToNull(type), pageable)
.map(this::mapToResponse);
}
@Transactional(readOnly = true)
public CategoryResponse getCategoryById(Long id) {
Category category = categoryRepository.findById(id)
.orElseThrow(() -> new ResourceNotFoundException("Category not found with id: " + id));

View File

@@ -5,6 +5,7 @@ import com.petshop.backend.dto.service.ServiceRequest;
import com.petshop.backend.dto.service.ServiceResponse;
import com.petshop.backend.exception.ResourceNotFoundException;
import com.petshop.backend.repository.ServiceRepository;
import com.petshop.backend.util.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
@@ -21,9 +22,8 @@ public class ServiceService {
@Transactional(readOnly = true)
public Page<ServiceResponse> getAllServices(String query, String species, Pageable pageable) {
String q = (query != null && !query.trim().isEmpty()) ? query.trim() : null;
String sp = (species != null && !species.trim().isEmpty()) ? species.trim() : null;
return serviceRepository.searchServices(q, sp, pageable).map(this::mapToResponse);
return serviceRepository.searchServices(StringUtils.trimToNull(query), StringUtils.trimToNull(species), pageable)
.map(this::mapToResponse);
}
@Transactional(readOnly = true)
@@ -36,14 +36,7 @@ public class ServiceService {
@Transactional
public ServiceResponse createService(ServiceRequest request) {
com.petshop.backend.entity.Service service = new com.petshop.backend.entity.Service();
service.setServiceName(request.getServiceName());
service.setServiceDesc(request.getServiceDesc());
service.setServicePrice(request.getServicePrice());
service.setServiceDuration(request.getServiceDuration());
if (request.getSpecies() != null) {
service.setSpecies(request.getSpecies());
}
applyFields(service, request);
service = serviceRepository.save(service);
return mapToResponse(service);
}
@@ -52,15 +45,7 @@ public class ServiceService {
public ServiceResponse updateService(Long id, ServiceRequest request) {
com.petshop.backend.entity.Service service = serviceRepository.findById(id)
.orElseThrow(() -> new ResourceNotFoundException("Service not found with id: " + id));
service.setServiceName(request.getServiceName());
service.setServiceDesc(request.getServiceDesc());
service.setServicePrice(request.getServicePrice());
service.setServiceDuration(request.getServiceDuration());
if (request.getSpecies() != null) {
service.setSpecies(request.getSpecies());
}
applyFields(service, request);
service = serviceRepository.save(service);
return mapToResponse(service);
}
@@ -78,6 +63,16 @@ public class ServiceService {
serviceRepository.deleteAllById(request.getIds());
}
private void applyFields(com.petshop.backend.entity.Service service, ServiceRequest request) {
service.setServiceName(request.getServiceName());
service.setServiceDesc(request.getServiceDesc());
service.setServicePrice(request.getServicePrice());
service.setServiceDuration(request.getServiceDuration());
if (request.getSpecies() != null) {
service.setSpecies(request.getSpecies());
}
}
private ServiceResponse mapToResponse(com.petshop.backend.entity.Service service) {
return new ServiceResponse(
service.getServiceId(),

View File

@@ -6,6 +6,7 @@ import com.petshop.backend.dto.store.StoreResponse;
import com.petshop.backend.entity.StoreLocation;
import com.petshop.backend.exception.ResourceNotFoundException;
import com.petshop.backend.repository.StoreRepository;
import com.petshop.backend.util.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
@@ -20,16 +21,16 @@ public class StoreService {
this.storeRepository = storeRepository;
}
@Transactional(readOnly = true)
public Page<StoreResponse> getAllStores(String query, Pageable pageable) {
Page<StoreLocation> stores;
if (query != null && !query.trim().isEmpty()) {
stores = storeRepository.searchStores(query, pageable);
} else {
stores = storeRepository.findAll(pageable);
}
String q = StringUtils.trimToNull(query);
Page<StoreLocation> stores = q != null
? storeRepository.searchStores(q, pageable)
: storeRepository.findAll(pageable);
return stores.map(this::mapToResponse);
}
@Transactional(readOnly = true)
public StoreResponse getStoreById(Long id) {
StoreLocation store = storeRepository.findById(id)
.orElseThrow(() -> new ResourceNotFoundException("Store not found with id: " + id));
@@ -39,12 +40,7 @@ public class StoreService {
@Transactional
public StoreResponse createStore(StoreRequest request) {
StoreLocation store = new StoreLocation();
store.setStoreName(request.getStoreName());
store.setAddress(request.getAddress());
store.setPhone(request.getPhone());
store.setEmail(request.getEmail());
store.setImageUrl(request.getImageUrl());
applyFields(store, request);
store = storeRepository.save(store);
return mapToResponse(store);
}
@@ -53,13 +49,7 @@ public class StoreService {
public StoreResponse updateStore(Long id, StoreRequest request) {
StoreLocation store = storeRepository.findById(id)
.orElseThrow(() -> new ResourceNotFoundException("Store not found with id: " + id));
store.setStoreName(request.getStoreName());
store.setAddress(request.getAddress());
store.setPhone(request.getPhone());
store.setEmail(request.getEmail());
store.setImageUrl(request.getImageUrl());
applyFields(store, request);
store = storeRepository.save(store);
return mapToResponse(store);
}
@@ -77,6 +67,14 @@ public class StoreService {
storeRepository.deleteAllById(request.getIds());
}
private void applyFields(StoreLocation store, StoreRequest request) {
store.setStoreName(request.getStoreName());
store.setAddress(request.getAddress());
store.setPhone(request.getPhone());
store.setEmail(request.getEmail());
store.setImageUrl(request.getImageUrl());
}
private StoreResponse mapToResponse(StoreLocation store) {
return new StoreResponse(
store.getStoreId(),

View File

@@ -6,6 +6,7 @@ import com.petshop.backend.dto.supplier.SupplierResponse;
import com.petshop.backend.entity.Supplier;
import com.petshop.backend.exception.ResourceNotFoundException;
import com.petshop.backend.repository.SupplierRepository;
import com.petshop.backend.util.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
@@ -20,16 +21,16 @@ public class SupplierService {
this.supplierRepository = supplierRepository;
}
@Transactional(readOnly = true)
public Page<SupplierResponse> getAllSuppliers(String query, Pageable pageable) {
Page<Supplier> suppliers;
if (query != null && !query.trim().isEmpty()) {
suppliers = supplierRepository.searchSuppliers(query, pageable);
} else {
suppliers = supplierRepository.findAll(pageable);
}
String q = StringUtils.trimToNull(query);
Page<Supplier> suppliers = q != null
? supplierRepository.searchSuppliers(q, pageable)
: supplierRepository.findAll(pageable);
return suppliers.map(this::mapToResponse);
}
@Transactional(readOnly = true)
public SupplierResponse getSupplierById(Long id) {
Supplier supplier = supplierRepository.findById(id)
.orElseThrow(() -> new ResourceNotFoundException("Supplier not found with id: " + id));
@@ -39,12 +40,7 @@ public class SupplierService {
@Transactional
public SupplierResponse createSupplier(SupplierRequest request) {
Supplier supplier = new Supplier();
supplier.setSupCompany(request.getSupCompany());
supplier.setSupContactFirstName(request.getSupContactFirstName());
supplier.setSupContactLastName(request.getSupContactLastName());
supplier.setSupEmail(request.getSupEmail());
supplier.setSupPhone(request.getSupPhone());
applyFields(supplier, request);
supplier = supplierRepository.save(supplier);
return mapToResponse(supplier);
}
@@ -53,13 +49,7 @@ public class SupplierService {
public SupplierResponse updateSupplier(Long id, SupplierRequest request) {
Supplier supplier = supplierRepository.findById(id)
.orElseThrow(() -> new ResourceNotFoundException("Supplier not found with id: " + id));
supplier.setSupCompany(request.getSupCompany());
supplier.setSupContactFirstName(request.getSupContactFirstName());
supplier.setSupContactLastName(request.getSupContactLastName());
supplier.setSupEmail(request.getSupEmail());
supplier.setSupPhone(request.getSupPhone());
applyFields(supplier, request);
supplier = supplierRepository.save(supplier);
return mapToResponse(supplier);
}
@@ -77,6 +67,14 @@ public class SupplierService {
supplierRepository.deleteAllById(request.getIds());
}
private void applyFields(Supplier supplier, SupplierRequest request) {
supplier.setSupCompany(request.getSupCompany());
supplier.setSupContactFirstName(request.getSupContactFirstName());
supplier.setSupContactLastName(request.getSupContactLastName());
supplier.setSupEmail(request.getSupEmail());
supplier.setSupPhone(request.getSupPhone());
}
private SupplierResponse mapToResponse(Supplier supplier) {
return new SupplierResponse(
supplier.getSupId(),