Skip to content

Commit de5bd5b

Browse files
committed
InitCommit
0 parents  commit de5bd5b

File tree

10 files changed

+307
-0
lines changed

10 files changed

+307
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.sadtech.example.swagger;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class SwaggerApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(SwaggerApplication.class, args);
11+
}
12+
13+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.sadtech.example.swagger.config;
2+
3+
import org.sadtech.example.swagger.dto.Gender;
4+
import org.sadtech.example.swagger.dto.UserDto;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.context.annotation.Configuration;
7+
8+
import java.util.Map;
9+
import java.util.stream.Collectors;
10+
import java.util.stream.Stream;
11+
12+
/**
13+
* // TODO: 30.12.2020 Добавить описание.
14+
*
15+
* @author upagge 30.12.2020
16+
*/
17+
@Configuration
18+
public class AppConfig {
19+
20+
@Bean
21+
public Map<String, UserDto> userRepository() {
22+
return Stream.of(
23+
UserDto.of("key1", "value1", Gender.MAN),
24+
UserDto.of("key2", "value2", Gender.WOMAN)
25+
).collect(Collectors.toMap(UserDto::getKey, userDto -> userDto));
26+
}
27+
28+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.sadtech.example.swagger.config;
2+
3+
import io.swagger.v3.oas.models.OpenAPI;
4+
import io.swagger.v3.oas.models.info.Info;
5+
import org.springframework.context.annotation.Bean;
6+
import org.springframework.context.annotation.Configuration;
7+
8+
/**
9+
*
10+
* @author upagge 30.12.2020
11+
*/
12+
@Configuration
13+
public class SwaggerConfig {
14+
15+
@Bean
16+
public OpenAPI customOpenAPI() {
17+
return new OpenAPI()
18+
.info(
19+
new Info()
20+
.title("Loyalty System Api")
21+
.version("1.0.0")
22+
);
23+
}
24+
25+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.sadtech.example.swagger.controller;
2+
3+
import io.swagger.v3.oas.annotations.Operation;
4+
import io.swagger.v3.oas.annotations.Parameter;
5+
import io.swagger.v3.oas.annotations.tags.Tag;
6+
import org.sadtech.example.swagger.dto.TypeOperation;
7+
import org.sadtech.example.swagger.dto.UserDto;
8+
import org.springframework.http.HttpStatus;
9+
import org.springframework.web.bind.annotation.PathVariable;
10+
import org.springframework.web.bind.annotation.PostMapping;
11+
import org.springframework.web.bind.annotation.RequestMapping;
12+
import org.springframework.web.bind.annotation.RequestPart;
13+
import org.springframework.web.bind.annotation.RestController;
14+
15+
import java.util.Map;
16+
17+
/**
18+
* @author upagge 30.12.2020
19+
*/
20+
@RestController
21+
@RequestMapping("api/user/point")
22+
@Tag(name = "Система лояльности", description = "Управляет балами пользователей")
23+
public class PointController {
24+
25+
private final Map<String, UserDto> repository;
26+
27+
public PointController(Map<String, UserDto> repository) {
28+
this.repository = repository;
29+
}
30+
31+
@PostMapping("{key}")
32+
@Operation(summary = "Управление баллами", description = "Позволяет удалить или добавить баллы пользователю")
33+
public HttpStatus changePoints(
34+
@PathVariable @Parameter(description = "Идентификатор пользователя") String key,
35+
@RequestPart("point") @Parameter(description = "Количество баллов") Long point,
36+
@RequestPart("type") @Parameter(description = "Тип операции") TypeOperation type
37+
) {
38+
final UserDto userDto = repository.get(key);
39+
userDto.setPoints(
40+
TypeOperation.PLUS.equals(type) ? userDto.getPoints() + point : userDto.getPoints() - point
41+
);
42+
return HttpStatus.OK;
43+
}
44+
45+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package org.sadtech.example.swagger.controller;
2+
3+
import io.swagger.v3.oas.annotations.Hidden;
4+
import io.swagger.v3.oas.annotations.tags.Tag;
5+
import org.sadtech.example.swagger.dto.UserDto;
6+
import org.springframework.http.HttpStatus;
7+
import org.springframework.web.bind.annotation.GetMapping;
8+
import org.springframework.web.bind.annotation.RequestMapping;
9+
import org.springframework.web.bind.annotation.RestController;
10+
11+
import java.util.Map;
12+
13+
/**
14+
* @author upagge 30.12.2020
15+
*/
16+
@RestController
17+
@RequestMapping("api/secret")
18+
@Hidden
19+
@Tag(name = "Секретный контролер", description = "Позволяет удалить всех пользователей")
20+
public class SecretController {
21+
22+
private final Map<String, UserDto> repository;
23+
24+
public SecretController(Map<String, UserDto> repository) {
25+
this.repository = repository;
26+
}
27+
28+
@GetMapping(value = "destroy")
29+
public HttpStatus destroy() {
30+
repository.clear();
31+
return HttpStatus.OK;
32+
}
33+
34+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package org.sadtech.example.swagger.controller;
2+
3+
import io.swagger.v3.oas.annotations.Operation;
4+
import io.swagger.v3.oas.annotations.tags.Tag;
5+
import org.sadtech.example.swagger.dto.UserDto;
6+
import org.springframework.http.HttpStatus;
7+
import org.springframework.http.ResponseEntity;
8+
import org.springframework.web.bind.annotation.GetMapping;
9+
import org.springframework.web.bind.annotation.PathVariable;
10+
import org.springframework.web.bind.annotation.PostMapping;
11+
import org.springframework.web.bind.annotation.PutMapping;
12+
import org.springframework.web.bind.annotation.RequestBody;
13+
import org.springframework.web.bind.annotation.RequestMapping;
14+
import org.springframework.web.bind.annotation.RestController;
15+
16+
import java.util.Map;
17+
18+
import static org.springframework.util.MimeTypeUtils.APPLICATION_JSON_VALUE;
19+
20+
/**
21+
* @author upagge 30.12.2020
22+
*/
23+
@RestController
24+
@RequestMapping("/api/user")
25+
@Tag(name = "Пользователи", description = "Взаимодействие с пользователями")
26+
public class UserController {
27+
28+
private final Map<String, UserDto> repository;
29+
30+
public UserController(Map<String, UserDto> repository) {
31+
this.repository = repository;
32+
}
33+
34+
@PutMapping(produces = APPLICATION_JSON_VALUE)
35+
@Operation(summary = "Регистрация пользователя", description = "Позволяет зарегистрировать пользователя")
36+
public HttpStatus registerUser(@RequestBody UserDto userDto) {
37+
userDto.setPoints(0L);
38+
repository.put(userDto.getKey(), userDto);
39+
return HttpStatus.OK;
40+
}
41+
42+
@PostMapping(produces = APPLICATION_JSON_VALUE)
43+
@Operation(summary = "Обновление пользователя")
44+
public HttpStatus updateUser(@RequestBody UserDto userDto) {
45+
if (!repository.containsKey(userDto.getKey())) return HttpStatus.NOT_FOUND;
46+
if (!repository.get(userDto.getKey()).getPoints().equals(userDto.getPoints())) return HttpStatus.BAD_REQUEST;
47+
repository.put(userDto.getKey(), userDto);
48+
return HttpStatus.OK;
49+
}
50+
51+
@GetMapping(value = "{key}", produces = APPLICATION_JSON_VALUE)
52+
@Operation(summary = "Получить пользователя")
53+
public ResponseEntity<UserDto> getSimpleDto(@PathVariable("key") String key) {
54+
return ResponseEntity.ok(repository.get(key));
55+
}
56+
57+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.sadtech.example.swagger.dto;
2+
3+
/**
4+
*
5+
* @author upagge 04.01.2021
6+
*/
7+
public enum Gender {
8+
9+
MAN, WOMAN
10+
11+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.sadtech.example.swagger.dto;
2+
3+
/**
4+
* // TODO: 04.01.2021 Добавить описание.
5+
*
6+
* @author upagge 04.01.2021
7+
*/
8+
public enum TypeOperation {
9+
10+
MINUS,
11+
PLUS
12+
13+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package org.sadtech.example.swagger.dto;
2+
3+
import io.swagger.v3.oas.annotations.media.Schema;
4+
5+
import java.time.LocalDateTime;
6+
7+
/**
8+
* @author upagge 30.12.2020
9+
*/
10+
@Schema(description = "Пользователь")
11+
public class UserDto {
12+
13+
@Schema(description = "Идентификатор", accessMode = Schema.AccessMode.READ_ONLY)
14+
private String key;
15+
16+
@Schema(description = "ФИО", example = "Иванов Иван Иванович")
17+
private String name;
18+
19+
@Schema(description = "Баллы пользователя")
20+
private Long points = 0L;
21+
22+
@Schema(description = "Пол пользователя")
23+
private Gender gender;
24+
25+
@Schema(description = "Дата и время регистрации", accessMode = Schema.AccessMode.READ_ONLY)
26+
private LocalDateTime regDate = LocalDateTime.now();
27+
28+
public UserDto() {
29+
}
30+
31+
public UserDto(String key, String name, Gender gender) {
32+
this.key = key;
33+
this.name = name;
34+
this.gender = gender;
35+
}
36+
37+
public static UserDto of(String key, String value, Gender gender) {
38+
return new UserDto(key, value, gender);
39+
}
40+
41+
public String getKey() {
42+
return key;
43+
}
44+
45+
public void setKey(String key) {
46+
this.key = key;
47+
}
48+
49+
public String getName() {
50+
return name;
51+
}
52+
53+
public void setName(String name) {
54+
this.name = name;
55+
}
56+
57+
public Long getPoints() {
58+
return points;
59+
}
60+
61+
public void setPoints(Long points) {
62+
this.points = points;
63+
}
64+
65+
public Gender getGender() {
66+
return gender;
67+
}
68+
69+
public void setGender(Gender gender) {
70+
this.gender = gender;
71+
}
72+
73+
public LocalDateTime getRegDate() {
74+
return regDate;
75+
}
76+
77+
public void setRegDate(LocalDateTime regDate) {
78+
this.regDate = regDate;
79+
}
80+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

0 commit comments

Comments
 (0)