feat: OpenAPI

This commit is contained in:
Piotr Dec 2024-06-27 01:06:33 +02:00
parent cf0bf5d97e
commit a37f1c2834
Signed by: stawros
GPG key ID: F89F27AD8F881A91
2 changed files with 43 additions and 0 deletions

View file

@ -2,11 +2,18 @@ package eu.ztsh.wymiana.web.controller;
import eu.ztsh.wymiana.exception.InsufficientFundsException;
import eu.ztsh.wymiana.exception.UserNotFoundException;
import eu.ztsh.wymiana.model.User;
import eu.ztsh.wymiana.service.CurrencyService;
import eu.ztsh.wymiana.web.model.CurrencyExchangeRequest;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
@ -22,6 +29,21 @@ public class ExchangeController {
private final CurrencyService currencyService;
@Operation(summary = "Perform exchange")
@ApiResponses(value = {
@ApiResponse(responseCode = "200",
description = "Exchange performed successfully",
content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = User.class))),
@ApiResponse(responseCode = "400",
description = "Insufficient funds",
content = @Content(mediaType = MediaType.TEXT_PLAIN_VALUE)),
@ApiResponse(responseCode = "404",
description = "User not found",
content = @Content(mediaType = MediaType.TEXT_PLAIN_VALUE)),
@ApiResponse(responseCode = "500",
description = "Another error has occurred",
content = @Content(mediaType = MediaType.TEXT_PLAIN_VALUE))
})
@PostMapping
public ResponseEntity<Object> exchange(@Valid @RequestBody CurrencyExchangeRequest request) {
try {

View file

@ -6,10 +6,16 @@ import eu.ztsh.wymiana.model.User;
import eu.ztsh.wymiana.service.UserService;
import eu.ztsh.wymiana.validation.ValidationFailedException;
import eu.ztsh.wymiana.web.model.UserCreateRequest;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.ValidationException;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
@ -23,10 +29,18 @@ import org.springframework.web.bind.annotation.RestController;
@Validated
@RestController
@RequestMapping(path = "/api/user", produces = "application/json")
@Tag(name="User management", description = "Create or get user")
public class UserController {
private final UserService userService;
@Operation(summary = "Get user by PESEL")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "User found"),
@ApiResponse(responseCode = "400", description = "Request not valid", content = @Content),
@ApiResponse(responseCode = "404", description = "User not found", content = @Content),
@ApiResponse(responseCode = "500", description = "Another error has occurred", content = @Content)
})
@GetMapping("{pesel}")
public ResponseEntity<User> get(@PathVariable("pesel") String pesel) {
try {
@ -42,6 +56,13 @@ public class UserController {
}
}
@Operation(summary = "Create user")
@ApiResponses(value = {
@ApiResponse(responseCode = "204", description = "User created", content = @Content),
@ApiResponse(responseCode = "400", description = "Request not valid", content = @Content(mediaType = MediaType.TEXT_PLAIN_VALUE)),
@ApiResponse(responseCode = "409", description = "User already exists", content = @Content(mediaType = MediaType.TEXT_PLAIN_VALUE)),
@ApiResponse(responseCode = "500", description = "Another error has occurred", content = @Content(mediaType = MediaType.TEXT_PLAIN_VALUE))
})
@PostMapping
public ResponseEntity<String> create(@Valid @RequestBody UserCreateRequest request) {
try {