fix: List in User response instead of Map
This commit is contained in:
parent
a37f1c2834
commit
177f673696
4 changed files with 21 additions and 4 deletions
|
@ -4,6 +4,7 @@ import eu.ztsh.wymiana.data.entity.CurrencyEntity;
|
|||
import eu.ztsh.wymiana.data.entity.UserEntity;
|
||||
import eu.ztsh.wymiana.model.User;
|
||||
import eu.ztsh.wymiana.web.model.UserCreateRequest;
|
||||
import eu.ztsh.wymiana.web.model.UserResponse;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
@ -19,6 +20,10 @@ 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(UserCreateRequest request) {
|
||||
return new UserEntity(request.pesel(), request.name(), request.surname(),
|
||||
List.of(new CurrencyEntity(request.pesel(), "PLN", request.initial())));
|
||||
|
|
|
@ -2,9 +2,9 @@ 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 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;
|
||||
|
@ -33,7 +33,8 @@ public class ExchangeController {
|
|||
@ApiResponses(value = {
|
||||
@ApiResponse(responseCode = "200",
|
||||
description = "Exchange performed successfully",
|
||||
content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = User.class))),
|
||||
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)),
|
||||
|
|
|
@ -2,10 +2,11 @@ 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;
|
||||
|
@ -42,9 +43,10 @@ public class UserController {
|
|||
@ApiResponse(responseCode = "500", description = "Another error has occurred", content = @Content)
|
||||
})
|
||||
@GetMapping("{pesel}")
|
||||
public ResponseEntity<User> get(@PathVariable("pesel") String pesel) {
|
||||
public ResponseEntity<UserResponse> get(@PathVariable("pesel") String pesel) {
|
||||
try {
|
||||
return userService.get(pesel)
|
||||
.map(UserMapper::pojoToResponse)
|
||||
.map(ResponseEntity::ok)
|
||||
.orElseThrow(() -> new UserNotFoundException(pesel));
|
||||
} catch (Exception e) {
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
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) {
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue