feat: Adjust json responses

This commit is contained in:
Phan Huy Tran 2025-03-26 14:20:31 +01:00
parent 85d2b218aa
commit 1b9bc90920
4 changed files with 71 additions and 7 deletions

View file

@ -23,11 +23,13 @@ import java.util.Optional;
public class BlackJackGameController { public class BlackJackGameController {
private final UserService userService; private final UserService userService;
private final BlackJackService blackJackService;
private final BlackJackGameRepository blackJackGameRepository; private final BlackJackGameRepository blackJackGameRepository;
private final UserRepository userRepository; private final UserRepository userRepository;
public BlackJackGameController(UserService userService, BlackJackGameRepository blackJackGameRepository, UserRepository userRepository) { public BlackJackGameController(UserService userService, BlackJackService blackJackService, BlackJackGameRepository blackJackGameRepository, UserRepository userRepository) {
this.userService = userService; this.userService = userService;
this.blackJackService = blackJackService;
this.blackJackGameRepository = blackJackGameRepository; this.blackJackGameRepository = blackJackGameRepository;
this.userRepository = userRepository; this.userRepository = userRepository;
} }
@ -60,6 +62,14 @@ public class BlackJackGameController {
game.setUser(user); game.setUser(user);
game.setBet(betAmount); game.setBet(betAmount);
for (int i = 0; i < 2; i++) {
CardEntity playerCard = blackJackService.createRandomCard(game);
game.getPlayerCards().add(playerCard);
}
CardEntity dealerCard = blackJackService.createRandomCard(game);
game.getDealerCards().add(dealerCard);
user.setBalance(user.getBalance().subtract(betAmount)); user.setBalance(user.getBalance().subtract(betAmount));
userRepository.save(user); userRepository.save(user);

View file

@ -1,5 +1,7 @@
package de.szut.casino.blackjack; package de.szut.casino.blackjack;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import de.szut.casino.user.UserEntity; import de.szut.casino.user.UserEntity;
import jakarta.persistence.*; import jakarta.persistence.*;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
@ -9,6 +11,7 @@ import lombok.Setter;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
@Entity @Entity
@Getter @Getter
@ -19,9 +22,25 @@ public class BlackJackGameEntity {
@Id @Id
@GeneratedValue @GeneratedValue
private Long id; private Long id;
@ManyToOne @ManyToOne
@JoinColumn(name = "user_id", nullable = false) @JoinColumn(name = "user_id", nullable = false)
@JsonIgnore
private UserEntity user; private UserEntity user;
// Expose UserID to JSON output
public Long getUserId() {
return user != null ? user.getId() : null;
}
private String state; private String state;
private BigDecimal bet; private BigDecimal bet;
@OneToMany(mappedBy = "game", cascade = CascadeType.ALL, orphanRemoval = true)
@JsonManagedReference
private List<CardEntity> playerCards = new ArrayList<>();
@OneToMany(mappedBy = "game", cascade = CascadeType.ALL, orphanRemoval = true)
@JsonManagedReference
private List<CardEntity> dealerCards = new ArrayList<>();
} }

View file

@ -0,0 +1,17 @@
package de.szut.casino.blackjack;
import org.springframework.stereotype.Service;
import java.util.Random;
@Service
public class BlackJackService {
private final Random random = new Random();
public CardEntity createRandomCard(BlackJackGameEntity game) {
CardEntity card = new CardEntity();
card.setGame(game);
card.setSuit(Suit.values()[random.nextInt(Suit.values().length)]);
card.setRank(Rank.values()[random.nextInt(Rank.values().length)]);
return card;
}
}

View file

@ -1,15 +1,33 @@
package de.szut.casino.blackjack; package de.szut.casino.blackjack;
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
@Entity
@Getter @Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class CardEntity { public class CardEntity {
private final Suit suit; @Id
private final Rank rank; @GeneratedValue
@JsonIgnore
private Long id;
public CardEntity(Suit suit, Rank rank) { @ManyToOne
this.suit = suit; @JoinColumn(name = "game_id", nullable = false)
this.rank = rank; @JsonBackReference
} private BlackJackGameEntity game;
@Enumerated(EnumType.STRING)
private Suit suit;
@Enumerated(EnumType.STRING)
private Rank rank;
} }