-
Notifications
You must be signed in to change notification settings - Fork 122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
전남대 BE_안원모 1주차 과제(3단계) #184
Open
Wonmoan
wants to merge
17
commits into
kakao-tech-campus-2nd-step2:main
Choose a base branch
from
Wonmoan:step3
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
73f2f9c
Feat : H2 데이터베이스 설정
Wonmoan 50bb6a2
Docs : 데이터베이스 초기화
Wonmoan a4af7c0
Feat : Product 도메인 모델 구현
Wonmoan 8e61113
Feat : JdbcTemplate 사용한 repository 구현
Wonmoan f208b22
Feat : 삼품 조회 기능 구현
Wonmoan 1695887
상품 수정,삭제,저장 기능 구현
Wonmoan adc89e4
Feat : 서비스 및 컨트롤러 생성
Wonmoan 403e6aa
Feat : 모든 제품 검색해 list 형태로 반환하는 메서드 구현
Wonmoan f4f8eaf
Feat : id 검색하여 product 객체로 반환하는 메서드 구현
Wonmoan d82b782
Feat : 세로운 제품 데이터베이스에 저장하는 메서드 구현
Wonmoan 83b7bb4
Feat : 기존 제품 업데이트 & 해당 id 제품 db에서 삭제 메서드 구현
Wonmoan 09be87f
feat : ProdUCT Controller 구현
Wonmoan d7fa466
Refactor : Java 버전 변경 (21->17)
Wonmoan 775a515
Docs : READM.MD 작성
Wonmoan bc0f8b6
Refactor : 코드 리뷰 피드백 수정 (가독성,불필요 코드 삭제)_Reviwers : Eugine Park
Wonmoan 6d42453
Fix : queryForObject 메서드 deprecated isssue로수정
Wonmoan 216a04b
Fix : spring.datasource.platform deprecated isssue로수정
Wonmoan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,32 @@ | ||
# spring-gift-product | ||
# spring-gift-product | ||
## 제품 정보를 관리하는 간단한 애플리케이션 | ||
H2 데이터베이스를 사용하여 제품 정보를 저장하며, RESTful API를 통해 제품 데이터를 관리할 수 있습니다. | ||
|
||
### 기능 | ||
- 제품에 대한 CRUD 작업 (생성, 조회, 수정, 삭제) | ||
- H2 데이터베이스 초기화 및 샘플 데이터 삽입 | ||
- 제품 관리를 위한 RESTful API 제공 | ||
- H2 데이터베이스 콘솔을 통한 데이터베이스 관리 | ||
|
||
### REST API 사용법 | ||
Postman 또는 브라우저를 사용하여 다음 REST API 엔드포인트를 테스트할 수 있습니다: | ||
|
||
- 모든 제품 조회: GET /products | ||
- 특정 제품 조회: GET /products/{id} | ||
- 제품 생성: POST /products | ||
Body (JSON): | ||
|
||
{ | ||
"name": "New Product", | ||
"price": 15.99, | ||
"description": "Description for new product" | ||
} | ||
|
||
제품 수정: PUT /products/{id} | ||
Body (JSON): | ||
{ | ||
"name": "Updated Product", | ||
"price": 18.99, | ||
"description": "Updated description" | ||
} | ||
제품 삭제: DELETE /products/{id} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package gift; | ||
|
||
import java.math.BigDecimal; | ||
|
||
public class Product { | ||
private Long id; | ||
private String name; | ||
private BigDecimal price; | ||
private String description; | ||
|
||
// Getters and Setters | ||
public Long getId() { | ||
return id; | ||
} | ||
|
||
public void setId(Long id) { | ||
this.id = id; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public BigDecimal getPrice() { | ||
return price; | ||
} | ||
|
||
public void setPrice(BigDecimal price) { | ||
this.price = price; | ||
} | ||
|
||
public String getDescription() { | ||
return description; | ||
} | ||
|
||
public void setDescription(String description) { | ||
this.description = description; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package gift; | ||
|
||
import org.springframework.web.bind.annotation.*; | ||
|
||
import java.util.List; | ||
|
||
@RestController | ||
@RequestMapping("/products") | ||
public class ProductController { | ||
|
||
private final ProductService productService; | ||
|
||
public ProductController(ProductService productService) { | ||
this.productService = productService; | ||
} | ||
|
||
@GetMapping | ||
public List<Product> getAllProducts() { | ||
return productService.getAllProducts(); | ||
} | ||
|
||
@GetMapping("/{id}") | ||
public Product getProductById(@PathVariable Long id) { | ||
return productService.getProductById(id); | ||
} | ||
|
||
@PostMapping | ||
public void createProduct(@RequestBody Product product) { | ||
productService.saveProduct(product); | ||
} | ||
|
||
@PutMapping("/{id}") | ||
public void updateProduct(@PathVariable Long id, @RequestBody Product product) { | ||
product.setId(id); | ||
productService.updateProduct(product); | ||
} | ||
|
||
@DeleteMapping("/{id}") | ||
public void deleteProduct(@PathVariable Long id) { | ||
productService.deleteProduct(id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package gift; | ||
import org.springframework.jdbc.core.ResultSetExtractor; | ||
import org.springframework.dao.DataAccessException; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.jdbc.core.RowMapper; | ||
import org.springframework.stereotype.Repository; | ||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
import java.util.List; | ||
|
||
@Repository | ||
public class ProductRepository { | ||
|
||
private final JdbcTemplate jdbcTemplate; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
public ProductRepository(JdbcTemplate jdbcTemplate) { | ||
this.jdbcTemplate = jdbcTemplate; | ||
} | ||
|
||
private static final class ProductRowMapper implements RowMapper<Product> { | ||
|
||
@Override | ||
public Product mapRow(ResultSet rs, int rowNum) throws SQLException { | ||
Product product = new Product(); | ||
product.setId(rs.getLong("id")); | ||
product.setName(rs.getString("name")); | ||
product.setPrice(rs.getBigDecimal("price")); | ||
product.setDescription(rs.getString("description")); | ||
return product; | ||
} | ||
} | ||
Comment on lines
+20
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 잘 만들어주셨네요! |
||
|
||
public List<Product> findAll() { | ||
return jdbcTemplate.query("SELECT * FROM product", new ProductRowMapper()); | ||
} | ||
|
||
public Product findById(Long id) { | ||
return jdbcTemplate.query( | ||
"SELECT * FROM product WHERE id = ?", | ||
new Object[]{id}, | ||
new ResultSetExtractor<Product>() { | ||
@Override | ||
public Product extractData(ResultSet rs) throws SQLException, DataAccessException { | ||
if (rs.next()) { | ||
Product product = new Product(); | ||
product.setId(rs.getLong("id")); | ||
product.setName(rs.getString("name")); | ||
product.setPrice(rs.getBigDecimal("price")); | ||
product.setDescription(rs.getString("description")); | ||
return product; | ||
} | ||
return null; // or throw an exception if the product is not found | ||
} | ||
} | ||
); | ||
} | ||
|
||
public void save(Product product) { | ||
jdbcTemplate.update("INSERT INTO product (name, price, description) VALUES (?, ?, ?)", | ||
product.getName(), product.getPrice(), product.getDescription()); | ||
} | ||
|
||
public void update(Product product) { | ||
jdbcTemplate.update("UPDATE product SET name = ?, price = ?, description = ? WHERE id = ?", | ||
product.getName(), product.getPrice(), product.getDescription(), product.getId()); | ||
} | ||
|
||
public void deleteById(Long id) { | ||
jdbcTemplate.update("DELETE FROM product WHERE id = ?", id); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package gift; | ||
|
||
import java.util.List; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class ProductService { | ||
|
||
private final ProductRepository productRepository; | ||
|
||
public ProductService(ProductRepository productRepository) { | ||
this.productRepository = productRepository; | ||
} | ||
|
||
public List<Product> getAllProducts() { | ||
return productRepository.findAll(); | ||
} | ||
|
||
public Product getProductById(Long id) { | ||
return productRepository.findById(id); | ||
} | ||
|
||
public void saveProduct(Product product) { | ||
productRepository.save(product); | ||
} | ||
|
||
public void updateProduct(Product product) { | ||
productRepository.update(product); | ||
} | ||
|
||
public void deleteProduct(Long id) { | ||
productRepository.deleteById(id); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,11 @@ | ||
spring.application.name=spring-gift | ||
# H2 Database configuration | ||
spring.datasource.url=jdbc:h2:mem:testdb | ||
spring.datasource.driverClassName=org.h2.Driver | ||
spring.datasource.username=sa | ||
spring.datasource.password=password | ||
|
||
# Initialize the database with schema.sql and data.sql | ||
spring.sql.init.mode=always | ||
spring.sql.init.schema-locations=classpath:schema.sql | ||
spring.sql.init.data-locations=classpath:data.sql |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
INSERT INTO product (name, price, description) VALUES ('Product1', 10.00, 'Description for Product1'); | ||
INSERT INTO product (name, price, description) VALUES ('Product2', 20.00, 'Description for Product2'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
CREATE TABLE IF NOT EXISTS product ( | ||
id BIGINT AUTO_INCREMENT PRIMARY KEY, | ||
name VARCHAR(255) NOT NULL, | ||
price DECIMAL(10, 2) NOT NULL, | ||
description VARCHAR(255) | ||
); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
step 1, step 2, 그리고 step 3 가 각각 별도의 커밋 기록을 가지고 있는데 혹시 어떻게 작업해주신건지 궁금하네요...!