logging of Transaction CRUD operations
This commit is contained in:
@@ -1,33 +0,0 @@
|
|||||||
package de.tilman.transactions.controller;
|
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.data.rest.webmvc.RepositoryRestController;
|
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestMethod;
|
|
||||||
import org.springframework.web.bind.annotation.ResponseBody;
|
|
||||||
|
|
||||||
import de.tilman.transactions.domain.Transaction;
|
|
||||||
import de.tilman.transactions.repository.TransactionRepository;
|
|
||||||
|
|
||||||
//@RepositoryRestController
|
|
||||||
public class TransactionController {
|
|
||||||
|
|
||||||
// private final TransactionRepository repository;
|
|
||||||
//
|
|
||||||
// @Autowired
|
|
||||||
// public TransactionController(TransactionRepository repository) {
|
|
||||||
// this.repository = repository;
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// @RequestMapping(method = RequestMethod.DELETE, value = "/transactions/{id}")
|
|
||||||
// public @ResponseBody Iterable<Transaction> deleteTransaction() {
|
|
||||||
// Iterable<Transaction> producers = repository.findAll();
|
|
||||||
//
|
|
||||||
// //
|
|
||||||
// // do some intermediate processing, logging, etc. with the producers
|
|
||||||
// //
|
|
||||||
//
|
|
||||||
// return producers; // or some filtered/altered/mapped version
|
|
||||||
// }
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
package de.tilman.transactions.domain;
|
package de.tilman.transactions.domain;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
|
import java.text.DecimalFormat;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
@@ -10,6 +11,14 @@ import javax.persistence.ManyToOne;
|
|||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
public class Transaction {
|
public class Transaction {
|
||||||
|
|
||||||
|
private static DecimalFormat df = new DecimalFormat();
|
||||||
|
static {
|
||||||
|
df.getDecimalFormatSymbols().setDecimalSeparator('.');
|
||||||
|
df.setMaximumFractionDigits(2);
|
||||||
|
df.setMinimumFractionDigits(0);
|
||||||
|
df.setGroupingUsed(false);
|
||||||
|
}
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue
|
@GeneratedValue
|
||||||
@@ -91,5 +100,23 @@ public class Transaction {
|
|||||||
public void setCategory(Category category) {
|
public void setCategory(Category category) {
|
||||||
this.category = category;
|
this.category = category;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
String s = "{ id: " + id + ", amount: " + df.format(amount) + ", date: " + date + ", lastChange: " + lastChange + ", description: \"" + description + "\"";
|
||||||
|
|
||||||
|
if (creditor != null)
|
||||||
|
s += ", creditor: \"" + creditor.getName() + "\"";
|
||||||
|
else
|
||||||
|
s += ", creditor: null";
|
||||||
|
|
||||||
|
s += ", account: " + account.getId();
|
||||||
|
s += ", category: \"" + category.getName() + "\"";
|
||||||
|
s += "}";
|
||||||
|
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package de.tilman.transactions.domain;
|
|||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
import javax.persistence.CascadeType;
|
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
import javax.persistence.GeneratedValue;
|
import javax.persistence.GeneratedValue;
|
||||||
import javax.persistence.Id;
|
import javax.persistence.Id;
|
||||||
|
|||||||
@@ -4,6 +4,9 @@ import java.util.Date;
|
|||||||
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.data.rest.core.annotation.HandleAfterCreate;
|
||||||
|
import org.springframework.data.rest.core.annotation.HandleAfterDelete;
|
||||||
|
import org.springframework.data.rest.core.annotation.HandleAfterSave;
|
||||||
import org.springframework.data.rest.core.annotation.HandleBeforeCreate;
|
import org.springframework.data.rest.core.annotation.HandleBeforeCreate;
|
||||||
import org.springframework.data.rest.core.annotation.HandleBeforeSave;
|
import org.springframework.data.rest.core.annotation.HandleBeforeSave;
|
||||||
import org.springframework.data.rest.core.annotation.RepositoryEventHandler;
|
import org.springframework.data.rest.core.annotation.RepositoryEventHandler;
|
||||||
@@ -22,7 +25,21 @@ public class SpringDataRestEventHandler {
|
|||||||
@HandleBeforeSave
|
@HandleBeforeSave
|
||||||
public void applyUserInformationUsingSecurityContext(Transaction transaction) {
|
public void applyUserInformationUsingSecurityContext(Transaction transaction) {
|
||||||
Date timestamp = new Date();
|
Date timestamp = new Date();
|
||||||
log.info("Setting date '" + timestamp + "' on transaction: " + transaction.getId() + "(" + transaction.getDescription() + ")");
|
|
||||||
transaction.setLastChange(timestamp);
|
transaction.setLastChange(timestamp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@HandleAfterCreate
|
||||||
|
public void logCreation(Transaction transaction) {
|
||||||
|
log.info("Created transaction: " + transaction.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@HandleAfterSave
|
||||||
|
public void logUpdate(Transaction transaction) {
|
||||||
|
log.info("Updated transaction: " + transaction.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
@HandleAfterDelete
|
||||||
|
public void logDeletion(Transaction transaction) {
|
||||||
|
log.info("Deleted transaction: " + transaction.toString());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -15,8 +15,8 @@ import de.tilman.transactions.domain.Transaction;
|
|||||||
@RepositoryRestResource(excerptProjection = TransactionInlineProjection.class)
|
@RepositoryRestResource(excerptProjection = TransactionInlineProjection.class)
|
||||||
public interface TransactionRepository extends PagingAndSortingRepository<Transaction, Long> {
|
public interface TransactionRepository extends PagingAndSortingRepository<Transaction, Long> {
|
||||||
|
|
||||||
// @RestResource(exported = false)
|
@RestResource(exported = false)
|
||||||
// Page<Transaction> findAll(Pageable pageable);
|
Page<Transaction> findAll(Pageable pageable);
|
||||||
|
|
||||||
// @PreAuthorize("hasRole('ROLE_ADMIN')")
|
// @PreAuthorize("hasRole('ROLE_ADMIN')")
|
||||||
// @Override
|
// @Override
|
||||||
|
|||||||
Reference in New Issue
Block a user