44import java .util .stream .Stream ;
55
66import hawk .entity .Item ;
7+ import hawk .entity .User ;
78import hawk .repos .ItemRepo ;
8- import hawk .repos .ItemsRepo ;
9+ import hawk .repos .UserRepo ;
910import org .springframework .beans .factory .annotation .Value ;
1011import org .springframework .boot .CommandLineRunner ;
1112import org .springframework .boot .SpringApplication ;
1213import org .springframework .boot .autoconfigure .SpringBootApplication ;
1314import org .springframework .context .ApplicationContext ;
1415import org .springframework .context .annotation .Bean ;
15- import org .springframework .data .jpa .repository .config .EnableJpaRepositories ;
1616
1717@ SpringBootApplication
1818public class Application {
@@ -25,7 +25,7 @@ public static void main(String[] args) {
2525 private String dbUrl ;
2626
2727 @ Bean
28- public CommandLineRunner commandLineRunner (ApplicationContext ctx , ItemRepo repo ) {
28+ public CommandLineRunner commandLineRunner (ApplicationContext ctx , ItemRepo repo , UserRepo userRepo ) {
2929
3030
3131 return args -> {
@@ -55,6 +55,20 @@ public CommandLineRunner commandLineRunner(ApplicationContext ctx, ItemRepo repo
5555 repo .findAll ().forEach (item -> System .out .println (String .format ("item: %s" , item .getName ())));
5656 }
5757
58+ System .out .println (String .format ("Users in DB %d" , userRepo .count ()));
59+
60+ if (userRepo .count () == 0 ) {
61+ userRepo .findAll ().forEach (item -> System .out .println (String .format ("item: %s" , item .getName ())));
62+
63+ Stream .of (1 , 2 , 3 ).forEach (i -> {
64+ System .out .println (String .format ("Adding item%d" , i ));
65+ userRepo .save (new User (String .format ("user%d" , i ), String .format ("we have the best users, users%d" , i )));
66+ });
67+
68+ System .out .println (String .format ("Items in DB %d" , userRepo .count ()));
69+ userRepo .findAll ().forEach (item -> System .out .println (String .format ("item: %s" , item .getName ())));
70+ }
71+
5872 };
5973 }
6074
0 commit comments