From b7a7064a1b57941f082df25388e43eeb8d22aaf5 Mon Sep 17 00:00:00 2001 From: canyos <4581974@naver.com> Date: Mon, 1 Jul 2024 14:19:16 +0900 Subject: [PATCH 01/10] step0 --- build.gradle | 1 + src/main/java/gift/Application.java | 16 ++- src/main/java/gift/Product.java | 40 ++++++ src/main/java/gift/ProductController.java | 132 ++++++++++++++++++ src/main/resources/templates/admin/add.html | 29 ++++ .../resources/templates/admin/delete.html | 18 +++ .../resources/templates/admin/modify.html | 31 ++++ .../resources/templates/admin/products.html | 30 ++++ src/test/java/gift/Test.java | 52 +++++++ 9 files changed, 348 insertions(+), 1 deletion(-) create mode 100644 src/main/java/gift/Product.java create mode 100644 src/main/java/gift/ProductController.java create mode 100644 src/main/resources/templates/admin/add.html create mode 100644 src/main/resources/templates/admin/delete.html create mode 100644 src/main/resources/templates/admin/modify.html create mode 100644 src/main/resources/templates/admin/products.html create mode 100644 src/test/java/gift/Test.java diff --git a/build.gradle b/build.gradle index df7db9334..7be0f17d6 100644 --- a/build.gradle +++ b/build.gradle @@ -18,6 +18,7 @@ repositories { } dependencies { + implementation 'org.thymeleaf:thymeleaf:3.1.2.RELEASE' implementation 'org.springframework.boot:spring-boot-starter-jdbc' implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' implementation 'org.springframework.boot:spring-boot-starter-web' diff --git a/src/main/java/gift/Application.java b/src/main/java/gift/Application.java index 61603cca0..14cb35bac 100644 --- a/src/main/java/gift/Application.java +++ b/src/main/java/gift/Application.java @@ -1,11 +1,25 @@ 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 { + @Autowired + JdbcTemplate jdbcTemplate; + public static void main(String[] args) { SpringApplication.run(Application.class, args); } + + @Override + public void run(String... strings) throws Exception { + jdbcTemplate.execute("DROP TABLE product IF EXISTS"); + jdbcTemplate.execute("CREATE TABLE product(id SERIAL, name VARCHAR(255), price int, imageUrl varchar(255))"); + jdbcTemplate.execute("Drop table option IF exists"); + jdbcTemplate.execute("CREAte table option (id int, option varchar(255))"); + } } diff --git a/src/main/java/gift/Product.java b/src/main/java/gift/Product.java new file mode 100644 index 000000000..8ff349312 --- /dev/null +++ b/src/main/java/gift/Product.java @@ -0,0 +1,40 @@ +package gift; + +import com.fasterxml.jackson.annotation.JsonAutoDetect; + +@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY) +public class Product { + int id; + String name; + int price; + String imageUrl; + String option; + + public int getId() { + return id; + } + + public String getName() { + return name; + } + + public int getPrice() { + return price; + } + + public String getImageUrl() { + return imageUrl; + } + + public String getOption() { + return option; + } + + public Product(int id, String name, int price, String imageUrl,String option) { + this.id = id; + this.name = name; + this.price = price; + this.imageUrl = imageUrl; + this.option = option; + } +} diff --git a/src/main/java/gift/ProductController.java b/src/main/java/gift/ProductController.java new file mode 100644 index 000000000..aa3819c1c --- /dev/null +++ b/src/main/java/gift/ProductController.java @@ -0,0 +1,132 @@ +package gift; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.servlet.ModelAndView; + +import java.util.Arrays; +import java.util.List; + + +@RestController +public class ProductController { + //private Map products = new HashMap<>(); + //private static Long count = 1L; + + @Autowired + JdbcTemplate jdbcTemplate; + + @GetMapping("/admin/products") + public ModelAndView adminProducts(Model model){ + var sql = "select * from PRODUCT inner join option on product.id = option.id"; + List products = jdbcTemplate.query(sql,(rs, rowNum) -> new Product( + rs.getInt("id"), + rs.getString("name"), + rs.getInt("price"), + rs.getString("imageUrl"), + rs.getString("option") + )); + model.addAttribute("products", products); + return new ModelAndView("admin/products"); + } + @GetMapping("/admin/add") + public ModelAndView adminProductsAdd(Model model){ + return new ModelAndView("admin/add"); + } + @GetMapping("/admin/modify") + public ModelAndView adminProductsModify(Model model){ + return new ModelAndView("admin/modify"); + } + @GetMapping("/admin/delete") + public ModelAndView adminProductsDelete(Model model){ + return new ModelAndView("admin/delete"); + } + + @GetMapping("/api/products") + public String getProducts() { + + String allProducts = ""; + ObjectMapper objectMapper = new ObjectMapper(); + var sql = "select * from product inner join option on product.id = option.id"; + List products = jdbcTemplate.query(sql,(rs, rowNum) -> new Product( + rs.getInt("id"), + rs.getString("name"), + rs.getInt("price"), + rs.getString("imageUrl"), + rs.getString("option") + )); + try { + allProducts = objectMapper.writeValueAsString(products); + } catch (JsonProcessingException e) { + e.printStackTrace(); + } + return allProducts; + } + + @PostMapping("/api/products/add") + public void addProduct(@RequestParam("id") int id, @RequestParam("name") String name, + @RequestParam("price") int price, @RequestParam("imageUrl") String imageUrl, + @RequestParam("options") String options) { + + var sql = "insert into product(id,name,price,imageUrl) values (?,?,?,?)"; + jdbcTemplate.update(sql, id,name,price,imageUrl); + //jdbcTemplate.execute(sql); + + List optionList = Arrays.stream(options.split(",")).toList(); + for(String opt : optionList){ + sql = "insert into option(id,option) values(?,?)"; + jdbcTemplate.update(sql, id, opt); + //jdbcTemplate.execute(sql); + } + //products.put(count, product); + //count++; + } + + @PostMapping("/api/products/delete") + public void deleteProduct(@RequestParam("id") int id) { +// Iterator> iterator = products.entrySet().iterator(); +// while (iterator.hasNext()) { +// Map.Entry entry = iterator.next(); +// if (entry.getValue().id == id) { +// iterator.remove(); +// } +// } + var sql = "delete from product where id = ?"; + jdbcTemplate.update(sql, id); + //jdbcTemplate.execute(sql); + + sql ="delete from option where id = ?"; + jdbcTemplate.update(sql, id); + //jdbcTemplate.execute(sql); + + } + + @PostMapping("/api/products/modify") + public void modifyProduct(@RequestParam("id") int id, @RequestParam("name") String name, @RequestParam("price") int price, @RequestParam("imageUrl") String imageUrl, @RequestParam("options") String options) { +// Iterator> iterator = products.entrySet().iterator(); +// while (iterator.hasNext()) { +// Map.Entry entry = iterator.next(); +// if (entry.getValue().id == id) { +// entry.getValue().name = name; +// entry.getValue().price = price; +// entry.getValue().imageUrl = imageUrl; +// entry.getValue().options = Arrays.stream(options.split(",")).toList(); +// } +// } + deleteProduct(id); + addProduct(id, name, price, imageUrl, options); + } + +// public void setProducts(Map products) { +// this.products = products; +// } + +} + diff --git a/src/main/resources/templates/admin/add.html b/src/main/resources/templates/admin/add.html new file mode 100644 index 000000000..137ec9214 --- /dev/null +++ b/src/main/resources/templates/admin/add.html @@ -0,0 +1,29 @@ + + + + + add product + + + +

Add New Product

+
+ + +
+ + +
+ + +
+ + +
+ + +
+ +
+ + \ No newline at end of file diff --git a/src/main/resources/templates/admin/delete.html b/src/main/resources/templates/admin/delete.html new file mode 100644 index 000000000..f19cc9ebe --- /dev/null +++ b/src/main/resources/templates/admin/delete.html @@ -0,0 +1,18 @@ + + + + + delete product + + + +

Delete Product

+
+ + + +
+ +
+ + \ No newline at end of file diff --git a/src/main/resources/templates/admin/modify.html b/src/main/resources/templates/admin/modify.html new file mode 100644 index 000000000..fbe01477f --- /dev/null +++ b/src/main/resources/templates/admin/modify.html @@ -0,0 +1,31 @@ + + + + + modify product + + + +

Update Product

+
+ + + +
+ + +
+ + +
+ + +
+ + +
+ +
+ + + \ No newline at end of file diff --git a/src/main/resources/templates/admin/products.html b/src/main/resources/templates/admin/products.html new file mode 100644 index 000000000..230667cb2 --- /dev/null +++ b/src/main/resources/templates/admin/products.html @@ -0,0 +1,30 @@ + + + + Product List + + + +

Product List

+ + + + + + + + + + + + + + + + + + + +
IDNamePriceImageUrlOptions
+ + diff --git a/src/test/java/gift/Test.java b/src/test/java/gift/Test.java new file mode 100644 index 000000000..d68205e3e --- /dev/null +++ b/src/test/java/gift/Test.java @@ -0,0 +1,52 @@ +package gift; + + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.jdbc.core.JdbcTemplate; + +import java.sql.JDBCType; +import java.util.HashMap; +import java.util.Map; + +@SpringBootTest +public class Test { + @Autowired + ProductController productController; + @Autowired + JdbcTemplate jdbcTemplate; + + @org.junit.jupiter.api.Test + public void getTest() { + //ProductController productController = new ProductController(); + productController.addProduct( 1,"1",1,"1","1"); + productController.addProduct( 2,"2",2,"2","2"); + System.out.println(productController.getProducts()); + } + + @org.junit.jupiter.api.Test + public void addTest(){ + //ProductController productController = new ProductController(); + productController.addProduct(1,"1",1,"1","1"); + System.out.println(productController.getProducts()); + } + + @org.junit.jupiter.api.Test + public void deleteTest() { + //ProductController productController = new ProductController(); + productController.addProduct( 1,"1",1,"1","1"); + productController.addProduct( 2,"2",2,"2","2"); + productController.deleteProduct(1); + System.out.println(productController.getProducts()); + } + + @org.junit.jupiter.api.Test + public void modifyTest(){ + //ProductController productController = new ProductController(); + productController.addProduct( 1,"1",1,"1","1"); + productController.addProduct( 2,"2",2,"2","2"); + productController.modifyProduct(1,"modifyname",3,"3","3"); + System.out.println(productController.getProducts()); + } +} From 492c2ffb6779337932c89394d0a89ff849ed598f Mon Sep 17 00:00:00 2001 From: canyos <4581974@naver.com> Date: Mon, 1 Jul 2024 14:29:24 +0900 Subject: [PATCH 02/10] step0 --- build.gradle | 2 + src/main/java/gift/DTO/SaveProductDTO.java | 34 +++++ src/main/java/gift/ProductController.java | 132 ------------------ .../gift/controller/ProductController.java | 78 +++++++++++ src/main/java/gift/entity/Option.java | 23 +++ src/main/java/gift/{ => entity}/Product.java | 2 +- .../gift/repository/ProductRepository.java | 87 ++++++++++++ .../java/gift/service/ProductService.java | 75 ++++++++++ 8 files changed, 300 insertions(+), 133 deletions(-) create mode 100644 src/main/java/gift/DTO/SaveProductDTO.java delete mode 100644 src/main/java/gift/ProductController.java create mode 100644 src/main/java/gift/controller/ProductController.java create mode 100644 src/main/java/gift/entity/Option.java rename src/main/java/gift/{ => entity}/Product.java (97%) create mode 100644 src/main/java/gift/repository/ProductRepository.java create mode 100644 src/main/java/gift/service/ProductService.java diff --git a/build.gradle b/build.gradle index 7be0f17d6..1e9abd913 100644 --- a/build.gradle +++ b/build.gradle @@ -18,7 +18,9 @@ repositories { } dependencies { + implementation 'org.thymeleaf:thymeleaf:3.1.2.RELEASE' + implementation 'org.springframework.boot:spring-boot-starter-validation:2.7.0' implementation 'org.springframework.boot:spring-boot-starter-jdbc' implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' implementation 'org.springframework.boot:spring-boot-starter-web' diff --git a/src/main/java/gift/DTO/SaveProductDTO.java b/src/main/java/gift/DTO/SaveProductDTO.java new file mode 100644 index 000000000..7b72c930d --- /dev/null +++ b/src/main/java/gift/DTO/SaveProductDTO.java @@ -0,0 +1,34 @@ +package gift.DTO; + +import com.fasterxml.jackson.annotation.JsonAutoDetect; + +@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY) +public class SaveProductDTO { + int id; + String name; + int price; + String imageUrl; + + public int getId() { + return id; + } + + public String getName() { + return name; + } + + public int getPrice() { + return price; + } + + public String getImageUrl() { + return imageUrl; + } + + public SaveProductDTO(int id, String name, int price, String imageUrl) { + this.id = id; + this.name = name; + this.price = price; + this.imageUrl = imageUrl; + } +} diff --git a/src/main/java/gift/ProductController.java b/src/main/java/gift/ProductController.java deleted file mode 100644 index aa3819c1c..000000000 --- a/src/main/java/gift/ProductController.java +++ /dev/null @@ -1,132 +0,0 @@ -package gift; - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.jdbc.core.JdbcTemplate; -import org.springframework.ui.Model; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; -import org.springframework.web.servlet.ModelAndView; - -import java.util.Arrays; -import java.util.List; - - -@RestController -public class ProductController { - //private Map products = new HashMap<>(); - //private static Long count = 1L; - - @Autowired - JdbcTemplate jdbcTemplate; - - @GetMapping("/admin/products") - public ModelAndView adminProducts(Model model){ - var sql = "select * from PRODUCT inner join option on product.id = option.id"; - List products = jdbcTemplate.query(sql,(rs, rowNum) -> new Product( - rs.getInt("id"), - rs.getString("name"), - rs.getInt("price"), - rs.getString("imageUrl"), - rs.getString("option") - )); - model.addAttribute("products", products); - return new ModelAndView("admin/products"); - } - @GetMapping("/admin/add") - public ModelAndView adminProductsAdd(Model model){ - return new ModelAndView("admin/add"); - } - @GetMapping("/admin/modify") - public ModelAndView adminProductsModify(Model model){ - return new ModelAndView("admin/modify"); - } - @GetMapping("/admin/delete") - public ModelAndView adminProductsDelete(Model model){ - return new ModelAndView("admin/delete"); - } - - @GetMapping("/api/products") - public String getProducts() { - - String allProducts = ""; - ObjectMapper objectMapper = new ObjectMapper(); - var sql = "select * from product inner join option on product.id = option.id"; - List products = jdbcTemplate.query(sql,(rs, rowNum) -> new Product( - rs.getInt("id"), - rs.getString("name"), - rs.getInt("price"), - rs.getString("imageUrl"), - rs.getString("option") - )); - try { - allProducts = objectMapper.writeValueAsString(products); - } catch (JsonProcessingException e) { - e.printStackTrace(); - } - return allProducts; - } - - @PostMapping("/api/products/add") - public void addProduct(@RequestParam("id") int id, @RequestParam("name") String name, - @RequestParam("price") int price, @RequestParam("imageUrl") String imageUrl, - @RequestParam("options") String options) { - - var sql = "insert into product(id,name,price,imageUrl) values (?,?,?,?)"; - jdbcTemplate.update(sql, id,name,price,imageUrl); - //jdbcTemplate.execute(sql); - - List optionList = Arrays.stream(options.split(",")).toList(); - for(String opt : optionList){ - sql = "insert into option(id,option) values(?,?)"; - jdbcTemplate.update(sql, id, opt); - //jdbcTemplate.execute(sql); - } - //products.put(count, product); - //count++; - } - - @PostMapping("/api/products/delete") - public void deleteProduct(@RequestParam("id") int id) { -// Iterator> iterator = products.entrySet().iterator(); -// while (iterator.hasNext()) { -// Map.Entry entry = iterator.next(); -// if (entry.getValue().id == id) { -// iterator.remove(); -// } -// } - var sql = "delete from product where id = ?"; - jdbcTemplate.update(sql, id); - //jdbcTemplate.execute(sql); - - sql ="delete from option where id = ?"; - jdbcTemplate.update(sql, id); - //jdbcTemplate.execute(sql); - - } - - @PostMapping("/api/products/modify") - public void modifyProduct(@RequestParam("id") int id, @RequestParam("name") String name, @RequestParam("price") int price, @RequestParam("imageUrl") String imageUrl, @RequestParam("options") String options) { -// Iterator> iterator = products.entrySet().iterator(); -// while (iterator.hasNext()) { -// Map.Entry entry = iterator.next(); -// if (entry.getValue().id == id) { -// entry.getValue().name = name; -// entry.getValue().price = price; -// entry.getValue().imageUrl = imageUrl; -// entry.getValue().options = Arrays.stream(options.split(",")).toList(); -// } -// } - deleteProduct(id); - addProduct(id, name, price, imageUrl, options); - } - -// public void setProducts(Map products) { -// this.products = products; -// } - -} - diff --git a/src/main/java/gift/controller/ProductController.java b/src/main/java/gift/controller/ProductController.java new file mode 100644 index 000000000..8f6c4a862 --- /dev/null +++ b/src/main/java/gift/controller/ProductController.java @@ -0,0 +1,78 @@ +package gift.controller; + +import gift.entity.Product; +import gift.service.ProductService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.servlet.ModelAndView; + +import java.util.List; + + +@RestController +public class ProductController { + //private Map products = new HashMap<>(); + //private static Long count = 1L; + + @Autowired + private JdbcTemplate jdbcTemplate; + @Autowired + private ProductService productService; + + @GetMapping("/admin/products") + public ModelAndView adminProducts(Model model){ + List products = productService.getAllProducts(); + model.addAttribute("products", products); + return new ModelAndView("admin/products"); + } + + @GetMapping("/admin/add") + public ModelAndView adminProductsAdd(Model model){ + return new ModelAndView("admin/add"); + } + + @GetMapping("/admin/modify") + public ModelAndView adminProductsModify(Model model){ + return new ModelAndView("admin/modify"); + } + + @GetMapping("/admin/delete") + public ModelAndView adminProductsDelete(Model model){ + return new ModelAndView("admin/delete"); + } + + @GetMapping("/api/products") + public String getProducts() { + String jsonProducts = productService.getJsonAllProducts(); + return jsonProducts; + } + + @PostMapping("/api/products/add") + public void addProduct(@RequestParam("id") int id, @RequestParam("name") String name, + @RequestParam("price") int price, @RequestParam("imageUrl") String imageUrl, + @RequestParam("options") String options) { + productService.saveProduct(new Product(id,name,price,imageUrl,options)); + } + + @PostMapping("/api/products/delete") + public void deleteProduct(@RequestParam("id") int id) { + productService.deleteProduct(id); + } + + @PostMapping("/api/products/modify") + public void modifyProduct(@RequestParam("id") int id, @RequestParam("name") String name, + @RequestParam("price") int price, @RequestParam("imageUrl") String imageUrl, + @RequestParam("options") String options) { + productService.modifyProduct(new Product(id,name,price,imageUrl,options)); + } + + @GetMapping("/api/product/{id}") + public String getProduct(@PathVariable int id){ + String product = productService.getProductByID(id); + return product; + } + +} + diff --git a/src/main/java/gift/entity/Option.java b/src/main/java/gift/entity/Option.java new file mode 100644 index 000000000..fc02f539c --- /dev/null +++ b/src/main/java/gift/entity/Option.java @@ -0,0 +1,23 @@ +package gift.entity; + +import com.fasterxml.jackson.annotation.JsonAutoDetect; + +@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY) +public class Option { + int id; + String option; + + public int getId() { + return id; + } + + public String getOption() { + return option; + } + + public Option(int id, String option) { + this.id = id; + this.option = option; + } + +} \ No newline at end of file diff --git a/src/main/java/gift/Product.java b/src/main/java/gift/entity/Product.java similarity index 97% rename from src/main/java/gift/Product.java rename to src/main/java/gift/entity/Product.java index 8ff349312..0fb0f0170 100644 --- a/src/main/java/gift/Product.java +++ b/src/main/java/gift/entity/Product.java @@ -1,4 +1,4 @@ -package gift; +package gift.entity; import com.fasterxml.jackson.annotation.JsonAutoDetect; diff --git a/src/main/java/gift/repository/ProductRepository.java b/src/main/java/gift/repository/ProductRepository.java new file mode 100644 index 000000000..b6b434110 --- /dev/null +++ b/src/main/java/gift/repository/ProductRepository.java @@ -0,0 +1,87 @@ +package gift.repository; + +import gift.DTO.SaveProductDTO; +import gift.entity.Option; +import gift.entity.Product; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.stereotype.Repository; + +import java.util.List; + +@Repository +public class ProductRepository { + @Autowired + JdbcTemplate jdbcTemplate; + + public List getAllProduct(){ + var sql = "select * from PRODUCT inner join option on product.id = option.id"; + List products = jdbcTemplate.query(sql,(rs, rowNum) -> new Product( + rs.getInt("id"), + rs.getString("name"), + rs.getInt("price"), + rs.getString("imageUrl"), + rs.getString("option") + )); + return products; + } + + public void saveProduct(SaveProductDTO saveProductDTO) { + var sql = "insert into product(id,name,price,imageUrl) values (?,?,?,?)"; + jdbcTemplate.update(sql, saveProductDTO.getId(),saveProductDTO.getName(),saveProductDTO.getPrice(),saveProductDTO.getImageUrl()); + } + + public void saveOption(Option option) { + var sql = "insert into option(id,option) values(?,?)"; + jdbcTemplate.update(sql, option.getId(), option.getOption()); + } + + public void deleteProductByID(int id) { + var sql = "delete from product where id = ?"; + jdbcTemplate.update(sql, id); + } + + public void deleteOptionsByID(int id) { + var sql ="delete from option where id = ?"; + jdbcTemplate.update(sql, id); + } + + public List findProductByID(int id) { + var sql = "select * from PRODUCT inner join option on product.id = option.id where product.id = ?"; + List products = jdbcTemplate.query(sql,new Object[]{id}, (rs, rowNum) -> new Product( + rs.getInt("id"), + rs.getString("name"), + rs.getInt("price"), + rs.getString("imageUrl"), + rs.getString("option") + )); + return products; + } + public List