Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# spring-gift-product
# spring-gift-product
-상품 조회, 수정, 추가, 삭제 구현
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ version = '0.0.1-SNAPSHOT'

java {
toolchain {
languageVersion = JavaLanguageVersion.of(21)
languageVersion = JavaLanguageVersion.of(17)
}
}

Expand All @@ -24,6 +24,7 @@ dependencies {
runtimeOnly 'com.h2database:h2'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
implementation 'org.springframework.boot:spring-boot-starter-validation'
}

tasks.named('test') {
Expand Down
77 changes: 77 additions & 0 deletions src/main/java/gift/Controller/ProductController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package gift.Controller;

import org.springframework.web.bind.annotation.*;
import java.util.*;
import gift.Model.Product;
import gift.Validation.ProductValidator;
import org.springframework.http.*;

@RestController
@RequestMapping("/api/products")
public class ProductController {

private final Map<Long, Product> products = new HashMap<>();
private long nextId = 1;

public ProductController() {
products.put(nextId, new Product(
nextId++,
"아이스 카페 아메리카노 T",
4500,
"https://st.kakaocdn.net/product/gift/product/20231010111814_9a667f9eccc943648797925498bdd8a3.jpg"
));
}

@GetMapping
public List<Product> getAllPRoducts(){
return new ArrayList<>(products.values());
}

@GetMapping("/{id}")
public ResponseEntity<Product> getProductById(@PathVariable Long id) {
Product product = products.get(id);
if (product == null) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
}
return ResponseEntity.ok(product);
}

@PostMapping
public ResponseEntity<?> addProduct(@RequestBody Product product) {
Map<String, String> errors = ProductValidator.validate(product);
if (!errors.isEmpty()) {
return ResponseEntity.badRequest().body(errors);
}

product.setId(nextId++);
products.put(product.getId(), product);
return ResponseEntity.status(HttpStatus.CREATED).body(product);
}

@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteProduct(@PathVariable Long id) {
if (!products.containsKey(id)) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
}
products.remove(id);
return ResponseEntity.noContent().build();
}

@PutMapping("/{id}")
public ResponseEntity<?> updateProduct(@PathVariable Long id, @RequestBody Product updated) {
Product existing = products.get(id);
if (existing == null) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
}

Map<String, String> errors = ProductValidator.validate(updated);
if (!errors.isEmpty()) {
return ResponseEntity.badRequest().body(errors);
}

existing.setName(updated.getName());
existing.setPrice(updated.getPrice());
existing.setImgURL(updated.getImgURL());
return ResponseEntity.ok(existing);
}
}
29 changes: 29 additions & 0 deletions src/main/java/gift/Model/Product.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package gift.Model;

public class Product {
private Long id;
private String name;
private int price;
private String imgURL;

public Product() {}

public Product(Long id, String name, int price, String imgURL) {
this.id = id;
this.name = name;
this.price = price;
this.imgURL = imgURL;
}

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 int getPrice() { return price; }
public void setPrice(int price) { this.price = price; }

public String getImgURL() { return imgURL; }
public void setImgURL(String imgURL) { this.imgURL = imgURL; }
}
26 changes: 26 additions & 0 deletions src/main/java/gift/Validation/ProductValidator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package gift.Validation;

import gift.Model.Product;
import java.util.HashMap;
import java.util.Map;

public class ProductValidator {

public static Map<String, String> validate(Product product) {
Map<String, String> errors = new HashMap<>();

if (product.getName() == null || product.getName().trim().isEmpty()) {
errors.put("name", "상품 이름이 비어있습니다.");
}

if (product.getPrice() < 0) {
errors.put("price", "상품 가격은 0 이상이어야 합니다.");
}

if (product.getImgURL() == null || product.getImgURL().trim().isEmpty()) {
errors.put("imgURL", "이미지 URL이 비어 있습니다.");
}

return errors;
}
}