View Javadoc
1   package de.japrost.jabudget.service;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   import java.util.Optional;
6   
7   import de.japrost.jabudget.domain.DomainException;
8   import de.japrost.jabudget.domain.DomainFailure;
9   import de.japrost.jabudget.domain.account.Account;
10  import de.japrost.jabudget.domain.account.Entry;
11  import de.japrost.jabudget.repository.AccountRepository;
12  
13  /**
14   * Business service for {@link Account}s.
15   */
16  public class AccountService {
17  
18  	private final AccountRepository accountRepository;
19  
20  	/**
21  	 * Initialize with all dependencies.
22  	 *
23  	 * @param accountRepository the {@link AccountRepository} to use.
24  	 */
25  	public AccountService(final AccountRepository accountRepository) {
26  		this.accountRepository = accountRepository;
27  	}
28  
29  	/**
30  	 * Create a new {@link Account}.
31  	 *
32  	 * @param account the Account to be created
33  	 * @return the new Account.
34  	 * @throws DomainException when the Account already exists.
35  	 */
36  	public Account create(final Account account) throws DomainException {
37  		// TODO the service creates the ID. Do not use create to import an existing account (e.g. created in an offline application).
38  		return accountRepository.create(account);
39  	}
40  
41  	/**
42  	 * Update an existing {@link Account}.
43  	 *
44  	 * @param account the Account to be updated
45  	 * @return the updated Account.
46  	 * @throws DomainException when the Account does not exist.
47  	 */
48  	public Account update(final Account account) throws DomainException {
49  		return accountRepository.update(account);
50  	}
51  
52  	/**
53  	 * Retrieve all accounts.
54  	 *
55  	 * @return all available accounts.
56  	 */
57  	public List<Account> retrieveAll() {
58  		return new ArrayList<>(accountRepository.findAll());
59  	}
60  
61  	/**
62  	 * Retrieve a single {@link Account} by its id.
63  	 *
64  	 * @param id the id of the {@link Account}.
65  	 * @return the {@link Account} with the id. An empty {@link Optional} if no {@link Account} is available for the given
66  	 *         id.
67  	 */
68  	public Optional<Account> retrieveById(final String id) {
69  		return accountRepository.findById(id);
70  	}
71  
72  	/**
73  	 * Delete a single {@link Account} by its id.
74  	 *
75  	 * @param id of the {@link Account} to delete.
76  	 * @return {@link Boolean#TRUE} if the account is removed after this operation.
77  	 */
78  	public Boolean erase(final String id) {
79  		return accountRepository.delete(id);
80  	}
81  
82  	/**
83  	 * Create a new {@link Entry} with the given values.
84  	 *
85  	 * @param entry the entry to create.
86  	 * @return The entry as stored in the repository.
87  	 * @throws DomainException with {@link DomainFailure#DUPLICATE_ENTITY} if the given entry already exists.
88  	 * @throws DomainException with {@link DomainFailure#MISSING_ENTITY_REFERENCE} if the account for the entry does not exists.
89  	 */
90  	public Entry create(final Entry entry) throws DomainException {
91  		// TODO who creates the Entry-Code.
92  		return accountRepository.create(entry);
93  	}
94  
95  }