This repository was archived by the owner on Apr 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCartFetcher.java
More file actions
53 lines (45 loc) · 1.47 KB
/
CartFetcher.java
File metadata and controls
53 lines (45 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package group.rohlik.application;
import group.rohlik.entity.Cart;
import group.rohlik.entity.CartRepository;
import group.rohlik.entity.Discount;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Comparator;
import java.util.List;
@AllArgsConstructor
@Service
@Transactional(readOnly = true)
public class CartFetcher {
private final CartRepository cartRepository;
public CartDetail fetch(long cartId) {
Cart cart = cartRepository.findById(cartId).orElseThrow();
return new CartDetail(
cart.getId(),
cart.totalPrice(),
cart
.getLines()
.stream()
.map(line -> new Line(line.getProduct().getSku(), line.getProduct().getName(), line.getQuantity()))
.sorted(Comparator.comparing(line -> line.sku))
.toList(),
cart
.getDiscounts()
.stream()
.map(Discount::getName)
.sorted()
.toList()
);
}
private record CartDetail(
long id,
double total,
List<Line> lines,
List<String> discounts
) { }
private record Line(
String sku,
String name,
int quantity
) { }
}