-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAccountController.java
60 lines (50 loc) · 2.47 KB
/
AccountController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package io.myfinbox.account.adapter.web;
import io.myfinbox.account.application.CreateAccountUseCase;
import io.myfinbox.account.application.CreateAccountUseCase.CreateAccountCommand;
import io.myfinbox.rest.AccountCreateResource;
import io.myfinbox.shared.ApiFailureHandler;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.Currency;
import java.util.Locale;
import static java.util.Objects.isNull;
import static org.springframework.http.HttpHeaders.ACCEPT_LANGUAGE;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
import static org.springframework.http.ResponseEntity.created;
import static org.springframework.web.servlet.support.ServletUriComponentsBuilder.fromCurrentRequest;
@RestController
@RequestMapping(path = "/v1/accounts")
@RequiredArgsConstructor
final class AccountController implements AccountsApi {
static final Locale defaultLocale = Locale.of("en", "MD");
private final CreateAccountUseCase createAccountUseCase;
private final ApiFailureHandler apiFailureHandler;
@PostMapping(consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE)
public ResponseEntity<?> create(@RequestHeader(name = ACCEPT_LANGUAGE, required = false) Locale locale,
@RequestBody AccountCreateResource resource) {
var command = CreateAccountCommand.builder()
.firstName(resource.getFirstName())
.lastName(resource.getLastName())
.emailAddress(resource.getEmailAddress())
.currency(Currency.getInstance(resolve(locale)).getCurrencyCode())
.zoneId(resource.getZoneId())
.build();
return createAccountUseCase.create(command).fold(apiFailureHandler::handle,
account -> created(fromCurrentRequest().path("/{id}").build(account.getId().toString()))
.body(resource.accountId(account.getId().id())
.currency(command.currency())
.zoneId(resource.getZoneId())
));
}
private Locale resolve(Locale locale) {
try {
if (isNull(Currency.getInstance(locale).getCurrencyCode())) {
return defaultLocale;
}
} catch (NullPointerException | IllegalArgumentException e) {
return defaultLocale;
}
return locale;
}
}