-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJarController.java
99 lines (84 loc) · 4.67 KB
/
JarController.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package io.myfinbox.spendingplan.adapter.web;
import io.myfinbox.rest.JarCategoryModificationResource;
import io.myfinbox.rest.JarExpenseCategoryResource;
import io.myfinbox.rest.JarResource;
import io.myfinbox.shared.ApiFailureHandler;
import io.myfinbox.shared.Failure;
import io.myfinbox.spendingplan.application.*;
import io.myfinbox.spendingplan.application.AddOrRemoveJarCategoryUseCase.JarCategoryToAddOrRemove;
import lombok.RequiredArgsConstructor;
import org.springframework.core.convert.ConversionService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.Objects;
import java.util.UUID;
import static io.myfinbox.spendingplan.application.AddOrRemoveJarCategoryUseCase.JarCategoriesCommand;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
import static org.springframework.http.ResponseEntity.created;
import static org.springframework.http.ResponseEntity.ok;
import static org.springframework.web.servlet.support.ServletUriComponentsBuilder.fromCurrentRequest;
@RestController
@RequiredArgsConstructor
@RequestMapping(path = "/v1/plans")
final class JarController implements JarsApi {
private final CreateJarUseCase createJarUseCase;
private final AddOrRemoveJarCategoryUseCase addOrRemoveJarCategoryUseCase;
private final JarQuery jarQuery;
private final JarExpenseCategoryQuery jarExpenseCategoryQuery;
private final ApiFailureHandler apiFailureHandler;
private final ConversionService conversionService;
@PostMapping(path = "/{planId}/jars", consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE)
public ResponseEntity<?> create(@PathVariable UUID planId, @RequestBody JarResource resource) {
return createJarUseCase.create(planId, toCommand(resource))
.fold(apiFailureHandler::handle, jar -> created(fromCurrentRequest().path("/{id}").build(jar.getId().id()))
.body(conversionService.convert(jar, JarResource.class)));
}
@PutMapping(path = "/{planId}/jars/{jarId}/expense-categories", consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE)
public ResponseEntity<?> modifyExpenseCategories(@PathVariable UUID planId,
@PathVariable UUID jarId,
@RequestBody JarCategoryModificationResource resource) {
return addOrRemoveJarCategoryUseCase.addOrRemove(planId, jarId, toCommand(resource))
.fold(apiFailureHandler::handle, ok -> ok().build());
}
@GetMapping(path = "/{planId}/jars/{jarId}", produces = APPLICATION_JSON_VALUE)
public ResponseEntity<?> one(@PathVariable UUID planId, @PathVariable UUID jarId) {
var jars = jarQuery.search()
.withPlanId(planId)
.withJarId(jarId)
.list();
if (jars.isEmpty()) {
return apiFailureHandler.handle(Failure.ofNotFound("Jar with ID '%s' for plan ID '%s' was not found.".formatted(jarId, planId)));
}
return ok().body(conversionService.convert(jars.getFirst(), JarResource.class));
}
@GetMapping(path = "/{planId}/jars", produces = APPLICATION_JSON_VALUE)
public ResponseEntity<?> list(@PathVariable("planId") UUID planId) {
var jars = jarQuery.search()
.withPlanId(planId)
.list();
return ok().body(jars.stream()
.map(jar -> conversionService.convert(jar, JarResource.class))
.toList());
}
@GetMapping(path = "/{planId}/jars/{jarId}/expense-categories", produces = APPLICATION_JSON_VALUE)
public ResponseEntity<?> list(@PathVariable UUID planId, @PathVariable UUID jarId) {
return ok().body(jarExpenseCategoryQuery.search(planId, jarId).stream()
.map(expenseCategory -> conversionService.convert(expenseCategory, JarExpenseCategoryResource.class))
.toList());
}
private JarCommand toCommand(JarResource resource) {
return JarCommand.builder()
.name(resource.getName())
.percentage(resource.getPercentage())
.description(resource.getDescription())
.build();
}
private JarCategoriesCommand toCommand(JarCategoryModificationResource resource) {
var categoryToAdds = resource.getCategories()
.stream()
.filter(Objects::nonNull) // avoid null categoryToAdd
.map(categoryToAdd -> new JarCategoryToAddOrRemove(categoryToAdd.getCategoryId(), categoryToAdd.getCategoryName(), categoryToAdd.getToAdd()))
.toList();
return new JarCategoriesCommand(categoryToAdds);
}
}