feat(transaction): add transaction retrieval and DTO mapping
This commit is contained in:
parent
5575955440
commit
d6077645d9
13 changed files with 146 additions and 13 deletions
|
@ -6,6 +6,7 @@ import lombok.Getter;
|
||||||
import lombok.Setter;
|
import lombok.Setter;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
@Setter
|
@Setter
|
||||||
@Getter
|
@Getter
|
||||||
|
@ -26,4 +27,7 @@ public class TransactionEntity {
|
||||||
|
|
||||||
@Enumerated(EnumType.STRING)
|
@Enumerated(EnumType.STRING)
|
||||||
private TransactionStatus status = TransactionStatus.PROCESSING;
|
private TransactionStatus status = TransactionStatus.PROCESSING;
|
||||||
|
|
||||||
|
@Column(name = "created_at")
|
||||||
|
private Date createdAt = new Date();
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,10 +5,17 @@ import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
import org.springframework.data.jpa.repository.Query;
|
import org.springframework.data.jpa.repository.Query;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public interface TransactionRepository extends JpaRepository<TransactionEntity, Long> {
|
public interface TransactionRepository extends JpaRepository<TransactionEntity, Long> {
|
||||||
@Query("SELECT t FROM TransactionEntity t WHERE t.sessionId = ?1")
|
@Query("SELECT t FROM TransactionEntity t WHERE t.sessionId = ?1")
|
||||||
Optional<TransactionEntity> findOneBySessionID(String sessionId);
|
Optional<TransactionEntity> findOneBySessionID(String sessionId);
|
||||||
|
|
||||||
|
@Query("SELECT t FROM TransactionEntity t WHERE t.user = ?1")
|
||||||
|
List<TransactionEntity> findAllByUserId(UserEntity id);
|
||||||
|
|
||||||
|
@Query("SELECT t FROM TransactionEntity t WHERE t.user = ?1 ORDER BY t.id DESC LIMIT ?2")
|
||||||
|
List<TransactionEntity> findByUserIdWithLimit(UserEntity userEntity, Integer limit);
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,12 +4,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpHeaders;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
|
||||||
import org.springframework.web.bind.annotation.RequestHeader;
|
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
|
||||||
|
|
||||||
import de.szut.casino.user.dto.CreateUserDto;
|
import de.szut.casino.user.dto.CreateUserDto;
|
||||||
import de.szut.casino.user.dto.GetUserDto;
|
import de.szut.casino.user.dto.GetUserDto;
|
||||||
import jakarta.validation.Valid;
|
import jakarta.validation.Valid;
|
||||||
|
|
|
@ -0,0 +1,45 @@
|
||||||
|
package de.szut.casino.user.transaction;
|
||||||
|
|
||||||
|
import de.szut.casino.deposit.TransactionEntity;
|
||||||
|
import de.szut.casino.deposit.TransactionRepository;
|
||||||
|
import de.szut.casino.user.UserEntity;
|
||||||
|
import de.szut.casino.user.UserService;
|
||||||
|
import de.szut.casino.user.transaction.dto.GetTransactionDto;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class GetTransactionService {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UserService userService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private TransactionRepository transactionRepository;
|
||||||
|
|
||||||
|
public List<TransactionEntity> getUserTransactions(String authToken, Integer limit) {
|
||||||
|
Optional<UserEntity> user = this.userService.getCurrentUser(authToken);
|
||||||
|
if (user.isPresent()) {
|
||||||
|
if (limit != null && limit > 0) {
|
||||||
|
return this.transactionRepository.findByUserIdWithLimit(user.get(), limit);
|
||||||
|
} else {
|
||||||
|
return this.transactionRepository.findAllByUserId(user.get());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return List.of();
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<GetTransactionDto> mapTransactionsToDtos(List<TransactionEntity> transactions) {
|
||||||
|
return transactions.stream()
|
||||||
|
.map(transaction -> new GetTransactionDto(
|
||||||
|
transaction.getAmount(),
|
||||||
|
transaction.getStatus(),
|
||||||
|
transaction.getCreatedAt())
|
||||||
|
).toList();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,27 @@
|
||||||
|
package de.szut.casino.user.transaction;
|
||||||
|
|
||||||
|
import de.szut.casino.deposit.TransactionEntity;
|
||||||
|
import de.szut.casino.user.transaction.dto.GetTransactionDto;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestHeader;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
public class TransactionController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private GetTransactionService transactionService;
|
||||||
|
|
||||||
|
@GetMapping("/user/transactions")
|
||||||
|
public ResponseEntity<List<GetTransactionDto>> getUserTransactions(@RequestHeader("Authorization") String authToken, @RequestParam(value = "limit", required = false) Integer limit) {
|
||||||
|
List<TransactionEntity> transactionEntities = this.transactionService.getUserTransactions(authToken, limit);
|
||||||
|
|
||||||
|
return ResponseEntity.ok(this.transactionService.mapTransactionsToDtos(transactionEntities));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -0,0 +1,16 @@
|
||||||
|
package de.szut.casino.user.transaction.dto;
|
||||||
|
|
||||||
|
import de.szut.casino.deposit.TransactionStatus;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class GetTransactionDto {
|
||||||
|
public double amount = 0;
|
||||||
|
public TransactionStatus status = TransactionStatus.PROCESSING;
|
||||||
|
public Date createdAt = new Date();
|
||||||
|
}
|
||||||
|
|
|
@ -100,11 +100,11 @@
|
||||||
<div class="space-y-3">
|
<div class="space-y-3">
|
||||||
<div
|
<div
|
||||||
class="flex justify-between items-center"
|
class="flex justify-between items-center"
|
||||||
*ngFor="let transaction of recentTransactions"
|
*ngFor="let transaction of recentTransactions|async"
|
||||||
>
|
>
|
||||||
<div>
|
<div>
|
||||||
<p class="text-sm font-medium">{{ transaction.type }}</p>
|
<p class="text-sm font-medium">{{ transaction.type }}</p>
|
||||||
<p class="text-xs text-text-secondary">{{ transaction.date }}</p>
|
<p class="text-xs text-text-secondary">{{ transaction.createdAt | date }}</p>
|
||||||
</div>
|
</div>
|
||||||
<span [class]="transaction.amount > 0 ? 'text-emerald' : 'text-accent-red'">
|
<span [class]="transaction.amount > 0 ? 'text-emerald' : 'text-accent-red'">
|
||||||
{{ transaction.amount | currency: 'EUR' }}
|
{{ transaction.amount | currency: 'EUR' }}
|
||||||
|
|
|
@ -1,16 +1,18 @@
|
||||||
import { ChangeDetectionStrategy, Component, OnInit } from '@angular/core';
|
import { ChangeDetectionStrategy, Component, inject, OnInit } from '@angular/core';
|
||||||
import { CurrencyPipe, NgFor } from '@angular/common';
|
import { AsyncPipe, CurrencyPipe, DatePipe, NgFor } from '@angular/common';
|
||||||
import { DepositComponent } from '../deposit/deposit.component';
|
import { DepositComponent } from '../deposit/deposit.component';
|
||||||
import { ActivatedRoute, Router } from '@angular/router';
|
import { ActivatedRoute, Router } from '@angular/router';
|
||||||
import { ConfirmationComponent } from '@shared/components/confirmation/confirmation.component';
|
import { ConfirmationComponent } from '@shared/components/confirmation/confirmation.component';
|
||||||
import { Transaction } from 'app/model/Transaction';
|
import { Transaction } from 'app/model/Transaction';
|
||||||
import { NavbarComponent } from '@shared/components/navbar/navbar.component';
|
import { NavbarComponent } from '@shared/components/navbar/navbar.component';
|
||||||
import { Game } from 'app/model/Game';
|
import { Game } from 'app/model/Game';
|
||||||
|
import { Observable, of } from 'rxjs';
|
||||||
|
import { TransactionService } from '@service/transaction.service';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-homepage',
|
selector: 'app-homepage',
|
||||||
standalone: true,
|
standalone: true,
|
||||||
imports: [NavbarComponent, CurrencyPipe, NgFor, DepositComponent, ConfirmationComponent],
|
imports: [NavbarComponent, CurrencyPipe, NgFor, DepositComponent, ConfirmationComponent, AsyncPipe, DatePipe],
|
||||||
templateUrl: './home.component.html',
|
templateUrl: './home.component.html',
|
||||||
changeDetection: ChangeDetectionStrategy.OnPush,
|
changeDetection: ChangeDetectionStrategy.OnPush,
|
||||||
})
|
})
|
||||||
|
@ -74,7 +76,7 @@ export default class HomeComponent implements OnInit {
|
||||||
|
|
||||||
allGames: Game[] = [...this.featuredGames];
|
allGames: Game[] = [...this.featuredGames];
|
||||||
|
|
||||||
recentTransactions: Transaction[] = [];
|
recentTransactions: Observable<Transaction[]> = inject(TransactionService).getUsersTransactions(5);
|
||||||
|
|
||||||
openDepositModal() {
|
openDepositModal() {
|
||||||
this.isDepositModalOpen = true;
|
this.isDepositModalOpen = true;
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
<p>transaction-history works!</p>
|
|
@ -0,0 +1,17 @@
|
||||||
|
import { ChangeDetectionStrategy, Component, inject } from '@angular/core';
|
||||||
|
import { TransactionService } from '@service/transaction.service';
|
||||||
|
import { Observable } from 'rxjs';
|
||||||
|
|
||||||
|
@Component({
|
||||||
|
standalone: true,
|
||||||
|
selector: 'app-transaction-history',
|
||||||
|
imports: [],
|
||||||
|
templateUrl: './transaction-history.component.html',
|
||||||
|
styleUrl: './transaction-history.component.css',
|
||||||
|
changeDetection: ChangeDetectionStrategy.OnPush,
|
||||||
|
})
|
||||||
|
export class TransactionHistoryComponent {
|
||||||
|
private transactionService: TransactionService = inject(TransactionService);
|
||||||
|
|
||||||
|
transactions$: Observable<> = this.transactionService.getUsersTransactions();
|
||||||
|
}
|
|
@ -2,5 +2,5 @@ export interface Transaction {
|
||||||
id: string;
|
id: string;
|
||||||
type: string;
|
type: string;
|
||||||
amount: number;
|
amount: number;
|
||||||
date: string;
|
createdAt: string;
|
||||||
}
|
}
|
||||||
|
|
19
frontend/src/app/service/transaction.service.ts
Normal file
19
frontend/src/app/service/transaction.service.ts
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
import { inject, Injectable } from '@angular/core';
|
||||||
|
import { HttpClient } from '@angular/common/http';
|
||||||
|
|
||||||
|
@Injectable({
|
||||||
|
providedIn: 'root',
|
||||||
|
})
|
||||||
|
export class TransactionService {
|
||||||
|
private http: HttpClient = inject(HttpClient);
|
||||||
|
|
||||||
|
public getUsersTransactions(limit: number|null = null) {
|
||||||
|
const baseUrl = '/backend/user/transactions';
|
||||||
|
|
||||||
|
if (limit !== null) {
|
||||||
|
return this.http.get<any[]>(`${baseUrl}?limit=${limit}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.http.get<any[]>(`${baseUrl}`);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue