-
Notifications
You must be signed in to change notification settings - Fork 8
[3주차] Cart App #1 #6
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
base: main
Are you sure you want to change the base?
Changes from all commits
c477044
8270154
c9df4ad
d5117d2
c173f22
0819d91
d8fd264
42048e8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,25 @@ | ||
| package com.example.demo; | ||
|
|
||
| import com.mongodb.client.MongoClient; | ||
| import com.mongodb.client.MongoClients; | ||
| import com.mongodb.client.MongoDatabase; | ||
| import org.springframework.boot.SpringApplication; | ||
| import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
| import org.springframework.context.annotation.Bean; | ||
|
|
||
| @SpringBootApplication | ||
| public class Application { | ||
| public static void main(String[] args) { | ||
| SpringApplication.run(Application.class, args); | ||
| } | ||
|
|
||
| @Bean | ||
| public MongoClient mongoClient() { | ||
| return MongoClients.create("mongodb://localhost:27017"); | ||
| } | ||
|
Comment on lines
+16
to
+19
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. 🛠️ Refactor suggestion MongoDB 연결 설정 개선 필요 MongoDB 연결 문자열이 하드코딩되어 있어 환경에 따른 유연한 설정이 어렵습니다. 다음과 같이 수정하는 것을 제안합니다: +import org.springframework.beans.factory.annotation.Value;
@Bean
public MongoClient mongoClient() {
- return MongoClients.create("mongodb://localhost:27017");
+ @Value("${spring.data.mongodb.uri:mongodb://localhost:27017}")
+ private String mongoUri;
+ return MongoClients.create(mongoUri);
}
|
||
|
|
||
| @Bean | ||
| public MongoDatabase mongoDatabase(MongoClient mongoClient) { | ||
| return mongoClient.getDatabase("demo"); | ||
| } | ||
|
Comment on lines
+21
to
+24
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. 🛠️ Refactor suggestion 데이터베이스 이름을 설정 파일로 분리 필요 데이터베이스 이름도 환경에 따라 설정할 수 있도록 외부화가 필요합니다. 다음과 같이 수정하는 것을 제안합니다: @Bean
public MongoDatabase mongoDatabase(MongoClient mongoClient) {
- return mongoClient.getDatabase("demo");
+ @Value("${spring.data.mongodb.database:demo}")
+ private String databaseName;
+ return mongoClient.getDatabase(databaseName);
}
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| package com.example.demo.application; | ||
|
|
||
| import com.example.demo.infrastructure.LineItemDAO; | ||
| import com.example.demo.infrastructure.ProductDAO; | ||
| import com.example.demo.model.Cart; | ||
| import com.example.demo.model.LineItem; | ||
| import com.example.demo.model.Product; | ||
| import org.springframework.stereotype.Service; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @Service | ||
| public class CartService { | ||
| private final LineItemDAO lineItemDAO; | ||
| private final ProductDAO productDAO; | ||
|
|
||
| public CartService(LineItemDAO lineItemDAO, ProductDAO productDAO) { | ||
| this.lineItemDAO = lineItemDAO; | ||
| this.productDAO = productDAO; | ||
| } | ||
|
|
||
| public Cart getCart() { | ||
| List<LineItem> lineItems = lineItemDAO.findAll(); | ||
|
|
||
| lineItems.forEach(lineItem -> { | ||
| String productId = lineItem.getProductId(); | ||
| Product product = productDAO.find(productId); | ||
|
|
||
| int unitPrice = product.getPrice(); | ||
| int quantity = lineItem.getQuantity(); | ||
|
|
||
| lineItem.setProductName(product.getName()); | ||
| lineItem.setUnitPrice(product.getPrice()); | ||
| lineItem.setTotalPrice(unitPrice * quantity); | ||
| }); | ||
|
|
||
| int totalPrice = lineItems.stream() | ||
| .mapToInt(LineItem::getTotalPrice) | ||
| .sum(); | ||
|
|
||
| return new Cart(lineItems, totalPrice); | ||
| } | ||
|
|
||
| public void addProduct(String productId, int quantity) { | ||
|
|
||
| List<LineItem> lineItems = lineItemDAO.findAll(); | ||
|
|
||
| LineItem lineItem = lineItems.stream() | ||
| .filter(i -> i.getProductId().equals(productId)) | ||
| .findFirst() | ||
| .orElse(null); | ||
|
|
||
| if (lineItem == null) { | ||
| lineItem = new LineItem(productId, quantity); | ||
| lineItemDAO.add(lineItem); | ||
| return; | ||
| } | ||
| lineItem.setQuantity(lineItem.getQuantity() + quantity); | ||
| lineItemDAO.update(lineItem); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,42 @@ | ||
| package com.example.demo.controllers; | ||
|
|
||
| import com.example.demo.application.CartService; | ||
| import com.example.demo.controllers.dtos.CartDto; | ||
| import com.example.demo.model.Cart; | ||
| import com.example.demo.model.LineItem; | ||
| import org.springframework.web.bind.annotation.DeleteMapping; | ||
| import org.springframework.web.bind.annotation.GetMapping; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/cart") | ||
| public class CartController { | ||
|
|
||
| private final CartService cartService; | ||
|
|
||
| public CartController(CartService cartService) { | ||
| this.cartService = cartService; | ||
| } | ||
|
|
||
| @GetMapping | ||
| String detail() { | ||
| return ""; | ||
| CartDto detail() { | ||
|
|
||
| Cart cart = cartService.getCart(); | ||
| return new CartDto( | ||
| cart.getLineItems().stream() | ||
| .map(this::mapToDto) | ||
| .toList(), | ||
| cart.getTotalPrice()); | ||
| } | ||
|
|
||
| private CartDto.LineItemDto mapToDto(LineItem lineItem) { | ||
| return new CartDto.LineItemDto( | ||
| lineItem.getId(), | ||
| lineItem.getProductId(), | ||
| lineItem.getProductName(), | ||
| lineItem.getUnitPrice(), | ||
| lineItem.getQuantity(), | ||
| lineItem.getTotalPrice()); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,17 +1,33 @@ | ||
| package com.example.demo.controllers; | ||
|
|
||
| import com.example.demo.application.CartService; | ||
| import com.example.demo.controllers.dtos.AddProductToCartDto; | ||
| import jakarta.validation.Valid; | ||
| import org.springframework.http.HttpStatus; | ||
| import org.springframework.web.bind.annotation.PostMapping; | ||
| import org.springframework.web.bind.annotation.RequestBody; | ||
| import org.springframework.web.bind.annotation.RequestMapping; | ||
| import org.springframework.web.bind.annotation.ResponseStatus; | ||
| import org.springframework.web.bind.annotation.RestController; | ||
|
|
||
| @RestController | ||
| @RequestMapping("/cart/line-items") | ||
| public class LineItemController { | ||
|
|
||
| private final CartService cartService; | ||
|
|
||
| public LineItemController(CartService cartService) { | ||
| this.cartService = cartService; | ||
| } | ||
|
|
||
| @PostMapping | ||
| @ResponseStatus(HttpStatus.CREATED) | ||
| void create() { | ||
| // | ||
| void create( | ||
| @Valid @RequestBody AddProductToCartDto addProductToCartDto | ||
| ) { | ||
| cartService.addProduct( | ||
| addProductToCartDto.productId(), | ||
| addProductToCartDto.quantity() | ||
| ); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| package com.example.demo.controllers.dtos; | ||
|
|
||
| import jakarta.validation.constraints.NotBlank; | ||
| import jakarta.validation.constraints.Positive; | ||
|
|
||
| public record AddProductToCartDto( | ||
| @NotBlank | ||
| String productId, | ||
| @Positive | ||
| int quantity | ||
| ) { | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| package com.example.demo.controllers.dtos; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public record CartDto( | ||
| List<LineItemDto> lineItems, | ||
| int totalPrice | ||
| ) { | ||
| public record LineItemDto( | ||
| String id, | ||
| String productId, | ||
| String productName, | ||
| int unitPrice, | ||
| int quantity, | ||
| int totalPrice | ||
| ) { | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,56 @@ | ||||||||||||||||||||||||||||||||||||||||||
| package com.example.demo.infrastructure; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| import com.example.demo.model.LineItem; | ||||||||||||||||||||||||||||||||||||||||||
| import com.mongodb.client.MongoCollection; | ||||||||||||||||||||||||||||||||||||||||||
| import com.mongodb.client.MongoDatabase; | ||||||||||||||||||||||||||||||||||||||||||
| import com.mongodb.client.model.Filters; | ||||||||||||||||||||||||||||||||||||||||||
| import com.mongodb.client.model.Updates; | ||||||||||||||||||||||||||||||||||||||||||
| import org.bson.Document; | ||||||||||||||||||||||||||||||||||||||||||
| import org.bson.types.ObjectId; | ||||||||||||||||||||||||||||||||||||||||||
| import org.springframework.stereotype.Component; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| import java.util.ArrayList; | ||||||||||||||||||||||||||||||||||||||||||
| import java.util.List; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| @Component | ||||||||||||||||||||||||||||||||||||||||||
| public class LineItemDAO { | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| private final MongoCollection<Document> collection; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| public LineItemDAO(MongoDatabase mongoDatabase) { | ||||||||||||||||||||||||||||||||||||||||||
| this.collection = mongoDatabase.getCollection("line_items"); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| public List<LineItem> findAll() { | ||||||||||||||||||||||||||||||||||||||||||
| List<Document> documents = new ArrayList<>(); | ||||||||||||||||||||||||||||||||||||||||||
| collection.find().into(documents); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| return documents.stream().map(this::mapToModel).toList(); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+24
to
+29
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. 🛠️ Refactor suggestion 페이지네이션 구현이 필요합니다 대량의 데이터를 처리할 때 성능 문제가 발생할 수 있으므로, 페이지네이션을 구현하는 것이 좋습니다. - public List<LineItem> findAll() {
+ public List<LineItem> findAll(int skip, int limit) {
List<Document> documents = new ArrayList<>();
- collection.find().into(documents);
+ collection.find()
+ .skip(skip)
+ .limit(limit)
+ .into(documents);
return documents.stream().map(this::mapToModel).toList();
}📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| public void add(LineItem lineItem) { | ||||||||||||||||||||||||||||||||||||||||||
| Document document = new Document() | ||||||||||||||||||||||||||||||||||||||||||
| .append("product_id", lineItem.getProductId()) | ||||||||||||||||||||||||||||||||||||||||||
| .append("quantity", lineItem.getQuantity()); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| collection.insertOne(document); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+31
to
+37
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. LineItem의 모든 필드를 저장해야 합니다 현재 public void add(LineItem lineItem) {
Document document = new Document()
.append("product_id", lineItem.getProductId())
- .append("quantity", lineItem.getQuantity());
+ .append("quantity", lineItem.getQuantity())
+ .append("product_name", lineItem.getProductName())
+ .append("unit_price", lineItem.getUnitPrice());
collection.insertOne(document);
}📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| public void update(LineItem lineItem) { | ||||||||||||||||||||||||||||||||||||||||||
| collection.updateOne( | ||||||||||||||||||||||||||||||||||||||||||
| Filters.eq("_id", new ObjectId(lineItem.getId())), | ||||||||||||||||||||||||||||||||||||||||||
| Updates.combine( | ||||||||||||||||||||||||||||||||||||||||||
| Updates.set("product_id", lineItem.getProductId()), | ||||||||||||||||||||||||||||||||||||||||||
| Updates.set("quantity", lineItem.getQuantity()) | ||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+39
to
+47
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 void update(LineItem lineItem) {
collection.updateOne(
Filters.eq("_id", new ObjectId(lineItem.getId())),
Updates.combine(
Updates.set("product_id", lineItem.getProductId()),
- Updates.set("quantity", lineItem.getQuantity())
+ Updates.set("quantity", lineItem.getQuantity()),
+ Updates.set("product_name", lineItem.getProductName()),
+ Updates.set("unit_price", lineItem.getUnitPrice())
)
);
}📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| private LineItem mapToModel(Document document) { | ||||||||||||||||||||||||||||||||||||||||||
| return new LineItem( | ||||||||||||||||||||||||||||||||||||||||||
| document.getObjectId("_id").toString(), | ||||||||||||||||||||||||||||||||||||||||||
| document.getString("product_id"), | ||||||||||||||||||||||||||||||||||||||||||
| document.getInteger("quantity") | ||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+49
to
+55
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. 모든 필드를 매핑해야 합니다 Document에서 LineItem으로 변환 시 모든 필드가 매핑되어야 합니다. private LineItem mapToModel(Document document) {
return new LineItem(
document.getObjectId("_id").toString(),
document.getString("product_id"),
- document.getInteger("quantity")
+ document.getString("product_name"),
+ document.getInteger("quantity"),
+ document.getDouble("unit_price")
);
}📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,37 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| package com.example.demo.infrastructure; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import com.example.demo.model.LineItem; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import com.example.demo.model.Product; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import com.mongodb.client.MongoCollection; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import com.mongodb.client.MongoDatabase; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import com.mongodb.client.model.Filters; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.bson.Document; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.bson.types.ObjectId; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import org.springframework.stereotype.Component; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import java.util.List; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| @Component | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public class ProductDAO { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private final MongoDatabase mongoDatabase; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public ProductDAO(MongoDatabase mongoDatabase) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| this.mongoDatabase = mongoDatabase; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public Product find(String productId) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| MongoCollection<Document> collection = mongoDatabase.getCollection("products"); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Document document = collection.find( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Filters.eq("_id", new ObjectId(productId)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ).first(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return new Product( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| document.getObjectId("_id").toString(), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| document.getString("name"), | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| document.getInteger("price") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+23
to
+36
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 Product find(String productId) {
+ if (productId == null || productId.trim().isEmpty()) {
+ throw new IllegalArgumentException("productId는 null이거나 비어있을 수 없습니다.");
+ }
+
+ ObjectId objectId;
+ try {
+ objectId = new ObjectId(productId);
+ } catch (IllegalArgumentException e) {
+ throw new IllegalArgumentException("유효하지 않은 productId 형식입니다.", e);
+ }
MongoCollection<Document> collection = mongoDatabase.getCollection("products");
Document document = collection.find(
- Filters.eq("_id", new ObjectId(productId))
+ Filters.eq("_id", objectId)
).first();
+ if (document == null) {
+ throw new ProductNotFoundException("ID가 " + productId + "인 상품을 찾을 수 없습니다.");
+ }
return new Product(
document.getObjectId("_id").toString(),
document.getString("name"),
document.getInteger("price")
);
}📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package com.example.demo.model; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| public class Cart { | ||
| private List<LineItem> lineItems; | ||
| private int totalPrice; | ||
|
|
||
| public Cart(List<LineItem> lineItems, int totalPrice) { | ||
| this.lineItems = lineItems; | ||
| this.totalPrice = totalPrice; | ||
| } | ||
|
|
||
| public List<LineItem> getLineItems() { | ||
| return Collections.unmodifiableList(lineItems); | ||
| } | ||
|
|
||
| public int getTotalPrice() { | ||
| return totalPrice; | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,63 @@ | ||||||||||||||||||||||||||||||||||||||||||||||
| package com.example.demo.model; | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| public class LineItem { | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| private String id; | ||||||||||||||||||||||||||||||||||||||||||||||
| private String productId; | ||||||||||||||||||||||||||||||||||||||||||||||
| private int quantity; | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| private String productName; | ||||||||||||||||||||||||||||||||||||||||||||||
| private int unitPrice; | ||||||||||||||||||||||||||||||||||||||||||||||
| private int totalPrice; | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+3
to
+12
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. 🛠️ Refactor suggestion 모델 클래스에 유효성 검증이 필요합니다 수량(quantity)과 가격(unitPrice, totalPrice)에 대한 유효성 검증이 없습니다. 음수 값이 들어올 수 있는 위험이 있습니다. Jakarta Validation 어노테이션을 사용하여 유효성 검증을 추가하는 것을 추천드립니다: public class LineItem {
private String id;
private String productId;
+ @Min(value = 1, message = "수량은 1 이상이어야 합니다")
private int quantity;
private String productName;
+ @Min(value = 0, message = "단가는 0 이상이어야 합니다")
private int unitPrice;
+ @Min(value = 0, message = "총액은 0 이상이어야 합니다")
private int totalPrice;📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||
| public LineItem(String id, String productId, int quantity) { | ||||||||||||||||||||||||||||||||||||||||||||||
| this.id = id; | ||||||||||||||||||||||||||||||||||||||||||||||
| this.productId = productId; | ||||||||||||||||||||||||||||||||||||||||||||||
| this.quantity = quantity; | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| public LineItem(String productId, int quantity) { | ||||||||||||||||||||||||||||||||||||||||||||||
| this.productId = productId; | ||||||||||||||||||||||||||||||||||||||||||||||
| this.quantity = quantity; | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| public String getId() { | ||||||||||||||||||||||||||||||||||||||||||||||
| return id; | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| public String getProductId() { | ||||||||||||||||||||||||||||||||||||||||||||||
| return productId; | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| public int getQuantity() { | ||||||||||||||||||||||||||||||||||||||||||||||
| return quantity; | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| public String getProductName() { | ||||||||||||||||||||||||||||||||||||||||||||||
| return productName; | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| public void setProductName(String productName) { | ||||||||||||||||||||||||||||||||||||||||||||||
| this.productName = productName; | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| public int getUnitPrice() { | ||||||||||||||||||||||||||||||||||||||||||||||
| return unitPrice; | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| public void setUnitPrice(int unitPrice) { | ||||||||||||||||||||||||||||||||||||||||||||||
| this.unitPrice = unitPrice; | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| public int getTotalPrice() { | ||||||||||||||||||||||||||||||||||||||||||||||
| return totalPrice; | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| public void setTotalPrice(int totalPrice) { | ||||||||||||||||||||||||||||||||||||||||||||||
| this.totalPrice = totalPrice; | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
| public void setQuantity(int quantity) { | ||||||||||||||||||||||||||||||||||||||||||||||
| this.quantity = quantity; | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||
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.
💡 Verification agent
🧩 Analysis chain
MongoDB 드라이버 버전 호환성 확인 필요
Spring Boot 3.3.4와 MongoDB 드라이버 5.3.1의 호환성을 확인해야 합니다.
🌐 Web query:
💡 Result:
The recommended MongoDB driver version for Spring Boot 3.3.4 is mongodb-driver-sync 5.0.1[9]. This version is explicitly listed as a direct dependency in the
spring-boot-starter-data-mongodb:3.3.4artifact[9], ensuring compatibility with Spring Data MongoDB 4.x[4][9].Key compatibility details:
mongodb-driver-sync:5.0.xline supports MongoDB server versions from 5.0 to 7.x according to MongoDB's compatibility matrix[3].Avoid mixing older driver versions (e.g., 3.x or 4.x) with Spring Boot 3.x, as this causes conflicts like
NoSuchMethodErroror classpath incompatibilities[1][8]. If upgrading from legacy systems, ensure all related dependencies (Spring Data MongoDB and MongoDB Java Driver) are updated simultaneously[8][10].Citations:
MongoDB 드라이버 버전 불일치 문제: Spring Boot 3.3.4에는 mongodb-driver-sync 5.0.1 버전을 권장합니다
spring-boot-starter-data-mongodb에서는 mongodb-driver-sync 5.0.1 버전을 명시하고 있으므로, 권장 버전과 일치시켜 호환성 문제 및 예기치 않은 오류를 방지하는 것이 좋습니다.📝 Committable suggestion