Skip to content

Commit 685baca

Browse files
committed
tags from list to set
1 parent dd7a35b commit 685baca

File tree

4 files changed

+17
-10
lines changed

4 files changed

+17
-10
lines changed

core/src/main/java/com/qa/blog/core/domain/Post.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
import com.qa.blog.core.exception.BlogErrorCode;
44
import com.qa.blog.core.exception.BlogException;
55

6-
import java.util.List;
6+
import java.util.Set;
7+
78
public record Post(Long id,
89
Author author,
910
Category category,
10-
List<Tag> tags,
11+
Set<Tag> tags,
1112
String title,
1213
String content,
1314
String image) {

mariadb/src/main/java/com/qa/blog/mariadb/mapper/PostEntityMapper.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.qa.blog.mariadb.entity.TagEntity;
1111
import org.springframework.stereotype.Component;
1212

13+
import java.util.HashSet;
1314
import java.util.List;
1415
import java.util.Set;
1516
import java.util.stream.Collectors;
@@ -18,9 +19,9 @@ public class PostEntityMapper {
1819
public Post toDomain(PostEntity entity) {
1920
Author author = new Author(entity.getAuthor().getId(), entity.getAuthor().getName());
2021
Category category = new Category(entity.getCategory().getId(), entity.getCategory().getName());
21-
List<Tag> tags = entity.getTags().stream()
22+
Set<Tag> tags = entity.getTags().stream()
2223
.map(tagEntityEntity -> new Tag(tagEntityEntity.getId(), tagEntityEntity.getName()))
23-
.toList();
24+
.collect(Collectors.toSet());
2425

2526
return new Post(
2627
entity.getId(),

spring-web/src/main/java/com/qa/blog/springweb/mapper/PostWebMapper.java

+6-4
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,17 @@
1010
import com.qa.blog.core.domain.Tag;
1111

1212
import java.util.List;
13+
import java.util.Set;
14+
import java.util.stream.Collectors;
1315

1416
@Component
1517
public class PostWebMapper {
1618
public Post toDomain(PostRequest request) throws BlogException {
1719
Author author = new Author(null, request.author());
1820
Category category = new Category(null, request.category());
19-
List<Tag> tags = request.tags().stream()
21+
Set<Tag> tags = request.tags().stream()
2022
.map(tag -> new Tag(null, tag))
21-
.toList();
23+
.collect(Collectors.toSet());
2224
return new Post(
2325
null,
2426
author,
@@ -33,9 +35,9 @@ public Post toDomain(PostRequest request) throws BlogException {
3335
public Post toDomain(PostDTO postDTO) throws BlogException {
3436
Author author = new Author(null, postDTO.author());
3537
Category category = new Category(null, postDTO.category());
36-
List<Tag> tags = postDTO.tags().stream()
38+
Set<Tag> tags = postDTO.tags().stream()
3739
.map(tag -> new Tag(null, tag))
38-
.toList();
40+
.collect(Collectors.toSet());
3941
return new Post(
4042
postDTO.id(),
4143
author,

spring-web/src/test/java/com/qa/blog/springweb/post/PostControllerTest.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
2424

2525
import java.util.ArrayList;
26+
import java.util.HashSet;
2627
import java.util.List;
28+
import java.util.Set;
2729

2830
import static org.mockito.ArgumentMatchers.any;
2931
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
@@ -61,9 +63,10 @@ void createPostSuccessfully() throws Exception {
6163
var postDomain = new Post(42L,
6264
new Author(41L, "Antonio Alvino"),
6365
new Category(33L, "Tech"),
64-
new ArrayList<>(List.of(
66+
new HashSet<>(Set.of(
6567
new Tag(1L, "Java"),
66-
new Tag(2L, "Programming"))),
68+
new Tag(2L, "Programming"))) {
69+
},
6770
"Titolo",
6871
"Contenuto 123 Prova",
6972
"http://example.com/image.jpg");

0 commit comments

Comments
 (0)