Compare commits

..

No commits in common. "e02ca8c4ae23a6f575100a18a1a0b4fa30ecf876" and "5549c8618547098dd76866b19f0414e81e182e9a" have entirely different histories.

8 changed files with 8 additions and 74 deletions

View file

@ -28,7 +28,6 @@
<!-- dependencies -->
<wiremock.version>3.5.4</wiremock.version>
<openapi.version>2.5.0</openapi.version>
<!-- plugins -->
<jsonschema2pojo.version>1.2.1</jsonschema2pojo.version>
@ -63,11 +62,6 @@
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>${openapi.version}</version>
</dependency>
<!-- Database -->
<dependency>

View file

@ -79,6 +79,7 @@ Prosty mikroserwis stworzony na potrzeby rekrutacji
"type": "string"
},
"currencies": {
"$comment": "TODO: Map -> List",
"type": "array",
"items": {
"$ref": "#/def/currency"

View file

@ -4,7 +4,6 @@ import eu.ztsh.wymiana.data.entity.CurrencyEntity;
import eu.ztsh.wymiana.data.entity.UserEntity;
import eu.ztsh.wymiana.model.User;
import eu.ztsh.wymiana.model.UserCreateRequestConfiguredWrapper;
import eu.ztsh.wymiana.web.model.UserResponse;
import java.util.List;
@ -20,10 +19,6 @@ public class UserMapper {
CurrencyMapper.pojoMapToEntities(pojo.currencies(), pojo.pesel()));
}
public static UserResponse pojoToResponse(User pojo) {
return new UserResponse(pojo.name(), pojo.surname(), pojo.pesel(), pojo.currencies().values().stream().toList());
}
public static UserEntity requestToEntity(UserCreateRequestConfiguredWrapper request) {
return new UserEntity(request.pesel(), request.name(), request.surname(),
List.of(new CurrencyEntity(request.pesel(), request.initialSymbol(), request.initial())));

View file

@ -4,16 +4,9 @@ import eu.ztsh.wymiana.exception.InsufficientFundsException;
import eu.ztsh.wymiana.exception.UserNotFoundException;
import eu.ztsh.wymiana.service.CurrencyService;
import eu.ztsh.wymiana.web.model.CurrencyExchangeRequest;
import eu.ztsh.wymiana.web.model.UserResponse;
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;
@ -29,22 +22,6 @@ 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 = UserResponse.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

@ -2,21 +2,14 @@ package eu.ztsh.wymiana.web.controller;
import eu.ztsh.wymiana.exception.UserAlreadyExistsException;
import eu.ztsh.wymiana.exception.UserNotFoundException;
import eu.ztsh.wymiana.model.User;
import eu.ztsh.wymiana.service.UserService;
import eu.ztsh.wymiana.util.UserMapper;
import eu.ztsh.wymiana.validation.ValidationFailedException;
import eu.ztsh.wymiana.web.model.UserCreateRequest;
import eu.ztsh.wymiana.web.model.UserResponse;
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;
@ -30,23 +23,14 @@ 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<UserResponse> get(@PathVariable("pesel") String pesel) {
public ResponseEntity<User> get(@PathVariable("pesel") String pesel) {
try {
return userService.get(pesel)
.map(UserMapper::pojoToResponse)
.map(ResponseEntity::ok)
.orElseThrow(() -> new UserNotFoundException(pesel));
} catch (Exception e) {
@ -58,13 +42,6 @@ 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 {

View file

@ -2,7 +2,6 @@ package eu.ztsh.wymiana.web.model;
import eu.ztsh.wymiana.model.Symbol;
import eu.ztsh.wymiana.validation.ValidExchangeRequest;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotNull;
import lombok.Builder;
import org.hibernate.validator.constraints.pl.PESEL;
@ -12,11 +11,11 @@ import java.math.BigDecimal;
@Builder
@ValidExchangeRequest
public record CurrencyExchangeRequest(
@NotNull @PESEL String pesel,
@PESEL String pesel,
@NotNull Symbol from,
@NotNull Symbol to,
@Min(0) BigDecimal toBuy,
@Min(0) BigDecimal toSell
BigDecimal toBuy,
BigDecimal toSell
) {
}

View file

@ -12,7 +12,7 @@ import java.math.BigDecimal;
public record UserCreateRequest(
@NotNull String name,
@NotNull String surname,
@PESEL @Adult @NotNull String pesel,
@NotNull @Min(0) BigDecimal initial) {
@PESEL @Adult String pesel,
@Min(0) BigDecimal initial) {
}

View file

@ -1,9 +0,0 @@
package eu.ztsh.wymiana.web.model;
import eu.ztsh.wymiana.model.Currency;
import java.util.List;
public record UserResponse(String name, String surname, String pesel, List<Currency> currencies) {
}