Skip to content
This repository was archived by the owner on Mar 10, 2022. It is now read-only.

Commit 0bab745

Browse files
committed
Added Dto Validation
1 parent c8edd9e commit 0bab745

File tree

8 files changed

+108
-35
lines changed

8 files changed

+108
-35
lines changed

admin-panel/build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ dependencies {
1717

1818
implementation 'org.springframework.boot:spring-boot-starter-webflux'
1919
implementation 'io.netty:netty-tcnative-boringssl-static'
20-
implementation 'org.springframework.boot:spring-boot-starter-validation'
2120
implementation "org.springdoc:springdoc-openapi-webflux-ui:${springDocVersion}"
2221
implementation "org.springdoc:springdoc-openapi-security:${springDocVersion}"
2322

admin-panel/src/main/java/net/cryptic_game/backend/admin/converter/website/BlogPostConverter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public BlogPostModel toModel(final BlogPost dto) {
2323
dto.getImage(),
2424
dto.getCreated(),
2525
dto.getUpdated(),
26-
dto.isPublished(),
26+
dto.getPublished(),
2727
dto.getDescription(),
2828
dto.getContent()
2929
);
@@ -53,7 +53,7 @@ public void override(final BlogPostModel model, final BlogPost dto) {
5353
model.setTitle(dto.getTitle());
5454
model.setImage(dto.getImage());
5555
model.setContent(dto.getContent());
56-
model.setPublished(dto.isPublished());
56+
model.setPublished(dto.getPublished());
5757
model.setDescription(dto.getDescription());
5858
model.setContent(dto.getContent());
5959
}

admin-panel/src/main/java/net/cryptic_game/backend/admin/service/server/EndpointServiceImpl.java

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package net.cryptic_game.backend.admin.service.server;
22

3-
import lombok.Data;
3+
import java.util.Map;
4+
45
import lombok.RequiredArgsConstructor;
56
import net.cryptic_game.backend.dto.server.ServerEndpoint;
67
import org.springframework.stereotype.Service;
8+
import org.springframework.web.reactive.function.client.ClientResponse;
79
import org.springframework.web.reactive.function.client.WebClient;
810
import reactor.core.publisher.Flux;
911
import reactor.core.publisher.Mono;
@@ -29,29 +31,22 @@ public Flux<ServerEndpoint> findEndpoints() {
2931
public Mono<ServerEndpoint> disableEndpoint(final String id) {
3032
return this.client.post()
3133
.uri("/admin_panel/disable")
32-
.bodyValue(new ServerEndpointRequest(id))
33-
.exchangeToMono((response) ->
34-
response.statusCode().is2xxSuccessful()
35-
? response.bodyToMono(ServerEndpoint.class)
36-
: response.createException().flatMap(Mono::error)
37-
);
34+
.bodyValue(Map.of("id", id))
35+
.exchangeToMono(this::handleResponse);
36+
3837
}
3938

4039
@Override
4140
public Mono<ServerEndpoint> enableEndpoint(final String id) {
4241
return this.client.post()
4342
.uri("/admin_panel/enable")
44-
.bodyValue(new ServerEndpointRequest(id))
45-
.exchangeToMono((response) ->
46-
response.statusCode().is2xxSuccessful()
47-
? response.bodyToMono(ServerEndpoint.class)
48-
: response.createException().flatMap(Mono::error)
49-
);
43+
.bodyValue(Map.of("id", id))
44+
.exchangeToMono(this::handleResponse);
5045
}
5146

52-
@Data
53-
private static final class ServerEndpointRequest {
54-
55-
private final String id;
47+
private Mono<ServerEndpoint> handleResponse(final ClientResponse response) {
48+
return response.statusCode().is2xxSuccessful()
49+
? response.bodyToMono(ServerEndpoint.class)
50+
: response.createException().flatMap(Mono::error);
5651
}
5752
}

java-dto/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ dependencyManagement {
1111

1212
dependencies {
1313
implementation 'org.springframework.boot:spring-boot-starter-json'
14-
implementation 'org.springframework.boot:spring-boot-starter-validation'
14+
15+
implementation("net.getnova.framework:nova-core:${novaVersion}") { changing true }
1516

1617
// TODO: remove this
1718
implementation 'org.jsoup:jsoup:1.13.1'

java-dto/src/main/java/net/cryptic_game/backend/dto/website/BlogPost.java

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,18 @@
44

55
import java.time.OffsetDateTime;
66
import java.util.Set;
7-
import javax.validation.constraints.Pattern;
87

98
import lombok.AllArgsConstructor;
109
import lombok.Data;
10+
import net.getnova.framework.core.Validatable;
11+
import net.getnova.framework.core.exception.ValidationException;
1112
import org.jsoup.Jsoup;
1213
import org.jsoup.nodes.Document.OutputSettings;
1314
import org.jsoup.safety.Whitelist;
1415

1516
@Data
1617
@AllArgsConstructor
17-
public class BlogPost {
18+
public class BlogPost implements Validatable {
1819

1920
private static final String LANGUAGE_REGEX = "[a-z]{2}";
2021
private static final String POST_ID_REGEX = "[a-z-]+";
@@ -29,11 +30,10 @@ public class BlogPost {
2930

3031
private final Id id;
3132
private final String title;
32-
@Pattern(regexp = IMAGE_REGEX)
3333
private final String image;
3434
private final OffsetDateTime created;
3535
private final OffsetDateTime updated;
36-
private final boolean published;
36+
private final Boolean published;
3737
private final String description;
3838
private final String content;
3939
private final Set<String> languages;
@@ -44,7 +44,7 @@ public BlogPost(
4444
@JsonProperty("image") final String image,
4545
@JsonProperty("created") final OffsetDateTime created,
4646
@JsonProperty("updated") final OffsetDateTime updated,
47-
@JsonProperty("published") final boolean published,
47+
@JsonProperty("published") final Boolean published,
4848
@JsonProperty("description") final String description,
4949
@JsonProperty("content") final String content
5050
) {
@@ -59,12 +59,39 @@ public BlogPost(
5959
this.languages = null;
6060
}
6161

62+
@Override
63+
public void validate() throws ValidationException {
64+
if (this.id == null) {
65+
throw new ValidationException("id", "NOT_NULL");
66+
}
67+
68+
this.id.validate();
69+
70+
if (this.title == null || this.title.isBlank()) {
71+
throw new ValidationException("title", "NOT_BLANK");
72+
}
73+
74+
if (this.image == null) {
75+
throw new ValidationException("image", "NOT_NULL");
76+
}
77+
78+
if (IMAGE_REGEX.matches(this.image)) {
79+
throw new ValidationException("image", "MATCH_REGEX");
80+
}
81+
82+
if (this.published == null) {
83+
throw new ValidationException("published", "NOT_NULL");
84+
}
85+
86+
if (this.description == null || this.description.isBlank()) {
87+
throw new ValidationException("description", "NOT_BLANK");
88+
}
89+
}
90+
6291
@Data
63-
public static final class Id {
92+
public static final class Id implements Validatable {
6493

65-
@Pattern(regexp = LANGUAGE_REGEX)
6694
private final String language; // TODO java.util.Locale;
67-
@Pattern(regexp = POST_ID_REGEX)
6895
private final String postId;
6996

7097
public Id(
@@ -74,5 +101,24 @@ public Id(
74101
this.language = language;
75102
this.postId = postId;
76103
}
104+
105+
@Override
106+
public void validate() throws ValidationException {
107+
if (this.language == null) {
108+
throw new ValidationException("language", "NOT_NULL");
109+
}
110+
111+
if (LANGUAGE_REGEX.matches(this.language)) {
112+
throw new ValidationException("language", "MATCH_REGEX");
113+
}
114+
115+
if (this.postId == null) {
116+
throw new ValidationException("postId", "NOT_NULL");
117+
}
118+
119+
if (POST_ID_REGEX.matches(this.postId)) {
120+
throw new ValidationException("postId", "MATCH_REGEX");
121+
}
122+
}
77123
}
78124
}

java-dto/src/main/java/net/cryptic_game/backend/dto/website/BlogPostSmall.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class BlogPostSmall {
1313
private final String image;
1414
private final OffsetDateTime created;
1515
private final OffsetDateTime updated;
16-
private final boolean published;
16+
private final Boolean published;
1717
private final String description;
1818

1919
public BlogPostSmall(
@@ -22,7 +22,7 @@ public BlogPostSmall(
2222
/* @JsonProperty("image") */ final String image,
2323
/* @JsonProperty("created") */ final OffsetDateTime created,
2424
/* @JsonProperty("updated") */ final OffsetDateTime updated,
25-
/* @JsonProperty("published") */ final boolean published,
25+
/* @JsonProperty("published") */ final Boolean published,
2626
/* @JsonProperty("description") */ final String description
2727
) {
2828
this.id = id;

java-dto/src/main/java/net/cryptic_game/backend/dto/website/TeamDepartment.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66

77
import java.util.UUID;
88

9+
import net.getnova.framework.core.Validatable;
10+
import net.getnova.framework.core.exception.ValidationException;
11+
912
@Data
1013
@AllArgsConstructor
11-
public class TeamDepartment {
14+
public class TeamDepartment implements Validatable {
1215

1316
private final UUID id;
1417
private final String name;
@@ -22,4 +25,11 @@ public TeamDepartment(
2225
this.name = name;
2326
this.description = description;
2427
}
28+
29+
@Override
30+
public void validate() throws ValidationException {
31+
if (this.name == null || this.name.isBlank()) {
32+
throw new ValidationException("name", "NOT_BLANK");
33+
}
34+
}
2535
}

java-dto/src/main/java/net/cryptic_game/backend/dto/website/TeamMember.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
package net.cryptic_game.backend.dto.website;
22

33
import com.fasterxml.jackson.annotation.JsonProperty;
4-
import lombok.AllArgsConstructor;
5-
import lombok.Data;
64

75
import java.time.OffsetDateTime;
86
import java.util.UUID;
97

8+
import lombok.AllArgsConstructor;
9+
import lombok.Data;
10+
import net.getnova.framework.core.Validatable;
11+
import net.getnova.framework.core.exception.ValidationException;
12+
1013
@Data
1114
@AllArgsConstructor
12-
public class TeamMember {
15+
public class TeamMember implements Validatable {
1316

1417
private final UUID id;
1518
private final String name;
@@ -29,4 +32,23 @@ public TeamMember(
2932
this.departmentId = departmentId;
3033
this.joined = joined;
3134
}
35+
36+
@Override
37+
public void validate() throws ValidationException {
38+
if (this.name == null || this.name.isBlank()) {
39+
throw new ValidationException("name", "NOT_BLANK");
40+
}
41+
42+
if (this.githubId == null) {
43+
throw new ValidationException("githubId", "NOT_NULL");
44+
}
45+
46+
if (this.departmentId == null) {
47+
throw new ValidationException("departmentId", "NOT_NULL");
48+
}
49+
50+
if (this.joined == null) {
51+
throw new ValidationException("joined", "NOT_NULL");
52+
}
53+
}
3254
}

0 commit comments

Comments
 (0)