View Javadoc
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   * REST controller for {@link Account}s.
21   */
22  @RestController @RequestMapping(BASE) public class AccountController {
23  
24  	private final AccountService accountService;
25  
26  	/**
27  	 * Instantiate with necessary dependencies.
28  	 *
29  	 * @param accountService the {@link AccountService} to use.
30  	 */
31  	public AccountController(final AccountService accountService) {
32  		this.accountService = accountService;
33  	}
34  
35  	/**
36  	 * Retrieve all accounts.
37  	 *
38  	 * @return all accounts
39  	 */
40  	@GetMapping(ACCOUNTS)
41  	public List<Account> retrieveAll() {
42  		return accountService.retrieveAll();
43  	}
44  
45  	/**
46  	 * Retrieve a single {@link Account} by id.
47  	 *
48  	 * @param id the id to look for
49  	 * @return the found {@link Account}
50  	 * @throws DomainException if finding fails
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  		// TODO do not use DomainException in Controller.
56  		return account.orElseThrow(() -> new DomainException(DomainFailure.ENTITY_NOT_AVAILABLE));
57  	}
58  
59  	/**
60  	 * Create a new Account.
61  	 *
62  	 * @param account the account to create
63  	 * @return the new account
64  	 * @throws DomainException if creating fails
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  	 * Update an existing Account.
73  	 *
74  	 * @param id      the id to look for
75  	 * @param account the account to update. The id from the account is ignored.
76  	 * @return the updated account
77  	 * @throws DomainException if creating fails
78  	 */
79  	@PutMapping(path = ACCOUNTS_ID, consumes = MediaType.APPLICATION_JSON_VALUE)
80  	// TODO use HTTP UPDATE?
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  	 * Delete an Account.
89  	 *
90  	 * @param id The id of the account to be deleted.
91  	 */
92  	@DeleteMapping(path = ACCOUNTS_ID)
93  	@ResponseStatus(code = HttpStatus.NO_CONTENT)
94  	public void delete(@PathVariable(ID_PARAM) final String id) {
95  		//Boolean result =
96  		accountService.erase(id);
97  		// TODO handle result on false
98  		// 200 (OK) if the response includes an entity describing the status
99  		// * @return {@link Boolean#TRUE} if the account is removed after this operation. {@link Boolean#FALSE} else.
100 		// return result;
101 		// 204 (No Content) if the action has been enacted but the response does not include an entity
102 	}
103 
104 	/**
105 	 * Create a new {@link Entry} with the given values.
106 	 *
107 	 * @param entry the entry to create.
108 	 * @return The entry as stored in the repository.
109 	 * @throws DomainException with {@link DomainFailure#DUPLICATE_ENTITY} if the given entry already exists.
110 	 * @throws DomainException with {@link DomainFailure#MISSING_ENTITY_REFERENCE} if the account for the entry does not exists.
111 	 */
112 	@PostMapping(path = ACCOUNTS_ENTRIES, consumes = MediaType.APPLICATION_JSON_VALUE)
113 	public Entry create(@RequestBody final EntryBuilder entry) throws DomainException {
114 		// TODO handle failures while binding
115 		return accountService.create(entry.build());
116 	}
117 	/**
118 	 * Retrieve all entries for an accounts.
119 	 *
120 	 * @return all entries for the account
121 	@GetMapping(ACCOUNTS_ENTRIES)
122 	public List<Entry> retrieveAll(@PathVariable(ID_PARAM) final String id) {
123 		return accountService.retrieveAllEntries(id);
124 	}
125 	 */
126 
127 }