Skip to content

Commit 7f20804

Browse files
authored
Merge pull request #23 from AymaneTech/security
Security
2 parents 31dd1a0 + e01f28a commit 7f20804

File tree

9 files changed

+53
-18
lines changed

9 files changed

+53
-18
lines changed

pom.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,15 @@
101101
<artifactId>spring-boot-starter-data-redis</artifactId>
102102
<version>3.3.5</version>
103103
</dependency>
104+
<dependency>
105+
<groupId>org.springframework.boot</groupId>
106+
<artifactId>spring-boot-starter-security</artifactId>
107+
</dependency>
108+
<dependency>
109+
<groupId>org.springframework.security</groupId>
110+
<artifactId>spring-security-test</artifactId>
111+
<scope>test</scope>
112+
</dependency>
104113

105114
</dependencies>
106115

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.wora.state_of_dev.common.util;
2+
3+
import com.wora.state_of_dev.common.domain.exception.EntityNotFoundException;
4+
5+
import java.util.Optional;
6+
7+
public class OptionalWrapper {
8+
public static <T> T orElseThrow(Optional<T> optional, String entity, Object id) {
9+
return optional.orElseThrow(() -> new EntityNotFoundException(entity, id));
10+
}
11+
12+
}

src/main/java/com/wora/state_of_dev/owner/domain/Owner.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@
99
import lombok.ToString;
1010
import lombok.experimental.Accessors;
1111

12+
import java.io.Serializable;
13+
1214
@Entity
1315
@Table(name = "owners")
1416

