diff --git a/README.md b/README.md index 8376bdfff..885e268b8 100644 --- a/README.md +++ b/README.md @@ -1 +1,29 @@ -# spring-gift-wishlist \ No newline at end of file +# spring-gift-product +KTC step2 클론코딩 선물하기 구현 + +## Product 객체 +- **id** : 상품 id +- **name** : 상품 이름 +- **price** : 상품 가격 +- **imageUrl** : 상품 사진 url + +## 기능 소개 +### ProductDao +- **selectAllProduct** : 모든 상품 목록 List반환 +- **selectProduct** : id에 해당하는 상품 하나를 반환 +- **insertProduct** : 상품 추가 +- **deleteProduct** : 상품 삭제 +- **updateProduct** : 상품 수정 + +### IndexController +- **index** : index.html 연결 +- **postform** : postform.html 연결 +- **editform** : editform.html 연결 + +### ProductController +- **getProductsController** : 모든 상품 목록 List index.html로 반환 +- **getProductsListController** : 모든 상품 목록 List반환 +- **getProductByIdController** : id에 해당하는 상품 하나를 반환 +- **postProductController** : 상품 추가 후 index로 리다이렉트 +- **deleteProductController** : 상품 삭제 후 index로 리다이렉트 +- **updateProductController** : 상품 수정 후 index로 리다이렉트 \ No newline at end of file diff --git a/build.gradle b/build.gradle index df7db9334..6794099d3 100644 --- a/build.gradle +++ b/build.gradle @@ -28,4 +28,4 @@ dependencies { tasks.named('test') { useJUnitPlatform() -} +} \ No newline at end of file diff --git a/src/main/java/gift/Controller/IndexController.java b/src/main/java/gift/Controller/IndexController.java new file mode 100644 index 000000000..b6bc5e2cb --- /dev/null +++ b/src/main/java/gift/Controller/IndexController.java @@ -0,0 +1,33 @@ +package gift.Controller; + +import gift.model.Product; +import gift.model.ProductDao; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.*; + +@Controller +public class IndexController { + private final ProductDao ProductDao; + + public IndexController(gift.model.ProductDao productDao) { + ProductDao = productDao; + } + + @GetMapping("/") + public String index() { + return "redirect:/api/getAllProducts"; + } + + @GetMapping("/postform") + public String postform(){ + return "postform"; + } + + @PostMapping("/editform/{id}") + public String editform(@PathVariable Long id, Model model){ + Product product = ProductDao.selectProduct(id); + model.addAttribute("product", product); + return "editform"; + } +} diff --git a/src/main/java/gift/Controller/InitCreateTable.java b/src/main/java/gift/Controller/InitCreateTable.java new file mode 100644 index 000000000..7cd1b36e2 --- /dev/null +++ b/src/main/java/gift/Controller/InitCreateTable.java @@ -0,0 +1,38 @@ +package gift.Controller; + +import jakarta.annotation.PostConstruct; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.stereotype.Service; + +//app실행 했을 때 테이블 생성 +@Service +public class InitCreateTable { + + private final JdbcTemplate jdbcTemplate; + + @Autowired + public InitCreateTable(JdbcTemplate jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + } + + @PostConstruct + public void init() { + //System.out.println("init"); + createProductTable(); + //System.out.println("create"); + } + + public void createProductTable() { + var sql = """ + create table product ( + id bigint auto_increment, + name varchar(255), + price int, + image_url varchar(255), + primary key (id) + ) + """; + jdbcTemplate.execute(sql); + } +} diff --git a/src/main/java/gift/Controller/ProductController.java b/src/main/java/gift/Controller/ProductController.java new file mode 100644 index 000000000..a34c6a49e --- /dev/null +++ b/src/main/java/gift/Controller/ProductController.java @@ -0,0 +1,88 @@ +package gift.Controller; + +import gift.model.Product; +import gift.model.ProductDao; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.*; +import org.springframework.ui.Model; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +@Controller +@RequestMapping("/api") +public class ProductController { + //private final Map products = new HashMap<>(); + + long id = 0L; + + private final ProductDao ProductDao; + + public ProductController(gift.model.ProductDao productDao) { + ProductDao = productDao; + } + + //모든 상품 반환 + @GetMapping("/getAllProducts") + public String getProductsController(Model model){ + //List productList = products.values().stream().collect(Collectors.toList()); + List productList = ProductDao.selectAllProduct(); + model.addAttribute("productList", productList); + return "index"; + } + + @GetMapping("/getAllProductList") + @ResponseBody + public List getProductsListController(Model model){ + //List productList = products.values().stream().collect(Collectors.toList()); + List productList = ProductDao.selectAllProduct(); + return productList; + } + + //id 상품 하나 반환 + @GetMapping("/getProduct/{id}") + @ResponseBody + public Product getProductByIdController(@PathVariable Long id){ + Product product = ProductDao.selectProduct(id); + return product; + //return products.get(id); + } + + //상품 추가 + @PostMapping("/postProduct") + public String postProductController(@ModelAttribute Product product){ + id++; + product.setId(id); + ProductDao.insertProduct(product); + //products.put(id, product); + return "redirect:/api/getAllProducts"; + } + + //상품 삭제 + @GetMapping("/deleteProduct/{id}") + public String deleteProductController(@PathVariable Long id){ + ProductDao.deleteProduct(id); + //products.remove(id); + return "redirect:/api/getAllProducts"; + } + + //상품 업데이트 + @PostMapping("/updateProduct/{id}") + public String updateProductController(@PathVariable Long id, @ModelAttribute Product newProduct){ + Product oldProduct = ProductDao.selectProduct(id); + //Product oldProduct = products.get(id); + if(newProduct.getName() != null && !newProduct.getName().isEmpty()){ + oldProduct.setName(newProduct.getName()); + } + if(newProduct.getPrice() != null){ + oldProduct.setPrice(newProduct.getPrice()); + } + if(newProduct.getImageUrl() != null && !newProduct.getImageUrl().isEmpty()){ + oldProduct.setImageUrl(newProduct.getImageUrl()); + } + //products.replace(id, oldProduct); + ProductDao.updateProduct(oldProduct); + return "redirect:/api/getAllProducts"; + } +} diff --git a/src/main/java/gift/model/Product.java b/src/main/java/gift/model/Product.java new file mode 100644 index 000000000..0f71e7a2d --- /dev/null +++ b/src/main/java/gift/model/Product.java @@ -0,0 +1,50 @@ +package gift.model; + +public class Product { + private long id; + private String name; + private Integer price; + private 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 Product() { + } + + public String getImageUrl() { + return imageUrl; + } + + public void setImageUrl(String imageUrl) { + this.imageUrl = imageUrl; + } + + public Integer getPrice() { + return price; + } + + public void setPrice(Integer price) { + this.price = price; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } +} diff --git a/src/main/java/gift/model/ProductDao.java b/src/main/java/gift/model/ProductDao.java new file mode 100644 index 000000000..4b7cfff96 --- /dev/null +++ b/src/main/java/gift/model/ProductDao.java @@ -0,0 +1,74 @@ +package gift.model; + +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.RowMapper; +import org.springframework.stereotype.Repository; + +import java.util.ArrayList; +import java.util.List; + +@Repository +public class ProductDao { + private final JdbcTemplate jdbcTemplate; + + public ProductDao(JdbcTemplate jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + } + + //상품 테이블 생성 + public void createCustomerTable(){ + var sql = """ + create table customer ( + id bigint, + name varchar(255), + price int, + image_url varchar(255), + primary key (id) + ) + """; + jdbcTemplate.execute(sql); //여닫는 과정X, throws가 안나옴(내부에서 관리) excetion 추상화 해서 제공 + } + + //데이터베이스 반환결과인 Result Set 객체로 변환 + public RowMapper ProductRowMapper() { + return ( (resultSet, rowNum) -> { + Product product = new Product(); + product.setId(resultSet.getLong("id")); + product.setName(resultSet.getString("name")); + product.setPrice(resultSet.getInt("price")); + product.setImageUrl(resultSet.getString("image_url")); + return product; + }); + } + + //모든 상품 리스트 반환 + public List selectAllProduct(){ + var sql = "select * from product"; + List list = new ArrayList<>(); + return jdbcTemplate.query(sql, ProductRowMapper(), list.toArray()); + } + + //상품 하나 반환 + public Product selectProduct(Long id){ + var sql = "select id, name, price, image_url from product where id = ?"; + return jdbcTemplate.queryForObject(sql, ProductRowMapper(), id); + } + + //상품 추가 + public void insertProduct(Product product){ + var sql = "insert into product (id, name, price, image_url) values (?, ?, ?, ?)"; + jdbcTemplate.update(sql, product.getId(), product.getName(), product.getPrice(), product.getImageUrl()); + } + + //상품 삭제 + public void deleteProduct(Long id){ + var sql = "delete from product where id = ?"; + jdbcTemplate.update(sql, id); + } + + //상품 업데이트 + public void updateProduct(Product product){ + var sql = "update product set name = ?, price = ?, image_url = ? where id = ?"; + jdbcTemplate.update(sql, product.getName(), product.getPrice(), product.getImageUrl(), product.getId()); + } +} diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 3d16b65f4..6f30f4bc4 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1 +1,2 @@ spring.application.name=spring-gift +spring.h2.console.enabled=true \ No newline at end of file diff --git a/src/main/resources/templates/editform.html b/src/main/resources/templates/editform.html new file mode 100644 index 000000000..406df21bd --- /dev/null +++ b/src/main/resources/templates/editform.html @@ -0,0 +1,40 @@ + + + + + + Edit Form + + +

