-
Notifications
You must be signed in to change notification settings - Fork 8
[Third week] Domain model Active record #5
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
Open
songtov
wants to merge
4
commits into
dal-lab:main
Choose a base branch
from
songtov:third-week-cart-domain-model
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
4 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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,11 +1,31 @@ | ||
| package com.example.demo; | ||
|
|
||
| import com.mongodb.client.MongoClient; | ||
| import com.mongodb.client.MongoClients; | ||
| import com.mongodb.client.MongoDatabase; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| 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( | ||
| @Value("${mongodb.url}") String url | ||
| ) { | ||
| return MongoClients.create(url); | ||
| } | ||
|
|
||
| @Bean | ||
| public MongoDatabase mongoDatabase( | ||
| MongoClient mongoClient, | ||
| @Value("${mongodb.database}") String database | ||
| ) { | ||
| return mongoClient.getDatabase(database); | ||
| } | ||
| } |
60 changes: 60 additions & 0 deletions
60
src/main/java/com/example/demo/application/CartService.java
This file contains hidden or 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,60 @@ | ||
| package com.example.demo.application; | ||
|
|
||
| import com.example.demo.infrastructure.LineItemDAO; | ||
| import com.example.demo.infrastructure.ProductDAO; | ||
| import com.example.demo.models.Cart; | ||
| import com.example.demo.models.LineItem; | ||
| import com.example.demo.models.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); | ||
|
|
||
|
|
||
| lineItem.setProduct(product); | ||
| }); | ||
|
|
||
|
|
||
| return new Cart(lineItems); | ||
| } | ||
|
|
||
| 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.addQuantity(quantity); | ||
|
|
||
| lineItemDAO.update(lineItem); | ||
| } | ||
| } |
32 changes: 30 additions & 2 deletions
32
src/main/java/com/example/demo/controllers/CartController.java
This file contains hidden or 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,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.models.Cart; | ||
| import com.example.demo.models.LineItem; | ||
| 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() | ||
| ); | ||
| } | ||
|
|
||
| } |
19 changes: 17 additions & 2 deletions
19
src/main/java/com/example/demo/controllers/LineItemController.java
This file contains hidden or 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,17 +1,32 @@ | ||
| 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() | ||
| ); | ||
| } | ||
| } |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/example/demo/controllers/dtos/AddProductToCartDto.java
This file contains hidden or 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,14 @@ | ||
| 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 | ||
| ) { | ||
|
|
||
| } |
18 changes: 18 additions & 0 deletions
18
src/main/java/com/example/demo/controllers/dtos/CartDto.java
This file contains hidden or 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,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 | ||
| ) { | ||
| } | ||
| } |
60 changes: 60 additions & 0 deletions
60
src/main/java/com/example/demo/infrastructure/LineItemDAO.java
This file contains hidden or 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,60 @@ | ||
| package com.example.demo.infrastructure; | ||
|
|
||
| import com.example.demo.models.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(); | ||
| } | ||
|
|
||
| private LineItem mapToModel(Document document) { | ||
| return new LineItem( | ||
| document.getObjectId("_id").toString(), | ||
| document.getString("product_id"), | ||
| document.getInteger("quantity") | ||
| ); | ||
| } | ||
|
|
||
| public void add(LineItem lineItem) { | ||
| Document document = new Document() | ||
| .append("product_id", lineItem.getProductId()) | ||
| .append("quantity", lineItem.getQuantity()); | ||
|
|
||
| collection.insertOne(document); | ||
| } | ||
|
|
||
|
|
||
| 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()) | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| } |
35 changes: 35 additions & 0 deletions
35
src/main/java/com/example/demo/infrastructure/ProductDAO.java
This file contains hidden or 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 com.example.demo.infrastructure; | ||
|
|
||
| import com.example.demo.models.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; | ||
|
|
||
| @Component | ||
| public class ProductDAO { | ||
| private final MongoCollection<Document> collection; | ||
|
|
||
| public ProductDAO(MongoDatabase mongoDatabase) { | ||
| this.collection = mongoDatabase.getCollection("line_items"); | ||
| } | ||
|
|
||
|
|
||
| public Product find(String productId) { | ||
| Document document = collection.find( | ||
| Filters.eq("_id", new ObjectId(productId)) | ||
| ).first(); | ||
|
|
||
| if (document == null) { | ||
| return null; | ||
| } | ||
|
|
||
| return new Product( | ||
| document.getObjectId("_id").toString(), | ||
| document.getString("name"), | ||
| document.getInteger("price") | ||
| ); | ||
| } | ||
| } |
This file contains hidden or 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,22 @@ | ||
| package com.example.demo.models; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.List; | ||
|
|
||
| public class Cart { | ||
| private List<LineItem> lineItems; | ||
|
|
||
| public Cart(List<LineItem> lineItems) { | ||
| this.lineItems = lineItems; | ||
| } | ||
|
|
||
| public List<LineItem> getLineItems() { | ||
| return Collections.unmodifiableList(lineItems); | ||
| } | ||
|
|
||
| public int getTotalPrice() { | ||
| return lineItems.stream() | ||
| .mapToInt(LineItem::getTotalPrice) | ||
| .sum(); | ||
| } | ||
| } |
This file contains hidden or 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,58 @@ | ||
| package com.example.demo.models; | ||
|
|
||
| public class LineItem { | ||
| //Similar to DB schema | ||
| private String id = null; | ||
| private String productId; | ||
| private int quantity; | ||
|
|
||
| private String productName; | ||
| private int unitPrice; | ||
| private int totalPrice; | ||
|
|
||
| public LineItem(String productId, int quantity) { | ||
| this.productId = productId; | ||
| this.quantity = quantity; | ||
| } | ||
|
|
||
| public LineItem(String id, String productId, int quantity) { | ||
| this.id = id; | ||
| 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 int getUnitPrice() { | ||
| return unitPrice; | ||
| } | ||
|
|
||
| //business logic | ||
|
|
||
| public void addQuantity(int quantity) { | ||
| this.quantity += quantity; | ||
| } | ||
|
|
||
| public int getTotalPrice() { | ||
| return unitPrice * quantity; | ||
| } | ||
|
|
||
| public void setProduct(Product product) { | ||
| this.productName = product.getName(); | ||
| this.unitPrice = product.getPrice(); | ||
| } | ||
| } |
Oops, something went wrong.
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.
💡 Codebase verification
🛠️ Refactor suggestion
Spring Boot 의존성 관리와 MongoDB 드라이버 버전 불일치 발견
Spring Boot 3.3.4는 MongoDB 드라이버 5.0.1 버전을 관리하고 있는데, 프로젝트에서는 5.2.0 버전을 직접 지정하고 있습니다. 이는 다음과 같은 문제를 일으킬 수 있습니다:
수정 방법:
만약 반드시 최신 버전의 MongoDB 드라이버가 필요하다면, 전체 프로젝트의 MongoDB 관련 의존성에 대해 버전을 일괄적으로 오버라이드하는 것을 권장합니다:
🔗 Analysis chain
의존성 버전 관리 개선이 필요합니다
Spring Boot의 의존성 관리 기능을 활용하지 않고 MongoDB 드라이버 버전을 직접 지정하고 있습니다. 이는 잠재적인 호환성 문제를 일으킬 수 있습니다.
버전을 명시적으로 지정하지 말고 Spring Boot의 의존성 관리를 활용하세요:
MongoDB 드라이버와 Spring Boot 버전의 호환성을 확인하기 위해 다음 스크립트를 실행하세요:
Let me gather more information about the Spring Boot version being used in the project.
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
Length of output: 831
Script:
Length of output: 696