1517
@Getter
1618
@Setter
1719
@Accessors(chain = true)
1820
@NoArgsConstructor
19-
public class Owner {
21+
public class Owner implements Serializable {
2022

2123
@EmbeddedId
2224
@AttributeOverride(name = "value", column = @Column(name = "id"))

src/main/java/com/wora/state_of_dev/survey/application/service/impl/DefaultQuestionService.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
import java.util.List;
2525

26+
import static com.wora.state_of_dev.common.util.OptionalWrapper.orElseThrow;
27+
2628
@Service
2729
@Transactional
2830
@Validated
@@ -33,13 +35,14 @@ public class DefaultQuestionService implements QuestionService {
3335
private final QuestionMapper mapper;
3436
private final AnswerMapper answerMapper;
3537

38+
// todo : add findByChapterId, findBySurveyEditionId
39+
3640
@Override
3741
public List<QuestionResponseDto> findAll() {
3842
return repository.findAll()
3943
.stream().map(mapper::toResponseDto)
4044
.toList();
4145
}
42-
// todo : add findByChapterId, findBySurveyEditionId
4346

4447
@Override
4548
public QuestionResponseDto findById(QuestionId id) {
@@ -51,11 +54,9 @@ public QuestionResponseDto findById(QuestionId id) {
5154
@Override
5255
public QuestionResponseDto create(QuestionRequestDto dto) {
5356
final ChapterId chapterId = new ChapterId(dto.chapterId());
54-
Chapter chapter = chapterRepository.findById(chapterId)
55-
.orElseThrow(() -> new EntityNotFoundException("chapter", dto.chapterId()));
57+
Chapter chapter = orElseThrow(chapterRepository.findById(chapterId), "chapter", dto.chapterId());
5658

57-
if (chapterRepository.isHasSubChapters(chapterId))
58-
throw new ChapterHasSubChaptersException(chapterId);
59+
ensureHasNoSubChapters(chapterId);
5960

6061
Question question = mapper.toEntity(dto)
6162
.setChapter(chapter)
@@ -68,14 +69,13 @@ public QuestionResponseDto create(QuestionRequestDto dto) {
6869
@Override
6970
public QuestionResponseDto update(QuestionId id, QuestionRequestDto dto) {
7071
final ChapterId chapterId = new ChapterId(dto.chapterId());
71-
Question question = repository.findById(id)
72-
.orElseThrow(() -> new EntityNotFoundException("question", id.value()));
72+
Question question = orElseThrow(repository.findById(id), "question", id.value());
73+
74+
ensureHasNoSubChapters(chapterId);
7375

74-
if (chapterRepository.isHasSubChapters(chapterId))
75-
throw new ChapterHasSubChaptersException(chapterId);
7676

77-
Chapter chapter = chapterRepository.findById(chapterId)
78-
.orElseThrow(() -> new EntityNotFoundException("chapter", dto.chapterId()));
77+
Chapter chapter = orElseThrow(chapterRepository.findById(chapterId), "chapter", dto.chapterId());
78+
7979
question.setText(dto.text())
8080
.setAnswerType(dto.answerType())
8181
.setChapter(chapter)
@@ -92,12 +92,17 @@ public void delete(QuestionId id) {
9292
}
9393

9494
private List<Answer> mapAnswersDtoToEntities(List<AnswerRequestDto> answerRequestDtos) {
95-
if(answerRequestDtos.isEmpty())
95+
if (answerRequestDtos.isEmpty())
9696
throw new AnswersCannotBeEmptyException("Cannot create question without answers");
9797

9898
return answerRequestDtos
9999
.stream()
100100
.map(answerMapper::toEntity)
101101
.toList();
102102
}
103+
104+
private void ensureHasNoSubChapters(ChapterId chapterId) {
105+
if (chapterRepository.isHasSubChapters(chapterId))
106+
throw new ChapterHasSubChaptersException(chapterId);
107+
}
103108
}

src/main/java/com/wora/state_of_dev/survey/domain/entities/Answer.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@
77
import lombok.Setter;
88
import lombok.experimental.Accessors;
99

10+
import java.io.Serializable;
11+
1012
@Entity
1113
@Table(name = "answers")
1214

1315
@Getter
1416
@Setter
1517
@Accessors(chain = true)
1618
@NoArgsConstructor
17-
public class Answer {
19+
public class Answer implements Serializable {
1820

1921
@EmbeddedId
2022
@AttributeOverride(name = "value", column = @Column(name = "id"))

src/main/java/com/wora/state_of_dev/survey/domain/entities/Chapter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import lombok.ToString;
1010
import lombok.experimental.Accessors;
1111

12+
import java.io.Serializable;
1213
import java.util.ArrayList;
1314
import java.util.List;
1415

@@ -20,7 +21,7 @@
2021
@Accessors(chain = true)
2122
@NoArgsConstructor
2223
@ToString
23-
public class Chapter {
24+
public class Chapter implements Serializable {
2425

2526
@EmbeddedId
2627
@AttributeOverride(name = "value", column = @Column(name = "id"))

src/main/java/com/wora/state_of_dev/survey/domain/entities/Question.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import lombok.Setter;
99
import lombok.experimental.Accessors;
1010

11+
import java.io.Serializable;
1112
import java.util.List;
1213

1314
@Entity
@@ -17,7 +18,7 @@
1718
@Setter
1819
@Accessors(chain = true)
1920
@NoArgsConstructor
20-
public class Question {
21+
public class Question implements Serializable {
2122

2223
@EmbeddedId
2324
@AttributeOverride(name = "value", column = @Column(name = "id"))

src/main/java/com/wora/state_of_dev/survey/domain/entities/Survey.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@
99
import lombok.Setter;
1010
import lombok.experimental.Accessors;
1111

12+
import java.io.Serializable;
13+
1214
@Entity
1315
@Table(name = "surveys")
1416

1517
@Getter
1618
@Setter
1719
@Accessors(chain = true)
1820
@NoArgsConstructor
19-
public class Survey {
21+
public class Survey implements Serializable {
2022

2123
@EmbeddedId
2224
@AttributeOverride(name = "value", column = @Column(name = "id"))

src/main/java/com/wora/state_of_dev/survey/domain/entities/SurveyEdition.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import lombok.Setter;
1212
import lombok.experimental.Accessors;
1313

14+
import java.io.Serializable;
1415
import java.time.LocalDateTime;
1516
import java.time.Year;
1617
import java.util.ArrayList;
@@ -23,7 +24,7 @@
2324
@Setter
2425
@Accessors(chain = true)
2526
@RequiredArgsConstructor
26-
public class SurveyEdition {
27+
public class SurveyEdition implements Serializable {
2728

2829
@EmbeddedId
2930
@AttributeOverride(name = "value", column = @Column(name = "id"))

0 commit comments

Comments
 (0)