fix: List in User response instead of Map

This commit is contained in:
Piotr Dec 2024-06-27 01:19:30 +02:00
parent a37f1c2834
commit 177f673696
Signed by: stawros
GPG key ID: F89F27AD8F881A91
4 changed files with 21 additions and 4 deletions

View file

@ -4,6 +4,7 @@ import eu.ztsh.wymiana.data.entity.CurrencyEntity;
import eu.ztsh.wymiana.data.entity.UserEntity; import eu.ztsh.wymiana.data.entity.UserEntity;
import eu.ztsh.wymiana.model.User; import eu.ztsh.wymiana.model.User;
import eu.ztsh.wymiana.web.model.UserCreateRequest; import eu.ztsh.wymiana.web.model.UserCreateRequest;
import eu.ztsh.wymiana.web.model.UserResponse;
import java.util.List; import java.util.List;
@ -19,6 +20,10 @@ public class UserMapper {
CurrencyMapper.pojoMapToEntities(pojo.currencies(), pojo.pesel())); 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) { public static UserEntity requestToEntity(UserCreateRequest request) {
return new UserEntity(request.pesel(), request.name(), request.surname(), return new UserEntity(request.pesel(), request.name(), request.surname(),
List.of(new CurrencyEntity(request.pesel(), "PLN", request.initial()))); List.of(new CurrencyEntity(request.pesel(), "PLN", request.initial())));

View file

@ -2,9 +2,9 @@ package eu.ztsh.wymiana.web.controller;
import eu.ztsh.wymiana.exception.InsufficientFundsException; import eu.ztsh.wymiana.exception.InsufficientFundsException;
import eu.ztsh.wymiana.exception.UserNotFoundException; import eu.ztsh.wymiana.exception.UserNotFoundException;
import eu.ztsh.wymiana.model.User;
import eu.ztsh.wymiana.service.CurrencyService; import eu.ztsh.wymiana.service.CurrencyService;
import eu.ztsh.wymiana.web.model.CurrencyExchangeRequest; 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.Operation;
import io.swagger.v3.oas.annotations.media.Content; import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.media.Schema;
@ -33,7 +33,8 @@ public class ExchangeController {
@ApiResponses(value = { @ApiResponses(value = {
@ApiResponse(responseCode = "200", @ApiResponse(responseCode = "200",
description = "Exchange performed successfully", 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", @ApiResponse(responseCode = "400",
description = "Insufficient funds", description = "Insufficient funds",
content = @Content(mediaType = MediaType.TEXT_PLAIN_VALUE)), content = @Content(mediaType = MediaType.TEXT_PLAIN_VALUE)),

View file

@ -2,10 +2,11 @@ package eu.ztsh.wymiana.web.controller;
import eu.ztsh.wymiana.exception.UserAlreadyExistsException; import eu.ztsh.wymiana.exception.UserAlreadyExistsException;
import eu.ztsh.wymiana.exception.UserNotFoundException; import eu.ztsh.wymiana.exception.UserNotFoundException;
import eu.ztsh.wymiana.model.User;
import eu.ztsh.wymiana.service.UserService; import eu.ztsh.wymiana.service.UserService;
import eu.ztsh.wymiana.util.UserMapper;
import eu.ztsh.wymiana.validation.ValidationFailedException; import eu.ztsh.wymiana.validation.ValidationFailedException;
import eu.ztsh.wymiana.web.model.UserCreateRequest; 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.Operation;
import io.swagger.v3.oas.annotations.media.Content; import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.responses.ApiResponse; 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) @ApiResponse(responseCode = "500", description = "Another error has occurred", content = @Content)
}) })
@GetMapping("{pesel}") @GetMapping("{pesel}")
public ResponseEntity<User> get(@PathVariable("pesel") String pesel) { public ResponseEntity<UserResponse> get(@PathVariable("pesel") String pesel) {
try { try {
return userService.get(pesel) return userService.get(pesel)
.map(UserMapper::pojoToResponse)
.map(ResponseEntity::ok) .map(ResponseEntity::ok)
.orElseThrow(() -> new UserNotFoundException(pesel)); .orElseThrow(() -> new UserNotFoundException(pesel));
} catch (Exception e) { } catch (Exception e) {

View file

@ -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) {
}