Skip to content

Commit 9e3e055

Browse files
authored
feat: add JPA and Flyway #30
* feat: add jpa #30 * docs(backend): write a readme on how to start the database #30 * chore: remove defined version #30 * chore: remove unnecassary dependency #30 * docs: add more structure to readme #30 * docs: write docs on how to start dev pcts #30 * refactor: move development properties to own file #30 * fix: replace long with Long so we can check if id is null or not #30 * feat: implement flyway and create migrations #30 * feat: create a test properties #30 * refactor: delete default spring test and format backend #30 * docs: make comment a todo #30 * refactor: clean up properties #30 * refactor: write sql statements in uppercase, move mapping from dto to entity into controller #30 * fix: hibernate generating an id already used by initial data #30 * refactor: change migration version to major version 0 #30 * refactor: change formatting and ids of default data and update docs #30
1 parent 9dd52a1 commit 9e3e055

19 files changed

Lines changed: 146 additions & 75 deletions

File tree

backend/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
# PCTS Backend
22
The backend of this project is written in Spring Boot.
33

4+
## Development
5+
### Dev-Database
6+
The database can be started via docker
7+
- cd into the root directory of the project
8+
- `cd docker`
9+
- Run `docker compose up`
10+
### Dev-Backend
11+
Make sure to set up the DB first
12+
#### Terminal
13+
You can start the backend via Terminal
14+
- cd to the project root
15+
- `cd backend`
16+
- `mvn spring-boot:run -Dspring-boot.run.arguments="--spring.profiles.active=dev"`
17+
418
## Formatting
519
We use the ***spotless*** Plugin for formatting the Java code:
620
https://github.com/diffplug/spotless

backend/pom.xml

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,31 @@
3535
<groupId>org.springframework.boot</groupId>
3636
<artifactId>spring-boot-starter-web</artifactId>
3737
</dependency>
38-
<dependency>
39-
<groupId>org.springdoc</groupId>
40-
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
41-
<version>2.8.9</version>
42-
</dependency>
38+
<dependency>
39+
<groupId>org.postgresql</groupId>
40+
<artifactId>postgresql</artifactId>
41+
<scope>runtime</scope>
42+
</dependency>
43+
<dependency>
44+
<groupId>org.springframework.boot</groupId>
45+
<artifactId>spring-boot-starter-data-jpa</artifactId>
46+
</dependency>
47+
<dependency>
48+
<groupId>org.flywaydb</groupId>
49+
<artifactId>flyway-core</artifactId>
50+
<version>11.11.1</version>
51+
</dependency>
52+
<dependency>
53+
<groupId>org.flywaydb</groupId>
54+
<artifactId>flyway-database-postgresql</artifactId>
55+
<version>11.11.1</version>
56+
<scope>runtime</scope>
57+
</dependency>
58+
<dependency>
59+
<groupId>org.springdoc</groupId>
60+
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
61+
<version>2.8.9</version>
62+
</dependency>
4363

4464
<!-- Dev Tools -->
4565
<dependency>
@@ -66,7 +86,6 @@
6686
<scope>test</scope>
6787
</dependency>
6888
</dependencies>
69-
7089
<build>
7190
<plugins>
7291
<plugin>

backend/src/main/java/ch/puzzle/pcts/controller/ExampleController.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package ch.puzzle.pcts.controller;
22

3-
import ch.puzzle.pcts.dto.example.CreateExampleDto;
43
import ch.puzzle.pcts.dto.example.ExampleDto;
54
import ch.puzzle.pcts.mapper.ExampleMapper;
65
import ch.puzzle.pcts.model.example.Example;
@@ -50,8 +49,8 @@ public ResponseEntity<ExampleDto> getExampleById(@PathVariable long id) {
5049
}
5150

5251
@PostMapping
53-
public ResponseEntity<ExampleDto> createNew(@Valid @RequestBody CreateExampleDto dto) {
54-
Example newExample = service.create(dto);
52+
public ResponseEntity<ExampleDto> createNew(@Valid @RequestBody ExampleDto dto) {
53+
Example newExample = service.create(mapper.fromDto(dto));
5554
return ResponseEntity.ok(mapper.toDto(newExample));
5655
}
5756
}

backend/src/main/java/ch/puzzle/pcts/dto/example/CreateExampleDto.java

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
package ch.puzzle.pcts.dto.example;
22

