From 267aef9faeda6ef407b7211b03545a065465ff62 Mon Sep 17 00:00:00 2001 From: doyooon Date: Mon, 24 Jun 2024 17:14:04 +0900 Subject: [PATCH 01/10] =?UTF-8?q?docs=20README.md:=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EC=9A=94=EA=B5=AC=EC=82=AC=ED=95=AD=20=EB=AA=A9=EB=A1=9D=20?= =?UTF-8?q?=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index cafde8a2c..7092c722a 100644 --- a/README.md +++ b/README.md @@ -1 +1,6 @@ -# spring-gift-product \ No newline at end of file +# spring-gift-product +- [ ] 상품을 조회, 추가, 수정, 삭제할 수 있는 HTTP API를 구현한다. + - [ ] 조회 + - [ ] 추가 + - [ ] 수정 + - [ ] 삭제 \ No newline at end of file From cde117ab049df7a5cf284a02b48185e797248153 Mon Sep 17 00:00:00 2001 From: doyooon Date: Mon, 24 Jun 2024 17:15:39 +0900 Subject: [PATCH 02/10] =?UTF-8?q?feat=20ProductController:=20=EC=83=81?= =?UTF-8?q?=ED=92=88=20=EC=A1=B0=ED=9A=8C,=20=EC=B6=94=EA=B0=80,=20?= =?UTF-8?q?=EC=88=98=EC=A0=95=20=EC=82=AD=EC=A0=9C=EB=A5=BC=20=ED=95=A0=20?= =?UTF-8?q?=EC=88=98=20=EC=9E=88=EB=8A=94=20HTTP=20API=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 10 ++--- build.gradle | 2 +- src/main/java/gift/Application.java | 1 + src/main/java/gift/Product.java | 38 ++++++++++++++++++ src/main/java/gift/ProductController.java | 47 +++++++++++++++++++++++ 5 files changed, 92 insertions(+), 6 deletions(-) create mode 100644 src/main/java/gift/Product.java create mode 100644 src/main/java/gift/ProductController.java diff --git a/README.md b/README.md index 7092c722a..92a35de24 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # spring-gift-product -- [ ] 상품을 조회, 추가, 수정, 삭제할 수 있는 HTTP API를 구현한다. - - [ ] 조회 - - [ ] 추가 - - [ ] 수정 - - [ ] 삭제 \ No newline at end of file +- [x] 상품을 조회, 추가, 수정, 삭제할 수 있는 HTTP API를 구현한다. + - [x] 조회 + - [x] 추가 + - [x] 수정 + - [x] 삭제 \ No newline at end of file diff --git a/build.gradle b/build.gradle index df7db9334..ebac4d980 100644 --- a/build.gradle +++ b/build.gradle @@ -9,7 +9,7 @@ version = '0.0.1-SNAPSHOT' java { toolchain { - languageVersion = JavaLanguageVersion.of(21) + languageVersion = JavaLanguageVersion.of(17) } } diff --git a/src/main/java/gift/Application.java b/src/main/java/gift/Application.java index 61603cca0..829be1343 100644 --- a/src/main/java/gift/Application.java +++ b/src/main/java/gift/Application.java @@ -6,6 +6,7 @@ @SpringBootApplication public class Application { public static void main(String[] args) { + SpringApplication.run(Application.class, args); } } diff --git a/src/main/java/gift/Product.java b/src/main/java/gift/Product.java new file mode 100644 index 000000000..a8f2967bc --- /dev/null +++ b/src/main/java/gift/Product.java @@ -0,0 +1,38 @@ +package gift; + +public class Product { + Long id; + String name; + int price; + String imageUrl; + + public Product(Long id, String name, int price, String imageUrl){ + this.id = id; + this.name = name; + this.price = price; + this.imageUrl = imageUrl; + } + + public Long getId(){ + return this.id; + } + + public String getName(){ + return this.name; + } + + public int getPrice(){ + return this.price; + } + + public String getImageUrl(){ + return this.imageUrl; + } + + public void setProduct(Product product){ + this.id = product.id; + this.name = product.name; + this.price = product.price; + this.imageUrl = product.imageUrl; + } +} diff --git a/src/main/java/gift/ProductController.java b/src/main/java/gift/ProductController.java new file mode 100644 index 000000000..a6f8ab99d --- /dev/null +++ b/src/main/java/gift/ProductController.java @@ -0,0 +1,47 @@ +package gift; + +import java.util.HashMap; +import java.util.Map; +import org.springframework.http.HttpStatus; +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.PostMapping; +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; + +@RestController +public class ProductController { + private Map products = new HashMap<>(); + + @GetMapping("/get") + public Product getProduct(@RequestParam(value = "id") Long id) { + return products.get(id); + } + + @PostMapping("/post") + public ResponseEntity createProduct(@RequestBody Product newProduct){ + var product = new Product(newProduct.id, newProduct.name, newProduct.price, newProduct.imageUrl); + products.put(product.id, product); + System.out.println(products.get(product.id).name); + return new ResponseEntity<>(HttpStatus.CREATED); + } + + @PutMapping("/put") + public ResponseEntity updateProduct(@RequestBody Product changeProduct){ + Product changedProduct = products.get(changeProduct.id); + changedProduct.setProduct(changeProduct); + + return new ResponseEntity<>(HttpStatus.OK); + } + + @DeleteMapping("/delete") + public ResponseEntity deleteProductt(@RequestParam(value = "id") Long id){ + products.remove(id); + + return new ResponseEntity<>(HttpStatus.ACCEPTED); + } + +} From d28d62b8de85fe3015a4c3a177eee85df618874f Mon Sep 17 00:00:00 2001 From: doyooon Date: Fri, 28 Jun 2024 17:16:53 +0900 Subject: [PATCH 03/10] =?UTF-8?q?docs=20README.md:=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EC=9A=94=EA=B5=AC=EC=82=AC=ED=95=AD=20=EB=AA=A9=EB=A1=9D=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 92a35de24..e96948782 100644 --- a/README.md +++ b/README.md @@ -3,4 +3,6 @@ - [x] 조회 - [x] 추가 - [x] 수정 - - [x] 삭제 \ No newline at end of file + - [x] 삭제 +- [] Thymeleaf를 사용하여 상품을 조회, 추가, 수정, 삭제할 수 있는 관리자 화면을 구현한다. + From bf10c23371ff0ec866e6efd74c5ff2c4a67ac18a Mon Sep 17 00:00:00 2001 From: doyooon Date: Fri, 28 Jun 2024 17:17:36 +0900 Subject: [PATCH 04/10] =?UTF-8?q?docs=20README.md:=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EC=9A=94=EA=B5=AC=EC=82=AC=ED=95=AD=20=EB=AA=A9=EB=A1=9D=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e96948782..1ffb8a937 100644 --- a/README.md +++ b/README.md @@ -4,5 +4,7 @@ - [x] 추가 - [x] 수정 - [x] 삭제 -- [] Thymeleaf를 사용하여 상품을 조회, 추가, 수정, 삭제할 수 있는 관리자 화면을 구현한다. +- [ ] Thymeleaf를 사용하여 상품을 조회, 추가, 수정, 삭제할 수 있는 관리자 화면을 구현한다. + - [ ] 관리자 페이지 구현 + - [ ] 페이지와 상호작용 할 수 있도록 api 수정 From 218f4e5b184da98813aa5be3805606e4771c9935 Mon Sep 17 00:00:00 2001 From: doyooon Date: Fri, 28 Jun 2024 17:18:07 +0900 Subject: [PATCH 05/10] =?UTF-8?q?feat=20home.html=20:=20=EA=B4=80=EB=A6=AC?= =?UTF-8?q?=EC=9E=90=20=ED=8E=98=EC=9D=B4=EC=A7=80=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- src/main/resources/templates/home.html | 93 ++++++++++++++++++++++++++ 2 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 src/main/resources/templates/home.html diff --git a/README.md b/README.md index 1ffb8a937..070afccdd 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,6 @@ - [x] 수정 - [x] 삭제 - [ ] Thymeleaf를 사용하여 상품을 조회, 추가, 수정, 삭제할 수 있는 관리자 화면을 구현한다. - - [ ] 관리자 페이지 구현 + - [x] 관리자 페이지 구현 - [ ] 페이지와 상호작용 할 수 있도록 api 수정 diff --git a/src/main/resources/templates/home.html b/src/main/resources/templates/home.html new file mode 100644 index 000000000..3530d4997 --- /dev/null +++ b/src/main/resources/templates/home.html @@ -0,0 +1,93 @@ + + + + + Title + + + +

