InitCommit

This commit is contained in:
uPagge 2021-01-06 19:42:35 +03:00
commit de5bd5b2d3
No known key found for this signature in database
GPG Key ID: 964B40928E4C9088
10 changed files with 307 additions and 0 deletions

View File

@ -0,0 +1,13 @@
package org.sadtech.example.swagger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SwaggerApplication {
public static void main(String[] args) {
SpringApplication.run(SwaggerApplication.class, args);
}
}

View File

@ -0,0 +1,28 @@
package org.sadtech.example.swagger.config;
import org.sadtech.example.swagger.dto.Gender;
import org.sadtech.example.swagger.dto.UserDto;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* // TODO: 30.12.2020 Добавить описание.
*
* @author upagge 30.12.2020
*/
@Configuration
public class AppConfig {
@Bean
public Map<String, UserDto> userRepository() {
return Stream.of(
UserDto.of("key1", "value1", Gender.MAN),
UserDto.of("key2", "value2", Gender.WOMAN)
).collect(Collectors.toMap(UserDto::getKey, userDto -> userDto));
}
}

View File

@ -0,0 +1,25 @@
package org.sadtech.example.swagger.config;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
*
* @author upagge 30.12.2020
*/
@Configuration
public class SwaggerConfig {
@Bean
public OpenAPI customOpenAPI() {
return new OpenAPI()
.info(
new Info()
.title("Loyalty System Api")
.version("1.0.0")
);
}
}

View File

@ -0,0 +1,45 @@
package org.sadtech.example.swagger.controller;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.sadtech.example.swagger.dto.TypeOperation;
import org.sadtech.example.swagger.dto.UserDto;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
/**
* @author upagge 30.12.2020
*/
@RestController
@RequestMapping("api/user/point")
@Tag(name = "Система лояльности", description = "Управляет балами пользователей")
public class PointController {
private final Map<String, UserDto> repository;
public PointController(Map<String, UserDto> repository) {
this.repository = repository;
}
@PostMapping("{key}")
@Operation(summary = "Управление баллами", description = "Позволяет удалить или добавить баллы пользователю")
public HttpStatus changePoints(
@PathVariable @Parameter(description = "Идентификатор пользователя") String key,
@RequestPart("point") @Parameter(description = "Количество баллов") Long point,
@RequestPart("type") @Parameter(description = "Тип операции") TypeOperation type
) {
final UserDto userDto = repository.get(key);
userDto.setPoints(
TypeOperation.PLUS.equals(type) ? userDto.getPoints() + point : userDto.getPoints() - point
);
return HttpStatus.OK;
}
}

View File

@ -0,0 +1,34 @@
package org.sadtech.example.swagger.controller;
import io.swagger.v3.oas.annotations.Hidden;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.sadtech.example.swagger.dto.UserDto;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
/**
* @author upagge 30.12.2020
*/
@RestController
@RequestMapping("api/secret")
@Hidden
@Tag(name = "Секретный контролер", description = "Позволяет удалить всех пользователей")
public class SecretController {
private final Map<String, UserDto> repository;
public SecretController(Map<String, UserDto> repository) {
this.repository = repository;
}
@GetMapping(value = "destroy")
public HttpStatus destroy() {
repository.clear();
return HttpStatus.OK;
}
}

View File

@ -0,0 +1,57 @@
package org.sadtech.example.swagger.controller;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.sadtech.example.swagger.dto.UserDto;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Map;
import static org.springframework.util.MimeTypeUtils.APPLICATION_JSON_VALUE;
/**
* @author upagge 30.12.2020
*/
@RestController
@RequestMapping("/api/user")
@Tag(name = "Пользователи", description = "Взаимодействие с пользователями")
public class UserController {
private final Map<String, UserDto> repository;
public UserController(Map<String, UserDto> repository) {
this.repository = repository;
}
@PutMapping(produces = APPLICATION_JSON_VALUE)
@Operation(summary = "Регистрация пользователя", description = "Позволяет зарегистрировать пользователя")
public HttpStatus registerUser(@RequestBody UserDto userDto) {
userDto.setPoints(0L);
repository.put(userDto.getKey(), userDto);
return HttpStatus.OK;
}
@PostMapping(produces = APPLICATION_JSON_VALUE)
@Operation(summary = "Обновление пользователя")
public HttpStatus updateUser(@RequestBody UserDto userDto) {
if (!repository.containsKey(userDto.getKey())) return HttpStatus.NOT_FOUND;
if (!repository.get(userDto.getKey()).getPoints().equals(userDto.getPoints())) return HttpStatus.BAD_REQUEST;
repository.put(userDto.getKey(), userDto);
return HttpStatus.OK;
}
@GetMapping(value = "{key}", produces = APPLICATION_JSON_VALUE)
@Operation(summary = "Получить пользователя")
public ResponseEntity<UserDto> getSimpleDto(@PathVariable("key") String key) {
return ResponseEntity.ok(repository.get(key));
}
}

View File

@ -0,0 +1,11 @@
package org.sadtech.example.swagger.dto;
/**
*
* @author upagge 04.01.2021
*/
public enum Gender {
MAN, WOMAN
}

View File

@ -0,0 +1,13 @@
package org.sadtech.example.swagger.dto;
/**
* // TODO: 04.01.2021 Добавить описание.
*
* @author upagge 04.01.2021
*/
public enum TypeOperation {
MINUS,
PLUS
}

View File

@ -0,0 +1,80 @@
package org.sadtech.example.swagger.dto;
import io.swagger.v3.oas.annotations.media.Schema;
import java.time.LocalDateTime;
/**
* @author upagge 30.12.2020
*/
@Schema(description = "Пользователь")
public class UserDto {
@Schema(description = "Идентификатор", accessMode = Schema.AccessMode.READ_ONLY)
private String key;
@Schema(description = "ФИО", example = "Иванов Иван Иванович")
private String name;
@Schema(description = "Баллы пользователя")
private Long points = 0L;
@Schema(description = "Пол пользователя")
private Gender gender;
@Schema(description = "Дата и время регистрации", accessMode = Schema.AccessMode.READ_ONLY)
private LocalDateTime regDate = LocalDateTime.now();
public UserDto() {
}
public UserDto(String key, String name, Gender gender) {
this.key = key;
this.name = name;
this.gender = gender;
}
public static UserDto of(String key, String value, Gender gender) {
return new UserDto(key, value, gender);
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getPoints() {
return points;
}
public void setPoints(Long points) {
this.points = points;
}
public Gender getGender() {
return gender;
}
public void setGender(Gender gender) {
this.gender = gender;
}
public LocalDateTime getRegDate() {
return regDate;
}
public void setRegDate(LocalDateTime regDate) {
this.regDate = regDate;
}
}

View File

@ -0,0 +1 @@