From b1aa33cfc65462b9e033a180c1324101ffae9f52 Mon Sep 17 00:00:00 2001 From: Shervin Z Date: Thu, 4 Sep 2025 22:50:44 -0400 Subject: [PATCH 1/2] Added the initial DTO and Controller --- .../shervinee/shop/products/ProductDto.java | 5 +++++ .../shop/products/ProductsController.java | 21 +++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 src/main/java/com/shervinee/shop/products/ProductDto.java create mode 100644 src/main/java/com/shervinee/shop/products/ProductsController.java diff --git a/src/main/java/com/shervinee/shop/products/ProductDto.java b/src/main/java/com/shervinee/shop/products/ProductDto.java new file mode 100644 index 0000000..f1be560 --- /dev/null +++ b/src/main/java/com/shervinee/shop/products/ProductDto.java @@ -0,0 +1,5 @@ +package com.shervinee.shop.products; + +import java.math.BigDecimal; + +public record ProductDto(Long id, String name, BigDecimal price, boolean inStock) {} diff --git a/src/main/java/com/shervinee/shop/products/ProductsController.java b/src/main/java/com/shervinee/shop/products/ProductsController.java new file mode 100644 index 0000000..02599fa --- /dev/null +++ b/src/main/java/com/shervinee/shop/products/ProductsController.java @@ -0,0 +1,21 @@ +package com.shervinee.shop.products; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.math.BigDecimal; +import java.util.List; + +@RestController +@RequestMapping("api/products") +public class ProductsController { + @GetMapping + public List list() { + return List.of( + new ProductDto(1L, "desk", new BigDecimal("122.67"), true ), + new ProductDto(1L, "monitor", new BigDecimal("252.31"), true ), + new ProductDto(1L, "chair", new BigDecimal("50.31"), false ) + ); + } +} From 6232dd5af3301bcd22993ca8687767e38a5d1fc0 Mon Sep 17 00:00:00 2001 From: Shervin Z Date: Tue, 7 Oct 2025 23:54:19 -0400 Subject: [PATCH 2/2] Added a dummy service using the initial hardcoded values --- .../products/InMemoryProductsService.java | 20 +++++++++++++++++++ .../shop/products/ProductsController.java | 12 +++++------ .../shop/products/ProductsService.java | 8 ++++++++ 3 files changed, 34 insertions(+), 6 deletions(-) create mode 100644 src/main/java/com/shervinee/shop/products/InMemoryProductsService.java create mode 100644 src/main/java/com/shervinee/shop/products/ProductsService.java diff --git a/src/main/java/com/shervinee/shop/products/InMemoryProductsService.java b/src/main/java/com/shervinee/shop/products/InMemoryProductsService.java new file mode 100644 index 0000000..3502dc6 --- /dev/null +++ b/src/main/java/com/shervinee/shop/products/InMemoryProductsService.java @@ -0,0 +1,20 @@ +package com.shervinee.shop.products; + +import org.springframework.context.annotation.Profile; +import org.springframework.stereotype.Service; + +import java.math.BigDecimal; +import java.util.List; + +@Service @Profile("dev") +public class InMemoryProductsService implements ProductsService { + + @Override + public List getAllProducts() { + return List.of( + new ProductDto(1L, "desk", new BigDecimal("122.67"), true ), + new ProductDto(1L, "monitor", new BigDecimal("252.31"), true ), + new ProductDto(1L, "chair", new BigDecimal("50.31"), false ) + ); + } +} diff --git a/src/main/java/com/shervinee/shop/products/ProductsController.java b/src/main/java/com/shervinee/shop/products/ProductsController.java index 02599fa..4154e07 100644 --- a/src/main/java/com/shervinee/shop/products/ProductsController.java +++ b/src/main/java/com/shervinee/shop/products/ProductsController.java @@ -1,21 +1,21 @@ package com.shervinee.shop.products; +import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; -import java.math.BigDecimal; import java.util.List; @RestController +@RequiredArgsConstructor @RequestMapping("api/products") public class ProductsController { + + private final ProductsService productsService; + @GetMapping public List list() { - return List.of( - new ProductDto(1L, "desk", new BigDecimal("122.67"), true ), - new ProductDto(1L, "monitor", new BigDecimal("252.31"), true ), - new ProductDto(1L, "chair", new BigDecimal("50.31"), false ) - ); + return productsService.getAllProducts(); } } diff --git a/src/main/java/com/shervinee/shop/products/ProductsService.java b/src/main/java/com/shervinee/shop/products/ProductsService.java new file mode 100644 index 0000000..a9c1319 --- /dev/null +++ b/src/main/java/com/shervinee/shop/products/ProductsService.java @@ -0,0 +1,8 @@ +package com.shervinee.shop.products; + +import java.util.List; + + +public interface ProductsService { + List getAllProducts(); +}