diff --git a/backend/pom.xml b/backend/pom.xml index d5069ee..989b623 100644 --- a/backend/pom.xml +++ b/backend/pom.xml @@ -31,21 +31,33 @@ neuefische https://sonarcloud.io + org.springframework.boot spring-boot-starter-web + + org.springframework.boot + spring-boot-starter-test + test + org.projectlombok lombok - true + annotationProcessor - org.springframework.boot - spring-boot-starter-test - test + org.springdoc + springdoc-openapi-starter-webmvc-ui + 2.2.0 + + + org.jetbrains + annotations + 17.0.0 + compile org.springframework.boot @@ -56,6 +68,11 @@ mysql-connector-j runtime + + com.h2database + h2 + test + diff --git a/backend/src/main/java/com/example/backend/controller/ActorController.java b/backend/src/main/java/com/example/backend/controller/ActorController.java index 4f3bead..37f29c4 100644 --- a/backend/src/main/java/com/example/backend/controller/ActorController.java +++ b/backend/src/main/java/com/example/backend/controller/ActorController.java @@ -1,19 +1,28 @@ package com.example.backend.controller; -import com.example.backend.model.ActorRepository; +import com.example.backend.dto.ActorResponse; +import com.example.backend.dto.CreateActorRequest; +import com.example.backend.model.Actor; +import com.example.backend.service.ActorService; import lombok.AllArgsConstructor; +import org.jetbrains.annotations.NotNull; import org.springframework.web.bind.annotation.*; +import java.util.List; + @RestController @AllArgsConstructor @RequestMapping("/api/actor") public class ActorController { - private final ActorRepository actorRepository; + private final ActorService actorService; @PostMapping - public void save() { - // TODO + public ActorResponse save(@RequestBody @NotNull CreateActorRequest request) { + Actor actor = request.toActor(); + actorService.createActor(actor); + + return ActorResponse.from(actor); } @GetMapping("/{id}") @@ -27,8 +36,8 @@ public void update() { } @GetMapping - public void getAll() { - // TODO + public List getAll() { + return actorService.getAllActors().stream().map(ActorResponse::from).toList(); } @DeleteMapping("/{id}") diff --git a/backend/src/main/java/com/example/backend/controller/DirectorController.java b/backend/src/main/java/com/example/backend/controller/DirectorController.java new file mode 100644 index 0000000..d81c406 --- /dev/null +++ b/backend/src/main/java/com/example/backend/controller/DirectorController.java @@ -0,0 +1,11 @@ +package com.example.backend.controller; + +import lombok.AllArgsConstructor; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@AllArgsConstructor +@RequestMapping("/api/director") +public class DirectorController { +} diff --git a/backend/src/main/java/com/example/backend/controller/MovieController.java b/backend/src/main/java/com/example/backend/controller/MovieController.java new file mode 100644 index 0000000..ef14dc4 --- /dev/null +++ b/backend/src/main/java/com/example/backend/controller/MovieController.java @@ -0,0 +1,11 @@ +package com.example.backend.controller; + +import lombok.AllArgsConstructor; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@AllArgsConstructor +@RequestMapping("/api/movie") +public class MovieController { +} diff --git a/backend/src/main/java/com/example/backend/dto/ActorResponse.java b/backend/src/main/java/com/example/backend/dto/ActorResponse.java new file mode 100644 index 0000000..1a1bb0a --- /dev/null +++ b/backend/src/main/java/com/example/backend/dto/ActorResponse.java @@ -0,0 +1,14 @@ +package com.example.backend.dto; + +import com.example.backend.model.Actor; +import lombok.Builder; + +@Builder +public record ActorResponse(Long id, String name) { + public static ActorResponse from(Actor actor) { + return ActorResponse.builder() + .id(actor.getId()) + .name(actor.getName()) + .build(); + } +} diff --git a/backend/src/main/java/com/example/backend/dto/CreateActorRequest.java b/backend/src/main/java/com/example/backend/dto/CreateActorRequest.java new file mode 100644 index 0000000..f2366f4 --- /dev/null +++ b/backend/src/main/java/com/example/backend/dto/CreateActorRequest.java @@ -0,0 +1,11 @@ +package com.example.backend.dto; + +import com.example.backend.model.Actor; + +public record CreateActorRequest( + String name +) { + public Actor toActor() { + return Actor.builder().name(name).build(); + } +} diff --git a/backend/src/main/java/com/example/backend/model/Actor.java b/backend/src/main/java/com/example/backend/model/Actor.java index 2b80674..895ec13 100644 --- a/backend/src/main/java/com/example/backend/model/Actor.java +++ b/backend/src/main/java/com/example/backend/model/Actor.java @@ -1,23 +1,23 @@ package com.example.backend.model; import jakarta.persistence.*; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.NoArgsConstructor; +import lombok.*; import java.util.Set; -@Builder @Entity -@NoArgsConstructor @AllArgsConstructor +@NoArgsConstructor +@Builder +@With +@Data public class Actor { @Id @GeneratedValue private Long id; - @ManyToMany(cascade = { CascadeType.ALL }) + @ManyToMany(cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH}) private Set movies; private String name; diff --git a/backend/src/main/java/com/example/backend/model/Director.java b/backend/src/main/java/com/example/backend/model/Director.java index 772c8cb..44eb96d 100644 --- a/backend/src/main/java/com/example/backend/model/Director.java +++ b/backend/src/main/java/com/example/backend/model/Director.java @@ -1,17 +1,23 @@ package com.example.backend.model; import jakarta.persistence.*; +import lombok.*; import java.util.Set; @Entity +@AllArgsConstructor +@NoArgsConstructor +@Builder +@With +@Data public class Director { @Id @GeneratedValue private Long id; - @ManyToMany(cascade = { CascadeType.ALL }) + @ManyToMany(cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH}) private Set movies; private String name; diff --git a/backend/src/main/java/com/example/backend/model/Movie.java b/backend/src/main/java/com/example/backend/model/Movie.java index 2c3fd52..2eed39c 100644 --- a/backend/src/main/java/com/example/backend/model/Movie.java +++ b/backend/src/main/java/com/example/backend/model/Movie.java @@ -12,10 +12,10 @@ public class Movie { private Double rating; - @ManyToMany(cascade = { CascadeType.ALL }) + @ManyToMany(cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH}) Set actors; - @ManyToMany(cascade = { CascadeType.ALL }) + @ManyToMany(cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH}) Set directors; private String name; diff --git a/backend/src/main/java/com/example/backend/service/ActorService.java b/backend/src/main/java/com/example/backend/service/ActorService.java index 87e0058..31588a7 100644 --- a/backend/src/main/java/com/example/backend/service/ActorService.java +++ b/backend/src/main/java/com/example/backend/service/ActorService.java @@ -1,12 +1,23 @@ package com.example.backend.service; +import com.example.backend.dto.CreateActorRequest; +import com.example.backend.model.Actor; import com.example.backend.model.ActorRepository; import lombok.AllArgsConstructor; import org.springframework.stereotype.Service; +import java.util.List; + @Service @AllArgsConstructor public class ActorService { private final ActorRepository actorRepository; + public Actor createActor(Actor actor) { + return actorRepository.save(actor); + } + + public List getAllActors() { + return actorRepository.findAll(); + } } diff --git a/backend/src/main/java/com/example/backend/service/DirectorService.java b/backend/src/main/java/com/example/backend/service/DirectorService.java new file mode 100644 index 0000000..5dbe575 --- /dev/null +++ b/backend/src/main/java/com/example/backend/service/DirectorService.java @@ -0,0 +1,11 @@ +package com.example.backend.service; + +import com.example.backend.model.DirectorRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +@Service +@RequiredArgsConstructor +public class DirectorService { + private final DirectorRepository directorRepository; +} diff --git a/backend/src/main/java/com/example/backend/service/MovieService.java b/backend/src/main/java/com/example/backend/service/MovieService.java new file mode 100644 index 0000000..2c65f4a --- /dev/null +++ b/backend/src/main/java/com/example/backend/service/MovieService.java @@ -0,0 +1,11 @@ +package com.example.backend.service; + +import com.example.backend.model.MovieRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +@Service +@RequiredArgsConstructor +public class MovieService { + private final MovieRepository movieRepository; +} diff --git a/backend/src/test/java/com/example/backend/BackendApplicationTests.java b/backend/src/test/java/com/example/backend/BackendApplicationTests.java deleted file mode 100644 index e6511ba..0000000 --- a/backend/src/test/java/com/example/backend/BackendApplicationTests.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.example.backend; - -import org.junit.jupiter.api.Test; -import org.springframework.boot.test.context.SpringBootTest; - -@SpringBootTest -class BackendApplicationTests { - - @Test - void contextLoads() { - } - -} diff --git a/backend/src/test/java/com/example/backend/controller/ActorControllerTest.java b/backend/src/test/java/com/example/backend/controller/ActorControllerTest.java new file mode 100644 index 0000000..ec0d6f3 --- /dev/null +++ b/backend/src/test/java/com/example/backend/controller/ActorControllerTest.java @@ -0,0 +1,61 @@ +package com.example.backend.controller; + +import com.example.backend.model.Actor; +import com.example.backend.model.ActorRepository; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.data.domain.Example; +import org.springframework.http.MediaType; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +@SpringBootTest +@AutoConfigureMockMvc +class ActorControllerTest { + @Autowired + private MockMvc mvc; + + @Autowired + private ActorRepository repository; + + @Test + @DirtiesContext + void save() throws Exception { + mvc.perform(MockMvcRequestBuilders.post("/api/actor") + .contentType(MediaType.APPLICATION_JSON) + .content( + """ + { + "name": "John Doe" + } + """ + )); + + List actual = repository.findAll(); + List expected = List.of(Actor.builder().id(1L).name("John Doe").build()); + assertEquals(expected, actual); + } + + @Test + void get() { + } + + @Test + void update() { + } + + @Test + void getAll() { + } + + @Test + void delete() { + } +} \ No newline at end of file diff --git a/backend/src/test/java/com/example/backend/service/ActorServiceTest.java b/backend/src/test/java/com/example/backend/service/ActorServiceTest.java new file mode 100644 index 0000000..abf390f --- /dev/null +++ b/backend/src/test/java/com/example/backend/service/ActorServiceTest.java @@ -0,0 +1,7 @@ +package com.example.backend.service; + +import static org.junit.jupiter.api.Assertions.*; + +class ActorServiceTest { + +} \ No newline at end of file diff --git a/backend/src/test/resources/application.properties b/backend/src/test/resources/application.properties new file mode 100644 index 0000000..1f56188 --- /dev/null +++ b/backend/src/test/resources/application.properties @@ -0,0 +1,5 @@ +jdbc.driverClassName=org.h2.Driver +jdbc.url=jdbc:h2:mem:myDb;DB_CLOSE_DELAY=-1;NON_KEYWORDS=KEY,VALUE + +hibernate.dialect=org.hibernate.dialect.H2Dialect +hibernate.hbm2ddl.auto=create \ No newline at end of file