Edit

+
+ +
+
+product ID: +
+ name:
+ price:
+ imageUrl:
+ +
+
+ + \ No newline at end of file diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html new file mode 100644 index 000000000..70d912d2c --- /dev/null +++ b/src/main/resources/templates/index.html @@ -0,0 +1,80 @@ + + + + Admin Page + + + +

+ Admin +

+
+
+ +
+ + + + + + + + + + + + + + + + + + + + + +
idnamepriceimageUrleditdelete
+
+ +
+
+
+ +
+
+
+ + \ No newline at end of file diff --git a/src/main/resources/templates/postform.html b/src/main/resources/templates/postform.html new file mode 100644 index 000000000..fd4d444f7 --- /dev/null +++ b/src/main/resources/templates/postform.html @@ -0,0 +1,39 @@ + + + + + + Post Form + + +

Post

+
+ +
+
+
+ name:
+ price:
+ imageUrl:
+ +
+
+ + \ No newline at end of file diff --git a/src/test/java/gift/ApplicationTest.java b/src/test/java/gift/ApplicationTest.java new file mode 100644 index 000000000..e57f52bbd --- /dev/null +++ b/src/test/java/gift/ApplicationTest.java @@ -0,0 +1,5 @@ +package gift; + +public class ApplicationTest { + +}