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