28
28
import lombok .RequiredArgsConstructor ;
29
29
30
30
import java .util .List ;
31
+ import java .util .function .Supplier ;
31
32
32
33
@ Controller ("/api/profile/export" )
33
34
@ Secured (SecurityRule .IS_AUTHENTICATED )
@@ -53,21 +54,16 @@ public HttpResponse<ExportJson> export() {
53
54
var exportJson = ExportJson .builder ()
54
55
.accounts (lookupAllOf (Account .class )
55
56
.map (account -> AccountJson .fromDomain (
56
- account ,
57
- () -> storageService .read (account .getImageFileToken ()).get ()))
57
+ account , loadFromStorage (account .getImageFileToken ())))
58
58
.toJava ())
59
59
.budgetPeriods (lookupAllOf (Budget .class ).map (BudgetJson ::fromDomain ).toJava ())
60
60
.categories (lookupAllOf (Category .class ).map (CategoryJson ::fromDomain ).toJava ())
61
61
.tags (lookupAllOf (Tag .class ).map (Tag ::name ).toJava ())
62
62
.contracts (lookupAllOf (Contract .class )
63
- .map (c -> ContractJson .fromDomain (
64
- c ,
65
- () -> storageService .read (c .getFileToken ()).get ()))
63
+ .map (c -> ContractJson .fromDomain (c , loadFromStorage (c .getFileToken ())))
66
64
.toJava ())
67
65
.rules (lookupAllOf (TransactionRule .class )
68
- .map (rule -> RuleConfigJson .RuleJson .fromDomain (
69
- rule ,
70
- this ::loadRelation ))
66
+ .map (rule -> RuleConfigJson .RuleJson .fromDomain (rule , this ::loadRelation ))
71
67
.toJava ())
72
68
.transactions (lookupRelevantTransactions ())
73
69
.build ();
@@ -77,6 +73,10 @@ public HttpResponse<ExportJson> export() {
77
73
.header (HttpHeaders .CONTENT_TYPE , MediaType .APPLICATION_JSON );
78
74
}
79
75
76
+ private Supplier <byte []> loadFromStorage (String fileToken ) {
77
+ return () -> storageService .read (fileToken ).getOrSupply (() -> new byte [0 ]);
78
+ }
79
+
80
80
@ SuppressWarnings ({"unchecked" , "rawtypes" })
81
81
private <T > Sequence <T > lookupAllOf (Class <T > forClass ) {
82
82
for (Exportable exporter : exportable ) {
0 commit comments