refactor: update ProductController to return ProductDTO; enhance ProductMapper for ID handling
This commit is contained in:
@@ -24,13 +24,19 @@ public class ProductController {
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public List<Product> getAllProducts() {
|
||||
return productService.getAllProducts();
|
||||
public List<ProductDTO> getAllProducts() {
|
||||
return productService.getAllProducts()
|
||||
.stream()
|
||||
.map(productMapper::toDto)
|
||||
.toList();
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public ResponseEntity<Product> getProductById(@PathVariable Long id) {
|
||||
public ResponseEntity<ProductDTO> getProductById(@PathVariable Long id) {
|
||||
return productService.getProductById(id)
|
||||
.stream()
|
||||
.map(productMapper::toDto)
|
||||
.findFirst()
|
||||
.map(ResponseEntity::ok)
|
||||
.orElse(ResponseEntity.notFound().build());
|
||||
}
|
||||
|
||||
@@ -10,6 +10,9 @@ import lombok.NoArgsConstructor;
|
||||
@NoArgsConstructor
|
||||
public class ProductDTO {
|
||||
|
||||
@JsonProperty("id")
|
||||
private Long id;
|
||||
|
||||
@JsonProperty("title")
|
||||
private String title;
|
||||
|
||||
|
||||
@@ -26,6 +26,7 @@ public class ProductMapper {
|
||||
public Product fromDto(ProductDTO productDTO) {
|
||||
if (productDTO == null) return null;
|
||||
Product product = new Product();
|
||||
product.setId(productDTO.getId());
|
||||
product.setTitle(productDTO.getTitle());
|
||||
product.setDescription(productDTO.getDescription());
|
||||
product.setPrice(productDTO.getPrice());
|
||||
@@ -39,7 +40,6 @@ public class ProductMapper {
|
||||
product.setCategory(null);
|
||||
}
|
||||
|
||||
// Platform + Brand handling — ensure platform is set on product
|
||||
if (productDTO.getPlatformDTO() != null && productDTO.getPlatformDTO().getName() != null) {
|
||||
String platformName = productDTO.getPlatformDTO().getName();
|
||||
Platform platform = this.platformService.getPlatformByName(platformName)
|
||||
@@ -65,7 +65,6 @@ public class ProductMapper {
|
||||
product.setPlatform(null);
|
||||
}
|
||||
|
||||
// Condition handling with null/invalid protection
|
||||
if (productDTO.getConditionDTO() != null && productDTO.getConditionDTO().getName() != null) {
|
||||
try {
|
||||
product.setCondition(Condition.valueOf(productDTO.getConditionDTO().getName()));
|
||||
@@ -82,6 +81,7 @@ public class ProductMapper {
|
||||
public ProductDTO toDto(Product product) {
|
||||
if (product == null) return null;
|
||||
ProductDTO productDTO = new ProductDTO();
|
||||
productDTO.setId(product.getId());
|
||||
productDTO.setTitle(product.getTitle());
|
||||
productDTO.setDescription(product.getDescription());
|
||||
productDTO.setPrice(product.getPrice());
|
||||
@@ -98,6 +98,7 @@ public class ProductMapper {
|
||||
if (product.getPlatform() != null) {
|
||||
Platform platform = product.getPlatform();
|
||||
PlatformDTO platformDTO = new PlatformDTO();
|
||||
platformDTO.setId(platform.getId());
|
||||
platformDTO.setName(platform.getName());
|
||||
|
||||
if (platform.getBrand() != null) {
|
||||
|
||||
Reference in New Issue
Block a user