3-
public record ExampleDto(long id, String text) {
3+
public record ExampleDto(Long id, String text) {
44
}

backend/src/main/java/ch/puzzle/pcts/model/error/ErrorKey.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
public enum ErrorKey {
44
VALIDATION,
55
VALIDATION_DOES_NOT_INCLUDE,
6-
INTERNAL
6+
INTERNAL,
7+
ID_IS_NOT_NULL
78
}
Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,29 @@
11
package ch.puzzle.pcts.model.example;
22

3+
import jakarta.persistence.*;
4+
5+
@Entity
36
public class Example {
4-
private long id;
7+
@Id
8+
@GeneratedValue(strategy = GenerationType.AUTO, generator = "sequence_example")
9+
@SequenceGenerator(name = "sequence_example", allocationSize = 1)
10+
private Long id;
11+
512
private String text;
613

7-
public Example(long id, String text) {
14+
public Example(Long id, String text) {
815
this.id = id;
916
this.text = text;
1017
}
1118

12-
public long getId() {
19+
public Example() {
20+
}
21+
22+
public Long getId() {
1323
return id;
1424
}
1525

16-
public void setId(long id) {
26+
public void setId(Long id) {
1727
this.id = id;
1828
}
1929

@@ -24,5 +34,4 @@ public String getText() {
2434
public void setText(String text) {
2535
this.text = text;
2636
}
27-
2837
}
Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,10 @@
11
package ch.puzzle.pcts.repository;
22

3-
import ch.puzzle.pcts.dto.example.CreateExampleDto;
43
import ch.puzzle.pcts.model.example.Example;
5-
import java.util.ArrayList;
6-
import java.util.List;
7-
import org.springframework.stereotype.Component;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.stereotype.Repository;
86

9-
// This should really be something like this:
10-
// @Repository
11-
// public interface ExampleRepository extends CrudRepository<> { ... }
12-
@Component
13-
public class ExampleRepository {
14-
private List<Example> database = new ArrayList<>();
15-
16-
public ExampleRepository() {
17-
this.database.add(new Example(1, "Some example"));
18-
this.database.add(new Example(2, "Another one"));
19-
}
20-
21-
public List<Example> getAll() {
22-
return database;
23-
}
24-
25-
public Example add(CreateExampleDto example) {
26-
database.add(new Example(database.size() + 1, example.text()));
27-
return database.getLast();
28-
}
29-
30-
public Example getById(long id) {
31-
return new Example(id, "This is example #" + id);
32-
}
7+
@Repository
8+
public interface ExampleRepository extends JpaRepository<Example, Long> {
9+
Example getById(long id);
3310
}

backend/src/main/java/ch/puzzle/pcts/service/business/ExampleService.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package ch.puzzle.pcts.service.business;
22

3-
import ch.puzzle.pcts.dto.example.CreateExampleDto;
43
import ch.puzzle.pcts.model.example.Example;
54
import ch.puzzle.pcts.service.persistence.ExamplePersistenceService;
65
import ch.puzzle.pcts.service.validation.ExampleValidationService;
6+
import jakarta.transaction.Transactional;
77
import java.util.List;
88
import org.springframework.stereotype.Service;
99

@@ -25,8 +25,9 @@ public List<Example> getAll() {
2525
return persistenceService.getAll();
2626
}
2727

28-
public Example create(CreateExampleDto dto) {
29-
validationService.validateOnCreate(dto);
30-
return persistenceService.create(dto);
28+
@Transactional
29+
public Example create(Example example) {
30+
validationService.validateOnCreate(example);
31+
return persistenceService.create(example);
3132
}
3233
}

backend/src/main/java/ch/puzzle/pcts/service/persistence/ExamplePersistenceService.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package ch.puzzle.pcts.service.persistence;
22

3-
import ch.puzzle.pcts.dto.example.CreateExampleDto;
43
import ch.puzzle.pcts.model.example.Example;
54
import ch.puzzle.pcts.repository.ExampleRepository;
65
import java.util.List;
@@ -16,15 +15,15 @@ public ExamplePersistenceService(ExampleRepository repository) {
1615
this.repository = repository;
1716
}
1817

19-
public Example create(CreateExampleDto dto) {
20-
return repository.add(dto);
18+
public Example create(Example example) {
19+
return repository.save(example);
2120
}
2221

2322
public Example getById(long id) {
2423
return repository.getById(id);
2524
}
2625

2726
public List<Example> getAll() {
28-
return repository.getAll();
27+
return repository.findAll();
2928
}
3029
}

0 commit comments

Comments
 (0)