Fix backend issues
This commit is contained in:
@@ -30,7 +30,7 @@ public class RefundController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping
|
@PostMapping
|
||||||
@PreAuthorize("hasAnyRole('CUSTOMER', 'STAFF', 'ADMIN')")
|
@PreAuthorize("hasAnyRole('CUSTOMER', 'STAFF')")
|
||||||
public ResponseEntity<RefundResponse> createRefund(@Valid @RequestBody RefundRequest request) {
|
public ResponseEntity<RefundResponse> createRefund(@Valid @RequestBody RefundRequest request) {
|
||||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||||
String role = authentication.getAuthorities().stream()
|
String role = authentication.getAuthorities().stream()
|
||||||
@@ -85,7 +85,7 @@ public class RefundController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@PutMapping("/{id}")
|
@PutMapping("/{id}")
|
||||||
@PreAuthorize("hasAnyRole('STAFF', 'ADMIN')")
|
@PreAuthorize("hasRole('STAFF')")
|
||||||
public ResponseEntity<RefundResponse> updateRefund(@PathVariable Long id, @Valid @RequestBody RefundUpdateRequest request) {
|
public ResponseEntity<RefundResponse> updateRefund(@PathVariable Long id, @Valid @RequestBody RefundUpdateRequest request) {
|
||||||
return ResponseEntity.ok(refundService.updateRefundStatus(id, request.getStatus()));
|
return ResponseEntity.ok(refundService.updateRefundStatus(id, request.getStatus()));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ public class SaleController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping
|
@PostMapping
|
||||||
@PreAuthorize("hasAnyRole('STAFF', 'ADMIN')")
|
@PreAuthorize("hasRole('STAFF')")
|
||||||
public ResponseEntity<SaleResponse> createSale(@Valid @RequestBody SaleRequest request) {
|
public ResponseEntity<SaleResponse> createSale(@Valid @RequestBody SaleRequest request) {
|
||||||
return ResponseEntity.status(HttpStatus.CREATED).body(saleService.createSale(request));
|
return ResponseEntity.status(HttpStatus.CREATED).body(saleService.createSale(request));
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -42,9 +42,10 @@ public class GlobalExceptionHandler {
|
|||||||
errors.put(fieldName, errorMessage);
|
errors.put(fieldName, errorMessage);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
String firstMessage = errors.values().stream().findFirst().orElse("Validation failed");
|
||||||
Map<String, Object> response = new HashMap<>();
|
Map<String, Object> response = new HashMap<>();
|
||||||
response.put("status", HttpStatus.BAD_REQUEST.value());
|
response.put("status", HttpStatus.BAD_REQUEST.value());
|
||||||
response.put("message", "Validation failed");
|
response.put("message", firstMessage);
|
||||||
response.put("errors", errors);
|
response.put("errors", errors);
|
||||||
response.put("details", buildDetails(ex));
|
response.put("details", buildDetails(ex));
|
||||||
response.put("path", request.getRequestURI());
|
response.put("path", request.getRequestURI());
|
||||||
|
|||||||
@@ -11,6 +11,10 @@ import org.springframework.stereotype.Repository;
|
|||||||
@Repository
|
@Repository
|
||||||
public interface ProductRepository extends JpaRepository<Product, Long> {
|
public interface ProductRepository extends JpaRepository<Product, Long> {
|
||||||
|
|
||||||
|
boolean existsByProdNameIgnoreCase(String prodName);
|
||||||
|
|
||||||
|
boolean existsByProdNameIgnoreCaseAndProdIdNot(String prodName, Long prodId);
|
||||||
|
|
||||||
@Query("SELECT p FROM Product p WHERE " +
|
@Query("SELECT p FROM Product p WHERE " +
|
||||||
"(:q IS NULL OR LOWER(p.prodName) LIKE LOWER(CONCAT('%', :q, '%')) OR LOWER(COALESCE(p.prodDesc, '')) LIKE LOWER(CONCAT('%', :q, '%'))) AND " +
|
"(:q IS NULL OR LOWER(p.prodName) LIKE LOWER(CONCAT('%', :q, '%')) OR LOWER(COALESCE(p.prodDesc, '')) LIKE LOWER(CONCAT('%', :q, '%'))) AND " +
|
||||||
"(:categoryId IS NULL OR p.category.categoryId = :categoryId)")
|
"(:categoryId IS NULL OR p.category.categoryId = :categoryId)")
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import com.petshop.backend.dto.product.ProductRequest;
|
|||||||
import com.petshop.backend.dto.product.ProductResponse;
|
import com.petshop.backend.dto.product.ProductResponse;
|
||||||
import com.petshop.backend.entity.Category;
|
import com.petshop.backend.entity.Category;
|
||||||
import com.petshop.backend.entity.Product;
|
import com.petshop.backend.entity.Product;
|
||||||
|
import com.petshop.backend.exception.BusinessException;
|
||||||
import com.petshop.backend.exception.ResourceNotFoundException;
|
import com.petshop.backend.exception.ResourceNotFoundException;
|
||||||
import com.petshop.backend.repository.CategoryRepository;
|
import com.petshop.backend.repository.CategoryRepository;
|
||||||
import com.petshop.backend.repository.ProductRepository;
|
import com.petshop.backend.repository.ProductRepository;
|
||||||
@@ -45,6 +46,10 @@ public class ProductService {
|
|||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
public ProductResponse createProduct(ProductRequest request) {
|
public ProductResponse createProduct(ProductRequest request) {
|
||||||
|
if (productRepository.existsByProdNameIgnoreCase(request.getProdName())) {
|
||||||
|
throw new BusinessException("A product with this name already exists");
|
||||||
|
}
|
||||||
|
|
||||||
Category category = categoryRepository.findById(request.getCategoryId())
|
Category category = categoryRepository.findById(request.getCategoryId())
|
||||||
.orElseThrow(() -> new ResourceNotFoundException("Category not found with id: " + request.getCategoryId()));
|
.orElseThrow(() -> new ResourceNotFoundException("Category not found with id: " + request.getCategoryId()));
|
||||||
|
|
||||||
@@ -63,6 +68,10 @@ public class ProductService {
|
|||||||
Product product = productRepository.findById(id)
|
Product product = productRepository.findById(id)
|
||||||
.orElseThrow(() -> new ResourceNotFoundException("Product not found with id: " + id));
|
.orElseThrow(() -> new ResourceNotFoundException("Product not found with id: " + id));
|
||||||
|
|
||||||
|
if (productRepository.existsByProdNameIgnoreCaseAndProdIdNot(request.getProdName(), id)) {
|
||||||
|
throw new BusinessException("A product with this name already exists");
|
||||||
|
}
|
||||||
|
|
||||||
Category category = categoryRepository.findById(request.getCategoryId())
|
Category category = categoryRepository.findById(request.getCategoryId())
|
||||||
.orElseThrow(() -> new ResourceNotFoundException("Category not found with id: " + request.getCategoryId()));
|
.orElseThrow(() -> new ResourceNotFoundException("Category not found with id: " + request.getCategoryId()));
|
||||||
|
|
||||||
|
|||||||
@@ -68,6 +68,8 @@ openrouter:
|
|||||||
model: ${OPENROUTER_MODEL:openrouter/free}
|
model: ${OPENROUTER_MODEL:openrouter/free}
|
||||||
|
|
||||||
logging:
|
logging:
|
||||||
|
file:
|
||||||
|
name: ${LOG_FILE:log.txt}
|
||||||
level:
|
level:
|
||||||
com.petshop: ${LOG_LEVEL:INFO}
|
com.petshop: ${LOG_LEVEL:INFO}
|
||||||
org.springframework.security: ${LOG_LEVEL_SECURITY:WARN}
|
org.springframework.security: ${LOG_LEVEL_SECURITY:WARN}
|
||||||
|
|||||||
Reference in New Issue
Block a user