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
-1단계 상품API RESTful API 만들기 코드리뷰 전
2 changes: 1 addition & 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 Down
56 changes: 56 additions & 0 deletions src/main/java/gift/Controller/ProductController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package gift.Controller;

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

@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 Product getProductById(@PathVariable Long id) {
return products.get(id);
}

@PostMapping
public Product addProduct(@RequestBody Product product){
product.setId(nextId);
products.put(nextId,product);
nextId++;
return product;
}

@DeleteMapping("/{id}")
public void deleteProduct(@PathVariable Long id){
products.remove(id);
}

@PutMapping("/{id}")
public Product updateProduct(@PathVariable Long id, @RequestBody Product updated){
Product product = products.get(id);
if(product != null){
product.setName(updated.getName());
product.setPrice(updated.getPrice());
product.setImgURL(updated.getImgURL());
}
return product;
}
}
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; }
}