Skip to content

Commit d00ed1d

Browse files
committed
feat(product): add image retrieval functionality for products
Implement a new endpoint to return product images by file name. This allows users to access images associated with products, enhancing the product display capabilities in the application.
1 parent 4a79932 commit d00ed1d

2 files changed

Lines changed: 26 additions & 4 deletions

File tree

src/main/java/com/example/store/product/application/ProductServiceImplV2.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@
33
import com.example.store.product.domain.Product;
44
import com.example.store.product.infrastructure.repository.implementation.ProductRepositoryImplV2;
55
import lombok.RequiredArgsConstructor;
6+
import org.springframework.core.io.Resource;
7+
import org.springframework.core.io.UrlResource;
68
import org.springframework.data.domain.Page;
79
import org.springframework.data.domain.Pageable;
810
import org.springframework.stereotype.Service;
911

12+
import java.net.MalformedURLException;
13+
import java.nio.file.Path;
14+
import java.nio.file.Paths;
15+
1016
@Service
1117
@RequiredArgsConstructor
1218
public class ProductServiceImplV2 {
@@ -20,4 +26,16 @@ public Page<Product> findAll(Pageable pageable) {
2026
public Page<Product> findByNameContaining(String name, Pageable pageable) {
2127
return productRepositoryImplV2.findAllByProductNameContaining(name, pageable);
2228
}
29+
30+
public Resource findImageByName(String name) {
31+
Path imagePath = Paths.get("public/images", name);
32+
if (!imagePath.toFile().exists()) {
33+
throw new IllegalArgumentException("Image not found: " + name);
34+
}
35+
try {
36+
return new UrlResource(imagePath.toUri());
37+
} catch (MalformedURLException e) {
38+
throw new IllegalArgumentException("Error retrieving image: " + name, e);
39+
}
40+
}
2341
}

src/main/java/com/example/store/product/infrastructure/ProductControllerImplV2.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@
44
import com.example.store.product.domain.Product;
55
import io.swagger.v3.oas.annotations.Operation;
66
import lombok.RequiredArgsConstructor;
7+
import org.springframework.core.io.Resource;
78
import org.springframework.data.domain.Page;
89
import org.springframework.data.domain.Pageable;
910
import org.springframework.data.web.PageableDefault;
1011
import org.springframework.http.HttpStatus;
1112
import org.springframework.http.ResponseEntity;
12-
import org.springframework.web.bind.annotation.GetMapping;
13-
import org.springframework.web.bind.annotation.RequestMapping;
14-
import org.springframework.web.bind.annotation.RequestParam;
15-
import org.springframework.web.bind.annotation.RestController;
13+
import org.springframework.web.bind.annotation.*;
1614

1715
@RestController
1816
@RequestMapping("/api/v2/products")
@@ -32,4 +30,10 @@ public ResponseEntity<Page<Product>> findAll(@PageableDefault Pageable pageable)
3230
public ResponseEntity<Page<Product>> findByNameContaining(@RequestParam String name, @PageableDefault Pageable pageable) {
3331
return ResponseEntity.status(HttpStatus.OK).body(productService.findByNameContaining(name, pageable));
3432
}
33+
34+
@GetMapping("/images/{fileName:.+}")
35+
@Operation(summary = "Return a product image by file name")
36+
public ResponseEntity<Resource> getImage(@PathVariable String fileName) {
37+
return ResponseEntity.status(HttpStatus.OK).body(productService.findImageByName(fileName));
38+
}
3539
}

0 commit comments

Comments
 (0)