-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathTransactionCreateCommand.java
More file actions
71 lines (56 loc) · 2.35 KB
/
TransactionCreateCommand.java
File metadata and controls
71 lines (56 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package edu.uark.registerapp.commands.transactions;
import java.util.Optional;
import javax.transaction.Transactional;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import edu.uark.registerapp.commands.ResultCommandInterface;
import edu.uark.registerapp.commands.exceptions.ConflictException;
import edu.uark.registerapp.commands.exceptions.UnprocessableEntityException;
import edu.uark.registerapp.models.api.Transaction;
import edu.uark.registerapp.models.entities.TransactionEntity;
import edu.uark.registerapp.models.repositories.TransactionRepository;
@Service
public class TransactionCreateCommand implements ResultCommandInterface<Transaction> {
@Override
public Transaction execute() {
this.validateProperties();
final TransactionEntity createdTransactionEntity = this.createTransactionEntity();
// Synchronize information generated by the database upon INSERT.
this.apiTransaction.setId(createdTransactionEntity.getId());
this.apiTransaction.setCreatedOn(createdTransactionEntity.getCreatedOn());
return this.apiTransaction;
}
// Helper methods
private void validateProperties() {
if (StringUtils.isBlank(this.apiTransaction.getLookupCode())) {
throw new UnprocessableEntityException("lookupcode");
}
}
@Transactional
private TransactionEntity createTransactionEntity() {
final Optional<TransactionEntity> queriedTransactionEntity =
this.transactionRepository
.findByLookupCode(this.apiTransaction.getLookupCode());
if (queriedTransactionEntity.isPresent()) {
// Lookupcode already defined for another transaction.
throw new ConflictException("lookupcode");
}
// No ENTITY object was returned from the database, thus the API object's
// lookupcode must be unique.
// Write, via an INSERT, the new record to the database.
return this.transactionRepository.save(
new TransactionEntity(apiTransaction));
}
// Properties
private Transaction apiTransaction;
public Transaction getApiTransaction() {
return this.apiTransaction;
}
public TransactionCreateCommand setApiTransaction(final Transaction apiTransaction) {
this.apiTransaction = apiTransaction;
return this;
}
@Autowired
private TransactionRepository transactionRepository;
}