TransactionEntity
This commit is contained in:
@@ -38,7 +38,7 @@ public class Charge implements Serializable {
|
||||
private BigDecimal amount;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JsonIgnoreProperties(value = { "registrations" }, allowSetters = true)
|
||||
@JsonIgnoreProperties(value = { "registrations", "transactions" }, allowSetters = true)
|
||||
private Event event;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
|
||||
@@ -47,6 +47,10 @@ public class Event implements Serializable {
|
||||
@JsonIgnoreProperties(value = { "user", "event" }, allowSetters = true)
|
||||
private Set<Registration> registrations = new HashSet<>();
|
||||
|
||||
@OneToMany(fetch = FetchType.LAZY, mappedBy = "event")
|
||||
@JsonIgnoreProperties(value = { "event" }, allowSetters = true)
|
||||
private Set<Transaction> transactions = new HashSet<>();
|
||||
|
||||
// jhipster-needle-entity-add-field - JHipster will add fields here
|
||||
|
||||
public Long getId() {
|
||||
@@ -158,6 +162,37 @@ public class Event implements Serializable {
|
||||
return this;
|
||||
}
|
||||
|
||||
public Set<Transaction> getTransactions() {
|
||||
return this.transactions;
|
||||
}
|
||||
|
||||
public void setTransactions(Set<Transaction> transactions) {
|
||||
if (this.transactions != null) {
|
||||
this.transactions.forEach(i -> i.setEvent(null));
|
||||
}
|
||||
if (transactions != null) {
|
||||
transactions.forEach(i -> i.setEvent(this));
|
||||
}
|
||||
this.transactions = transactions;
|
||||
}
|
||||
|
||||
public Event transactions(Set<Transaction> transactions) {
|
||||
this.setTransactions(transactions);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Event addTransaction(Transaction transaction) {
|
||||
this.transactions.add(transaction);
|
||||
transaction.setEvent(this);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Event removeTransaction(Transaction transaction) {
|
||||
this.transactions.remove(transaction);
|
||||
transaction.setEvent(null);
|
||||
return this;
|
||||
}
|
||||
|
||||
// jhipster-needle-entity-add-getters-setters - JHipster will add getters and setters here
|
||||
|
||||
@Override
|
||||
|
||||
@@ -41,7 +41,7 @@ public class Registration implements Serializable {
|
||||
private User user;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JsonIgnoreProperties(value = { "registrations" }, allowSetters = true)
|
||||
@JsonIgnoreProperties(value = { "registrations", "transactions" }, allowSetters = true)
|
||||
private Event event;
|
||||
|
||||
// jhipster-needle-entity-add-field - JHipster will add fields here
|
||||
|
||||
@@ -0,0 +1,135 @@
|
||||
package com.sasiedzi.event.domain;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
||||
import com.sasiedzi.event.domain.enumeration.TransactionType;
|
||||
import jakarta.persistence.*;
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDate;
|
||||
|
||||
/**
|
||||
* A Transaction.
|
||||
*/
|
||||
@Entity
|
||||
@Table(name = "transaction")
|
||||
@SuppressWarnings("common-java:DuplicatedBlocks")
|
||||
public class Transaction implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
|
||||
@SequenceGenerator(name = "sequenceGenerator")
|
||||
@Column(name = "id")
|
||||
private Long id;
|
||||
|
||||
@Enumerated(EnumType.STRING)
|
||||
@Column(name = "type")
|
||||
private TransactionType type;
|
||||
|
||||
@Column(name = "date")
|
||||
private LocalDate date;
|
||||
|
||||
@Column(name = "comment")
|
||||
private String comment;
|
||||
|
||||
@ManyToOne(fetch = FetchType.LAZY)
|
||||
@JsonIgnoreProperties(value = { "registrations", "transactions" }, allowSetters = true)
|
||||
private Event event;
|
||||
|
||||
// jhipster-needle-entity-add-field - JHipster will add fields here
|
||||
|
||||
public Long getId() {
|
||||
return this.id;
|
||||
}
|
||||
|
||||
public Transaction id(Long id) {
|
||||
this.setId(id);
|
||||
return this;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public TransactionType getType() {
|
||||
return this.type;
|
||||
}
|
||||
|
||||
public Transaction type(TransactionType type) {
|
||||
this.setType(type);
|
||||
return this;
|
||||
}
|
||||
|
||||
public void setType(TransactionType type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public LocalDate getDate() {
|
||||
return this.date;
|
||||
}
|
||||
|
||||
public Transaction date(LocalDate date) {
|
||||
this.setDate(date);
|
||||
return this;
|
||||
}
|
||||
|
||||
public void setDate(LocalDate date) {
|
||||
this.date = date;
|
||||
}
|
||||
|
||||
public String getComment() {
|
||||
return this.comment;
|
||||
}
|
||||
|
||||
public Transaction comment(String comment) {
|
||||
this.setComment(comment);
|
||||
return this;
|
||||
}
|
||||
|
||||
public void setComment(String comment) {
|
||||
this.comment = comment;
|
||||
}
|
||||
|
||||
public Event getEvent() {
|
||||
return this.event;
|
||||
}
|
||||
|
||||
public void setEvent(Event event) {
|
||||
this.event = event;
|
||||
}
|
||||
|
||||
public Transaction event(Event event) {
|
||||
this.setEvent(event);
|
||||
return this;
|
||||
}
|
||||
|
||||
// jhipster-needle-entity-add-getters-setters - JHipster will add getters and setters here
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (!(o instanceof Transaction)) {
|
||||
return false;
|
||||
}
|
||||
return getId() != null && getId().equals(((Transaction) o).getId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
// see https://vladmihalcea.com/how-to-implement-equals-and-hashcode-using-the-jpa-entity-identifier/
|
||||
return getClass().hashCode();
|
||||
}
|
||||
|
||||
// prettier-ignore
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Transaction{" +
|
||||
"id=" + getId() +
|
||||
", type='" + getType() + "'" +
|
||||
", date='" + getDate() + "'" +
|
||||
", comment='" + getComment() + "'" +
|
||||
"}";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.sasiedzi.event.domain.enumeration;
|
||||
|
||||
/**
|
||||
* The TransactionType enumeration.
|
||||
*/
|
||||
public enum TransactionType {
|
||||
PURCHASE,
|
||||
MATCH,
|
||||
FIELDPAYMENT,
|
||||
INTERNALTRANSFER,
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.sasiedzi.event.repository;
|
||||
|
||||
import com.sasiedzi.event.domain.Transaction;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.jpa.repository.*;
|
||||
import org.springframework.data.repository.query.Param;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
/**
|
||||
* Spring Data JPA repository for the Transaction entity.
|
||||
*/
|
||||
@Repository
|
||||
public interface TransactionRepository extends JpaRepository<Transaction, Long> {
|
||||
default Optional<Transaction> findOneWithEagerRelationships(Long id) {
|
||||
return this.findOneWithToOneRelationships(id);
|
||||
}
|
||||
|
||||
default List<Transaction> findAllWithEagerRelationships() {
|
||||
return this.findAllWithToOneRelationships();
|
||||
}
|
||||
|
||||
default Page<Transaction> findAllWithEagerRelationships(Pageable pageable) {
|
||||
return this.findAllWithToOneRelationships(pageable);
|
||||
}
|
||||
|
||||
@Query(
|
||||
value = "select transaction from Transaction transaction left join fetch transaction.event",
|
||||
countQuery = "select count(transaction) from Transaction transaction"
|
||||
)
|
||||
Page<Transaction> findAllWithToOneRelationships(Pageable pageable);
|
||||
|
||||
@Query("select transaction from Transaction transaction left join fetch transaction.event")
|
||||
List<Transaction> findAllWithToOneRelationships();
|
||||
|
||||
@Query("select transaction from Transaction transaction left join fetch transaction.event where transaction.id =:id")
|
||||
Optional<Transaction> findOneWithToOneRelationships(@Param("id") Long id);
|
||||
}
|
||||
@@ -0,0 +1,189 @@
|
||||
package com.sasiedzi.event.web.rest;
|
||||
|
||||
import com.sasiedzi.event.domain.Transaction;
|
||||
import com.sasiedzi.event.repository.TransactionRepository;
|
||||
import com.sasiedzi.event.web.rest.errors.BadRequestAlertException;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import tech.jhipster.web.util.HeaderUtil;
|
||||
import tech.jhipster.web.util.ResponseUtil;
|
||||
|
||||
/**
|
||||
* REST controller for managing {@link com.sasiedzi.event.domain.Transaction}.
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/api/transactions")
|
||||
@Transactional
|
||||
public class TransactionResource {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(TransactionResource.class);
|
||||
|
||||
private static final String ENTITY_NAME = "transaction";
|
||||
|
||||
@Value("${jhipster.clientApp.name}")
|
||||
private String applicationName;
|
||||
|
||||
private final TransactionRepository transactionRepository;
|
||||
|
||||
public TransactionResource(TransactionRepository transactionRepository) {
|
||||
this.transactionRepository = transactionRepository;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code POST /transactions} : Create a new transaction.
|
||||
*
|
||||
* @param transaction the transaction to create.
|
||||
* @return the {@link ResponseEntity} with status {@code 201 (Created)} and with body the new transaction, or with status {@code 400 (Bad Request)} if the transaction has already an ID.
|
||||
* @throws URISyntaxException if the Location URI syntax is incorrect.
|
||||
*/
|
||||
@PostMapping("")
|
||||
public ResponseEntity<Transaction> createTransaction(@RequestBody Transaction transaction) throws URISyntaxException {
|
||||
LOG.debug("REST request to save Transaction : {}", transaction);
|
||||
if (transaction.getId() != null) {
|
||||
throw new BadRequestAlertException("A new transaction cannot already have an ID", ENTITY_NAME, "idexists");
|
||||
}
|
||||
transaction = transactionRepository.save(transaction);
|
||||
return ResponseEntity.created(new URI("/api/transactions/" + transaction.getId()))
|
||||
.headers(HeaderUtil.createEntityCreationAlert(applicationName, false, ENTITY_NAME, transaction.getId().toString()))
|
||||
.body(transaction);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code PUT /transactions/:id} : Updates an existing transaction.
|
||||
*
|
||||
* @param id the id of the transaction to save.
|
||||
* @param transaction the transaction to update.
|
||||
* @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the updated transaction,
|
||||
* or with status {@code 400 (Bad Request)} if the transaction is not valid,
|
||||
* or with status {@code 500 (Internal Server Error)} if the transaction couldn't be updated.
|
||||
* @throws URISyntaxException if the Location URI syntax is incorrect.
|
||||
*/
|
||||
@PutMapping("/{id}")
|
||||
public ResponseEntity<Transaction> updateTransaction(
|
||||
@PathVariable(value = "id", required = false) final Long id,
|
||||
@RequestBody Transaction transaction
|
||||
) throws URISyntaxException {
|
||||
LOG.debug("REST request to update Transaction : {}, {}", id, transaction);
|
||||
if (transaction.getId() == null) {
|
||||
throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
|
||||
}
|
||||
if (!Objects.equals(id, transaction.getId())) {
|
||||
throw new BadRequestAlertException("Invalid ID", ENTITY_NAME, "idinvalid");
|
||||
}
|
||||
|
||||
if (!transactionRepository.existsById(id)) {
|
||||
throw new BadRequestAlertException("Entity not found", ENTITY_NAME, "idnotfound");
|
||||
}
|
||||
|
||||
transaction = transactionRepository.save(transaction);
|
||||
return ResponseEntity.ok()
|
||||
.headers(HeaderUtil.createEntityUpdateAlert(applicationName, false, ENTITY_NAME, transaction.getId().toString()))
|
||||
.body(transaction);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code PATCH /transactions/:id} : Partial updates given fields of an existing transaction, field will ignore if it is null
|
||||
*
|
||||
* @param id the id of the transaction to save.
|
||||
* @param transaction the transaction to update.
|
||||
* @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the updated transaction,
|
||||
* or with status {@code 400 (Bad Request)} if the transaction is not valid,
|
||||
* or with status {@code 404 (Not Found)} if the transaction is not found,
|
||||
* or with status {@code 500 (Internal Server Error)} if the transaction couldn't be updated.
|
||||
* @throws URISyntaxException if the Location URI syntax is incorrect.
|
||||
*/
|
||||
@PatchMapping(value = "/{id}", consumes = { "application/json", "application/merge-patch+json" })
|
||||
public ResponseEntity<Transaction> partialUpdateTransaction(
|
||||
@PathVariable(value = "id", required = false) final Long id,
|
||||
@RequestBody Transaction transaction
|
||||
) throws URISyntaxException {
|
||||
LOG.debug("REST request to partial update Transaction partially : {}, {}", id, transaction);
|
||||
if (transaction.getId() == null) {
|
||||
throw new BadRequestAlertException("Invalid id", ENTITY_NAME, "idnull");
|
||||
}
|
||||
if (!Objects.equals(id, transaction.getId())) {
|
||||
throw new BadRequestAlertException("Invalid ID", ENTITY_NAME, "idinvalid");
|
||||
}
|
||||
|
||||
if (!transactionRepository.existsById(id)) {
|
||||
throw new BadRequestAlertException("Entity not found", ENTITY_NAME, "idnotfound");
|
||||
}
|
||||
|
||||
Optional<Transaction> result = transactionRepository
|
||||
.findById(transaction.getId())
|
||||
.map(existingTransaction -> {
|
||||
if (transaction.getType() != null) {
|
||||
existingTransaction.setType(transaction.getType());
|
||||
}
|
||||
if (transaction.getDate() != null) {
|
||||
existingTransaction.setDate(transaction.getDate());
|
||||
}
|
||||
if (transaction.getComment() != null) {
|
||||
existingTransaction.setComment(transaction.getComment());
|
||||
}
|
||||
|
||||
return existingTransaction;
|
||||
})
|
||||
.map(transactionRepository::save);
|
||||
|
||||
return ResponseUtil.wrapOrNotFound(
|
||||
result,
|
||||
HeaderUtil.createEntityUpdateAlert(applicationName, false, ENTITY_NAME, transaction.getId().toString())
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code GET /transactions} : get all the transactions.
|
||||
*
|
||||
* @param eagerload flag to eager load entities from relationships (This is applicable for many-to-many).
|
||||
* @return the {@link ResponseEntity} with status {@code 200 (OK)} and the list of transactions in body.
|
||||
*/
|
||||
@GetMapping("")
|
||||
public List<Transaction> getAllTransactions(
|
||||
@RequestParam(name = "eagerload", required = false, defaultValue = "true") boolean eagerload
|
||||
) {
|
||||
LOG.debug("REST request to get all Transactions");
|
||||
if (eagerload) {
|
||||
return transactionRepository.findAllWithEagerRelationships();
|
||||
} else {
|
||||
return transactionRepository.findAll();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code GET /transactions/:id} : get the "id" transaction.
|
||||
*
|
||||
* @param id the id of the transaction to retrieve.
|
||||
* @return the {@link ResponseEntity} with status {@code 200 (OK)} and with body the transaction, or with status {@code 404 (Not Found)}.
|
||||
*/
|
||||
@GetMapping("/{id}")
|
||||
public ResponseEntity<Transaction> getTransaction(@PathVariable("id") Long id) {
|
||||
LOG.debug("REST request to get Transaction : {}", id);
|
||||
Optional<Transaction> transaction = transactionRepository.findOneWithEagerRelationships(id);
|
||||
return ResponseUtil.wrapOrNotFound(transaction);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@code DELETE /transactions/:id} : delete the "id" transaction.
|
||||
*
|
||||
* @param id the id of the transaction to delete.
|
||||
* @return the {@link ResponseEntity} with status {@code 204 (NO_CONTENT)}.
|
||||
*/
|
||||
@DeleteMapping("/{id}")
|
||||
public ResponseEntity<Void> deleteTransaction(@PathVariable("id") Long id) {
|
||||
LOG.debug("REST request to delete Transaction : {}", id);
|
||||
transactionRepository.deleteById(id);
|
||||
return ResponseEntity.noContent()
|
||||
.headers(HeaderUtil.createEntityDeletionAlert(applicationName, false, ENTITY_NAME, id.toString()))
|
||||
.build();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user