AccountController.java

  1. package de.japrost.jabudget.spring;

  2. import static de.japrost.jabudget.spring.PathMapping.*;

  3. import java.util.List;
  4. import java.util.Optional;

  5. import org.springframework.http.HttpStatus;
  6. import org.springframework.http.MediaType;
  7. import org.springframework.web.bind.annotation.*;

  8. import de.japrost.jabudget.domain.DomainException;
  9. import de.japrost.jabudget.domain.DomainFailure;
  10. import de.japrost.jabudget.domain.account.Account;
  11. import de.japrost.jabudget.domain.account.Entry;
  12. import de.japrost.jabudget.domain.account.EntryBuilder;
  13. import de.japrost.jabudget.service.AccountService;

  14. /**
  15.  * REST controller for {@link Account}s.
  16.  */
  17. @RestController @RequestMapping(BASE) public class AccountController {

  18.     private final AccountService accountService;

  19.     /**
  20.      * Instantiate with necessary dependencies.
  21.      *
  22.      * @param accountService the {@link AccountService} to use.
  23.      */
  24.     public AccountController(final AccountService accountService) {
  25.         this.accountService = accountService;
  26.     }

  27.     /**
  28.      * Retrieve all accounts.
  29.      *
  30.      * @return all accounts
  31.      */
  32.     @GetMapping(ACCOUNTS)
  33.     public List<Account> retrieveAll() {
  34.         return accountService.retrieveAll();
  35.     }

  36.     /**
  37.      * Retrieve a single {@link Account} by id.
  38.      *
  39.      * @param id the id to look for
  40.      * @return the found {@link Account}
  41.      * @throws DomainException if finding fails
  42.      */
  43.     @GetMapping(ACCOUNTS_ID)
  44.     public Account retrieveById(@PathVariable(ID_PARAM) final String id) throws DomainException {
  45.         final Optional<Account> account = accountService.retrieveById(id);
  46.         // TODO do not use DomainException in Controller.
  47.         return account.orElseThrow(() -> new DomainException(DomainFailure.ENTITY_NOT_AVAILABLE));
  48.     }

  49.     /**
  50.      * Create a new Account.
  51.      *
  52.      * @param account the account to create
  53.      * @return the new account
  54.      * @throws DomainException if creating fails
  55.      */
  56.     @PostMapping(path = ACCOUNTS, consumes = MediaType.APPLICATION_JSON_VALUE)
  57.     public Account create(@RequestBody final Account.Builder account) throws DomainException {
  58.         return accountService.create(account.build());
  59.     }

  60.     /**
  61.      * Update an existing Account.
  62.      *
  63.      * @param id      the id to look for
  64.      * @param account the account to update. The id from the account is ignored.
  65.      * @return the updated account
  66.      * @throws DomainException if creating fails
  67.      */
  68.     @PutMapping(path = ACCOUNTS_ID, consumes = MediaType.APPLICATION_JSON_VALUE)
  69.     // TODO use HTTP UPDATE?
  70.     public Account update(@PathVariable(ID_PARAM) final String id, @RequestBody final Account.Builder account)
  71.         throws DomainException {
  72.         account.setId(id);
  73.         return accountService.update(account.build());
  74.     }

  75.     /**
  76.      * Delete an Account.
  77.      *
  78.      * @param id The id of the account to be deleted.
  79.      */
  80.     @DeleteMapping(path = ACCOUNTS_ID)
  81.     @ResponseStatus(code = HttpStatus.NO_CONTENT)
  82.     public void delete(@PathVariable(ID_PARAM) final String id) {
  83.         //Boolean result =
  84.         accountService.erase(id);
  85.         // TODO handle result on false
  86.         // 200 (OK) if the response includes an entity describing the status
  87.         // * @return {@link Boolean#TRUE} if the account is removed after this operation. {@link Boolean#FALSE} else.
  88.         // return result;
  89.         // 204 (No Content) if the action has been enacted but the response does not include an entity
  90.     }

  91.     /**
  92.      * Create a new {@link Entry} with the given values.
  93.      *
  94.      * @param entry the entry to create.
  95.      * @return The entry as stored in the repository.
  96.      * @throws DomainException with {@link DomainFailure#DUPLICATE_ENTITY} if the given entry already exists.
  97.      * @throws DomainException with {@link DomainFailure#MISSING_ENTITY_REFERENCE} if the account for the entry does not exists.
  98.      */
  99.     @PostMapping(path = ACCOUNTS_ENTRIES, consumes = MediaType.APPLICATION_JSON_VALUE)
  100.     public Entry create(@RequestBody final EntryBuilder entry) throws DomainException {
  101.         // TODO handle failures while binding
  102.         return accountService.create(entry.build());
  103.     }
  104.     /**
  105.      * Retrieve all entries for an accounts.
  106.      *
  107.      * @return all entries for the account
  108.     @GetMapping(ACCOUNTS_ENTRIES)
  109.     public List<Entry> retrieveAll(@PathVariable(ID_PARAM) final String id) {
  110.         return accountService.retrieveAllEntries(id);
  111.     }
  112.      */

  113. }