diff --git a/build.gradle b/build.gradle index 32668dc..e11bddf 100644 --- a/build.gradle +++ b/build.gradle @@ -1,6 +1,6 @@ plugins { - id 'org.springframework.boot' version '2.7.1' - id 'io.spring.dependency-management' version '1.0.11.RELEASE' + id 'org.springframework.boot' version '2.7.2' + id 'io.spring.dependency-management' version '1.0.12.RELEASE' id 'java' } @@ -20,17 +20,19 @@ repositories { } dependencies { - implementation 'org.springframework.boot:spring-boot-starter-webflux:2.7.1' - implementation 'org.springframework.boot:spring-boot-configuration-processor:2.7.1' - implementation 'org.springframework.boot:spring-boot-starter-validation:2.7.1' - implementation 'org.springframework.boot:spring-boot-starter-data-mongodb-reactive:2.7.1' - runtimeOnly 'org.springframework.boot:spring-boot-devtools:2.7.1' + implementation 'org.springframework.boot:spring-boot-starter-webflux' + implementation 'org.springframework.boot:spring-boot-configuration-processor' + implementation 'org.springframework.boot:spring-boot-starter-validation' + implementation 'org.springframework.boot:spring-boot-starter-data-jpa' + runtimeOnly 'org.postgresql:postgresql' + runtimeOnly 'org.springframework.boot:spring-boot-devtools' compileOnly 'org.projectlombok:lombok' annotationProcessor 'org.projectlombok:lombok' - testImplementation 'org.springframework.boot:spring-boot-starter-test:2.7.1' - testImplementation 'io.projectreactor:reactor-test:3.4.19' + testImplementation 'org.springframework.boot:spring-boot-starter-test' + testImplementation 'io.projectreactor:reactor-test' + runtimeOnly 'com.h2database:h2' } tasks.named('test') { useJUnitPlatform() -} +} \ No newline at end of file diff --git a/src/main/java/com/kikawet/reactiveMediaServer/beans/SchedulerBean.java b/src/main/java/com/kikawet/reactiveMediaServer/beans/SchedulerBean.java new file mode 100644 index 0000000..d17b807 --- /dev/null +++ b/src/main/java/com/kikawet/reactiveMediaServer/beans/SchedulerBean.java @@ -0,0 +1,18 @@ +package com.kikawet.reactiveMediaServer.beans; + +import java.util.concurrent.Executors; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import reactor.core.scheduler.Scheduler; +import reactor.core.scheduler.Schedulers; + +@Configuration +public class SchedulerBean { + @Bean + Scheduler jdbcScheduler(@Value("${spring.task.scheduling.pool.size}") int maxThreads) { + return Schedulers.fromExecutor(Executors.newFixedThreadPool(maxThreads)); + } +} diff --git a/src/main/java/com/kikawet/reactiveMediaServer/beans/StartUp.java b/src/main/java/com/kikawet/reactiveMediaServer/beans/StartUp.java index 38e7855..08c5383 100644 --- a/src/main/java/com/kikawet/reactiveMediaServer/beans/StartUp.java +++ b/src/main/java/com/kikawet/reactiveMediaServer/beans/StartUp.java @@ -3,41 +3,58 @@ import java.time.LocalDateTime; import java.util.ArrayList; import java.util.Arrays; +import java.util.Set; import javax.annotation.PostConstruct; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Profile; +import org.springframework.core.env.Environment; import org.springframework.stereotype.Component; import com.kikawet.reactiveMediaServer.model.User; import com.kikawet.reactiveMediaServer.model.Video; import com.kikawet.reactiveMediaServer.model.WatchedVideo; import com.kikawet.reactiveMediaServer.repository.UserRepository; -import com.kikawet.reactiveMediaServer.repository.VideoRepository; +import com.kikawet.reactiveMediaServer.service.VideoService; @Component +@Profile("!test") public class StartUp { + // TODO: remove this class when the user repo is done and move everything to sql + + @Autowired + Environment env; @Autowired private UserRepository users; @Autowired - private VideoRepository videos; + private VideoService videos; @PostConstruct void setUp() { User u = new User(); - Video v = new Video(":D"); + Video v = this.videos.createVideo(":D").block(); u.setLogin("tom"); + u.setAvailableVideos(Set.of(v)); + u.setHistory(new ArrayList<>( Arrays.asList( new WatchedVideo(u, v, LocalDateTime.now(), 17), new WatchedVideo(u, v, LocalDateTime.now(), 69), new WatchedVideo(u, v, LocalDateTime.now(), 33)))); - this.users.put(u.getLogin(), u); - this.videos.put(v.getTitle(), v); + this.users.save(u); + + User user = this.users.findByLogin(u.getLogin()); + if (user == null) { + this.users.save(u); + } + + System.out.println("Saved user " + user.getLogin()); + System.out.println("Saved video " + v.getTitle()); } } diff --git a/src/main/java/com/kikawet/reactiveMediaServer/model/User.java b/src/main/java/com/kikawet/reactiveMediaServer/model/User.java index e706274..db3e943 100644 --- a/src/main/java/com/kikawet/reactiveMediaServer/model/User.java +++ b/src/main/java/com/kikawet/reactiveMediaServer/model/User.java @@ -1,25 +1,43 @@ package com.kikawet.reactiveMediaServer.model; import java.util.ArrayList; -import java.util.Collection; +import java.util.HashSet; import java.util.List; +import java.util.Set; import java.util.stream.Stream; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.OneToMany; +import javax.persistence.Table; +import javax.persistence.Transient; + import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; +import lombok.NonNull; +import lombok.RequiredArgsConstructor; import lombok.Setter; @Getter @Setter @AllArgsConstructor @NoArgsConstructor +@RequiredArgsConstructor +@Entity +@Table(name = "USERS") // User is a keyword so can't create a table with that name public class User { + @Id + @NonNull String login; - List