-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlanController.java
73 lines (64 loc) · 3.33 KB
/
PlanController.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
61
62
63
64
65
66
67
68
69
70
71
72
73
package io.myfinbox.spendingplan.adapter.web;
import io.myfinbox.rest.CreateClassicPlanResource;
import io.myfinbox.rest.PlanResource;
import io.myfinbox.shared.ApiFailureHandler;
import io.myfinbox.spendingplan.application.ClassicPlanBuilderUseCase;
import io.myfinbox.spendingplan.application.ClassicPlanBuilderUseCase.CreateClassicPlanCommand;
import io.myfinbox.spendingplan.application.CreatePlanUseCase;
import io.myfinbox.spendingplan.application.PlanCommand;
import io.myfinbox.spendingplan.domain.Plan;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
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/plans")
@RequiredArgsConstructor
final class PlanController implements PlansApi {
private final CreatePlanUseCase createPlanUseCase;
private final ClassicPlanBuilderUseCase classicPlanBuilderUseCase;
private final ApiFailureHandler apiFailureHandler;
@PostMapping(consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE)
public ResponseEntity<?> create(@RequestBody PlanResource resource) {
return createPlanUseCase.create(toCommand(resource))
.fold(apiFailureHandler::handle, plan -> created(fromCurrentRequest().path("/{id}").build(plan.getId().id()))
.body(toResource(plan)));
}
@PostMapping(path = "/classic", consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE)
public ResponseEntity<?> createClassic(@RequestBody CreateClassicPlanResource resource) {
return classicPlanBuilderUseCase.create(toCommand(resource))
.fold(apiFailureHandler::handle, plan -> created(fromCurrentRequest().path("/{id}").build(plan.getId().id()))
.body(toResource(plan)));
}
private PlanResource toResource(Plan plan) {
return new PlanResource()
.planId(plan.getId().id())
.name(plan.getName())
.creationTimestamp(plan.getCreationTimestamp().toString())
.amount(plan.getAmountAsNumber())
.currencyCode(plan.getCurrencyCode())
.accountId(plan.getAccount().id())
.description(plan.getDescription());
}
private PlanCommand toCommand(PlanResource resource) {
return PlanCommand.builder()
.accountId(resource.getAccountId())
.name(resource.getName())
.amount(resource.getAmount())
.currencyCode(resource.getCurrencyCode())
.description(resource.getDescription())
.build();
}
private CreateClassicPlanCommand toCommand(CreateClassicPlanResource resource) {
return CreateClassicPlanCommand.builder()
.accountId(resource.getAccountId())
.amount(resource.getAmount())
.currencyCode(resource.getCurrencyCode())
.build();
}
}