Skip to content

Commit

Permalink
Database setup.
Browse files Browse the repository at this point in the history
  • Loading branch information
pavlo-bystrytskyi committed Oct 15, 2024
1 parent 3cdf0db commit 3ad47be
Show file tree
Hide file tree
Showing 16 changed files with 204 additions and 32 deletions.
25 changes: 21 additions & 4 deletions backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,33 @@
<sonar.organization>neuefische</sonar.organization>
<sonar.host.url>https://sonarcloud.io</sonar.host.url>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
<scope>annotationProcessor</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>17.0.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
Expand All @@ -56,6 +68,11 @@
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -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}")
Expand All @@ -27,8 +36,8 @@ public void update() {
}

@GetMapping
public void getAll() {
// TODO
public List<ActorResponse> getAll() {
return actorService.getAllActors().stream().map(ActorResponse::from).toList();
}

@DeleteMapping("/{id}")
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
}
Original file line number Diff line number Diff line change
@@ -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 {
}
14 changes: 14 additions & 0 deletions backend/src/main/java/com/example/backend/dto/ActorResponse.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
12 changes: 6 additions & 6 deletions backend/src/main/java/com/example/backend/model/Actor.java
Original file line number Diff line number Diff line change
@@ -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<Movie> movies;

private String name;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Movie> movies;

private String name;
Expand Down
4 changes: 2 additions & 2 deletions backend/src/main/java/com/example/backend/model/Movie.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Actor> actors;

@ManyToMany(cascade = { CascadeType.ALL })
@ManyToMany(cascade = {CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH})
Set<Director> directors;

private String name;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Actor> getAllActors() {
return actorRepository.findAll();
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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<Actor> actual = repository.findAll();
List<Actor> 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() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.backend.service;

import static org.junit.jupiter.api.Assertions.*;

class ActorServiceTest {

}
5 changes: 5 additions & 0 deletions backend/src/test/resources/application.properties
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 3ad47be

Please sign in to comment.