-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerateProfiles.java
More file actions
70 lines (60 loc) · 2.48 KB
/
GenerateProfiles.java
File metadata and controls
70 lines (60 loc) · 2.48 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
package com.acmeflix.bootstrap;
import com.acmeflix.base.AbstractLogComponent;
import com.acmeflix.domain.User;
import com.acmeflix.service.MovieService;
import com.acmeflix.service.ProfileService;
import com.acmeflix.service.TvShowService;
import com.acmeflix.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Profile;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Random;
@Component
@Profile("my-dev")
@RequiredArgsConstructor
@Order(4) // Last since we must have users, movies, shows and then create the profiles
public class GenerateProfiles extends AbstractLogComponent implements CommandLineRunner {
@NotNull
private final ProfileService profileService;
@NotNull
private final UserService userService;
@NotNull
private final MovieService movieService;
@NotNull
private final TvShowService tvShowService;
Random randomGenerator = new Random();
private Long getRandom(List<Long> list) {
return list.get(randomGenerator.nextInt(list.size()));
}
@Override
public void run(String... args) throws Exception {
var allusers = userService.findAll();
var allMovies = movieService.getAllIds();
var allSeries = tvShowService.getAllIds();
for (User user : allusers) {
//Creating 2 profiles for each user
com.acmeflix.domain.Profile profile = new com.acmeflix.domain.Profile();
com.acmeflix.domain.Profile profile2 = new com.acmeflix.domain.Profile();
profile.setUser(user);
profile.setName("myprofile1");
profile.setMovieHistory(List.of(getRandom(allMovies),
getRandom(allMovies),
getRandom(allMovies)));
profile.setTvShowHistory(List.of(getRandom(allSeries),
getRandom(allSeries)));
profileService.createUsingHistory(profile);
profile2.setUser(user);
profile2.setName("myprofile2");
profile2.setMovieHistory(List.of(getRandom(allMovies),
getRandom(allMovies),
getRandom(allMovies)));
profile2.setTvShowHistory(List.of(getRandom(allSeries),
getRandom(allSeries)));
profileService.createUsingHistory(profile2);
}
}
}