Skip to content

Commit 0f1ceba

Browse files
committed
validation
1 parent de5bd5b commit 0f1ceba

File tree

4 files changed

+90
-4
lines changed

4 files changed

+90
-4
lines changed

.gitignore

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
HELP.md
2+
target/
3+
!.mvn/wrapper/maven-wrapper.jar
4+
!**/src/main/**/target/
5+
!**/src/test/**/target/
6+
7+
### STS ###
8+
.apt_generated
9+
.classpath
10+
.factorypath
11+
.project
12+
.settings
13+
.springBeans
14+
.sts4-cache
15+
16+
### IntelliJ IDEA ###
17+
.idea
18+
*.iws
19+
*.iml
20+
*.ipr
21+
22+
### NetBeans ###
23+
/nbproject/private/
24+
/nbbuild/
25+
/dist/
26+
/nbdist/
27+
/.nb-gradle/
28+
build/
29+
!**/src/main/**/build/
30+
!**/src/test/**/build/
31+
32+
### VS Code ###
33+
.vscode/

pom.xml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.4.1</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>org.sadtech.example</groupId>
12+
<artifactId>swagger</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>swagger</name>
15+
<description>Demo project for Swagger</description>
16+
17+
<properties>
18+
<java.version>1.8</java.version>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-web</artifactId>
25+
</dependency>
26+
27+
<dependency>
28+
<groupId>io.swagger.core.v3</groupId>
29+
<artifactId>swagger-annotations</artifactId>
30+
<version>2.1.6</version>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.springdoc</groupId>
34+
<artifactId>springdoc-openapi-ui</artifactId>
35+
<version>1.5.2</version>
36+
</dependency>
37+
</dependencies>
38+
39+
<build>
40+
<plugins>
41+
<plugin>
42+
<groupId>org.springframework.boot</groupId>
43+
<artifactId>spring-boot-maven-plugin</artifactId>
44+
</plugin>
45+
</plugins>
46+
</build>
47+
48+
</project>

src/main/java/org/sadtech/example/swagger/controller/PointController.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,21 @@
66
import org.sadtech.example.swagger.dto.TypeOperation;
77
import org.sadtech.example.swagger.dto.UserDto;
88
import org.springframework.http.HttpStatus;
9+
import org.springframework.validation.annotation.Validated;
910
import org.springframework.web.bind.annotation.PathVariable;
1011
import org.springframework.web.bind.annotation.PostMapping;
1112
import org.springframework.web.bind.annotation.RequestMapping;
1213
import org.springframework.web.bind.annotation.RequestPart;
1314
import org.springframework.web.bind.annotation.RestController;
1415

16+
import javax.validation.constraints.Min;
17+
import javax.validation.constraints.NotBlank;
1518
import java.util.Map;
1619

1720
/**
1821
* @author upagge 30.12.2020
1922
*/
23+
@Validated
2024
@RestController
2125
@RequestMapping("api/user/point")
2226
@Tag(name = "Система лояльности", description = "Управляет балами пользователей")
@@ -31,9 +35,9 @@ public PointController(Map<String, UserDto> repository) {
3135
@PostMapping("{key}")
3236
@Operation(summary = "Управление баллами", description = "Позволяет удалить или добавить баллы пользователю")
3337
public HttpStatus changePoints(
34-
@PathVariable @Parameter(description = "Идентификатор пользователя") String key,
35-
@RequestPart("point") @Parameter(description = "Количество баллов") Long point,
36-
@RequestPart("type") @Parameter(description = "Тип операции") TypeOperation type
38+
@PathVariable @NotBlank @Parameter(description = "Идентификатор пользователя") String key,
39+
@RequestPart("point") @Min(0) @Parameter(description = "Количество баллов", required = true) Long point,
40+
@RequestPart("type") @Parameter(description = "Тип операции", required = true) TypeOperation type
3741
) {
3842
final UserDto userDto = repository.get(key);
3943
userDto.setPoints(

src/main/java/org/sadtech/example/swagger/dto/UserDto.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import io.swagger.v3.oas.annotations.media.Schema;
44

5+
import javax.validation.constraints.NotBlank;
56
import java.time.LocalDateTime;
67

78
/**
@@ -16,7 +17,7 @@ public class UserDto {
1617
@Schema(description = "ФИО", example = "Иванов Иван Иванович")
1718
private String name;
1819

19-
@Schema(description = "Баллы пользователя")
20+
@Schema(description = "Баллы пользователя", accessMode = Schema.AccessMode.READ_ONLY)
2021
private Long points = 0L;
2122

2223
@Schema(description = "Пол пользователя")

0 commit comments

Comments
 (0)