AccountService.java

  1. package de.japrost.jabudget.service;

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

  5. import de.japrost.jabudget.domain.DomainException;
  6. import de.japrost.jabudget.domain.DomainFailure;
  7. import de.japrost.jabudget.domain.account.Account;
  8. import de.japrost.jabudget.domain.account.Entry;
  9. import de.japrost.jabudget.repository.AccountRepository;

  10. /**
  11.  * Business service for {@link Account}s.
  12.  */
  13. public class AccountService {

  14.     private final AccountRepository accountRepository;

  15.     /**
  16.      * Initialize with all dependencies.
  17.      *
  18.      * @param accountRepository the {@link AccountRepository} to use.
  19.      */
  20.     public AccountService(final AccountRepository accountRepository) {
  21.         this.accountRepository = accountRepository;
  22.     }

  23.     /**
  24.      * Create a new {@link Account}.
  25.      *
  26.      * @param account the Account to be created
  27.      * @return the new Account.
  28.      * @throws DomainException when the Account already exists.
  29.      */
  30.     public Account create(final Account account) throws DomainException {
  31.         // TODO the service creates the ID. Do not use create to import an existing account (e.g. created in an offline application).
  32.         return accountRepository.create(account);
  33.     }

  34.     /**
  35.      * Update an existing {@link Account}.
  36.      *
  37.      * @param account the Account to be updated
  38.      * @return the updated Account.
  39.      * @throws DomainException when the Account does not exist.
  40.      */
  41.     public Account update(final Account account) throws DomainException {
  42.         return accountRepository.update(account);
  43.     }

  44.     /**
  45.      * Retrieve all accounts.
  46.      *
  47.      * @return all available accounts.
  48.      */
  49.     public List<Account> retrieveAll() {
  50.         return new ArrayList<>(accountRepository.findAll());
  51.     }

  52.     /**
  53.      * Retrieve a single {@link Account} by its id.
  54.      *
  55.      * @param id the id of the {@link Account}.
  56.      * @return the {@link Account} with the id. An empty {@link Optional} if no {@link Account} is available for the given
  57.      *         id.
  58.      */
  59.     public Optional<Account> retrieveById(final String id) {
  60.         return accountRepository.findById(id);
  61.     }

  62.     /**
  63.      * Delete a single {@link Account} by its id.
  64.      *
  65.      * @param id of the {@link Account} to delete.
  66.      * @return {@link Boolean#TRUE} if the account is removed after this operation.
  67.      */
  68.     public Boolean erase(final String id) {
  69.         return accountRepository.delete(id);
  70.     }

  71.     /**
  72.      * Create a new {@link Entry} with the given values.
  73.      *
  74.      * @param entry the entry to create.
  75.      * @return The entry as stored in the repository.
  76.      * @throws DomainException with {@link DomainFailure#DUPLICATE_ENTITY} if the given entry already exists.
  77.      * @throws DomainException with {@link DomainFailure#MISSING_ENTITY_REFERENCE} if the account for the entry does not exists.
  78.      */
  79.     public Entry create(final Entry entry) throws DomainException {
  80.         // TODO who creates the Entry-Code.
  81.         return accountRepository.create(entry);
  82.     }

  83. }