1 package de.japrost.jabudget.spring;
2
3 import static de.japrost.jabudget.spring.PathMapping.*;
4
5 import java.util.List;
6 import java.util.Optional;
7
8 import org.springframework.http.HttpStatus;
9 import org.springframework.http.MediaType;
10 import org.springframework.web.bind.annotation.*;
11
12 import de.japrost.jabudget.domain.DomainException;
13 import de.japrost.jabudget.domain.DomainFailure;
14 import de.japrost.jabudget.domain.account.Account;
15 import de.japrost.jabudget.domain.account.Entry;
16 import de.japrost.jabudget.domain.account.EntryBuilder;
17 import de.japrost.jabudget.service.AccountService;
18
19
20
21
22 @RestController @RequestMapping(BASE) public class AccountController {
23
24 private final AccountService accountService;
25
26
27
28
29
30
31 public AccountController(final AccountService accountService) {
32 this.accountService = accountService;
33 }
34
35
36
37
38
39
40 @GetMapping(ACCOUNTS)
41 public List<Account> retrieveAll() {
42 return accountService.retrieveAll();
43 }
44
45
46
47
48
49
50
51
52 @GetMapping(ACCOUNTS_ID)
53 public Account retrieveById(@PathVariable(ID_PARAM) final String id) throws DomainException {
54 final Optional<Account> account = accountService.retrieveById(id);
55
56 return account.orElseThrow(() -> new DomainException(DomainFailure.ENTITY_NOT_AVAILABLE));
57 }
58
59
60
61
62
63
64
65
66 @PostMapping(path = ACCOUNTS, consumes = MediaType.APPLICATION_JSON_VALUE)
67 public Account create(@RequestBody final Account.Builder account) throws DomainException {
68 return accountService.create(account.build());
69 }
70
71
72
73
74
75
76
77
78
79 @PutMapping(path = ACCOUNTS_ID, consumes = MediaType.APPLICATION_JSON_VALUE)
80
81 public Account update(@PathVariable(ID_PARAM) final String id, @RequestBody final Account.Builder account)
82 throws DomainException {
83 account.setId(id);
84 return accountService.update(account.build());
85 }
86
87
88
89
90
91
92 @DeleteMapping(path = ACCOUNTS_ID)
93 @ResponseStatus(code = HttpStatus.NO_CONTENT)
94 public void delete(@PathVariable(ID_PARAM) final String id) {
95
96 accountService.erase(id);
97
98
99
100
101
102 }
103
104
105
106
107
108
109
110
111
112 @PostMapping(path = ACCOUNTS_ENTRIES, consumes = MediaType.APPLICATION_JSON_VALUE)
113 public Entry create(@RequestBody final EntryBuilder entry) throws DomainException {
114
115 return accountService.create(entry.build());
116 }
117
118
119
120
121
122
123
124
125
126
127 }