Skip to content

Commit 62227f6

Browse files
authored
Features/bael 7975 vertical slice architecture (#16817)
* BAEL-7975: vsa skeleton * BAEL-7975: rename package * BAEL-7975: separate usecases from domain * BAEL-7975: wired components * BAEL-7975: tidy test * BAEL-7975: remove whitespace * BAEL-7975: added missing annotations * BAEL-7975: updaing ViewArticleUseCase * BAEL-7975: remove service class
1 parent 74c65b1 commit 62227f6

37 files changed

+717
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## Vertical Slice Architecture
2+
3+
This module contains articles about Vertical Slice Architecture
4+
5+
### Relevant articles
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
6+
<modelVersion>4.0.0</modelVersion>
7+
<artifactId>vertical-slice-architecture</artifactId>
8+
<name>vertical-slice-architecture</name>
9+
10+
<properties>
11+
<spring-boot.version>3.2.4</spring-boot.version> <!-- Requires at least spring framework version 6.1.X to work -->
12+
</properties>
13+
14+
<packaging>jar</packaging>
15+
<description>Vertical Slice Architecture Examples</description>
16+
17+
<parent>
18+
<groupId>com.baeldung</groupId>
19+
<artifactId>parent-boot-3</artifactId>
20+
<version>0.0.1-SNAPSHOT</version>
21+
<relativePath>../../parent-boot-3</relativePath>
22+
</parent>
23+
24+
<dependencies>
25+
<dependency>
26+
<groupId>org.springframework.boot</groupId>
27+
<artifactId>spring-boot-starter-data-jpa</artifactId>
28+
</dependency>
29+
<dependency>
30+
<groupId>com.h2database</groupId>
31+
<artifactId>h2</artifactId>
32+
<scope>runtime</scope>
33+
</dependency>
34+
<dependency>
35+
<groupId>org.springframework.boot</groupId>
36+
<artifactId>spring-boot-starter-web</artifactId>
37+
</dependency>
38+
39+
<dependency>
40+
<groupId>org.springframework.boot</groupId>
41+
<artifactId>spring-boot-starter-test</artifactId>
42+
<scope>test</scope>
43+
</dependency>
44+
<dependency>
45+
<groupId>org.springframework.boot</groupId>
46+
<artifactId>spring-boot-starter-jdbc</artifactId>
47+
<version>${spring-boot.version}</version>
48+
</dependency>
49+
50+
<dependency>
51+
<groupId>org.projectlombok</groupId>
52+
<artifactId>lombok</artifactId>
53+
<optional>true</optional>
54+
</dependency>
55+
56+
</dependencies>
57+
58+
<build>
59+
<plugins>
60+
<plugin>
61+
<groupId>org.apache.maven.plugins</groupId>
62+
<artifactId>maven-compiler-plugin</artifactId>
63+
<configuration>
64+
<source>17</source>
65+
<target>17</target>
66+
</configuration>
67+
</plugin>
68+
</plugins>
69+
</build>
70+
71+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.hexagonal;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class Application {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(Application.class, args);
11+
}
12+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.baeldung.hexagonal.controller;
2+
3+
import org.springframework.http.ResponseEntity;
4+
import org.springframework.web.bind.annotation.GetMapping;
5+
import org.springframework.web.bind.annotation.PathVariable;
6+
import org.springframework.web.bind.annotation.PostMapping;
7+
import org.springframework.web.bind.annotation.PutMapping;
8+
import org.springframework.web.bind.annotation.RequestBody;
9+
import org.springframework.web.bind.annotation.RequestMapping;
10+
import org.springframework.web.bind.annotation.RestController;
11+
12+
import com.baeldung.hexagonal.dto.ArticleDto;
13+
import com.baeldung.hexagonal.persistence.entity.Article;
14+
import com.baeldung.hexagonal.service.ArticleService;
15+
16+
@RestController
17+
@RequestMapping("articles")
18+
public class ArticleController {
19+
20+
private final ArticleService articleService;
21+
22+
public ArticleController(ArticleService articleService) {
23+
this.articleService = articleService;
24+
}
25+
26+
@PostMapping
27+
public ArticleDto createArticle(@RequestBody ArticleDto dto) {
28+
var article = mapToEntity(dto);
29+
var entity = articleService.create(article);
30+
return mapToDto(entity);
31+
}
32+
33+
@PutMapping
34+
public ArticleDto update(@RequestBody ArticleDto dto) {
35+
var updatedArticle = articleService.update(dto.id(), dto.content());
36+
return mapToDto(updatedArticle);
37+
}
38+
39+
@GetMapping("{id}")
40+
public ResponseEntity<ArticleDto> readArticle(@PathVariable Long id) {
41+
var article = articleService.findById(id)
42+
.map(ArticleController::mapToDto);
43+
return ResponseEntity.of(article);
44+
}
45+
46+
private static ArticleDto mapToDto(Article entity) {
47+
return new ArticleDto(entity.getId(), entity.getName(), entity.getContent(), entity.getSlug());
48+
}
49+
50+
private static Article mapToEntity(ArticleDto dto) {
51+
return new Article(dto.id(), dto.name(), dto.content(), dto.slug());
52+
}
53+
54+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.hexagonal.controller;
2+
3+
import org.springframework.web.bind.annotation.RequestMapping;
4+
import org.springframework.web.bind.annotation.RestController;
5+
6+
import com.baeldung.hexagonal.service.ArticleService;
7+
import com.baeldung.hexagonal.service.CommentService;
8+
9+
@RestController
10+
@RequestMapping("comments")
11+
public class CommentController {
12+
13+
private final CommentService service;
14+
15+
public CommentController(CommentService service) {
16+
this.service = service;
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.hexagonal.controller;
2+
3+
import org.springframework.web.bind.annotation.RequestMapping;
4+
import org.springframework.web.bind.annotation.RestController;
5+
6+
import com.baeldung.hexagonal.service.RecommendationService;
7+
8+
@RestController
9+
@RequestMapping("recommendations")
10+
public class RecommendationController {
11+
12+
private final RecommendationService service;
13+
14+
public RecommendationController(RecommendationService service) {
15+
this.service = service;
16+
}
17+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.hexagonal.dto;
2+
3+
public record ArticleDto(Long id, String name, String content, String slug) {
4+
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.hexagonal.dto;
2+
3+
public class CommentDto {
4+
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.hexagonal.dto;
2+
3+
public class RecommendationDto {
4+
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.hexagonal.dto;
2+
3+
public class UserDto {
4+
5+
}

0 commit comments

Comments
 (0)