Files
EasyDrop/src/main/java/de/rwu/easydrop/service/processing/TransactionHandler.java
2023-06-28 00:38:02 +02:00

48 lines
1.4 KiB
Java

package de.rwu.easydrop.service.processing;
import de.rwu.easydrop.api.dto.TransactionDTO;
import de.rwu.easydrop.data.connector.TransactionPersistenceInterface;
import de.rwu.easydrop.model.Offer;
import de.rwu.easydrop.model.Transaction;
import de.rwu.easydrop.service.mapping.TransactionMapper;
import de.rwu.easydrop.util.Timestamp;
/**
* Handles transactions.
*
* @since 0.3.0
*/
public class TransactionHandler {
/**
* Transaction persistence.
*/
private TransactionPersistenceInterface txPersistence;
/**
* Creates new transaction handler.
*
* @param txdb transaction persistence
*/
public TransactionHandler(final TransactionPersistenceInterface txdb) {
this.txPersistence = txdb;
}
/**
* Creates transaction from offer.
*
* @param offer Offer
*/
public void turnOfferToTransaction(final Offer offer) {
Transaction tx = new Transaction();
tx.setOfferId(offer.getOfferId());
tx.setVolume(offer.getTargetProduct().getCurrentPrice());
tx.setEarnings(offer.getTargetProduct().getCurrentPrice()
- offer.getSourceProduct().getCurrentPrice());
tx.setTransactionTime(Timestamp.now());
// Write transaction to persistence
TransactionDTO txDTO = TransactionMapper.mapTXToDTO(tx);
txPersistence.writeTX(txDTO);
}
}