-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJarController.java
65 lines (56 loc) · 3.22 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
package io.myfinbox.spendingplan.adapter.web;
import io.myfinbox.rest.JarCategoryModificationResource;
import io.myfinbox.rest.JarResource;
import io.myfinbox.shared.ApiFailureHandler;
import io.myfinbox.spendingplan.application.AddOrRemoveJarCategoryUseCase;
import io.myfinbox.spendingplan.application.AddOrRemoveJarCategoryUseCase.JarCategoryToAddOrRemove;
import io.myfinbox.spendingplan.application.CreateJarUseCase;
import io.myfinbox.spendingplan.application.JarCommand;
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 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());
}
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.getToAdd()))
.toList();
return new JarCategoriesCommand(categoryToAdds);
}
}