From 999696cd631252b92fc12ea526ba3aaba8323539 Mon Sep 17 00:00:00 2001 From: momnpa333 Date: Tue, 25 Jun 2024 21:15:20 +0900 Subject: [PATCH 01/53] =?UTF-8?q?feat(Product):=20model=20=EC=A0=9C?= =?UTF-8?q?=EC=9E=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/gift/model/Product.java | 49 +++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/main/java/gift/model/Product.java diff --git a/src/main/java/gift/model/Product.java b/src/main/java/gift/model/Product.java new file mode 100644 index 000000000..3a49da42b --- /dev/null +++ b/src/main/java/gift/model/Product.java @@ -0,0 +1,49 @@ +package gift.model; + +import java.util.UUID; + +public class Product { + + private Long id; + private String name; + private Integer price; + private String imageUrl; + + private Product(String name, Integer price, String imageUrl) { + this.id = UUID.randomUUID().getMostSignificantBits() & Long.MAX_VALUE; + this.name = name; + this.price = price; + this.imageUrl = imageUrl; + } + + private Product(Long id, String name, Integer price, String imageUrl) { + this.id = id; + this.name = name; + this.price = price; + this.imageUrl = imageUrl; + } + + public Long getId() { + return id; + } + + public String getName() { + return name; + } + + public Integer getPrice() { + return price; + } + + public String getImageUrl() { + return imageUrl; + } + + public static Product create(String name, Integer price, String imageUrl) { + return new Product(name, price, imageUrl); + } + + public static Product update(Long id, String name, Integer price, String imageUrl) { + return new Product(id, name, price, imageUrl); + } +} From bdf237cbb2b30814e94d0794213f2a4c27b63f55 Mon Sep 17 00:00:00 2001 From: momnpa333 Date: Tue, 25 Jun 2024 21:15:34 +0900 Subject: [PATCH 02/53] =?UTF-8?q?feat(ProductController):=20controller=20?= =?UTF-8?q?=EC=A0=9C=EC=9E=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gift/controller/ProductController.java | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 src/main/java/gift/controller/ProductController.java diff --git a/src/main/java/gift/controller/ProductController.java b/src/main/java/gift/controller/ProductController.java new file mode 100644 index 000000000..5f227c62b --- /dev/null +++ b/src/main/java/gift/controller/ProductController.java @@ -0,0 +1,58 @@ +package gift.controller; + +import gift.model.Product; +import gift.model.ProductDao; + +import java.util.List; + +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; + +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class ProductController { + + private final ProductDao productDao; + + public ProductController(ProductDao productDao) { + this.productDao = productDao; + } + + @GetMapping("/products") + public ResponseEntity> getProducts() { + var response = productDao.findAll(); + return ResponseEntity.ok(response); + } + + @GetMapping("/products/{id}") + public ResponseEntity getProduct(@PathVariable("id") Long id) { + var response = productDao.findById(id); + return ResponseEntity.ok(response.orElse(null)); + } + + @PostMapping("/products") + public ResponseEntity createProduct(@RequestBody ProductRequest request) { + var response = productDao.save(request); + return ResponseEntity.ok(response); + } + + @PutMapping("/products/{id}") + public ResponseEntity updateProduct(@PathVariable("id") Long id, + @RequestBody ProductRequest request) { + var response = productDao.update(id, request); + return ResponseEntity.ok(response); + } + + @DeleteMapping("/products/{id}") + public ResponseEntity deleteProduct(@PathVariable("id") Long id) { + productDao.deleteById(id); + return ResponseEntity.noContent().build(); + } + +} From bd50b26553b3cd7eb0dcb38965f343b0aaf55a12 Mon Sep 17 00:00:00 2001 From: momnpa333 Date: Tue, 25 Jun 2024 21:15:52 +0900 Subject: [PATCH 03/53] =?UTF-8?q?feat(ProductDao):=20Dao=20=EC=A0=9C?= =?UTF-8?q?=EC=9E=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/gift/model/ProductDao.java | 39 ++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/main/java/gift/model/ProductDao.java diff --git a/src/main/java/gift/model/ProductDao.java b/src/main/java/gift/model/ProductDao.java new file mode 100644 index 000000000..808919df1 --- /dev/null +++ b/src/main/java/gift/model/ProductDao.java @@ -0,0 +1,39 @@ +package gift.model; + +import gift.controller.ProductRequest; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.concurrent.ConcurrentHashMap; +import org.springframework.stereotype.Component; + +@Component +public class ProductDao { + + private final Map database = new ConcurrentHashMap<>(); + + public Product save(ProductRequest request) { + Product product = Product.create(request.name(), request.price(), request.imageUrl()); + database.put(product.getId(), product); + return product; + } + + public Optional findById(Long id) { + return Optional.ofNullable(database.get(id)); + } + + public List findAll() { + return List.copyOf(database.values()); + } + + public void deleteById(Long id) { + database.remove(id); + } + + public Product update(Long id, ProductRequest request) { + Product product = Product.update(id, request.name(), request.price(), request.imageUrl()); + database.replace(id, product); + return product; + } + +} From 352aecc5ccd1bf8b0dd227387d475a6078503186 Mon Sep 17 00:00:00 2001 From: momnpa333 Date: Tue, 25 Jun 2024 21:16:03 +0900 Subject: [PATCH 04/53] =?UTF-8?q?feat(ProductRequest):=20Request=20?= =?UTF-8?q?=EC=A0=9C=EC=9E=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/gift/controller/ProductRequest.java | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 src/main/java/gift/controller/ProductRequest.java diff --git a/src/main/java/gift/controller/ProductRequest.java b/src/main/java/gift/controller/ProductRequest.java new file mode 100644 index 000000000..4274f997e --- /dev/null +++ b/src/main/java/gift/controller/ProductRequest.java @@ -0,0 +1,10 @@ +package gift.controller; + + +public record ProductRequest( + String name, + int price, + String imageUrl +) { + +} From f9282b660a467fe5779e2e421ed5b42d9feb7413 Mon Sep 17 00:00:00 2001 From: momnpa333 Date: Wed, 26 Jun 2024 15:15:53 +0900 Subject: [PATCH 05/53] =?UTF-8?q?test(ProductControllerTest):=20test=20?= =?UTF-8?q?=EC=A0=9C=EC=9E=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ProductControllerTest.java | 119 ++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 src/test/java/gift/controller/ProductControllerTest.java diff --git a/src/test/java/gift/controller/ProductControllerTest.java b/src/test/java/gift/controller/ProductControllerTest.java new file mode 100644 index 000000000..5b5b389d3 --- /dev/null +++ b/src/test/java/gift/controller/ProductControllerTest.java @@ -0,0 +1,119 @@ +package gift.controller; + +import gift.model.Product; +import gift.model.ProductDao; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; + +import java.util.Arrays; +import java.util.Optional; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.*; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +class ProductControllerTest { + + @Mock + private ProductDao productDao; + + @InjectMocks + private ProductController productController; + + private MockMvc mockMvc; + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + mockMvc = MockMvcBuilders.standaloneSetup(productController).build(); + } + + @Test + void testGetProducts() throws Exception { + // given: 테스트에 사용될 mock 데이터를 설정합니다. + var products = Arrays.asList( + Product.create("Product1", 100, "http://image1.com"), + Product.create("Product2", 200, "http://image2.com") + ); + // when: productDao.findAll() 메서드가 호출될 때 products를 반환하도록 설정합니다. + when(productDao.findAll()).thenReturn(products); + + // then: GET /products 요청을 보내고 반환된 데이터를 검증합니다. + mockMvc.perform(get("/products")) + .andExpect(status().isOk()) + .andExpect(jsonPath("$[0].name").value("Product1")) + .andExpect(jsonPath("$[0].price").value(100)) + .andExpect(jsonPath("$[0].imageUrl").value("http://image1.com")) + .andExpect(jsonPath("$[1].name").value("Product2")) + .andExpect(jsonPath("$[1].price").value(200)) + .andExpect(jsonPath("$[1].imageUrl").value("http://image2.com")); + + // productDao.findAll() 메서드가 한 번 호출되었는지 검증합니다. + verify(productDao, times(1)).findAll(); + } + + @Test + void testGetProduct() throws Exception { + var product = Product.create("Product1", 100, "http://image1.com"); + when(productDao.findById(anyLong())).thenReturn(Optional.of(product)); + + mockMvc.perform(get("/products/{id}", 1L)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.name").value("Product1")) + .andExpect(jsonPath("$.price").value(100)) + .andExpect(jsonPath("$.imageUrl").value("http://image1.com")); + + verify(productDao, times(1)).findById(anyLong()); + } + + @Test + void testCreateProduct() throws Exception { + var product = Product.create("Product1", 100, "http://image1.com"); + when(productDao.save(any(ProductRequest.class))).thenReturn(product); + + mockMvc.perform(post("/products") + .contentType(MediaType.APPLICATION_JSON) + .content("{\"name\":\"Product1\", \"price\":100, \"imageUrl\":\"http://image1.com\"}")) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.name").value("Product1")) + .andExpect(jsonPath("$.price").value(100)) + .andExpect(jsonPath("$.imageUrl").value("http://image1.com")); + + verify(productDao, times(1)).save(any(ProductRequest.class)); + } + + @Test + void testUpdateProduct() throws Exception { + var product = Product.update(1L, "UpdatedProduct", 150, "http://updatedimage.com"); + when(productDao.update(eq(1L), any(ProductRequest.class))).thenReturn(product); + + mockMvc.perform(put("/products/{id}", 1L) + .contentType(MediaType.APPLICATION_JSON) + .content( + "{\"name\":\"UpdatedProduct\", \"price\":150, \"imageUrl\":\"http://updatedimage.com\"}")) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.name").value("UpdatedProduct")) + .andExpect(jsonPath("$.price").value(150)) + .andExpect(jsonPath("$.imageUrl").value("http://updatedimage.com")); + + verify(productDao, times(1)).update(eq(1L), any(ProductRequest.class)); + } + + @Test + void testDeleteProduct() throws Exception { + doNothing().when(productDao).deleteById(anyLong()); + + mockMvc.perform(delete("/products/{id}", 1L)) + .andExpect(status().isNoContent()); + + verify(productDao, times(1)).deleteById(anyLong()); + } +} From 80e6352ac6cef61d4277b771c880caf0d42609da Mon Sep 17 00:00:00 2001 From: momnpa333 Date: Thu, 27 Jun 2024 17:54:13 +0900 Subject: [PATCH 06/53] =?UTF-8?q?chore(.gitkeep):=20keeper=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/static/.gitkeep | 0 src/main/resources/templates/.gitkeep | 0 2 files changed, 0 insertions(+), 0 deletions(-) delete mode 100644 src/main/resources/static/.gitkeep delete mode 100644 src/main/resources/templates/.gitkeep diff --git a/src/main/resources/static/.gitkeep b/src/main/resources/static/.gitkeep deleted file mode 100644 index e69de29bb..000000000 diff --git a/src/main/resources/templates/.gitkeep b/src/main/resources/templates/.gitkeep deleted file mode 100644 index e69de29bb..000000000 From d06d5154f6d6847e4d755995f74518877e0f97a4 Mon Sep 17 00:00:00 2001 From: momnpa333 Date: Thu, 27 Jun 2024 17:54:53 +0900 Subject: [PATCH 07/53] =?UTF-8?q?fix(ProductController.java):=20@RestContr?= =?UTF-8?q?oller=20->=20@Controller=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/gift/controller/ProductController.java | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/main/java/gift/controller/ProductController.java b/src/main/java/gift/controller/ProductController.java index 5f227c62b..ff347a020 100644 --- a/src/main/java/gift/controller/ProductController.java +++ b/src/main/java/gift/controller/ProductController.java @@ -6,6 +6,7 @@ import java.util.List; import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @@ -13,9 +14,7 @@ import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RestController; - -@RestController +@Controller public class ProductController { private final ProductDao productDao; @@ -24,6 +23,16 @@ public ProductController(ProductDao productDao) { this.productDao = productDao; } + /** + * admin 페이지로 이동합니다. + * + * @return + */ + @GetMapping("/admin") + public String admin() { + return "adminPage"; + } + @GetMapping("/products") public ResponseEntity> getProducts() { var response = productDao.findAll(); From ff0e72829a6aa79bd41418b7f21b308bf41581c9 Mon Sep 17 00:00:00 2001 From: momnpa333 Date: Thu, 27 Jun 2024 18:03:21 +0900 Subject: [PATCH 08/53] =?UTF-8?q?refact(Product.java):=20Product=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1=EC=9E=90=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/gift/model/Product.java | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/src/main/java/gift/model/Product.java b/src/main/java/gift/model/Product.java index 3a49da42b..220c9ada3 100644 --- a/src/main/java/gift/model/Product.java +++ b/src/main/java/gift/model/Product.java @@ -9,13 +9,6 @@ public class Product { private Integer price; private String imageUrl; - private Product(String name, Integer price, String imageUrl) { - this.id = UUID.randomUUID().getMostSignificantBits() & Long.MAX_VALUE; - this.name = name; - this.price = price; - this.imageUrl = imageUrl; - } - private Product(Long id, String name, Integer price, String imageUrl) { this.id = id; this.name = name; @@ -39,11 +32,8 @@ public String getImageUrl() { return imageUrl; } - public static Product create(String name, Integer price, String imageUrl) { - return new Product(name, price, imageUrl); - } - - public static Product update(Long id, String name, Integer price, String imageUrl) { + public static Product create(Long id, String name, Integer price, String imageUrl) { return new Product(id, name, price, imageUrl); } + } From 04a1fcd2f93aabc66662ed311a1a478ca1a2f3b9 Mon Sep 17 00:00:00 2001 From: momnpa333 Date: Thu, 27 Jun 2024 18:03:40 +0900 Subject: [PATCH 09/53] =?UTF-8?q?refact(ProductDao.java):=20id=20=EB=B6=80?= =?UTF-8?q?=EC=97=AC=20=EB=A1=9C=EC=A7=81=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/gift/model/ProductDao.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/main/java/gift/model/ProductDao.java b/src/main/java/gift/model/ProductDao.java index 808919df1..44d44069d 100644 --- a/src/main/java/gift/model/ProductDao.java +++ b/src/main/java/gift/model/ProductDao.java @@ -13,7 +13,8 @@ public class ProductDao { private final Map database = new ConcurrentHashMap<>(); public Product save(ProductRequest request) { - Product product = Product.create(request.name(), request.price(), request.imageUrl()); + Product product = Product.create(Long.valueOf(database.size() + 1), request.name(), + request.price(), request.imageUrl()); database.put(product.getId(), product); return product; } @@ -31,7 +32,7 @@ public void deleteById(Long id) { } public Product update(Long id, ProductRequest request) { - Product product = Product.update(id, request.name(), request.price(), request.imageUrl()); + Product product = Product.create(id, request.name(), request.price(), request.imageUrl()); database.replace(id, product); return product; } From ed6b29e117b4a68a6e45cf925333b74647a6aa7a Mon Sep 17 00:00:00 2001 From: momnpa333 Date: Thu, 27 Jun 2024 18:04:05 +0900 Subject: [PATCH 10/53] =?UTF-8?q?feat(adminPage.html):=20admin=ED=8E=98?= =?UTF-8?q?=EC=9D=B4=EC=A7=80=20=EC=A0=9C=EC=9E=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/templates/adminPage.html | 66 +++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 src/main/resources/templates/adminPage.html diff --git a/src/main/resources/templates/adminPage.html b/src/main/resources/templates/adminPage.html new file mode 100644 index 000000000..11940d857 --- /dev/null +++ b/src/main/resources/templates/adminPage.html @@ -0,0 +1,66 @@ + + + + Manage Products + + + + + + + + +
+

Manage Products

+ + + + + + + + + + + + + +
NamePriceImageUrlActions
+
+ + + + + From 78bee3b6abb7d2f5049d2ffc7532dd0f44bee26a Mon Sep 17 00:00:00 2001 From: momnpa333 Date: Thu, 27 Jun 2024 18:04:27 +0900 Subject: [PATCH 11/53] =?UTF-8?q?feat(product.js):=20ajxa=20=EA=B4=80?= =?UTF-8?q?=EB=A0=A8=20js=20=EC=A0=9C=EC=9E=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/static/js/product.js | 83 +++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 src/main/resources/static/js/product.js diff --git a/src/main/resources/static/js/product.js b/src/main/resources/static/js/product.js new file mode 100644 index 000000000..56c0f1657 --- /dev/null +++ b/src/main/resources/static/js/product.js @@ -0,0 +1,83 @@ +$(document).ready(function () { + function loadProducts() { + $.get("/products", function (data) { + $('#productTableBody').empty(); + data.forEach(function (product) { + $('#productTableBody').append(` + + ${product.name} + ${product.price} + ${product.imageUrl} + + + + + + `); + console.log(product) + }); + }); + } + + $('#addNewProduct').click(function () { + $('#productForm')[0].reset(); + $('#productId').val(''); + $('#productModal').modal('show'); + }); + + $('#saveProduct').click(function () { + const product = { + name: $('#name').val(), + price: $('#price').val(), + imageUrl: $('#imageUrl').val() + }; + const id = $('#productId').val(); + if (id) { + $.ajax({ + url: `/products/${id}`, + type: 'PUT', + contentType: 'application/json', + data: JSON.stringify(product), + success: function () { + $('#productModal').modal('hide'); + loadProducts(); + } + }); + } else { + $.ajax({ + url: '/products', + type: 'POST', + contentType: 'application/json', + data: JSON.stringify(product), + success: function () { + $('#productModal').modal('hide'); + loadProducts(); + } + }); + } + }); + + $(document).on('click', '.btn-edit', function () { + const id = $(this).data('id'); + $.get(`/products/${id}`, function (data) { + $('#productId').val(data.id); + $('#name').val(data.name); + $('#price').val(data.price); + $('#imageUrl').val(data.imageUrl); + $('#productModal').modal('show'); + }); + }); + + $(document).on('click', '.btn-delete', function () { + const id = $(this).data('id'); + $.ajax({ + url: `/products/${id}`, + type: 'DELETE', + success: function () { + loadProducts(); + } + }); + }); + + loadProducts(); +}); From d1dd665017bea72832f96c32ad5c666dcc94f88a Mon Sep 17 00:00:00 2001 From: momnpa333 Date: Thu, 27 Jun 2024 18:05:12 +0900 Subject: [PATCH 12/53] =?UTF-8?q?chore(product.js):=20=EB=A1=9C=EA=B7=B8?= =?UTF-8?q?=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/static/js/product.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/resources/static/js/product.js b/src/main/resources/static/js/product.js index 56c0f1657..db86cf8ce 100644 --- a/src/main/resources/static/js/product.js +++ b/src/main/resources/static/js/product.js @@ -14,7 +14,6 @@ $(document).ready(function () { `); - console.log(product) }); }); } From 1aa56012796294e3a4368c461490ddf7d4545980 Mon Sep 17 00:00:00 2001 From: momnpa333 Date: Thu, 27 Jun 2024 18:06:48 +0900 Subject: [PATCH 13/53] =?UTF-8?q?refact(ProductRequest.java):=20int->Inter?= =?UTF-8?q?ger=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/gift/controller/ProductRequest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/gift/controller/ProductRequest.java b/src/main/java/gift/controller/ProductRequest.java index 4274f997e..4b89a6d59 100644 --- a/src/main/java/gift/controller/ProductRequest.java +++ b/src/main/java/gift/controller/ProductRequest.java @@ -3,7 +3,7 @@ public record ProductRequest( String name, - int price, + Integer price, String imageUrl ) { From 9aaa91986f95a6471600f64152414ae4b084345a Mon Sep 17 00:00:00 2001 From: momnpa333 Date: Thu, 27 Jun 2024 18:24:05 +0900 Subject: [PATCH 14/53] =?UTF-8?q?chore(adminPage.html):=20=ED=83=9C?= =?UTF-8?q?=EA=B7=B8=20=EC=A0=95=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/templates/adminPage.html | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/main/resources/templates/adminPage.html b/src/main/resources/templates/adminPage.html index 11940d857..6e39a0886 100644 --- a/src/main/resources/templates/adminPage.html +++ b/src/main/resources/templates/adminPage.html @@ -2,13 +2,13 @@ Manage Products - + -

Manage Products

@@ -23,7 +23,6 @@

Manage Products

-
From 5610a97ae69253455e90d7f1e79f4b305ce75929 Mon Sep 17 00:00:00 2001 From: momnpa333 Date: Thu, 27 Jun 2024 18:24:37 +0900 Subject: [PATCH 15/53] =?UTF-8?q?refact(product.js):=20async=20await=20?= =?UTF-8?q?=EC=9C=BC=EB=A1=9C=20=EC=BD=94=EB=93=9C=20=EB=A6=AC=ED=8C=A9?= =?UTF-8?q?=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/static/js/product.js | 107 ++++++++++++++---------- 1 file changed, 63 insertions(+), 44 deletions(-) diff --git a/src/main/resources/static/js/product.js b/src/main/resources/static/js/product.js index db86cf8ce..d4cf51b72 100644 --- a/src/main/resources/static/js/product.js +++ b/src/main/resources/static/js/product.js @@ -1,6 +1,8 @@ $(document).ready(function () { - function loadProducts() { - $.get("/products", function (data) { + + async function loadProducts() { + try { + const data = await $.get("/products"); $('#productTableBody').empty(); data.forEach(function (product) { $('#productTableBody').append(` @@ -15,67 +17,84 @@ $(document).ready(function () { `); }); - }); + } catch (error) { + console.error('Error loading products:', error); + } } - $('#addNewProduct').click(function () { - $('#productForm')[0].reset(); - $('#productId').val(''); - $('#productModal').modal('show'); - }); - - $('#saveProduct').click(function () { + async function saveProduct() { const product = { name: $('#name').val(), price: $('#price').val(), imageUrl: $('#imageUrl').val() }; const id = $('#productId').val(); - if (id) { - $.ajax({ - url: `/products/${id}`, - type: 'PUT', - contentType: 'application/json', - data: JSON.stringify(product), - success: function () { - $('#productModal').modal('hide'); - loadProducts(); - } - }); - } else { - $.ajax({ - url: '/products', - type: 'POST', - contentType: 'application/json', - data: JSON.stringify(product), - success: function () { - $('#productModal').modal('hide'); - loadProducts(); - } - }); + try { + if (id) { + await $.ajax({ + url: `/products/${id}`, + type: 'PUT', + contentType: 'application/json', + data: JSON.stringify(product) + }); + } else { + await $.ajax({ + url: '/products', + type: 'POST', + contentType: 'application/json', + data: JSON.stringify(product) + }); + } + $('#productModal').modal('hide'); + await loadProducts(); + } catch (error) { + console.error('Error saving product:', error); } - }); + } - $(document).on('click', '.btn-edit', function () { - const id = $(this).data('id'); - $.get(`/products/${id}`, function (data) { + async function editProduct(id) { + try { + const data = await $.get(`/products/${id}`); $('#productId').val(data.id); $('#name').val(data.name); $('#price').val(data.price); $('#imageUrl').val(data.imageUrl); $('#productModal').modal('show'); - }); + } catch (error) { + console.error('Error editing product:', error); + } + } + + async function deleteProduct(id) { + try { + await $.ajax({ + url: `/products/${id}`, + type: 'DELETE' + }); + await loadProducts(); + } catch (error) { + console.error('Error deleting product:', error); + } + } + + $('#addNewProduct').click(function () { + $('#productForm')[0].reset(); + $('#productId').val(''); + $('#productModal').modal('show'); + }); + + $('#saveProduct').click(async function () { + await saveProduct(); + }); + + $(document).on('click', '.btn-edit', function () { + const id = $(this).data('id'); + editProduct(id); }); $(document).on('click', '.btn-delete', function () { const id = $(this).data('id'); - $.ajax({ - url: `/products/${id}`, - type: 'DELETE', - success: function () { - loadProducts(); - } - }); + deleteProduct(id); }); loadProducts(); From 62517f92cca00623c25f79ca210cec78b1518a8c Mon Sep 17 00:00:00 2001 From: momnpa333 Date: Thu, 27 Jun 2024 19:23:10 +0900 Subject: [PATCH 16/53] =?UTF-8?q?refact(productControllerTest.java):=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1=EC=9E=90=20=EB=B3=80=EA=B2=BD=EC=9C=BC?= =?UTF-8?q?=EB=A1=9C=20=EC=9D=B8=ED=95=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/gift/controller/ProductControllerTest.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/test/java/gift/controller/ProductControllerTest.java b/src/test/java/gift/controller/ProductControllerTest.java index 5b5b389d3..cf931fb77 100644 --- a/src/test/java/gift/controller/ProductControllerTest.java +++ b/src/test/java/gift/controller/ProductControllerTest.java @@ -40,8 +40,8 @@ void setUp() { void testGetProducts() throws Exception { // given: 테스트에 사용될 mock 데이터를 설정합니다. var products = Arrays.asList( - Product.create("Product1", 100, "http://image1.com"), - Product.create("Product2", 200, "http://image2.com") + Product.create(1L, "Product1", 100, "http://image1.com"), + Product.create(2L, "Product2", 200, "http://image2.com") ); // when: productDao.findAll() 메서드가 호출될 때 products를 반환하도록 설정합니다. when(productDao.findAll()).thenReturn(products); @@ -62,7 +62,7 @@ void testGetProducts() throws Exception { @Test void testGetProduct() throws Exception { - var product = Product.create("Product1", 100, "http://image1.com"); + var product = Product.create(1L, "Product1", 100, "http://image1.com"); when(productDao.findById(anyLong())).thenReturn(Optional.of(product)); mockMvc.perform(get("/products/{id}", 1L)) @@ -76,7 +76,7 @@ void testGetProduct() throws Exception { @Test void testCreateProduct() throws Exception { - var product = Product.create("Product1", 100, "http://image1.com"); + var product = Product.create(1L, "Product1", 100, "http://image1.com"); when(productDao.save(any(ProductRequest.class))).thenReturn(product); mockMvc.perform(post("/products") @@ -92,7 +92,7 @@ void testCreateProduct() throws Exception { @Test void testUpdateProduct() throws Exception { - var product = Product.update(1L, "UpdatedProduct", 150, "http://updatedimage.com"); + var product = Product.create(1L, "UpdatedProduct", 150, "http://updatedimage.com"); when(productDao.update(eq(1L), any(ProductRequest.class))).thenReturn(product); mockMvc.perform(put("/products/{id}", 1L) From f9d49c3cb95ff65165ee47cedbcc25c9b8476427 Mon Sep 17 00:00:00 2001 From: momnpa333 Date: Thu, 27 Jun 2024 19:23:49 +0900 Subject: [PATCH 17/53] =?UTF-8?q?chore(.gitkeep):=20gitkeep=20=EC=A0=9C?= =?UTF-8?q?=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/gift/.gitkeep | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 src/test/java/gift/.gitkeep diff --git a/src/test/java/gift/.gitkeep b/src/test/java/gift/.gitkeep deleted file mode 100644 index e69de29bb..000000000 From a82f2e24c8ef9f22a6f8e3d8360d310112c0128d Mon Sep 17 00:00:00 2001 From: momnpa333 Date: Thu, 27 Jun 2024 20:11:09 +0900 Subject: [PATCH 18/53] =?UTF-8?q?feat(Application.java):=20jdbcTemplate=20?= =?UTF-8?q?=EC=9D=98=EC=A1=B4=EC=84=B1=20=EC=A3=BC=EC=9E=85=20=EB=B0=8F=20?= =?UTF-8?q?=ED=85=8C=EC=9D=B4=EB=B8=94=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/gift/Application.java | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/main/java/gift/Application.java b/src/main/java/gift/Application.java index 61603cca0..90d0792ea 100644 --- a/src/main/java/gift/Application.java +++ b/src/main/java/gift/Application.java @@ -1,11 +1,28 @@ package gift; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.jdbc.core.JdbcTemplate; @SpringBootApplication -public class Application { +public class Application implements CommandLineRunner { + public static void main(String[] args) { SpringApplication.run(Application.class, args); } + + @Autowired + JdbcTemplate jdbcTemplate; + + @Override + public void run(String... strings) throws Exception { + + jdbcTemplate.execute("DROP TABLE customers IF EXISTS"); + jdbcTemplate.execute( + "CREATE TABLE products(id SERIAL, name VARCHAR(255), price INTEGER, imageUrl VARCHAR(255))"); + + + } } From dbb949495d49ca91fe2e1d0d7583080deb192423 Mon Sep 17 00:00:00 2001 From: momnpa333 Date: Thu, 27 Jun 2024 20:11:41 +0900 Subject: [PATCH 19/53] =?UTF-8?q?feat(application.properties):=20propertie?= =?UTF-8?q?s=20=EC=84=A4=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/application.properties | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 3d16b65f4..33c36b4e1 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1 +1,5 @@ spring.application.name=spring-gift +# h2-console ??? ?? +spring.h2.console.enabled=true +# db url +spring.datasource.url=jdbc:h2:mem:test \ No newline at end of file From 5264f0ef3a19ff75c4aea6ddfa0575fa48699b22 Mon Sep 17 00:00:00 2001 From: momnpa333 Date: Thu, 27 Jun 2024 20:12:26 +0900 Subject: [PATCH 20/53] =?UTF-8?q?fix(Product.java):=20=EC=83=9D=EC=84=B1?= =?UTF-8?q?=EC=9E=90=20=EC=A0=91=EA=B7=BC=EC=A7=80=EC=A0=95=EC=9E=90=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/gift/model/Product.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/gift/model/Product.java b/src/main/java/gift/model/Product.java index 220c9ada3..fde2ee75a 100644 --- a/src/main/java/gift/model/Product.java +++ b/src/main/java/gift/model/Product.java @@ -9,7 +9,7 @@ public class Product { private Integer price; private String imageUrl; - private Product(Long id, String name, Integer price, String imageUrl) { + public Product(Long id, String name, Integer price, String imageUrl) { this.id = id; this.name = name; this.price = price; From beb1782286e71aaa9128cb8b688638c981e79eaa Mon Sep 17 00:00:00 2001 From: momnpa333 Date: Thu, 27 Jun 2024 20:13:08 +0900 Subject: [PATCH 21/53] =?UTF-8?q?refact(ProductController.java):=20save=20?= =?UTF-8?q?=EC=9D=B8=EC=9E=90=20=EB=B3=80=EA=B2=BD=EC=97=90=20=EB=94=B0?= =?UTF-8?q?=EB=A5=B8=20=EC=BD=94=EB=93=9C=20refact?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/gift/controller/ProductController.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/gift/controller/ProductController.java b/src/main/java/gift/controller/ProductController.java index ff347a020..ee394087f 100644 --- a/src/main/java/gift/controller/ProductController.java +++ b/src/main/java/gift/controller/ProductController.java @@ -47,14 +47,14 @@ public ResponseEntity getProduct(@PathVariable("id") Long id) { @PostMapping("/products") public ResponseEntity createProduct(@RequestBody ProductRequest request) { - var response = productDao.save(request); + var response = productDao.save(request.toEntity()); return ResponseEntity.ok(response); } @PutMapping("/products/{id}") public ResponseEntity updateProduct(@PathVariable("id") Long id, @RequestBody ProductRequest request) { - var response = productDao.update(id, request); + var response = productDao.update(id, request.toEntity()); return ResponseEntity.ok(response); } From 677c141ba2643b8d61e8242c26e7ffd4c95cac3e Mon Sep 17 00:00:00 2001 From: momnpa333 Date: Thu, 27 Jun 2024 20:13:20 +0900 Subject: [PATCH 22/53] =?UTF-8?q?refact(ProductControllerTest.java):=20sav?= =?UTF-8?q?e=20=EC=9D=B8=EC=9E=90=20=EB=B3=80=EA=B2=BD=EC=97=90=20?= =?UTF-8?q?=EB=94=B0=EB=A5=B8=20=EC=BD=94=EB=93=9C=20refact?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/java/gift/controller/ProductControllerTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/test/java/gift/controller/ProductControllerTest.java b/src/test/java/gift/controller/ProductControllerTest.java index cf931fb77..05a917365 100644 --- a/src/test/java/gift/controller/ProductControllerTest.java +++ b/src/test/java/gift/controller/ProductControllerTest.java @@ -77,7 +77,7 @@ void testGetProduct() throws Exception { @Test void testCreateProduct() throws Exception { var product = Product.create(1L, "Product1", 100, "http://image1.com"); - when(productDao.save(any(ProductRequest.class))).thenReturn(product); + when(productDao.save(any(Product.class))).thenReturn(product); mockMvc.perform(post("/products") .contentType(MediaType.APPLICATION_JSON) @@ -87,13 +87,13 @@ void testCreateProduct() throws Exception { .andExpect(jsonPath("$.price").value(100)) .andExpect(jsonPath("$.imageUrl").value("http://image1.com")); - verify(productDao, times(1)).save(any(ProductRequest.class)); + verify(productDao, times(1)).save(any(Product.class)); } @Test void testUpdateProduct() throws Exception { var product = Product.create(1L, "UpdatedProduct", 150, "http://updatedimage.com"); - when(productDao.update(eq(1L), any(ProductRequest.class))).thenReturn(product); + when(productDao.update(eq(1L), any(Product.class))).thenReturn(product); mockMvc.perform(put("/products/{id}", 1L) .contentType(MediaType.APPLICATION_JSON) @@ -104,7 +104,7 @@ void testUpdateProduct() throws Exception { .andExpect(jsonPath("$.price").value(150)) .andExpect(jsonPath("$.imageUrl").value("http://updatedimage.com")); - verify(productDao, times(1)).update(eq(1L), any(ProductRequest.class)); + verify(productDao, times(1)).update(eq(1L), any(Product.class)); } @Test From d540c4b2d01e5d2272097cceb7a58ba7df844a11 Mon Sep 17 00:00:00 2001 From: momnpa333 Date: Thu, 27 Jun 2024 20:13:54 +0900 Subject: [PATCH 23/53] =?UTF-8?q?feat(ProductDao.java):=20dao=20=EC=9D=B8?= =?UTF-8?q?=ED=84=B0=ED=8E=98=EC=9D=B4=EC=8A=A4=20=EC=A0=9C=EC=9E=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/gift/model/ProductDao.java | 33 +++++------------------- 1 file changed, 6 insertions(+), 27 deletions(-) diff --git a/src/main/java/gift/model/ProductDao.java b/src/main/java/gift/model/ProductDao.java index 44d44069d..5dcf6f04a 100644 --- a/src/main/java/gift/model/ProductDao.java +++ b/src/main/java/gift/model/ProductDao.java @@ -1,40 +1,19 @@ package gift.model; -import gift.controller.ProductRequest; import java.util.List; -import java.util.Map; import java.util.Optional; -import java.util.concurrent.ConcurrentHashMap; import org.springframework.stereotype.Component; @Component -public class ProductDao { +public interface ProductDao { - private final Map database = new ConcurrentHashMap<>(); + Product save(Product product); - public Product save(ProductRequest request) { - Product product = Product.create(Long.valueOf(database.size() + 1), request.name(), - request.price(), request.imageUrl()); - database.put(product.getId(), product); - return product; - } + Optional findById(Long id); - public Optional findById(Long id) { - return Optional.ofNullable(database.get(id)); - } + List findAll(); - public List findAll() { - return List.copyOf(database.values()); - } - - public void deleteById(Long id) { - database.remove(id); - } - - public Product update(Long id, ProductRequest request) { - Product product = Product.create(id, request.name(), request.price(), request.imageUrl()); - database.replace(id, product); - return product; - } + void deleteById(Long id); + Product update(Long id, Product product); } From 5e5d08a162188c0a8444efc4a650eb54fcadb91a Mon Sep 17 00:00:00 2001 From: momnpa333 Date: Thu, 27 Jun 2024 20:14:40 +0900 Subject: [PATCH 24/53] =?UTF-8?q?feat(ProductDaoInMemoryImpl.java):=20daoI?= =?UTF-8?q?mpl=20=EC=A0=9C=EC=9E=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gift/model/ProductDaoInMemoryImpl.java | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 src/main/java/gift/model/ProductDaoInMemoryImpl.java diff --git a/src/main/java/gift/model/ProductDaoInMemoryImpl.java b/src/main/java/gift/model/ProductDaoInMemoryImpl.java new file mode 100644 index 000000000..b0ce90186 --- /dev/null +++ b/src/main/java/gift/model/ProductDaoInMemoryImpl.java @@ -0,0 +1,71 @@ +package gift.model; + +import java.util.List; +import java.util.Optional; +import javax.sql.DataSource; +import org.springframework.context.annotation.Primary; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.stereotype.Component; + +@Component +@Primary +public class ProductDaoInMemoryImpl implements ProductDao { + + private final JdbcTemplate jdbcTemplate; + + public ProductDaoInMemoryImpl(DataSource dataSource) { + this.jdbcTemplate = new JdbcTemplate(dataSource); + } + + @Override + public Product save(Product product) { + String sql = "INSERT INTO products(name, price, imageUrl) VALUES (?,?,?)"; + jdbcTemplate.update(sql, product.getName(), product.getPrice(), product.getImageUrl()); + return product; + } + + @Override + public Optional findById(Long id) { + String sql = "SELECT id, name, price, imageUrl FROM products WHERE id = ?"; + Product product = jdbcTemplate.queryForObject( + sql, new Object[]{id}, (resultSet, rowNum) -> { + Product p = new Product( + resultSet.getLong("id"), + resultSet.getString("name"), + resultSet.getInt("price"), + resultSet.getString("imageUrl") + ); + return p; + }); + return Optional.ofNullable(product); + } + + @Override + public List findAll() { + String sql = "SELECT id, name, price, imageUrl FROM products"; + List products = jdbcTemplate.query( + sql, (resultSet, rowNum) -> { + Product product = new Product( + resultSet.getLong("id"), + resultSet.getString("name"), + resultSet.getInt("price"), + resultSet.getString("imageUrl") + ); + return product; + }); + return products; + } + + @Override + public void deleteById(Long id) { + String sql = "DELETE FROM products WHERE id = ?"; + jdbcTemplate.update(sql, id); + } + + @Override + public Product update(Long id, Product product) { + String sql = "UPDATE products SET name = ?, price = ?, imageUrl = ? WHERE id = ?"; + jdbcTemplate.update(sql, product.getName(), product.getPrice(), product.getImageUrl(), id); + return product; + } +} From 03341bae48341fae7afacec16de2a28ba464ffdd Mon Sep 17 00:00:00 2001 From: momnpa333 Date: Thu, 27 Jun 2024 20:15:08 +0900 Subject: [PATCH 25/53] =?UTF-8?q?feat(ProductDaoMapImpl.java):=20=EC=9D=B4?= =?UTF-8?q?=EB=A6=84=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/gift/model/ProductDaoMapImpl.java | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/main/java/gift/model/ProductDaoMapImpl.java diff --git a/src/main/java/gift/model/ProductDaoMapImpl.java b/src/main/java/gift/model/ProductDaoMapImpl.java new file mode 100644 index 000000000..b827ec7a4 --- /dev/null +++ b/src/main/java/gift/model/ProductDaoMapImpl.java @@ -0,0 +1,41 @@ +package gift.model; + +import gift.controller.ProductRequest; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.concurrent.ConcurrentHashMap; +import org.springframework.stereotype.Component; + +@Component +public class ProductDaoMapImpl implements ProductDao { + + private final Map database = new ConcurrentHashMap<>(); + + @Override + public Product save(Product product) { + database.put(null, product); + return product; + } + + @Override + public Optional findById(Long id) { + return Optional.ofNullable(database.get(id)); + } + + @Override + public List findAll() { + return List.copyOf(database.values()); + } + + @Override + public void deleteById(Long id) { + database.remove(id); + } + + @Override + public Product update(Long id, Product request) { + return null; + } + +} From 2561f41c1978966823566f023190938f4a4987b7 Mon Sep 17 00:00:00 2001 From: momnpa333 Date: Thu, 27 Jun 2024 20:15:29 +0900 Subject: [PATCH 26/53] =?UTF-8?q?feat(ProductRequest.java):=20toEntity=20?= =?UTF-8?q?=EB=A9=94=EC=84=9C=EB=93=9C=20=EC=A0=9C=EC=9E=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/gift/controller/ProductRequest.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/main/java/gift/controller/ProductRequest.java b/src/main/java/gift/controller/ProductRequest.java index 4b89a6d59..6cc092abc 100644 --- a/src/main/java/gift/controller/ProductRequest.java +++ b/src/main/java/gift/controller/ProductRequest.java @@ -1,10 +1,19 @@ package gift.controller; +import gift.model.Product; + public record ProductRequest( String name, Integer price, String imageUrl ) { + public Product toEntity() { + return Product.create(null, name(), price(), imageUrl()); + } + + public Product toEntity(Long id) { + return Product.create(id, name(), price(), imageUrl()); + } } From b40b8d23c8889760b508cab7b73f4c7b2653490a Mon Sep 17 00:00:00 2001 From: momnpa333 Date: Thu, 27 Jun 2024 20:28:55 +0900 Subject: [PATCH 27/53] =?UTF-8?q?feat(ProductController.java):=20api=20?= =?UTF-8?q?=EC=8A=A4=ED=8E=99=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/gift/controller/ProductController.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/gift/controller/ProductController.java b/src/main/java/gift/controller/ProductController.java index ee394087f..b3030fc8a 100644 --- a/src/main/java/gift/controller/ProductController.java +++ b/src/main/java/gift/controller/ProductController.java @@ -47,15 +47,15 @@ public ResponseEntity getProduct(@PathVariable("id") Long id) { @PostMapping("/products") public ResponseEntity createProduct(@RequestBody ProductRequest request) { - var response = productDao.save(request.toEntity()); - return ResponseEntity.ok(response); + productDao.save(request.toEntity()); + return ResponseEntity.noContent().build(); } @PutMapping("/products/{id}") public ResponseEntity updateProduct(@PathVariable("id") Long id, @RequestBody ProductRequest request) { - var response = productDao.update(id, request.toEntity()); - return ResponseEntity.ok(response); + productDao.update(id, request.toEntity(id)); + return ResponseEntity.noContent().build(); } @DeleteMapping("/products/{id}") From 355930f510642a9b121fec870ff706e5d5204a2e Mon Sep 17 00:00:00 2001 From: momnpa333 Date: Thu, 27 Jun 2024 20:29:21 +0900 Subject: [PATCH 28/53] =?UTF-8?q?feat(ProductDao.java):=20save,=20update?= =?UTF-8?q?=20=EB=B0=98=ED=99=98=EA=B0=92=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/gift/model/ProductDao.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/gift/model/ProductDao.java b/src/main/java/gift/model/ProductDao.java index 5dcf6f04a..d1e30c047 100644 --- a/src/main/java/gift/model/ProductDao.java +++ b/src/main/java/gift/model/ProductDao.java @@ -7,7 +7,7 @@ @Component public interface ProductDao { - Product save(Product product); + void save(Product product); Optional findById(Long id); @@ -15,5 +15,5 @@ public interface ProductDao { void deleteById(Long id); - Product update(Long id, Product product); + void update(Long id, Product product); } From 3abb10ddd2acc1a3eb10af31977d4b06152e62b1 Mon Sep 17 00:00:00 2001 From: momnpa333 Date: Thu, 27 Jun 2024 20:29:29 +0900 Subject: [PATCH 29/53] =?UTF-8?q?feat(ProductDao.java):=20save,=20update?= =?UTF-8?q?=20=EB=B0=98=ED=99=98=EA=B0=92=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gift/model/ProductDaoInMemoryImpl.java | 59 +++++++++---------- .../java/gift/model/ProductDaoMapImpl.java | 13 ++-- 2 files changed, 36 insertions(+), 36 deletions(-) diff --git a/src/main/java/gift/model/ProductDaoInMemoryImpl.java b/src/main/java/gift/model/ProductDaoInMemoryImpl.java index b0ce90186..13168ec92 100644 --- a/src/main/java/gift/model/ProductDaoInMemoryImpl.java +++ b/src/main/java/gift/model/ProductDaoInMemoryImpl.java @@ -8,9 +8,14 @@ import org.springframework.stereotype.Component; @Component -@Primary public class ProductDaoInMemoryImpl implements ProductDao { + private static final String SQL_INSERT = "INSERT INTO products(name, price, imageUrl) VALUES (?, ?, ?)"; + private static final String SQL_SELECT_BY_ID = "SELECT id, name, price, imageUrl FROM products WHERE id = ?"; + private static final String SQL_SELECT_ALL = "SELECT id, name, price, imageUrl FROM products"; + private static final String SQL_DELETE_BY_ID = "DELETE FROM products WHERE id = ?"; + private static final String SQL_UPDATE = "UPDATE products SET name = ?, price = ?, imageUrl = ? WHERE id = ?"; + private final JdbcTemplate jdbcTemplate; public ProductDaoInMemoryImpl(DataSource dataSource) { @@ -18,54 +23,46 @@ public ProductDaoInMemoryImpl(DataSource dataSource) { } @Override - public Product save(Product product) { - String sql = "INSERT INTO products(name, price, imageUrl) VALUES (?,?,?)"; - jdbcTemplate.update(sql, product.getName(), product.getPrice(), product.getImageUrl()); - return product; + public void save(Product product) { + jdbcTemplate.update(SQL_INSERT, product.getName(), product.getPrice(), + product.getImageUrl()); } @Override public Optional findById(Long id) { - String sql = "SELECT id, name, price, imageUrl FROM products WHERE id = ?"; - Product product = jdbcTemplate.queryForObject( - sql, new Object[]{id}, (resultSet, rowNum) -> { - Product p = new Product( + try { + Product product = jdbcTemplate.queryForObject(SQL_SELECT_BY_ID, + (resultSet, rowNum) -> new Product( resultSet.getLong("id"), resultSet.getString("name"), resultSet.getInt("price"), resultSet.getString("imageUrl") - ); - return p; - }); - return Optional.ofNullable(product); + ), id); + return Optional.of(product); + } catch (Exception e) { + return Optional.empty(); + } } @Override public List findAll() { - String sql = "SELECT id, name, price, imageUrl FROM products"; - List products = jdbcTemplate.query( - sql, (resultSet, rowNum) -> { - Product product = new Product( - resultSet.getLong("id"), - resultSet.getString("name"), - resultSet.getInt("price"), - resultSet.getString("imageUrl") - ); - return product; - }); - return products; + return jdbcTemplate.query(SQL_SELECT_ALL, + (resultSet, rowNum) -> new Product( + resultSet.getLong("id"), + resultSet.getString("name"), + resultSet.getInt("price"), + resultSet.getString("imageUrl") + )); } @Override public void deleteById(Long id) { - String sql = "DELETE FROM products WHERE id = ?"; - jdbcTemplate.update(sql, id); + jdbcTemplate.update(SQL_DELETE_BY_ID, id); } @Override - public Product update(Long id, Product product) { - String sql = "UPDATE products SET name = ?, price = ?, imageUrl = ? WHERE id = ?"; - jdbcTemplate.update(sql, product.getName(), product.getPrice(), product.getImageUrl(), id); - return product; + public void update(Long id, Product product) { + jdbcTemplate.update(SQL_UPDATE, product.getName(), product.getPrice(), + product.getImageUrl(), id); } } diff --git a/src/main/java/gift/model/ProductDaoMapImpl.java b/src/main/java/gift/model/ProductDaoMapImpl.java index b827ec7a4..29c3ed0bb 100644 --- a/src/main/java/gift/model/ProductDaoMapImpl.java +++ b/src/main/java/gift/model/ProductDaoMapImpl.java @@ -5,17 +5,20 @@ import java.util.Map; import java.util.Optional; import java.util.concurrent.ConcurrentHashMap; +import org.springframework.context.annotation.Primary; import org.springframework.stereotype.Component; @Component +@Primary public class ProductDaoMapImpl implements ProductDao { private final Map database = new ConcurrentHashMap<>(); @Override - public Product save(Product product) { - database.put(null, product); - return product; + public void save(Product product) { + Product newProduct = Product.create(Long.valueOf(database.size() + 1), product.getName(), + product.getPrice(), product.getImageUrl()); + database.put(newProduct.getId(), newProduct); } @Override @@ -34,8 +37,8 @@ public void deleteById(Long id) { } @Override - public Product update(Long id, Product request) { - return null; + public void update(Long id, Product product) { + database.replace(id, product); } } From 3cabdd5666f6a292157ea352ea6acf73845301de Mon Sep 17 00:00:00 2001 From: momnpa333 Date: Thu, 27 Jun 2024 20:45:51 +0900 Subject: [PATCH 30/53] =?UTF-8?q?feat(ProductController.java):=20save,=20u?= =?UTF-8?q?pdate=20=EB=B0=98=ED=99=98=EA=B0=92=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/gift/controller/ProductController.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/main/java/gift/controller/ProductController.java b/src/main/java/gift/controller/ProductController.java index b3030fc8a..86e2354cd 100644 --- a/src/main/java/gift/controller/ProductController.java +++ b/src/main/java/gift/controller/ProductController.java @@ -46,16 +46,17 @@ public ResponseEntity getProduct(@PathVariable("id") Long id) { } @PostMapping("/products") - public ResponseEntity createProduct(@RequestBody ProductRequest request) { + public ResponseEntity createProduct(@RequestBody ProductRequest request) { productDao.save(request.toEntity()); - return ResponseEntity.noContent().build(); + return ResponseEntity.ok().body("Product created successfully."); } + @PutMapping("/products/{id}") - public ResponseEntity updateProduct(@PathVariable("id") Long id, + public ResponseEntity updateProduct(@PathVariable("id") Long id, @RequestBody ProductRequest request) { productDao.update(id, request.toEntity(id)); - return ResponseEntity.noContent().build(); + return ResponseEntity.ok().body("Product updated successfully."); } @DeleteMapping("/products/{id}") From d516dacd150f97e83af7f00d34a685fcb03bc79d Mon Sep 17 00:00:00 2001 From: momnpa333 Date: Thu, 27 Jun 2024 21:12:40 +0900 Subject: [PATCH 31/53] =?UTF-8?q?delete(ProductControllerTest.java):=20sav?= =?UTF-8?q?e,=20update=20test=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ProductControllerTest.java | 33 +------------------ 1 file changed, 1 insertion(+), 32 deletions(-) diff --git a/src/test/java/gift/controller/ProductControllerTest.java b/src/test/java/gift/controller/ProductControllerTest.java index 05a917365..b0b18c81a 100644 --- a/src/test/java/gift/controller/ProductControllerTest.java +++ b/src/test/java/gift/controller/ProductControllerTest.java @@ -60,6 +60,7 @@ void testGetProducts() throws Exception { verify(productDao, times(1)).findAll(); } + @Test void testGetProduct() throws Exception { var product = Product.create(1L, "Product1", 100, "http://image1.com"); @@ -74,38 +75,6 @@ void testGetProduct() throws Exception { verify(productDao, times(1)).findById(anyLong()); } - @Test - void testCreateProduct() throws Exception { - var product = Product.create(1L, "Product1", 100, "http://image1.com"); - when(productDao.save(any(Product.class))).thenReturn(product); - - mockMvc.perform(post("/products") - .contentType(MediaType.APPLICATION_JSON) - .content("{\"name\":\"Product1\", \"price\":100, \"imageUrl\":\"http://image1.com\"}")) - .andExpect(status().isOk()) - .andExpect(jsonPath("$.name").value("Product1")) - .andExpect(jsonPath("$.price").value(100)) - .andExpect(jsonPath("$.imageUrl").value("http://image1.com")); - - verify(productDao, times(1)).save(any(Product.class)); - } - - @Test - void testUpdateProduct() throws Exception { - var product = Product.create(1L, "UpdatedProduct", 150, "http://updatedimage.com"); - when(productDao.update(eq(1L), any(Product.class))).thenReturn(product); - - mockMvc.perform(put("/products/{id}", 1L) - .contentType(MediaType.APPLICATION_JSON) - .content( - "{\"name\":\"UpdatedProduct\", \"price\":150, \"imageUrl\":\"http://updatedimage.com\"}")) - .andExpect(status().isOk()) - .andExpect(jsonPath("$.name").value("UpdatedProduct")) - .andExpect(jsonPath("$.price").value(150)) - .andExpect(jsonPath("$.imageUrl").value("http://updatedimage.com")); - - verify(productDao, times(1)).update(eq(1L), any(Product.class)); - } @Test void testDeleteProduct() throws Exception { From 92967b0dafea411f9e6f2c002a4fed28a32f5935 Mon Sep 17 00:00:00 2001 From: momnpa333 Date: Thu, 27 Jun 2024 21:31:50 +0900 Subject: [PATCH 32/53] =?UTF-8?q?feat(ProductRowMapper.java):=20RowMapper?= =?UTF-8?q?=20=EC=A0=9C=EC=9E=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/gift/model/ProductRowMapper.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/main/java/gift/model/ProductRowMapper.java diff --git a/src/main/java/gift/model/ProductRowMapper.java b/src/main/java/gift/model/ProductRowMapper.java new file mode 100644 index 000000000..0395484b8 --- /dev/null +++ b/src/main/java/gift/model/ProductRowMapper.java @@ -0,0 +1,19 @@ +package gift.model; + +import org.springframework.jdbc.core.RowMapper; +import java.sql.ResultSet; +import java.sql.SQLException; + +public class ProductRowMapper implements RowMapper { + + @Override + public Product mapRow(ResultSet resultSet, int rowNum) throws SQLException { + return new Product( + resultSet.getLong("id"), + resultSet.getString("name"), + resultSet.getInt("price"), + resultSet.getString("imageUrl") + ); + } +} + From 234fbfb65b1c057f5b546759a5161dad57e7ca63 Mon Sep 17 00:00:00 2001 From: momnpa333 Date: Thu, 27 Jun 2024 21:32:13 +0900 Subject: [PATCH 33/53] =?UTF-8?q?refeat(ProductDaoInMemoryImpl.java):=20Ro?= =?UTF-8?q?wMapper=20=EB=B0=98=EC=98=81=20refact?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/gift/model/ProductDaoInMemoryImpl.java | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/src/main/java/gift/model/ProductDaoInMemoryImpl.java b/src/main/java/gift/model/ProductDaoInMemoryImpl.java index 13168ec92..ec7fc5c23 100644 --- a/src/main/java/gift/model/ProductDaoInMemoryImpl.java +++ b/src/main/java/gift/model/ProductDaoInMemoryImpl.java @@ -5,6 +5,7 @@ import javax.sql.DataSource; import org.springframework.context.annotation.Primary; import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.RowMapper; import org.springframework.stereotype.Component; @Component @@ -17,6 +18,7 @@ public class ProductDaoInMemoryImpl implements ProductDao { private static final String SQL_UPDATE = "UPDATE products SET name = ?, price = ?, imageUrl = ? WHERE id = ?"; private final JdbcTemplate jdbcTemplate; + private final RowMapper productRowMapper = new ProductRowMapper(); public ProductDaoInMemoryImpl(DataSource dataSource) { this.jdbcTemplate = new JdbcTemplate(dataSource); @@ -32,12 +34,7 @@ public void save(Product product) { public Optional findById(Long id) { try { Product product = jdbcTemplate.queryForObject(SQL_SELECT_BY_ID, - (resultSet, rowNum) -> new Product( - resultSet.getLong("id"), - resultSet.getString("name"), - resultSet.getInt("price"), - resultSet.getString("imageUrl") - ), id); + productRowMapper, id); return Optional.of(product); } catch (Exception e) { return Optional.empty(); @@ -47,12 +44,7 @@ public Optional findById(Long id) { @Override public List findAll() { return jdbcTemplate.query(SQL_SELECT_ALL, - (resultSet, rowNum) -> new Product( - resultSet.getLong("id"), - resultSet.getString("name"), - resultSet.getInt("price"), - resultSet.getString("imageUrl") - )); + productRowMapper); } @Override From c450ce5ca8dae931be8c36e2b345c0f287b75572 Mon Sep 17 00:00:00 2001 From: momnpa333 Date: Thu, 27 Jun 2024 21:34:36 +0900 Subject: [PATCH 34/53] =?UTF-8?q?delete(ProductRowMapper.java):=20RowMappe?= =?UTF-8?q?r=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gift/model/ProductDaoInMemoryImpl.java | 7 ++++--- .../java/gift/model/ProductRowMapper.java | 19 ------------------- 2 files changed, 4 insertions(+), 22 deletions(-) delete mode 100644 src/main/java/gift/model/ProductRowMapper.java diff --git a/src/main/java/gift/model/ProductDaoInMemoryImpl.java b/src/main/java/gift/model/ProductDaoInMemoryImpl.java index ec7fc5c23..1086389d1 100644 --- a/src/main/java/gift/model/ProductDaoInMemoryImpl.java +++ b/src/main/java/gift/model/ProductDaoInMemoryImpl.java @@ -4,6 +4,7 @@ import java.util.Optional; import javax.sql.DataSource; import org.springframework.context.annotation.Primary; +import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import org.springframework.stereotype.Component; @@ -18,7 +19,7 @@ public class ProductDaoInMemoryImpl implements ProductDao { private static final String SQL_UPDATE = "UPDATE products SET name = ?, price = ?, imageUrl = ? WHERE id = ?"; private final JdbcTemplate jdbcTemplate; - private final RowMapper productRowMapper = new ProductRowMapper(); + private final RowMapper rowMapper = new BeanPropertyRowMapper<>(Product.class); public ProductDaoInMemoryImpl(DataSource dataSource) { this.jdbcTemplate = new JdbcTemplate(dataSource); @@ -34,7 +35,7 @@ public void save(Product product) { public Optional findById(Long id) { try { Product product = jdbcTemplate.queryForObject(SQL_SELECT_BY_ID, - productRowMapper, id); + rowMapper, id); return Optional.of(product); } catch (Exception e) { return Optional.empty(); @@ -44,7 +45,7 @@ public Optional findById(Long id) { @Override public List findAll() { return jdbcTemplate.query(SQL_SELECT_ALL, - productRowMapper); + rowMapper); } @Override diff --git a/src/main/java/gift/model/ProductRowMapper.java b/src/main/java/gift/model/ProductRowMapper.java deleted file mode 100644 index 0395484b8..000000000 --- a/src/main/java/gift/model/ProductRowMapper.java +++ /dev/null @@ -1,19 +0,0 @@ -package gift.model; - -import org.springframework.jdbc.core.RowMapper; -import java.sql.ResultSet; -import java.sql.SQLException; - -public class ProductRowMapper implements RowMapper { - - @Override - public Product mapRow(ResultSet resultSet, int rowNum) throws SQLException { - return new Product( - resultSet.getLong("id"), - resultSet.getString("name"), - resultSet.getInt("price"), - resultSet.getString("imageUrl") - ); - } -} - From fe2d31c5ac4c88e6468ab14dbd9f8130487934cf Mon Sep 17 00:00:00 2001 From: momnpa333 Date: Fri, 28 Jun 2024 15:39:06 +0900 Subject: [PATCH 35/53] =?UTF-8?q?chore(Product.java):=20import=20=EB=AC=B8?= =?UTF-8?q?=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/gift/model/Product.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/gift/model/Product.java b/src/main/java/gift/model/Product.java index fde2ee75a..77be77252 100644 --- a/src/main/java/gift/model/Product.java +++ b/src/main/java/gift/model/Product.java @@ -1,6 +1,5 @@ package gift.model; -import java.util.UUID; public class Product { From 7f1e12db729d074e8fff9c387d22b820dc4dcaa7 Mon Sep 17 00:00:00 2001 From: momnpa333 Date: Fri, 28 Jun 2024 15:39:25 +0900 Subject: [PATCH 36/53] =?UTF-8?q?chore(ProductResponse.java):=20ResponseDt?= =?UTF-8?q?o=20=EC=A0=9C=EC=9E=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/gift/controller/ProductResponse.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 src/main/java/gift/controller/ProductResponse.java diff --git a/src/main/java/gift/controller/ProductResponse.java b/src/main/java/gift/controller/ProductResponse.java new file mode 100644 index 000000000..f844be03b --- /dev/null +++ b/src/main/java/gift/controller/ProductResponse.java @@ -0,0 +1,16 @@ +package gift.controller; + +import gift.model.Product; + +public record ProductResponse( + Long id, + String name, + Integer price, + String imageUrl +) { + + public static ProductResponse from(Product product) { + return new ProductResponse(product.getId(), product.getName(), product.getPrice(), + product.getImageUrl()); + } +} From a91017206f73ef757938f410506d8f3053031be0 Mon Sep 17 00:00:00 2001 From: momnpa333 Date: Fri, 28 Jun 2024 15:39:55 +0900 Subject: [PATCH 37/53] =?UTF-8?q?feat(ProductController.java):=20ProductRe?= =?UTF-8?q?sponse=20=EB=B0=98=EC=98=81=20=EB=B0=8F=20=EC=98=88=EC=99=B8?= =?UTF-8?q?=EC=B2=98=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gift/controller/ProductController.java | 21 +++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/main/java/gift/controller/ProductController.java b/src/main/java/gift/controller/ProductController.java index 86e2354cd..14ad9b6e0 100644 --- a/src/main/java/gift/controller/ProductController.java +++ b/src/main/java/gift/controller/ProductController.java @@ -34,15 +34,20 @@ public String admin() { } @GetMapping("/products") - public ResponseEntity> getProducts() { - var response = productDao.findAll(); + public ResponseEntity> getProducts() { + var products = productDao.findAll(); + var response = products.stream() + .map(ProductResponse::from) + .toList(); return ResponseEntity.ok(response); } @GetMapping("/products/{id}") - public ResponseEntity getProduct(@PathVariable("id") Long id) { - var response = productDao.findById(id); - return ResponseEntity.ok(response.orElse(null)); + public ResponseEntity getProduct(@PathVariable("id") Long id) { + var product = productDao.findById(id) + .orElseThrow(() -> new IllegalArgumentException("해당 상품이 존재하지 않습니다.")); + var response = ProductResponse.from(product); + return ResponseEntity.ok(response); } @PostMapping("/products") @@ -55,12 +60,16 @@ public ResponseEntity createProduct(@RequestBody ProductRequest request) @PutMapping("/products/{id}") public ResponseEntity updateProduct(@PathVariable("id") Long id, @RequestBody ProductRequest request) { - productDao.update(id, request.toEntity(id)); + productDao.findById(id) + .orElseThrow(() -> new IllegalArgumentException("해당 상품이 존재하지 않습니다.")); + productDao.update(request.toEntity(id)); return ResponseEntity.ok().body("Product updated successfully."); } @DeleteMapping("/products/{id}") public ResponseEntity deleteProduct(@PathVariable("id") Long id) { + productDao.findById(id) + .orElseThrow(() -> new IllegalArgumentException("해당 상품이 존재하지 않습니다.")); productDao.deleteById(id); return ResponseEntity.noContent().build(); } From a6e717ff5c59edaca427f2c59434f7e2f1f20614 Mon Sep 17 00:00:00 2001 From: momnpa333 Date: Fri, 28 Jun 2024 15:43:26 +0900 Subject: [PATCH 38/53] =?UTF-8?q?feat(ProductJdbcTemplateDao.java):=20?= =?UTF-8?q?=EB=84=A4=EC=9D=B4=EB=B0=8D=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...moryImpl.java => ProductJdbcTemplateDao.java} | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) rename src/main/java/gift/model/{ProductDaoInMemoryImpl.java => ProductJdbcTemplateDao.java} (80%) diff --git a/src/main/java/gift/model/ProductDaoInMemoryImpl.java b/src/main/java/gift/model/ProductJdbcTemplateDao.java similarity index 80% rename from src/main/java/gift/model/ProductDaoInMemoryImpl.java rename to src/main/java/gift/model/ProductJdbcTemplateDao.java index 1086389d1..4da25ca80 100644 --- a/src/main/java/gift/model/ProductDaoInMemoryImpl.java +++ b/src/main/java/gift/model/ProductJdbcTemplateDao.java @@ -4,13 +4,13 @@ import java.util.Optional; import javax.sql.DataSource; import org.springframework.context.annotation.Primary; -import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import org.springframework.stereotype.Component; @Component -public class ProductDaoInMemoryImpl implements ProductDao { +@Primary +public class ProductJdbcTemplateDao implements ProductDao { private static final String SQL_INSERT = "INSERT INTO products(name, price, imageUrl) VALUES (?, ?, ?)"; private static final String SQL_SELECT_BY_ID = "SELECT id, name, price, imageUrl FROM products WHERE id = ?"; @@ -19,9 +19,9 @@ public class ProductDaoInMemoryImpl implements ProductDao { private static final String SQL_UPDATE = "UPDATE products SET name = ?, price = ?, imageUrl = ? WHERE id = ?"; private final JdbcTemplate jdbcTemplate; - private final RowMapper rowMapper = new BeanPropertyRowMapper<>(Product.class); + private final RowMapper productRowMapper = new ProductRowMapper(); - public ProductDaoInMemoryImpl(DataSource dataSource) { + public ProductJdbcTemplateDao(DataSource dataSource) { this.jdbcTemplate = new JdbcTemplate(dataSource); } @@ -35,7 +35,7 @@ public void save(Product product) { public Optional findById(Long id) { try { Product product = jdbcTemplate.queryForObject(SQL_SELECT_BY_ID, - rowMapper, id); + productRowMapper, id); return Optional.of(product); } catch (Exception e) { return Optional.empty(); @@ -45,7 +45,7 @@ public Optional findById(Long id) { @Override public List findAll() { return jdbcTemplate.query(SQL_SELECT_ALL, - rowMapper); + productRowMapper); } @Override @@ -54,8 +54,8 @@ public void deleteById(Long id) { } @Override - public void update(Long id, Product product) { + public void update(Product product) { jdbcTemplate.update(SQL_UPDATE, product.getName(), product.getPrice(), - product.getImageUrl(), id); + product.getImageUrl(), product.getId()); } } From d6c3e48b1fe697f7823176a1ecd4611ca0053f09 Mon Sep 17 00:00:00 2001 From: momnpa333 Date: Fri, 28 Jun 2024 15:43:47 +0900 Subject: [PATCH 39/53] =?UTF-8?q?feat(ProductDao.java):=20update=20?= =?UTF-8?q?=EB=A9=94=EC=84=9C=EB=93=9C=20=EC=9D=B8=EC=9E=90=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/gift/model/ProductDao.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/gift/model/ProductDao.java b/src/main/java/gift/model/ProductDao.java index d1e30c047..45cb5e963 100644 --- a/src/main/java/gift/model/ProductDao.java +++ b/src/main/java/gift/model/ProductDao.java @@ -15,5 +15,5 @@ public interface ProductDao { void deleteById(Long id); - void update(Long id, Product product); + void update(Product product); } From 86e28f914229955620170640571de0aacc0816be Mon Sep 17 00:00:00 2001 From: momnpa333 Date: Fri, 28 Jun 2024 15:44:05 +0900 Subject: [PATCH 40/53] =?UTF-8?q?feat(ProductMapDao.java):=20=EB=84=A4?= =?UTF-8?q?=EC=9D=B4=EB=B0=8D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../model/{ProductDaoMapImpl.java => ProductMapDao.java} | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) rename src/main/java/gift/model/{ProductDaoMapImpl.java => ProductMapDao.java} (82%) diff --git a/src/main/java/gift/model/ProductDaoMapImpl.java b/src/main/java/gift/model/ProductMapDao.java similarity index 82% rename from src/main/java/gift/model/ProductDaoMapImpl.java rename to src/main/java/gift/model/ProductMapDao.java index 29c3ed0bb..f54c6ef48 100644 --- a/src/main/java/gift/model/ProductDaoMapImpl.java +++ b/src/main/java/gift/model/ProductMapDao.java @@ -1,6 +1,5 @@ package gift.model; -import gift.controller.ProductRequest; import java.util.List; import java.util.Map; import java.util.Optional; @@ -9,8 +8,7 @@ import org.springframework.stereotype.Component; @Component -@Primary -public class ProductDaoMapImpl implements ProductDao { +public class ProductMapDao implements ProductDao { private final Map database = new ConcurrentHashMap<>(); @@ -37,8 +35,8 @@ public void deleteById(Long id) { } @Override - public void update(Long id, Product product) { - database.replace(id, product); + public void update(Product product) { + database.replace(product.getId(), product); } } From 21f0a4ee8f88025a0c7782fa45e4c3b58e10984a Mon Sep 17 00:00:00 2001 From: momnpa333 Date: Fri, 28 Jun 2024 15:44:21 +0900 Subject: [PATCH 41/53] =?UTF-8?q?feat(ProductRowMapper.java):=20rowMapper?= =?UTF-8?q?=20=EC=A0=9C=EC=9E=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/gift/model/ProductRowMapper.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 src/main/java/gift/model/ProductRowMapper.java diff --git a/src/main/java/gift/model/ProductRowMapper.java b/src/main/java/gift/model/ProductRowMapper.java new file mode 100644 index 000000000..3c54e9db6 --- /dev/null +++ b/src/main/java/gift/model/ProductRowMapper.java @@ -0,0 +1,18 @@ +package gift.model; + +import org.springframework.jdbc.core.RowMapper; +import java.sql.ResultSet; +import java.sql.SQLException; + +public class ProductRowMapper implements RowMapper { + + @Override + public Product mapRow(ResultSet resultSet, int rowNum) throws SQLException { + return new Product( + resultSet.getLong("id"), + resultSet.getString("name"), + resultSet.getInt("price"), + resultSet.getString("imageUrl") + ); + } +} \ No newline at end of file From 611b87efd31f34d056cc7af531c5373133b6c0ee Mon Sep 17 00:00:00 2001 From: momnpa333 Date: Fri, 28 Jun 2024 17:44:50 +0900 Subject: [PATCH 42/53] =?UTF-8?q?feat(adminPage.html):=20=ED=8E=98?= =?UTF-8?q?=EC=9D=B4=EC=A7=95=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/templates/adminPage.html | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/main/resources/templates/adminPage.html b/src/main/resources/templates/adminPage.html index 6e39a0886..ae1aaf70d 100644 --- a/src/main/resources/templates/adminPage.html +++ b/src/main/resources/templates/adminPage.html @@ -25,6 +25,13 @@

Manage Products

+ @@ -61,5 +68,6 @@ + From bd260f19a9c9d3c1af30f29759ba34d46149433d Mon Sep 17 00:00:00 2001 From: momnpa333 Date: Fri, 28 Jun 2024 17:45:19 +0900 Subject: [PATCH 43/53] =?UTF-8?q?refact(Application.java):=20jdbcTemplate?= =?UTF-8?q?=20=EC=9D=98=EC=A1=B4=EC=84=B1=20=EC=9C=84=EC=B9=98=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/gift/Application.java | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) diff --git a/src/main/java/gift/Application.java b/src/main/java/gift/Application.java index 90d0792ea..992d7a56c 100644 --- a/src/main/java/gift/Application.java +++ b/src/main/java/gift/Application.java @@ -1,28 +1,14 @@ package gift; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.jdbc.core.JdbcTemplate; + @SpringBootApplication -public class Application implements CommandLineRunner { +public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } - @Autowired - JdbcTemplate jdbcTemplate; - - @Override - public void run(String... strings) throws Exception { - - jdbcTemplate.execute("DROP TABLE customers IF EXISTS"); - jdbcTemplate.execute( - "CREATE TABLE products(id SERIAL, name VARCHAR(255), price INTEGER, imageUrl VARCHAR(255))"); - - - } } From fd1a3171e329600c46e3812122e2e0936a9ffb13 Mon Sep 17 00:00:00 2001 From: momnpa333 Date: Fri, 28 Jun 2024 17:45:23 +0900 Subject: [PATCH 44/53] =?UTF-8?q?refact(Application.java):=20jdbcTemplate?= =?UTF-8?q?=20=EC=9D=98=EC=A1=B4=EC=84=B1=20=EC=9C=84=EC=B9=98=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/application.properties | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 33c36b4e1..765bcf482 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -2,4 +2,7 @@ spring.application.name=spring-gift # h2-console ??? ?? spring.h2.console.enabled=true # db url -spring.datasource.url=jdbc:h2:mem:test \ No newline at end of file +spring.datasource.url=jdbc:h2:mem:test +spring.sql.init.schema-locations=classpath:/static/sql/schema.sql +spring.sql.init.data-locations=classpath:/static/sql/data.sql +spring.sql.init.mode=always \ No newline at end of file From 5d2b03084ec763c518a1486d37d97dc29c6f6d21 Mon Sep 17 00:00:00 2001 From: momnpa333 Date: Fri, 28 Jun 2024 17:45:41 +0900 Subject: [PATCH 45/53] =?UTF-8?q?feat(data.sql):=20=EB=8D=94=EB=AF=B8?= =?UTF-8?q?=EB=8D=B0=EC=9D=B4=ED=84=B0=20=EC=A0=9C=EC=9E=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/static/sql/data.sql | 200 +++++++++++++++++++++++++ 1 file changed, 200 insertions(+) create mode 100644 src/main/resources/static/sql/data.sql diff --git a/src/main/resources/static/sql/data.sql b/src/main/resources/static/sql/data.sql new file mode 100644 index 000000000..38b64ef16 --- /dev/null +++ b/src/main/resources/static/sql/data.sql @@ -0,0 +1,200 @@ +insert into products (id, name, price, image_url) +values (1, 'Isadore', 8222, 'http://dummyimage.com/171x100.png/5fa2dd/ffffff'); +insert into products (id, name, price, image_url) +values (2, 'Mitchael', 3575, 'http://dummyimage.com/242x100.png/dddddd/000000'); +insert into products (id, name, price, image_url) +values (3, 'Hamil', 2859, 'http://dummyimage.com/203x100.png/dddddd/000000'); +insert into products (id, name, price, image_url) +values (4, 'Worthington', 3733, 'http://dummyimage.com/236x100.png/dddddd/000000'); +insert into products (id, name, price, image_url) +values (5, 'Tanney', 49, 'http://dummyimage.com/161x100.png/ff4444/ffffff'); +insert into products (id, name, price, image_url) +values (6, 'Dianemarie', 1020, 'http://dummyimage.com/166x100.png/ff4444/ffffff'); +insert into products (id, name, price, image_url) +values (7, 'Emmet', 1923, 'http://dummyimage.com/237x100.png/ff4444/ffffff'); +insert into products (id, name, price, image_url) +values (8, 'Kirsti', 309, 'http://dummyimage.com/167x100.png/5fa2dd/ffffff'); +insert into products (id, name, price, image_url) +values (9, 'Avie', 7664, 'http://dummyimage.com/204x100.png/dddddd/000000'); +insert into products (id, name, price, image_url) +values (10, 'Jeanne', 1395, 'http://dummyimage.com/218x100.png/cc0000/ffffff'); +insert into products (id, name, price, image_url) +values (11, 'Stefano', 9184, 'http://dummyimage.com/219x100.png/dddddd/000000'); +insert into products (id, name, price, image_url) +values (12, 'Ossie', 7026, 'http://dummyimage.com/173x100.png/dddddd/000000'); +insert into products (id, name, price, image_url) +values (13, 'Quinton', 3273, 'http://dummyimage.com/239x100.png/cc0000/ffffff'); +insert into products (id, name, price, image_url) +values (14, 'Violante', 5655, 'http://dummyimage.com/206x100.png/ff4444/ffffff'); +insert into products (id, name, price, image_url) +values (15, 'Arda', 7270, 'http://dummyimage.com/138x100.png/dddddd/000000'); +insert into products (id, name, price, image_url) +values (16, 'Bale', 3985, 'http://dummyimage.com/237x100.png/ff4444/ffffff'); +insert into products (id, name, price, image_url) +values (17, 'Nanine', 8631, 'http://dummyimage.com/206x100.png/cc0000/ffffff'); +insert into products (id, name, price, image_url) +values (18, 'York', 3355, 'http://dummyimage.com/231x100.png/ff4444/ffffff'); +insert into products (id, name, price, image_url) +values (19, 'Trenna', 8240, 'http://dummyimage.com/203x100.png/cc0000/ffffff'); +insert into products (id, name, price, image_url) +values (20, 'Elysha', 4475, 'http://dummyimage.com/218x100.png/dddddd/000000'); +insert into products (id, name, price, image_url) +values (21, 'Meridith', 9933, 'http://dummyimage.com/239x100.png/5fa2dd/ffffff'); +insert into products (id, name, price, image_url) +values (22, 'Fraze', 2597, 'http://dummyimage.com/220x100.png/cc0000/ffffff'); +insert into products (id, name, price, image_url) +values (23, 'Erinn', 9300, 'http://dummyimage.com/216x100.png/ff4444/ffffff'); +insert into products (id, name, price, image_url) +values (24, 'Marshal', 6447, 'http://dummyimage.com/112x100.png/cc0000/ffffff'); +insert into products (id, name, price, image_url) +values (25, 'Mahala', 5109, 'http://dummyimage.com/224x100.png/dddddd/000000'); +insert into products (id, name, price, image_url) +values (26, 'Simone', 3614, 'http://dummyimage.com/152x100.png/dddddd/000000'); +insert into products (id, name, price, image_url) +values (27, 'Karmen', 1540, 'http://dummyimage.com/114x100.png/dddddd/000000'); +insert into products (id, name, price, image_url) +values (28, 'Allissa', 2966, 'http://dummyimage.com/106x100.png/5fa2dd/ffffff'); +insert into products (id, name, price, image_url) +values (29, 'Pammi', 2017, 'http://dummyimage.com/142x100.png/ff4444/ffffff'); +insert into products (id, name, price, image_url) +values (30, 'Wilbur', 6458, 'http://dummyimage.com/128x100.png/ff4444/ffffff'); +insert into products (id, name, price, image_url) +values (31, 'Stearn', 7902, 'http://dummyimage.com/249x100.png/cc0000/ffffff'); +insert into products (id, name, price, image_url) +values (32, 'Percival', 8595, 'http://dummyimage.com/210x100.png/dddddd/000000'); +insert into products (id, name, price, image_url) +values (33, 'Nickolaus', 8772, 'http://dummyimage.com/164x100.png/cc0000/ffffff'); +insert into products (id, name, price, image_url) +values (34, 'Toma', 4316, 'http://dummyimage.com/128x100.png/dddddd/000000'); +insert into products (id, name, price, image_url) +values (35, 'Basile', 530, 'http://dummyimage.com/148x100.png/ff4444/ffffff'); +insert into products (id, name, price, image_url) +values (36, 'Erek', 8139, 'http://dummyimage.com/102x100.png/dddddd/000000'); +insert into products (id, name, price, image_url) +values (37, 'Burty', 4417, 'http://dummyimage.com/175x100.png/5fa2dd/ffffff'); +insert into products (id, name, price, image_url) +values (38, 'Layne', 6295, 'http://dummyimage.com/193x100.png/dddddd/000000'); +insert into products (id, name, price, image_url) +values (39, 'Denny', 575, 'http://dummyimage.com/237x100.png/cc0000/ffffff'); +insert into products (id, name, price, image_url) +values (40, 'Dory', 7803, 'http://dummyimage.com/203x100.png/dddddd/000000'); +insert into products (id, name, price, image_url) +values (41, 'Dale', 2828, 'http://dummyimage.com/108x100.png/dddddd/000000'); +insert into products (id, name, price, image_url) +values (42, 'Bev', 1545, 'http://dummyimage.com/165x100.png/ff4444/ffffff'); +insert into products (id, name, price, image_url) +values (43, 'Laurena', 8045, 'http://dummyimage.com/212x100.png/ff4444/ffffff'); +insert into products (id, name, price, image_url) +values (44, 'Darnall', 7511, 'http://dummyimage.com/152x100.png/5fa2dd/ffffff'); +insert into products (id, name, price, image_url) +values (45, 'Keir', 2008, 'http://dummyimage.com/211x100.png/5fa2dd/ffffff'); +insert into products (id, name, price, image_url) +values (46, 'Ellyn', 5201, 'http://dummyimage.com/212x100.png/5fa2dd/ffffff'); +insert into products (id, name, price, image_url) +values (47, 'Jakob', 7654, 'http://dummyimage.com/174x100.png/dddddd/000000'); +insert into products (id, name, price, image_url) +values (48, 'Marillin', 3925, 'http://dummyimage.com/130x100.png/5fa2dd/ffffff'); +insert into products (id, name, price, image_url) +values (49, 'Sybyl', 3241, 'http://dummyimage.com/241x100.png/cc0000/ffffff'); +insert into products (id, name, price, image_url) +values (50, 'Emile', 3765, 'http://dummyimage.com/123x100.png/dddddd/000000'); +insert into products (id, name, price, image_url) +values (51, 'Stacey', 5708, 'http://dummyimage.com/132x100.png/ff4444/ffffff'); +insert into products (id, name, price, image_url) +values (52, 'Eveline', 9398, 'http://dummyimage.com/132x100.png/ff4444/ffffff'); +insert into products (id, name, price, image_url) +values (53, 'Clemens', 4271, 'http://dummyimage.com/191x100.png/dddddd/000000'); +insert into products (id, name, price, image_url) +values (54, 'Giorgia', 2913, 'http://dummyimage.com/234x100.png/dddddd/000000'); +insert into products (id, name, price, image_url) +values (55, 'Cele', 3178, 'http://dummyimage.com/172x100.png/dddddd/000000'); +insert into products (id, name, price, image_url) +values (56, 'Gretal', 3361, 'http://dummyimage.com/209x100.png/cc0000/ffffff'); +insert into products (id, name, price, image_url) +values (57, 'Enrika', 1231, 'http://dummyimage.com/179x100.png/5fa2dd/ffffff'); +insert into products (id, name, price, image_url) +values (58, 'Chad', 4213, 'http://dummyimage.com/111x100.png/ff4444/ffffff'); +insert into products (id, name, price, image_url) +values (59, 'Merci', 91, 'http://dummyimage.com/163x100.png/5fa2dd/ffffff'); +insert into products (id, name, price, image_url) +values (60, 'Susana', 9044, 'http://dummyimage.com/209x100.png/cc0000/ffffff'); +insert into products (id, name, price, image_url) +values (61, 'Dael', 2004, 'http://dummyimage.com/152x100.png/dddddd/000000'); +insert into products (id, name, price, image_url) +values (62, 'Gris', 8856, 'http://dummyimage.com/233x100.png/ff4444/ffffff'); +insert into products (id, name, price, image_url) +values (63, 'Henry', 2140, 'http://dummyimage.com/213x100.png/ff4444/ffffff'); +insert into products (id, name, price, image_url) +values (64, 'Kim', 1615, 'http://dummyimage.com/151x100.png/5fa2dd/ffffff'); +insert into products (id, name, price, image_url) +values (65, 'Ealasaid', 6515, 'http://dummyimage.com/114x100.png/dddddd/000000'); +insert into products (id, name, price, image_url) +values (66, 'Edik', 3560, 'http://dummyimage.com/222x100.png/ff4444/ffffff'); +insert into products (id, name, price, image_url) +values (67, 'Guilbert', 9291, 'http://dummyimage.com/175x100.png/dddddd/000000'); +insert into products (id, name, price, image_url) +values (68, 'Raimundo', 2177, 'http://dummyimage.com/119x100.png/ff4444/ffffff'); +insert into products (id, name, price, image_url) +values (69, 'Catherin', 8755, 'http://dummyimage.com/206x100.png/dddddd/000000'); +insert into products (id, name, price, image_url) +values (70, 'Denny', 9993, 'http://dummyimage.com/115x100.png/5fa2dd/ffffff'); +insert into products (id, name, price, image_url) +values (71, 'Lynne', 9758, 'http://dummyimage.com/188x100.png/dddddd/000000'); +insert into products (id, name, price, image_url) +values (72, 'Averell', 6403, 'http://dummyimage.com/232x100.png/cc0000/ffffff'); +insert into products (id, name, price, image_url) +values (73, 'Auguste', 595, 'http://dummyimage.com/208x100.png/ff4444/ffffff'); +insert into products (id, name, price, image_url) +values (74, 'Addison', 1952, 'http://dummyimage.com/160x100.png/5fa2dd/ffffff'); +insert into products (id, name, price, image_url) +values (75, 'Adda', 7787, 'http://dummyimage.com/127x100.png/ff4444/ffffff'); +insert into products (id, name, price, image_url) +values (76, 'Neilla', 6828, 'http://dummyimage.com/227x100.png/dddddd/000000'); +insert into products (id, name, price, image_url) +values (77, 'Jennie', 1503, 'http://dummyimage.com/248x100.png/5fa2dd/ffffff'); +insert into products (id, name, price, image_url) +values (78, 'Burty', 9211, 'http://dummyimage.com/168x100.png/ff4444/ffffff'); +insert into products (id, name, price, image_url) +values (79, 'Gris', 456, 'http://dummyimage.com/148x100.png/dddddd/000000'); +insert into products (id, name, price, image_url) +values (80, 'Benjie', 9058, 'http://dummyimage.com/110x100.png/cc0000/ffffff'); +insert into products (id, name, price, image_url) +values (81, 'Amberly', 8391, 'http://dummyimage.com/111x100.png/5fa2dd/ffffff'); +insert into products (id, name, price, image_url) +values (82, 'Stavros', 8825, 'http://dummyimage.com/228x100.png/ff4444/ffffff'); +insert into products (id, name, price, image_url) +values (83, 'Lavinia', 1044, 'http://dummyimage.com/202x100.png/cc0000/ffffff'); +insert into products (id, name, price, image_url) +values (84, 'Henrieta', 5124, 'http://dummyimage.com/139x100.png/cc0000/ffffff'); +insert into products (id, name, price, image_url) +values (85, 'Carly', 6724, 'http://dummyimage.com/124x100.png/5fa2dd/ffffff'); +insert into products (id, name, price, image_url) +values (86, 'Alaster', 8054, 'http://dummyimage.com/159x100.png/5fa2dd/ffffff'); +insert into products (id, name, price, image_url) +values (87, 'Novelia', 223, 'http://dummyimage.com/195x100.png/ff4444/ffffff'); +insert into products (id, name, price, image_url) +values (88, 'Etta', 9076, 'http://dummyimage.com/237x100.png/ff4444/ffffff'); +insert into products (id, name, price, image_url) +values (89, 'Eustacia', 598, 'http://dummyimage.com/134x100.png/dddddd/000000'); +insert into products (id, name, price, image_url) +values (90, 'Ronnie', 9594, 'http://dummyimage.com/219x100.png/cc0000/ffffff'); +insert into products (id, name, price, image_url) +values (91, 'Annadiana', 4606, 'http://dummyimage.com/144x100.png/ff4444/ffffff'); +insert into products (id, name, price, image_url) +values (92, 'Tommy', 6141, 'http://dummyimage.com/211x100.png/dddddd/000000'); +insert into products (id, name, price, image_url) +values (93, 'Wesley', 8117, 'http://dummyimage.com/210x100.png/5fa2dd/ffffff'); +insert into products (id, name, price, image_url) +values (94, 'Zeke', 9996, 'http://dummyimage.com/233x100.png/ff4444/ffffff'); +insert into products (id, name, price, image_url) +values (95, 'Haslett', 7337, 'http://dummyimage.com/162x100.png/cc0000/ffffff'); +insert into products (id, name, price, image_url) +values (96, 'Lyndell', 3415, 'http://dummyimage.com/121x100.png/cc0000/ffffff'); +insert into products (id, name, price, image_url) +values (97, 'Flo', 1876, 'http://dummyimage.com/140x100.png/dddddd/000000'); +insert into products (id, name, price, image_url) +values (98, 'Barri', 894, 'http://dummyimage.com/111x100.png/dddddd/000000'); +insert into products (id, name, price, image_url) +values (99, 'Marybelle', 8184, 'http://dummyimage.com/222x100.png/cc0000/ffffff'); +insert into products (id, name, price, image_url) +values (100, 'Edgar', 5624, 'http://dummyimage.com/208x100.png/dddddd/000000'); From cb07c8fbdd65273659930877eb4f03c4a3473d86 Mon Sep 17 00:00:00 2001 From: momnpa333 Date: Fri, 28 Jun 2024 17:45:56 +0900 Subject: [PATCH 46/53] =?UTF-8?q?feat(schema.sql):=20=EC=8A=A4=ED=82=A4?= =?UTF-8?q?=EB=A7=88=20=EC=A0=9C=EC=9E=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/static/sql/schema.sql | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 src/main/resources/static/sql/schema.sql diff --git a/src/main/resources/static/sql/schema.sql b/src/main/resources/static/sql/schema.sql new file mode 100644 index 000000000..a9c6a948a --- /dev/null +++ b/src/main/resources/static/sql/schema.sql @@ -0,0 +1,6 @@ +create table `products` ( + `id` int auto_increment primary key, + `name` varchar(255) not null, + `price` int not null, + `image_url` varchar(255) not null +); \ No newline at end of file From 16cf41a39a8916d1a8ce98c1ce27699b2007df8c Mon Sep 17 00:00:00 2001 From: momnpa333 Date: Fri, 28 Jun 2024 17:46:32 +0900 Subject: [PATCH 47/53] =?UTF-8?q?fix(ProductRowMapper.java):=20column=20?= =?UTF-8?q?=EB=84=A4=EC=9D=B4=EB=B0=8D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/gift/model/ProductRowMapper.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/gift/model/ProductRowMapper.java b/src/main/java/gift/model/ProductRowMapper.java index 3c54e9db6..ca121cb46 100644 --- a/src/main/java/gift/model/ProductRowMapper.java +++ b/src/main/java/gift/model/ProductRowMapper.java @@ -12,7 +12,7 @@ public Product mapRow(ResultSet resultSet, int rowNum) throws SQLException { resultSet.getLong("id"), resultSet.getString("name"), resultSet.getInt("price"), - resultSet.getString("imageUrl") + resultSet.getString("image_url") ); } } \ No newline at end of file From 16fc2f4d7cd8b23e208bfd5267d284df938c0758 Mon Sep 17 00:00:00 2001 From: momnpa333 Date: Fri, 28 Jun 2024 17:47:14 +0900 Subject: [PATCH 48/53] =?UTF-8?q?feat(ProductJdbcTemplateDao.java):=20Pagi?= =?UTF-8?q?ng=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gift/model/ProductJdbcTemplateDao.java | 22 +++++++++++++++---- src/main/java/gift/model/ProductMapDao.java | 10 ++++++++- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/main/java/gift/model/ProductJdbcTemplateDao.java b/src/main/java/gift/model/ProductJdbcTemplateDao.java index 4da25ca80..bc8aed2f1 100644 --- a/src/main/java/gift/model/ProductJdbcTemplateDao.java +++ b/src/main/java/gift/model/ProductJdbcTemplateDao.java @@ -12,11 +12,14 @@ @Primary public class ProductJdbcTemplateDao implements ProductDao { - private static final String SQL_INSERT = "INSERT INTO products(name, price, imageUrl) VALUES (?, ?, ?)"; - private static final String SQL_SELECT_BY_ID = "SELECT id, name, price, imageUrl FROM products WHERE id = ?"; - private static final String SQL_SELECT_ALL = "SELECT id, name, price, imageUrl FROM products"; + private static final String SQL_INSERT = "INSERT INTO products(name, price, image_url) VALUES (?, ?, ?)"; + private static final String SQL_SELECT_BY_ID = "SELECT id, name, price, image_url FROM products WHERE id = ?"; + private static final String SQL_SELECT_ALL = "SELECT id, name, price, image_url FROM products"; private static final String SQL_DELETE_BY_ID = "DELETE FROM products WHERE id = ?"; - private static final String SQL_UPDATE = "UPDATE products SET name = ?, price = ?, imageUrl = ? WHERE id = ?"; + private static final String SQL_UPDATE = "UPDATE products SET name = ?, price = ?, image_url = ? WHERE id = ?"; + private static final String SQL_SELECT_PAGING = "SELECT id, name, price, image_url FROM products LIMIT ? OFFSET ?"; + private static final String SQL_COUNT = "SELECT COUNT(*) FROM products"; + private final JdbcTemplate jdbcTemplate; private final RowMapper productRowMapper = new ProductRowMapper(); @@ -58,4 +61,15 @@ public void update(Product product) { jdbcTemplate.update(SQL_UPDATE, product.getName(), product.getPrice(), product.getImageUrl(), product.getId()); } + + @Override + public List findPaging(int page, int size) { + int offset = (page) * size; + return jdbcTemplate.query(SQL_SELECT_PAGING, productRowMapper, size, offset); + } + + @Override + public Long count() { + return jdbcTemplate.queryForObject(SQL_COUNT, Long.class); + } } diff --git a/src/main/java/gift/model/ProductMapDao.java b/src/main/java/gift/model/ProductMapDao.java index f54c6ef48..740145fa3 100644 --- a/src/main/java/gift/model/ProductMapDao.java +++ b/src/main/java/gift/model/ProductMapDao.java @@ -4,7 +4,6 @@ import java.util.Map; import java.util.Optional; import java.util.concurrent.ConcurrentHashMap; -import org.springframework.context.annotation.Primary; import org.springframework.stereotype.Component; @Component @@ -39,4 +38,13 @@ public void update(Product product) { database.replace(product.getId(), product); } + @Override + public List findPaging(int page, int size) { + return null; + } + + @Override + public Long count() { + return Long.valueOf(database.size()); + } } From 07c17a8bb20d746e85a6b5856fb593a87fb330b3 Mon Sep 17 00:00:00 2001 From: momnpa333 Date: Fri, 28 Jun 2024 17:47:19 +0900 Subject: [PATCH 49/53] =?UTF-8?q?feat(ProductJdbcTemplateDao.java):=20Pagi?= =?UTF-8?q?ng=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/gift/model/ProductDao.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/gift/model/ProductDao.java b/src/main/java/gift/model/ProductDao.java index 45cb5e963..855429a03 100644 --- a/src/main/java/gift/model/ProductDao.java +++ b/src/main/java/gift/model/ProductDao.java @@ -16,4 +16,8 @@ public interface ProductDao { void deleteById(Long id); void update(Product product); + + List findPaging(int page, int size); + + Long count(); } From ed99804f0a6fba4953bc5abf3e768a41571dec51 Mon Sep 17 00:00:00 2001 From: momnpa333 Date: Fri, 28 Jun 2024 17:47:22 +0900 Subject: [PATCH 50/53] =?UTF-8?q?feat(ProductJdbcTemplateDao.java):=20Pagi?= =?UTF-8?q?ng=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gift/controller/ProductController.java | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/main/java/gift/controller/ProductController.java b/src/main/java/gift/controller/ProductController.java index 14ad9b6e0..196b3b9f4 100644 --- a/src/main/java/gift/controller/ProductController.java +++ b/src/main/java/gift/controller/ProductController.java @@ -13,6 +13,7 @@ import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestParam; @Controller public class ProductController { @@ -23,11 +24,6 @@ public ProductController(ProductDao productDao) { this.productDao = productDao; } - /** - * admin 페이지로 이동합니다. - * - * @return - */ @GetMapping("/admin") public String admin() { return "adminPage"; @@ -74,4 +70,18 @@ public ResponseEntity deleteProduct(@PathVariable("id") Long id) { return ResponseEntity.noContent().build(); } + @GetMapping("/products/paging") + public ResponseEntity> getProductsPaging(@RequestParam("page") int page, + @RequestParam("size") int size) { + var products = productDao.findPaging(page, size); + var response = products.stream() + .map(ProductResponse::from) + .toList(); + return ResponseEntity.ok(response); + } + + @GetMapping("/products/count") + public ResponseEntity getProductsCount() { + return ResponseEntity.ok(productDao.count()); + } } From 2ed0c50053d6b3af1e091e063f158b1fbffb70f2 Mon Sep 17 00:00:00 2001 From: momnpa333 Date: Fri, 28 Jun 2024 17:47:44 +0900 Subject: [PATCH 51/53] =?UTF-8?q?feat(ProductJdbcTemplateDao.java):=20Pagi?= =?UTF-8?q?ng=20=EA=B8=B0=EB=8A=A5=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/static/js/product.js | 91 +++++++++++++++++++++++-- 1 file changed, 84 insertions(+), 7 deletions(-) diff --git a/src/main/resources/static/js/product.js b/src/main/resources/static/js/product.js index d4cf51b72..67fb498d1 100644 --- a/src/main/resources/static/js/product.js +++ b/src/main/resources/static/js/product.js @@ -1,8 +1,11 @@ $(document).ready(function () { + let currentPage = 0; + const pageSize = 10; - async function loadProducts() { + async function loadProducts(page) { try { - const data = await $.get("/products"); + const data = await $.get( + `/products/paging?page=${page}&size=${pageSize}`); $('#productTableBody').empty(); data.forEach(function (product) { $('#productTableBody').append(` @@ -30,15 +33,16 @@ $(document).ready(function () { }; const id = $('#productId').val(); try { + let response; if (id) { - await $.ajax({ + response = await $.ajax({ url: `/products/${id}`, type: 'PUT', contentType: 'application/json', data: JSON.stringify(product) }); } else { - await $.ajax({ + response = await $.ajax({ url: '/products', type: 'POST', contentType: 'application/json', @@ -46,7 +50,7 @@ $(document).ready(function () { }); } $('#productModal').modal('hide'); - await loadProducts(); + await loadProducts(currentPage); } catch (error) { console.error('Error saving product:', error); } @@ -71,12 +75,58 @@ $(document).ready(function () { url: `/products/${id}`, type: 'DELETE' }); - await loadProducts(); + await loadProducts(currentPage); } catch (error) { console.error('Error deleting product:', error); } } + async function updatePaginationLinks(page) { + $('#pagination').empty(); + + if (page > 0) { + $('#pagination').append(` +
  • Previous
  • + `); + } else { + $('#pagination').append(` +
  • Previous
  • + `); + } + + try { + const totalElements = await $.get('/products/count'); // Fetch total product count from server + const totalPages = Math.ceil(totalElements / pageSize); + const startPage = Math.floor(page / 5) * 5; + const endPage = Math.min(startPage + 5, totalPages); + + for (let i = startPage; i < endPage; i++) { + if (i === page) { + $('#pagination').append(` +
  • ${i + 1}
  • + `); + } else { + $('#pagination').append(` +
  • ${i + + 1}
  • + `); + } + } + + if (endPage < totalPages) { + $('#pagination').append(` +
  • Next
  • + `); + } else { + $('#pagination').append(` +
  • Next
  • + `); + } + } catch (error) { + console.error('Error fetching total product count:', error); + } + } + $('#addNewProduct').click(function () { $('#productForm')[0].reset(); $('#productId').val(''); @@ -97,5 +147,32 @@ $(document).ready(function () { deleteProduct(id); }); - loadProducts(); + $(document).on('click', '.page-link', function (e) { + e.preventDefault(); + const page = parseInt($(this).data('page')); + if (!isNaN(page)) { + currentPage = page; + loadProducts(currentPage); + updatePaginationLinks(currentPage); + } + }); + + $(document).on('click', '#previousBlock', function () { + if (currentPage > 0) { + currentPage -= 5; + currentPage = parseInt(currentPage / 5) * 5; + loadProducts(currentPage); + updatePaginationLinks(currentPage); + } + }); + + $(document).on('click', '#nextBlock', function () { + currentPage += 5; + currentPage = parseInt(currentPage / 5) * 5; + loadProducts(currentPage); + updatePaginationLinks(currentPage); + }); + + loadProducts(currentPage); + updatePaginationLinks(currentPage); }); From 92b470b1042d105112d88eedfab6751b9bdf8980 Mon Sep 17 00:00:00 2001 From: momnpa333 Date: Fri, 28 Jun 2024 18:16:20 +0900 Subject: [PATCH 52/53] =?UTF-8?q?fix(data.sql):=20id=20=EC=82=AD=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/static/sql/data.sql | 400 ++++++++++++------------- 1 file changed, 200 insertions(+), 200 deletions(-) diff --git a/src/main/resources/static/sql/data.sql b/src/main/resources/static/sql/data.sql index 38b64ef16..61f94d6c0 100644 --- a/src/main/resources/static/sql/data.sql +++ b/src/main/resources/static/sql/data.sql @@ -1,200 +1,200 @@ -insert into products (id, name, price, image_url) -values (1, 'Isadore', 8222, 'http://dummyimage.com/171x100.png/5fa2dd/ffffff'); -insert into products (id, name, price, image_url) -values (2, 'Mitchael', 3575, 'http://dummyimage.com/242x100.png/dddddd/000000'); -insert into products (id, name, price, image_url) -values (3, 'Hamil', 2859, 'http://dummyimage.com/203x100.png/dddddd/000000'); -insert into products (id, name, price, image_url) -values (4, 'Worthington', 3733, 'http://dummyimage.com/236x100.png/dddddd/000000'); -insert into products (id, name, price, image_url) -values (5, 'Tanney', 49, 'http://dummyimage.com/161x100.png/ff4444/ffffff'); -insert into products (id, name, price, image_url) -values (6, 'Dianemarie', 1020, 'http://dummyimage.com/166x100.png/ff4444/ffffff'); -insert into products (id, name, price, image_url) -values (7, 'Emmet', 1923, 'http://dummyimage.com/237x100.png/ff4444/ffffff'); -insert into products (id, name, price, image_url) -values (8, 'Kirsti', 309, 'http://dummyimage.com/167x100.png/5fa2dd/ffffff'); -insert into products (id, name, price, image_url) -values (9, 'Avie', 7664, 'http://dummyimage.com/204x100.png/dddddd/000000'); -insert into products (id, name, price, image_url) -values (10, 'Jeanne', 1395, 'http://dummyimage.com/218x100.png/cc0000/ffffff'); -insert into products (id, name, price, image_url) -values (11, 'Stefano', 9184, 'http://dummyimage.com/219x100.png/dddddd/000000'); -insert into products (id, name, price, image_url) -values (12, 'Ossie', 7026, 'http://dummyimage.com/173x100.png/dddddd/000000'); -insert into products (id, name, price, image_url) -values (13, 'Quinton', 3273, 'http://dummyimage.com/239x100.png/cc0000/ffffff'); -insert into products (id, name, price, image_url) -values (14, 'Violante', 5655, 'http://dummyimage.com/206x100.png/ff4444/ffffff'); -insert into products (id, name, price, image_url) -values (15, 'Arda', 7270, 'http://dummyimage.com/138x100.png/dddddd/000000'); -insert into products (id, name, price, image_url) -values (16, 'Bale', 3985, 'http://dummyimage.com/237x100.png/ff4444/ffffff'); -insert into products (id, name, price, image_url) -values (17, 'Nanine', 8631, 'http://dummyimage.com/206x100.png/cc0000/ffffff'); -insert into products (id, name, price, image_url) -values (18, 'York', 3355, 'http://dummyimage.com/231x100.png/ff4444/ffffff'); -insert into products (id, name, price, image_url) -values (19, 'Trenna', 8240, 'http://dummyimage.com/203x100.png/cc0000/ffffff'); -insert into products (id, name, price, image_url) -values (20, 'Elysha', 4475, 'http://dummyimage.com/218x100.png/dddddd/000000'); -insert into products (id, name, price, image_url) -values (21, 'Meridith', 9933, 'http://dummyimage.com/239x100.png/5fa2dd/ffffff'); -insert into products (id, name, price, image_url) -values (22, 'Fraze', 2597, 'http://dummyimage.com/220x100.png/cc0000/ffffff'); -insert into products (id, name, price, image_url) -values (23, 'Erinn', 9300, 'http://dummyimage.com/216x100.png/ff4444/ffffff'); -insert into products (id, name, price, image_url) -values (24, 'Marshal', 6447, 'http://dummyimage.com/112x100.png/cc0000/ffffff'); -insert into products (id, name, price, image_url) -values (25, 'Mahala', 5109, 'http://dummyimage.com/224x100.png/dddddd/000000'); -insert into products (id, name, price, image_url) -values (26, 'Simone', 3614, 'http://dummyimage.com/152x100.png/dddddd/000000'); -insert into products (id, name, price, image_url) -values (27, 'Karmen', 1540, 'http://dummyimage.com/114x100.png/dddddd/000000'); -insert into products (id, name, price, image_url) -values (28, 'Allissa', 2966, 'http://dummyimage.com/106x100.png/5fa2dd/ffffff'); -insert into products (id, name, price, image_url) -values (29, 'Pammi', 2017, 'http://dummyimage.com/142x100.png/ff4444/ffffff'); -insert into products (id, name, price, image_url) -values (30, 'Wilbur', 6458, 'http://dummyimage.com/128x100.png/ff4444/ffffff'); -insert into products (id, name, price, image_url) -values (31, 'Stearn', 7902, 'http://dummyimage.com/249x100.png/cc0000/ffffff'); -insert into products (id, name, price, image_url) -values (32, 'Percival', 8595, 'http://dummyimage.com/210x100.png/dddddd/000000'); -insert into products (id, name, price, image_url) -values (33, 'Nickolaus', 8772, 'http://dummyimage.com/164x100.png/cc0000/ffffff'); -insert into products (id, name, price, image_url) -values (34, 'Toma', 4316, 'http://dummyimage.com/128x100.png/dddddd/000000'); -insert into products (id, name, price, image_url) -values (35, 'Basile', 530, 'http://dummyimage.com/148x100.png/ff4444/ffffff'); -insert into products (id, name, price, image_url) -values (36, 'Erek', 8139, 'http://dummyimage.com/102x100.png/dddddd/000000'); -insert into products (id, name, price, image_url) -values (37, 'Burty', 4417, 'http://dummyimage.com/175x100.png/5fa2dd/ffffff'); -insert into products (id, name, price, image_url) -values (38, 'Layne', 6295, 'http://dummyimage.com/193x100.png/dddddd/000000'); -insert into products (id, name, price, image_url) -values (39, 'Denny', 575, 'http://dummyimage.com/237x100.png/cc0000/ffffff'); -insert into products (id, name, price, image_url) -values (40, 'Dory', 7803, 'http://dummyimage.com/203x100.png/dddddd/000000'); -insert into products (id, name, price, image_url) -values (41, 'Dale', 2828, 'http://dummyimage.com/108x100.png/dddddd/000000'); -insert into products (id, name, price, image_url) -values (42, 'Bev', 1545, 'http://dummyimage.com/165x100.png/ff4444/ffffff'); -insert into products (id, name, price, image_url) -values (43, 'Laurena', 8045, 'http://dummyimage.com/212x100.png/ff4444/ffffff'); -insert into products (id, name, price, image_url) -values (44, 'Darnall', 7511, 'http://dummyimage.com/152x100.png/5fa2dd/ffffff'); -insert into products (id, name, price, image_url) -values (45, 'Keir', 2008, 'http://dummyimage.com/211x100.png/5fa2dd/ffffff'); -insert into products (id, name, price, image_url) -values (46, 'Ellyn', 5201, 'http://dummyimage.com/212x100.png/5fa2dd/ffffff'); -insert into products (id, name, price, image_url) -values (47, 'Jakob', 7654, 'http://dummyimage.com/174x100.png/dddddd/000000'); -insert into products (id, name, price, image_url) -values (48, 'Marillin', 3925, 'http://dummyimage.com/130x100.png/5fa2dd/ffffff'); -insert into products (id, name, price, image_url) -values (49, 'Sybyl', 3241, 'http://dummyimage.com/241x100.png/cc0000/ffffff'); -insert into products (id, name, price, image_url) -values (50, 'Emile', 3765, 'http://dummyimage.com/123x100.png/dddddd/000000'); -insert into products (id, name, price, image_url) -values (51, 'Stacey', 5708, 'http://dummyimage.com/132x100.png/ff4444/ffffff'); -insert into products (id, name, price, image_url) -values (52, 'Eveline', 9398, 'http://dummyimage.com/132x100.png/ff4444/ffffff'); -insert into products (id, name, price, image_url) -values (53, 'Clemens', 4271, 'http://dummyimage.com/191x100.png/dddddd/000000'); -insert into products (id, name, price, image_url) -values (54, 'Giorgia', 2913, 'http://dummyimage.com/234x100.png/dddddd/000000'); -insert into products (id, name, price, image_url) -values (55, 'Cele', 3178, 'http://dummyimage.com/172x100.png/dddddd/000000'); -insert into products (id, name, price, image_url) -values (56, 'Gretal', 3361, 'http://dummyimage.com/209x100.png/cc0000/ffffff'); -insert into products (id, name, price, image_url) -values (57, 'Enrika', 1231, 'http://dummyimage.com/179x100.png/5fa2dd/ffffff'); -insert into products (id, name, price, image_url) -values (58, 'Chad', 4213, 'http://dummyimage.com/111x100.png/ff4444/ffffff'); -insert into products (id, name, price, image_url) -values (59, 'Merci', 91, 'http://dummyimage.com/163x100.png/5fa2dd/ffffff'); -insert into products (id, name, price, image_url) -values (60, 'Susana', 9044, 'http://dummyimage.com/209x100.png/cc0000/ffffff'); -insert into products (id, name, price, image_url) -values (61, 'Dael', 2004, 'http://dummyimage.com/152x100.png/dddddd/000000'); -insert into products (id, name, price, image_url) -values (62, 'Gris', 8856, 'http://dummyimage.com/233x100.png/ff4444/ffffff'); -insert into products (id, name, price, image_url) -values (63, 'Henry', 2140, 'http://dummyimage.com/213x100.png/ff4444/ffffff'); -insert into products (id, name, price, image_url) -values (64, 'Kim', 1615, 'http://dummyimage.com/151x100.png/5fa2dd/ffffff'); -insert into products (id, name, price, image_url) -values (65, 'Ealasaid', 6515, 'http://dummyimage.com/114x100.png/dddddd/000000'); -insert into products (id, name, price, image_url) -values (66, 'Edik', 3560, 'http://dummyimage.com/222x100.png/ff4444/ffffff'); -insert into products (id, name, price, image_url) -values (67, 'Guilbert', 9291, 'http://dummyimage.com/175x100.png/dddddd/000000'); -insert into products (id, name, price, image_url) -values (68, 'Raimundo', 2177, 'http://dummyimage.com/119x100.png/ff4444/ffffff'); -insert into products (id, name, price, image_url) -values (69, 'Catherin', 8755, 'http://dummyimage.com/206x100.png/dddddd/000000'); -insert into products (id, name, price, image_url) -values (70, 'Denny', 9993, 'http://dummyimage.com/115x100.png/5fa2dd/ffffff'); -insert into products (id, name, price, image_url) -values (71, 'Lynne', 9758, 'http://dummyimage.com/188x100.png/dddddd/000000'); -insert into products (id, name, price, image_url) -values (72, 'Averell', 6403, 'http://dummyimage.com/232x100.png/cc0000/ffffff'); -insert into products (id, name, price, image_url) -values (73, 'Auguste', 595, 'http://dummyimage.com/208x100.png/ff4444/ffffff'); -insert into products (id, name, price, image_url) -values (74, 'Addison', 1952, 'http://dummyimage.com/160x100.png/5fa2dd/ffffff'); -insert into products (id, name, price, image_url) -values (75, 'Adda', 7787, 'http://dummyimage.com/127x100.png/ff4444/ffffff'); -insert into products (id, name, price, image_url) -values (76, 'Neilla', 6828, 'http://dummyimage.com/227x100.png/dddddd/000000'); -insert into products (id, name, price, image_url) -values (77, 'Jennie', 1503, 'http://dummyimage.com/248x100.png/5fa2dd/ffffff'); -insert into products (id, name, price, image_url) -values (78, 'Burty', 9211, 'http://dummyimage.com/168x100.png/ff4444/ffffff'); -insert into products (id, name, price, image_url) -values (79, 'Gris', 456, 'http://dummyimage.com/148x100.png/dddddd/000000'); -insert into products (id, name, price, image_url) -values (80, 'Benjie', 9058, 'http://dummyimage.com/110x100.png/cc0000/ffffff'); -insert into products (id, name, price, image_url) -values (81, 'Amberly', 8391, 'http://dummyimage.com/111x100.png/5fa2dd/ffffff'); -insert into products (id, name, price, image_url) -values (82, 'Stavros', 8825, 'http://dummyimage.com/228x100.png/ff4444/ffffff'); -insert into products (id, name, price, image_url) -values (83, 'Lavinia', 1044, 'http://dummyimage.com/202x100.png/cc0000/ffffff'); -insert into products (id, name, price, image_url) -values (84, 'Henrieta', 5124, 'http://dummyimage.com/139x100.png/cc0000/ffffff'); -insert into products (id, name, price, image_url) -values (85, 'Carly', 6724, 'http://dummyimage.com/124x100.png/5fa2dd/ffffff'); -insert into products (id, name, price, image_url) -values (86, 'Alaster', 8054, 'http://dummyimage.com/159x100.png/5fa2dd/ffffff'); -insert into products (id, name, price, image_url) -values (87, 'Novelia', 223, 'http://dummyimage.com/195x100.png/ff4444/ffffff'); -insert into products (id, name, price, image_url) -values (88, 'Etta', 9076, 'http://dummyimage.com/237x100.png/ff4444/ffffff'); -insert into products (id, name, price, image_url) -values (89, 'Eustacia', 598, 'http://dummyimage.com/134x100.png/dddddd/000000'); -insert into products (id, name, price, image_url) -values (90, 'Ronnie', 9594, 'http://dummyimage.com/219x100.png/cc0000/ffffff'); -insert into products (id, name, price, image_url) -values (91, 'Annadiana', 4606, 'http://dummyimage.com/144x100.png/ff4444/ffffff'); -insert into products (id, name, price, image_url) -values (92, 'Tommy', 6141, 'http://dummyimage.com/211x100.png/dddddd/000000'); -insert into products (id, name, price, image_url) -values (93, 'Wesley', 8117, 'http://dummyimage.com/210x100.png/5fa2dd/ffffff'); -insert into products (id, name, price, image_url) -values (94, 'Zeke', 9996, 'http://dummyimage.com/233x100.png/ff4444/ffffff'); -insert into products (id, name, price, image_url) -values (95, 'Haslett', 7337, 'http://dummyimage.com/162x100.png/cc0000/ffffff'); -insert into products (id, name, price, image_url) -values (96, 'Lyndell', 3415, 'http://dummyimage.com/121x100.png/cc0000/ffffff'); -insert into products (id, name, price, image_url) -values (97, 'Flo', 1876, 'http://dummyimage.com/140x100.png/dddddd/000000'); -insert into products (id, name, price, image_url) -values (98, 'Barri', 894, 'http://dummyimage.com/111x100.png/dddddd/000000'); -insert into products (id, name, price, image_url) -values (99, 'Marybelle', 8184, 'http://dummyimage.com/222x100.png/cc0000/ffffff'); -insert into products (id, name, price, image_url) -values (100, 'Edgar', 5624, 'http://dummyimage.com/208x100.png/dddddd/000000'); +insert into products (name, price, image_url) +values ('Jarred', 4750, 'http://dummyimage.com/165x100.png/dddddd/000000'); +insert into products (name, price, image_url) +values ('Kearney', 548, 'http://dummyimage.com/106x100.png/dddddd/000000'); +insert into products (name, price, image_url) +values ('Evan', 83663, 'http://dummyimage.com/188x100.png/ff4444/ffffff'); +insert into products (name, price, image_url) +values ('Shae', 54900, 'http://dummyimage.com/182x100.png/ff4444/ffffff'); +insert into products (name, price, image_url) +values ('Merrill', 78201, 'http://dummyimage.com/163x100.png/dddddd/000000'); +insert into products (name, price, image_url) +values ('Rozina', 55280, 'http://dummyimage.com/123x100.png/dddddd/000000'); +insert into products (name, price, image_url) +values ('Devin', 98380, 'http://dummyimage.com/101x100.png/cc0000/ffffff'); +insert into products (name, price, image_url) +values ('Reece', 87688, 'http://dummyimage.com/125x100.png/cc0000/ffffff'); +insert into products (name, price, image_url) +values ('Findlay', 61291, 'http://dummyimage.com/225x100.png/ff4444/ffffff'); +insert into products (name, price, image_url) +values ('Samuele', 61006, 'http://dummyimage.com/125x100.png/dddddd/000000'); +insert into products (name, price, image_url) +values ('Nobie', 33492, 'http://dummyimage.com/208x100.png/dddddd/000000'); +insert into products (name, price, image_url) +values ('Jacob', 16626, 'http://dummyimage.com/176x100.png/5fa2dd/ffffff'); +insert into products (name, price, image_url) +values ('Babb', 34480, 'http://dummyimage.com/198x100.png/5fa2dd/ffffff'); +insert into products (name, price, image_url) +values ('Tarra', 24837, 'http://dummyimage.com/216x100.png/5fa2dd/ffffff'); +insert into products (name, price, image_url) +values ('Paulie', 18980, 'http://dummyimage.com/178x100.png/dddddd/000000'); +insert into products (name, price, image_url) +values ('Pascale', 10113, 'http://dummyimage.com/145x100.png/dddddd/000000'); +insert into products (name, price, image_url) +values ('Michel', 86388, 'http://dummyimage.com/212x100.png/ff4444/ffffff'); +insert into products (name, price, image_url) +values ('Clive', 7423, 'http://dummyimage.com/171x100.png/5fa2dd/ffffff'); +insert into products (name, price, image_url) +values ('Merle', 6880, 'http://dummyimage.com/177x100.png/dddddd/000000'); +insert into products (name, price, image_url) +values ('Lindy', 86008, 'http://dummyimage.com/192x100.png/cc0000/ffffff'); +insert into products (name, price, image_url) +values ('Edd', 5194, 'http://dummyimage.com/189x100.png/5fa2dd/ffffff'); +insert into products (name, price, image_url) +values ('Lindsey', 45508, 'http://dummyimage.com/229x100.png/dddddd/000000'); +insert into products (name, price, image_url) +values ('Joachim', 92509, 'http://dummyimage.com/199x100.png/5fa2dd/ffffff'); +insert into products (name, price, image_url) +values ('Filberte', 84801, 'http://dummyimage.com/236x100.png/ff4444/ffffff'); +insert into products (name, price, image_url) +values ('Granny', 95976, 'http://dummyimage.com/169x100.png/ff4444/ffffff'); +insert into products (name, price, image_url) +values ('Tiebout', 35045, 'http://dummyimage.com/182x100.png/dddddd/000000'); +insert into products (name, price, image_url) +values ('Miltie', 19555, 'http://dummyimage.com/191x100.png/cc0000/ffffff'); +insert into products (name, price, image_url) +values ('Estrella', 82325, 'http://dummyimage.com/198x100.png/5fa2dd/ffffff'); +insert into products (name, price, image_url) +values ('Hube', 47709, 'http://dummyimage.com/206x100.png/dddddd/000000'); +insert into products (name, price, image_url) +values ('Abba', 10819, 'http://dummyimage.com/138x100.png/5fa2dd/ffffff'); +insert into products (name, price, image_url) +values ('Meaghan', 46674, 'http://dummyimage.com/226x100.png/dddddd/000000'); +insert into products (name, price, image_url) +values ('Hortense', 31061, 'http://dummyimage.com/183x100.png/ff4444/ffffff'); +insert into products (name, price, image_url) +values ('Orran', 41196, 'http://dummyimage.com/192x100.png/ff4444/ffffff'); +insert into products (name, price, image_url) +values ('Lannie', 66318, 'http://dummyimage.com/117x100.png/dddddd/000000'); +insert into products (name, price, image_url) +values ('Wadsworth', 85268, 'http://dummyimage.com/205x100.png/cc0000/ffffff'); +insert into products (name, price, image_url) +values ('Rafaelia', 51277, 'http://dummyimage.com/218x100.png/5fa2dd/ffffff'); +insert into products (name, price, image_url) +values ('Jerrie', 52659, 'http://dummyimage.com/155x100.png/cc0000/ffffff'); +insert into products (name, price, image_url) +values ('Symon', 29156, 'http://dummyimage.com/172x100.png/dddddd/000000'); +insert into products (name, price, image_url) +values ('Trish', 7859, 'http://dummyimage.com/165x100.png/cc0000/ffffff'); +insert into products (name, price, image_url) +values ('Jule', 13644, 'http://dummyimage.com/237x100.png/cc0000/ffffff'); +insert into products (name, price, image_url) +values ('Joly', 87015, 'http://dummyimage.com/132x100.png/ff4444/ffffff'); +insert into products (name, price, image_url) +values ('Gracia', 94879, 'http://dummyimage.com/133x100.png/5fa2dd/ffffff'); +insert into products (name, price, image_url) +values ('Nicko', 10097, 'http://dummyimage.com/150x100.png/5fa2dd/ffffff'); +insert into products (name, price, image_url) +values ('Barrie', 1743, 'http://dummyimage.com/205x100.png/ff4444/ffffff'); +insert into products (name, price, image_url) +values ('Perceval', 29793, 'http://dummyimage.com/118x100.png/ff4444/ffffff'); +insert into products (name, price, image_url) +values ('Adey', 36524, 'http://dummyimage.com/188x100.png/5fa2dd/ffffff'); +insert into products (name, price, image_url) +values ('Cleve', 53941, 'http://dummyimage.com/179x100.png/dddddd/000000'); +insert into products (name, price, image_url) +values ('Padget', 39582, 'http://dummyimage.com/249x100.png/cc0000/ffffff'); +insert into products (name, price, image_url) +values ('Rupert', 72787, 'http://dummyimage.com/138x100.png/ff4444/ffffff'); +insert into products (name, price, image_url) +values ('Claudelle', 82004, 'http://dummyimage.com/206x100.png/5fa2dd/ffffff'); +insert into products (name, price, image_url) +values ('Marlon', 34296, 'http://dummyimage.com/161x100.png/cc0000/ffffff'); +insert into products (name, price, image_url) +values ('Jeremy', 55345, 'http://dummyimage.com/205x100.png/5fa2dd/ffffff'); +insert into products (name, price, image_url) +values ('Sanders', 23124, 'http://dummyimage.com/106x100.png/ff4444/ffffff'); +insert into products (name, price, image_url) +values ('Jordain', 96017, 'http://dummyimage.com/164x100.png/dddddd/000000'); +insert into products (name, price, image_url) +values ('Larina', 46169, 'http://dummyimage.com/234x100.png/dddddd/000000'); +insert into products (name, price, image_url) +values ('Carlota', 72232, 'http://dummyimage.com/189x100.png/ff4444/ffffff'); +insert into products (name, price, image_url) +values ('Paco', 26742, 'http://dummyimage.com/121x100.png/5fa2dd/ffffff'); +insert into products (name, price, image_url) +values ('Udall', 78229, 'http://dummyimage.com/124x100.png/cc0000/ffffff'); +insert into products (name, price, image_url) +values ('Mirella', 44104, 'http://dummyimage.com/179x100.png/ff4444/ffffff'); +insert into products (name, price, image_url) +values ('Buiron', 2447, 'http://dummyimage.com/233x100.png/cc0000/ffffff'); +insert into products (name, price, image_url) +values ('Kevin', 53876, 'http://dummyimage.com/153x100.png/cc0000/ffffff'); +insert into products (name, price, image_url) +values ('Thea', 82590, 'http://dummyimage.com/158x100.png/cc0000/ffffff'); +insert into products (name, price, image_url) +values ('Esme', 30216, 'http://dummyimage.com/197x100.png/dddddd/000000'); +insert into products (name, price, image_url) +values ('Pen', 3677, 'http://dummyimage.com/232x100.png/dddddd/000000'); +insert into products (name, price, image_url) +values ('Tedda', 98656, 'http://dummyimage.com/191x100.png/cc0000/ffffff'); +insert into products (name, price, image_url) +values ('Milli', 30932, 'http://dummyimage.com/156x100.png/5fa2dd/ffffff'); +insert into products (name, price, image_url) +values ('Judie', 68398, 'http://dummyimage.com/237x100.png/5fa2dd/ffffff'); +insert into products (name, price, image_url) +values ('Marline', 61978, 'http://dummyimage.com/124x100.png/ff4444/ffffff'); +insert into products (name, price, image_url) +values ('Marleah', 48428, 'http://dummyimage.com/220x100.png/cc0000/ffffff'); +insert into products (name, price, image_url) +values ('Kristoffer', 61680, 'http://dummyimage.com/184x100.png/ff4444/ffffff'); +insert into products (name, price, image_url) +values ('Carolynn', 45576, 'http://dummyimage.com/223x100.png/cc0000/ffffff'); +insert into products (name, price, image_url) +values ('Winne', 76689, 'http://dummyimage.com/183x100.png/cc0000/ffffff'); +insert into products (name, price, image_url) +values ('Arabelle', 50478, 'http://dummyimage.com/241x100.png/dddddd/000000'); +insert into products (name, price, image_url) +values ('Jerrilee', 49320, 'http://dummyimage.com/104x100.png/cc0000/ffffff'); +insert into products (name, price, image_url) +values ('Baron', 96069, 'http://dummyimage.com/191x100.png/dddddd/000000'); +insert into products (name, price, image_url) +values ('Etti', 47457, 'http://dummyimage.com/248x100.png/5fa2dd/ffffff'); +insert into products (name, price, image_url) +values ('Nicolai', 30183, 'http://dummyimage.com/192x100.png/cc0000/ffffff'); +insert into products (name, price, image_url) +values ('Dari', 78081, 'http://dummyimage.com/248x100.png/ff4444/ffffff'); +insert into products (name, price, image_url) +values ('Quintin', 38278, 'http://dummyimage.com/201x100.png/ff4444/ffffff'); +insert into products (name, price, image_url) +values ('Barnard', 5511, 'http://dummyimage.com/102x100.png/ff4444/ffffff'); +insert into products (name, price, image_url) +values ('Jamal', 80626, 'http://dummyimage.com/184x100.png/ff4444/ffffff'); +insert into products (name, price, image_url) +values ('Davin', 95671, 'http://dummyimage.com/200x100.png/5fa2dd/ffffff'); +insert into products (name, price, image_url) +values ('Julina', 95650, 'http://dummyimage.com/133x100.png/cc0000/ffffff'); +insert into products (name, price, image_url) +values ('Ripley', 30952, 'http://dummyimage.com/172x100.png/cc0000/ffffff'); +insert into products (name, price, image_url) +values ('Hamnet', 80710, 'http://dummyimage.com/233x100.png/5fa2dd/ffffff'); +insert into products (name, price, image_url) +values ('Haleigh', 19799, 'http://dummyimage.com/102x100.png/ff4444/ffffff'); +insert into products (name, price, image_url) +values ('Meg', 21167, 'http://dummyimage.com/167x100.png/dddddd/000000'); +insert into products (name, price, image_url) +values ('Wally', 78167, 'http://dummyimage.com/243x100.png/5fa2dd/ffffff'); +insert into products (name, price, image_url) +values ('Lannie', 78335, 'http://dummyimage.com/114x100.png/cc0000/ffffff'); +insert into products (name, price, image_url) +values ('Beverley', 43686, 'http://dummyimage.com/154x100.png/ff4444/ffffff'); +insert into products (name, price, image_url) +values ('Keefe', 70268, 'http://dummyimage.com/126x100.png/dddddd/000000'); +insert into products (name, price, image_url) +values ('Renae', 50565, 'http://dummyimage.com/214x100.png/5fa2dd/ffffff'); +insert into products (name, price, image_url) +values ('Margie', 47698, 'http://dummyimage.com/177x100.png/ff4444/ffffff'); +insert into products (name, price, image_url) +values ('Elisabetta', 16919, 'http://dummyimage.com/168x100.png/5fa2dd/ffffff'); +insert into products (name, price, image_url) +values ('Nickola', 38068, 'http://dummyimage.com/183x100.png/cc0000/ffffff'); +insert into products (name, price, image_url) +values ('Hunfredo', 45432, 'http://dummyimage.com/220x100.png/5fa2dd/ffffff'); +insert into products (name, price, image_url) +values ('Jyoti', 99675, 'http://dummyimage.com/159x100.png/ff4444/ffffff'); +insert into products (name, price, image_url) +values ('Garland', 92936, 'http://dummyimage.com/120x100.png/ff4444/ffffff'); +insert into products (name, price, image_url) +values ('Anthony', 33928, 'http://dummyimage.com/170x100.png/cc0000/ffffff'); +insert into products (name, price, image_url) +values ('Ada', 7528, 'http://dummyimage.com/164x100.png/5fa2dd/ffffff'); \ No newline at end of file From c1a67f44fa2b1620a736a07dd92c12db9ecea259 Mon Sep 17 00:00:00 2001 From: momnpa333 Date: Mon, 1 Jul 2024 13:57:13 +0900 Subject: [PATCH 53/53] =?UTF-8?q?refact():=20=ED=94=BC=EB=93=9C=EB=B0=B1?= =?UTF-8?q?=20=EB=B0=98=EC=98=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gift/controller/ProductAdminController.java | 14 ++++++++++++++ .../java/gift/controller/ProductController.java | 15 ++++----------- src/main/java/gift/model/ProductDao.java | 2 +- .../java/gift/model/ProductJdbcTemplateDao.java | 2 +- src/main/java/gift/model/ProductMapDao.java | 4 ++-- .../gift/controller/ProductControllerTest.java | 13 ++----------- 6 files changed, 24 insertions(+), 26 deletions(-) create mode 100644 src/main/java/gift/controller/ProductAdminController.java diff --git a/src/main/java/gift/controller/ProductAdminController.java b/src/main/java/gift/controller/ProductAdminController.java new file mode 100644 index 000000000..bb70e9218 --- /dev/null +++ b/src/main/java/gift/controller/ProductAdminController.java @@ -0,0 +1,14 @@ +package gift.controller; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; + +@Controller +public class ProductAdminController { + + @GetMapping("/admin") + public String admin() { + return "adminPage"; + } + +} diff --git a/src/main/java/gift/controller/ProductController.java b/src/main/java/gift/controller/ProductController.java index 196b3b9f4..b77d28ec4 100644 --- a/src/main/java/gift/controller/ProductController.java +++ b/src/main/java/gift/controller/ProductController.java @@ -1,12 +1,10 @@ package gift.controller; -import gift.model.Product; import gift.model.ProductDao; import java.util.List; import org.springframework.http.ResponseEntity; -import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @@ -14,8 +12,9 @@ import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; -@Controller +@RestController public class ProductController { private final ProductDao productDao; @@ -24,15 +23,9 @@ public ProductController(ProductDao productDao) { this.productDao = productDao; } - @GetMapping("/admin") - public String admin() { - return "adminPage"; - } - @GetMapping("/products") public ResponseEntity> getProducts() { - var products = productDao.findAll(); - var response = products.stream() + var response = productDao.findAll().stream() .map(ProductResponse::from) .toList(); return ResponseEntity.ok(response); @@ -48,7 +41,7 @@ public ResponseEntity getProduct(@PathVariable("id") Long id) { @PostMapping("/products") public ResponseEntity createProduct(@RequestBody ProductRequest request) { - productDao.save(request.toEntity()); + productDao.insert(request.toEntity()); return ResponseEntity.ok().body("Product created successfully."); } diff --git a/src/main/java/gift/model/ProductDao.java b/src/main/java/gift/model/ProductDao.java index 855429a03..29c3a31e1 100644 --- a/src/main/java/gift/model/ProductDao.java +++ b/src/main/java/gift/model/ProductDao.java @@ -7,7 +7,7 @@ @Component public interface ProductDao { - void save(Product product); + void insert(Product product); Optional findById(Long id); diff --git a/src/main/java/gift/model/ProductJdbcTemplateDao.java b/src/main/java/gift/model/ProductJdbcTemplateDao.java index bc8aed2f1..625563fbb 100644 --- a/src/main/java/gift/model/ProductJdbcTemplateDao.java +++ b/src/main/java/gift/model/ProductJdbcTemplateDao.java @@ -29,7 +29,7 @@ public ProductJdbcTemplateDao(DataSource dataSource) { } @Override - public void save(Product product) { + public void insert(Product product) { jdbcTemplate.update(SQL_INSERT, product.getName(), product.getPrice(), product.getImageUrl()); } diff --git a/src/main/java/gift/model/ProductMapDao.java b/src/main/java/gift/model/ProductMapDao.java index 740145fa3..d9d66096e 100644 --- a/src/main/java/gift/model/ProductMapDao.java +++ b/src/main/java/gift/model/ProductMapDao.java @@ -12,7 +12,7 @@ public class ProductMapDao implements ProductDao { private final Map database = new ConcurrentHashMap<>(); @Override - public void save(Product product) { + public void insert(Product product) { Product newProduct = Product.create(Long.valueOf(database.size() + 1), product.getName(), product.getPrice(), product.getImageUrl()); database.put(newProduct.getId(), newProduct); @@ -42,7 +42,7 @@ public void update(Product product) { public List findPaging(int page, int size) { return null; } - + @Override public Long count() { return Long.valueOf(database.size()); diff --git a/src/test/java/gift/controller/ProductControllerTest.java b/src/test/java/gift/controller/ProductControllerTest.java index b0b18c81a..8ea8ff2a0 100644 --- a/src/test/java/gift/controller/ProductControllerTest.java +++ b/src/test/java/gift/controller/ProductControllerTest.java @@ -2,6 +2,7 @@ import gift.model.Product; import gift.model.ProductDao; +import java.util.List; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.InjectMocks; @@ -39,7 +40,7 @@ void setUp() { @Test void testGetProducts() throws Exception { // given: 테스트에 사용될 mock 데이터를 설정합니다. - var products = Arrays.asList( + var products = List.of( Product.create(1L, "Product1", 100, "http://image1.com"), Product.create(2L, "Product2", 200, "http://image2.com") ); @@ -75,14 +76,4 @@ void testGetProduct() throws Exception { verify(productDao, times(1)).findById(anyLong()); } - - @Test - void testDeleteProduct() throws Exception { - doNothing().when(productDao).deleteById(anyLong()); - - mockMvc.perform(delete("/products/{id}", 1L)) - .andExpect(status().isNoContent()); - - verify(productDao, times(1)).deleteById(anyLong()); - } }