-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSeedData.java
172 lines (144 loc) · 4.77 KB
/
SeedData.java
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package com.lambdaschool.shoppingcart;
import com.lambdaschool.shoppingcart.models.Product;
import com.lambdaschool.shoppingcart.models.Role;
import com.lambdaschool.shoppingcart.models.User;
import com.lambdaschool.shoppingcart.models.UserRoles;
import com.lambdaschool.shoppingcart.services.CartItemService;
import com.lambdaschool.shoppingcart.services.ProductService;
import com.lambdaschool.shoppingcart.services.RoleService;
import com.lambdaschool.shoppingcart.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
/**
* SeedData puts both known and random data into the database. It implements CommandLineRunner.
* <p>
* CoomandLineRunner: Spring Boot automatically runs the run method once and only once
* after the application context has been loaded.
*/
@Transactional
@ConditionalOnProperty(
prefix = "command.line.runner",
value = "enabled",
havingValue = "true",
matchIfMissing = true)
@Component
public class SeedData
implements CommandLineRunner
{
/**
* Connects the Role Service to this process
*/
@Autowired
RoleService roleService;
/**
* Connects the user service to this process
*/
@Autowired
UserService userService;
@Autowired
ProductService productService;
@Autowired
CartItemService cartItemService;
/**
* Generates test, seed data for our application
*
* @param args The parameter is required by the parent interface but is not used in this process.
*/
@Transactional
@Override
public void run(String[] args) throws
Exception
{
// Adding users
userService.deleteAll();
roleService.deleteAll();
productService.deleteAll();
// System.out.println("Users: " + userService.findAll().size());
// System.out.println("Roles: " + roleService.findAll().size());
// System.out.println("Products: " + productService.findAll().size());
Role r1 = new Role("admin");
Role r2 = new Role("user");
r1 = roleService.save(r1);
r2 = roleService.save(r2);
User u1 = new User("barnbarn",
"LambdaLlama",
"added via seed data");
u1.getRoles()
.add(new UserRoles(u1,
r1));
u1.getRoles()
.add(new UserRoles(u1,
r2));
u1 = userService.save(u1);
User u2 = new User("cinnamon",
"LambdaLlama",
"added via seed data");
u2.getRoles()
.add(new UserRoles(u2,
r2));
u2 = userService.save(u2);
User u3 = new User("stumps",
"LambdaLlama",
"added via seed data");
u3.getRoles()
.add(new UserRoles(u3,
r2));
u3 = userService.save(u3);
// Adding Products
Product p1 = new Product();
p1.setName("PEN");
p1.setDescription("MAKES WORDS");
p1.setPrice(2.50);
p1.setComments("added via seed data");
Product p2 = new Product();
p2.setName("PENCIL");
p2.setDescription("DOES MATH");
p2.setPrice(1.50);
p2.setComments("added via seed data");
Product p3 = new Product();
p3.setName("COFFEE");
p3.setDescription("EVERYONE NEEDS COFFEE");
p3.setPrice(4.00);
p3.setComments("added via seed data");
p1 = productService.save(p1);
p2 = productService.save(p2);
p3 = productService.save(p3);
// Creating Carts
for (int i = 0; i < 4; i++)
{
cartItemService.addToCart(u1.getUserid(),
p1.getProductid(),
"added via seed data");
}
for (int i = 0; i < 3; i++)
{
cartItemService.addToCart(u1.getUserid(),
p2.getProductid(),
"added via seed data");
}
for (int i = 0; i < 2; i++)
{
cartItemService.addToCart(u1.getUserid(),
p3.getProductid(),
"added via seed data");
}
for (int i = 0; i < 1; i++)
{
cartItemService.addToCart(u2.getUserid(),
p3.getProductid(),
"added via seed data");
}
for (int i = 0; i < 17; i++)
{
cartItemService.addToCart(u3.getUserid(),
p3.getProductid(),
"added via seed data");
}
}
}