Product Management

+ +

Product List

+ + + + + + + + + + + + + + + + + + + +
IDNamePriceImageActions
1Product Name100.00no image + Delete +
+ +

Create New Product

+
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ +
+
+ +

Update Product

+
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ +
+
+ + + From f6ea3011f62ac6c45a9f055971ab5066e9c56a9b Mon Sep 17 00:00:00 2001 From: doyooon Date: Fri, 28 Jun 2024 17:18:26 +0900 Subject: [PATCH 06/10] =?UTF-8?q?feat:=20Product,=20ProductController=20:?= =?UTF-8?q?=20=ED=8E=98=EC=9D=B4=EC=A7=80=EC=99=80=20=EC=83=81=ED=98=B8?= =?UTF-8?q?=EC=9E=91=EC=9A=A9=20=ED=95=A0=20=EC=88=98=20=EC=9E=88=EB=8F=84?= =?UTF-8?q?=EB=A1=9D=20api=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 +-- src/main/java/gift/Application.java | 2 ++ src/main/java/gift/Product.java | 20 ++++++++++++ src/main/java/gift/ProductController.java | 39 ++++++++++++++++------- 4 files changed, 52 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 070afccdd..4331a698c 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ - [x] 추가 - [x] 수정 - [x] 삭제 -- [ ] Thymeleaf를 사용하여 상품을 조회, 추가, 수정, 삭제할 수 있는 관리자 화면을 구현한다. +- [x] Thymeleaf를 사용하여 상품을 조회, 추가, 수정, 삭제할 수 있는 관리자 화면을 구현한다. - [x] 관리자 페이지 구현 - - [ ] 페이지와 상호작용 할 수 있도록 api 수정 + - [x] 페이지와 상호작용 할 수 있도록 api 수정 diff --git a/src/main/java/gift/Application.java b/src/main/java/gift/Application.java index 829be1343..b3ab6a000 100644 --- a/src/main/java/gift/Application.java +++ b/src/main/java/gift/Application.java @@ -2,6 +2,8 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; +import org.springframework.web.filter.HiddenHttpMethodFilter; @SpringBootApplication public class Application { diff --git a/src/main/java/gift/Product.java b/src/main/java/gift/Product.java index a8f2967bc..c6b272eb5 100644 --- a/src/main/java/gift/Product.java +++ b/src/main/java/gift/Product.java @@ -6,6 +6,10 @@ public class Product { int price; String imageUrl; + public Product(){ + + } + public Product(Long id, String name, int price, String imageUrl){ this.id = id; this.name = name; @@ -17,18 +21,34 @@ public Long getId(){ return this.id; } + public void setId(Long id){ + this.id = id; + } + public String getName(){ return this.name; } + public void setName(String name){ + this.name = name; + } + public int getPrice(){ return this.price; } + public void setPrice(int price){ + this.price = price; + } + public String getImageUrl(){ return this.imageUrl; } + public void setImageUrl(String imageUrl){ + this.imageUrl = imageUrl; + } + public void setProduct(Product product){ this.id = product.id; this.name = product.name; diff --git a/src/main/java/gift/ProductController.java b/src/main/java/gift/ProductController.java index a6f8ab99d..3ec189bfd 100644 --- a/src/main/java/gift/ProductController.java +++ b/src/main/java/gift/ProductController.java @@ -1,47 +1,64 @@ package gift; +import java.util.Collection; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; import java.util.HashMap; import java.util.Map; import org.springframework.http.HttpStatus; 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.ModelAttribute; +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.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; -@RestController + +@Controller public class ProductController { private Map products = new HashMap<>(); - @GetMapping("/get") + @GetMapping("/") + public String listProducts(Model model) { + Collection productCollection = products.values(); + model.addAttribute("products", productCollection); + model.addAttribute("newProduct", new Product()); // 새 상품 객체 + model.addAttribute("product", new Product()); // 편집을 위한 빈 객체*/ + return "home"; // Thymeleaf 템플릿 이름 + } + + /*@GetMapping("/") + @ResponseBody public Product getProduct(@RequestParam(value = "id") Long id) { return products.get(id); } - +*/ @PostMapping("/post") - public ResponseEntity createProduct(@RequestBody Product newProduct){ + public String createProduct(@ModelAttribute Product newProduct){ var product = new Product(newProduct.id, newProduct.name, newProduct.price, newProduct.imageUrl); products.put(product.id, product); System.out.println(products.get(product.id).name); - return new ResponseEntity<>(HttpStatus.CREATED); + return "redirect:/"; } - @PutMapping("/put") - public ResponseEntity updateProduct(@RequestBody Product changeProduct){ + @PostMapping ("/update") + public String updateProduct(@ModelAttribute Product changeProduct){ Product changedProduct = products.get(changeProduct.id); changedProduct.setProduct(changeProduct); - return new ResponseEntity<>(HttpStatus.OK); + return "redirect:/"; } - @DeleteMapping("/delete") - public ResponseEntity deleteProductt(@RequestParam(value = "id") Long id){ + @GetMapping ("/delete/{id}") + public String deleteProduct(@PathVariable Long id){ products.remove(id); - return new ResponseEntity<>(HttpStatus.ACCEPTED); + return "redirect:/"; } } From e6883f7f1fd9e2150986bdcb28762fdd4cec9a6f Mon Sep 17 00:00:00 2001 From: doyooon Date: Fri, 28 Jun 2024 17:38:57 +0900 Subject: [PATCH 07/10] =?UTF-8?q?docs=20README.md:=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EC=9A=94=EA=B5=AC=EC=82=AC=ED=95=AD=20=EB=AA=A9=EB=A1=9D=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4331a698c..6e8ebcc84 100644 --- a/README.md +++ b/README.md @@ -7,4 +7,7 @@ - [x] Thymeleaf를 사용하여 상품을 조회, 추가, 수정, 삭제할 수 있는 관리자 화면을 구현한다. - [x] 관리자 페이지 구현 - [x] 페이지와 상호작용 할 수 있도록 api 수정 - +- [ ] 상품 정보를 데이터베이스에 저장한다. + - [ ] H2 데이터베이스를 사용 설정한다. + - [ ] 데이터베이스와 통신할 repository를 생성한다. + - [ ] repository를 통해서 DB에 요청을 보내도록 api 수정 From c1cf02b611641546249da32f727c8209f4dfeec3 Mon Sep 17 00:00:00 2001 From: doyooon Date: Fri, 28 Jun 2024 17:39:47 +0900 Subject: [PATCH 08/10] =?UTF-8?q?feat=20:=20H2=20=EB=8D=B0=EC=9D=B4?= =?UTF-8?q?=ED=84=B0=EB=B2=A0=EC=9D=B4=EC=8A=A4=20=EC=82=AC=EC=9A=A9=20?= =?UTF-8?q?=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 | 7 +++++++ src/main/resources/schema.sql | 6 ++++++ 2 files changed, 13 insertions(+) create mode 100644 src/main/resources/schema.sql diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 3d16b65f4..326792fcc 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1 +1,8 @@ spring.application.name=spring-gift +spring.datasource.url=jdbc:h2:mem:testdb +spring.datasource.driverClassName=org.h2.Driver +spring.datasource.username=sa +spring.datasource.password=password +spring.jpa.database-platform=org.hibernate.dialect.H2Dialect +spring.h2.console.enabled=true +spring.sql.init.mode=always diff --git a/src/main/resources/schema.sql b/src/main/resources/schema.sql new file mode 100644 index 000000000..504a73a8b --- /dev/null +++ b/src/main/resources/schema.sql @@ -0,0 +1,6 @@ +CREATE TABLE IF NOT EXISTS products ( + id BIGINT AUTO_INCREMENT PRIMARY KEY, + name VARCHAR(255) NOT NULL, + price DECIMAL(10, 2) NOT NULL, + image_url VARCHAR(255) +); From 376a6f92d06d1d3940ec65128f954ee249571cdc Mon Sep 17 00:00:00 2001 From: doyooon Date: Fri, 28 Jun 2024 17:42:21 +0900 Subject: [PATCH 09/10] =?UTF-8?q?feat=20ProductRepository=20:=20=EB=8D=B0?= =?UTF-8?q?=EC=9D=B4=ED=84=B0=EB=B2=A0=EC=9D=B4=EC=8A=A4=EC=99=80=20?= =?UTF-8?q?=ED=86=B5=EC=8B=A0=ED=95=A0=20ProductRepository=EB=A5=BC=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1=ED=95=9C=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 +- src/main/java/gift/ProductRepository.java | 50 +++++++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 src/main/java/gift/ProductRepository.java diff --git a/README.md b/README.md index 6e8ebcc84..bb257f830 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,6 @@ - [x] 관리자 페이지 구현 - [x] 페이지와 상호작용 할 수 있도록 api 수정 - [ ] 상품 정보를 데이터베이스에 저장한다. - - [ ] H2 데이터베이스를 사용 설정한다. - - [ ] 데이터베이스와 통신할 repository를 생성한다. + - [x] H2 데이터베이스를 사용 설정한다. + - [x] 데이터베이스와 통신할 repository를 생성한다. - [ ] repository를 통해서 DB에 요청을 보내도록 api 수정 diff --git a/src/main/java/gift/ProductRepository.java b/src/main/java/gift/ProductRepository.java new file mode 100644 index 000000000..7e5a56aaf --- /dev/null +++ b/src/main/java/gift/ProductRepository.java @@ -0,0 +1,50 @@ +package gift; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import javax.sql.DataSource; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jdbc.core.BeanPropertyRowMapper; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.simple.SimpleJdbcInsert; +import org.springframework.stereotype.Repository; + +@Repository +public class ProductRepository { + private final JdbcTemplate jdbcTemplate; + private final SimpleJdbcInsert insertProduct; + + @Autowired + public ProductRepository(JdbcTemplate jdbcTemplate, DataSource dataSource) { + this.jdbcTemplate = jdbcTemplate; + this.insertProduct = new SimpleJdbcInsert(dataSource) + .withTableName("products") + .usingGeneratedKeyColumns("id"); + } + + public List findAll() { + return jdbcTemplate.query("SELECT * FROM products", new BeanPropertyRowMapper<>(Product.class)); + } + + public Product findById(Long id) { + return jdbcTemplate.queryForObject("SELECT * FROM products WHERE id = ?", new BeanPropertyRowMapper<>(Product.class), id); + } + + public long save(Product product) { + Map parameters = new HashMap<>(); + parameters.put("name", product.getName()); + parameters.put("price", product.getPrice()); + parameters.put("image_url", product.getImageUrl()); + return insertProduct.executeAndReturnKey(parameters).longValue(); + } + + public int update(Product product) { + return jdbcTemplate.update("UPDATE products SET name = ?, price = ?, image_url = ? WHERE id = ?", + product.getName(), product.getPrice(), product.getImageUrl(), product.getId()); + } + + public int deleteById(Long id) { + return jdbcTemplate.update("DELETE FROM products WHERE id = ?", id); + } +} From 0ba4c7f356fcd4f64c331b03712de1e0cf0db0d1 Mon Sep 17 00:00:00 2001 From: doyooon Date: Fri, 28 Jun 2024 17:44:14 +0900 Subject: [PATCH 10/10] =?UTF-8?q?feat=20ProductController=20:=20repository?= =?UTF-8?q?=EB=A5=BC=20=ED=86=B5=ED=95=B4=EC=84=9C=20DB=EC=97=90=20?= =?UTF-8?q?=EC=9A=94=EC=B2=AD=EC=9D=84=20=EB=B3=B4=EB=82=B4=EB=8F=84?= =?UTF-8?q?=EB=A1=9D=20api=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 +-- src/main/java/gift/ProductController.java | 34 ++++++----------------- 2 files changed, 10 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index bb257f830..73046af6a 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ - [x] Thymeleaf를 사용하여 상품을 조회, 추가, 수정, 삭제할 수 있는 관리자 화면을 구현한다. - [x] 관리자 페이지 구현 - [x] 페이지와 상호작용 할 수 있도록 api 수정 -- [ ] 상품 정보를 데이터베이스에 저장한다. +- [x] 상품 정보를 데이터베이스에 저장한다. - [x] H2 데이터베이스를 사용 설정한다. - [x] 데이터베이스와 통신할 repository를 생성한다. - - [ ] repository를 통해서 DB에 요청을 보내도록 api 수정 + - [x] repository를 통해서 DB에 요청을 보내도록 api 수정 diff --git a/src/main/java/gift/ProductController.java b/src/main/java/gift/ProductController.java index 3ec189bfd..2705c71a4 100644 --- a/src/main/java/gift/ProductController.java +++ b/src/main/java/gift/ProductController.java @@ -1,62 +1,44 @@ package gift; -import java.util.Collection; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; -import java.util.HashMap; -import java.util.Map; -import org.springframework.http.HttpStatus; -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.ModelAttribute; 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.RequestParam; -import org.springframework.web.bind.annotation.ResponseBody; -import org.springframework.web.bind.annotation.RestController; @Controller public class ProductController { - private Map products = new HashMap<>(); + + @Autowired + private ProductRepository productRepository; @GetMapping("/") public String listProducts(Model model) { - Collection productCollection = products.values(); - model.addAttribute("products", productCollection); + model.addAttribute("products", productRepository.findAll()); model.addAttribute("newProduct", new Product()); // 새 상품 객체 model.addAttribute("product", new Product()); // 편집을 위한 빈 객체*/ return "home"; // Thymeleaf 템플릿 이름 } - /*@GetMapping("/") - @ResponseBody - public Product getProduct(@RequestParam(value = "id") Long id) { - return products.get(id); - } -*/ @PostMapping("/post") public String createProduct(@ModelAttribute Product newProduct){ - var product = new Product(newProduct.id, newProduct.name, newProduct.price, newProduct.imageUrl); - products.put(product.id, product); - System.out.println(products.get(product.id).name); + productRepository.save(newProduct); return "redirect:/"; } @PostMapping ("/update") public String updateProduct(@ModelAttribute Product changeProduct){ - Product changedProduct = products.get(changeProduct.id); - changedProduct.setProduct(changeProduct); + productRepository.update(changeProduct); return "redirect:/"; } @GetMapping ("/delete/{id}") public String deleteProduct(@PathVariable Long id){ - products.remove(id); + productRepository.deleteById(id); return "redirect:/"; }