-
Notifications
You must be signed in to change notification settings - Fork 599
Expand file tree
/
Copy pathBootstrapData.java
More file actions
100 lines (83 loc) · 3.3 KB
/
Copy pathBootstrapData.java
File metadata and controls
100 lines (83 loc) · 3.3 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package guru.springframework.spring6restmvc.bootstrap;
import guru.springframework.spring6restmvc.entities.Beer;
import guru.springframework.spring6restmvc.entities.Customer;
import guru.springframework.spring6restmvc.model.BeerStyle;
import guru.springframework.spring6restmvc.repositories.BeerRepository;
import guru.springframework.spring6restmvc.repositories.CustomerRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Arrays;
/**
* Created by jt, Spring Framework Guru.
*/
@Component
@RequiredArgsConstructor
public class BootstrapData implements CommandLineRunner {
private final BeerRepository beerRepository;
private final CustomerRepository customerRepository;
@Override
public void run(String... args) throws Exception {
loadBeerData();
loadCustomerData();
}
private void loadBeerData() {
if (beerRepository.count() == 0){
Beer beer1 = Beer.builder()
.beerName("Galaxy Cat")
.beerStyle(BeerStyle.PALE_ALE)
.upc("12356")
.price(new BigDecimal("12.99"))
.quantityOnHand(122)
.createdDate(LocalDateTime.now())
.updateDate(LocalDateTime.now())
.build();
Beer beer2 = Beer.builder()
.beerName("Crank")
.beerStyle(BeerStyle.PALE_ALE)
.upc("12356222")
.price(new BigDecimal("11.99"))
.quantityOnHand(392)
.createdDate(LocalDateTime.now())
.updateDate(LocalDateTime.now())
.build();
Beer beer3 = Beer.builder()
.beerName("Sunshine City")
.beerStyle(BeerStyle.IPA)
.upc("12356")
.price(new BigDecimal("13.99"))
.quantityOnHand(144)
.createdDate(LocalDateTime.now())
.updateDate(LocalDateTime.now())
.build();
beerRepository.save(beer1);
beerRepository.save(beer2);
beerRepository.save(beer3);
}
}
private void loadCustomerData() {
if (customerRepository.count() == 0) {
Customer customer1 = Customer.builder()
.name("Customer 1")
.version(1)
.createdDate(LocalDateTime.now())
.updateDate(LocalDateTime.now())
.build();
Customer customer2 = Customer.builder()
.name("Customer 2")
.version(1)
.createdDate(LocalDateTime.now())
.updateDate(LocalDateTime.now())
.build();
Customer customer3 = Customer.builder()
.name("Customer 3")
.version(1)
.createdDate(LocalDateTime.now())
.updateDate(LocalDateTime.now())
.build();
customerRepository.saveAll(Arrays.asList(customer1, customer2, customer3));
}
}
}