feat: Adjust json responses
This commit is contained in:
parent
85d2b218aa
commit
1b9bc90920
4 changed files with 71 additions and 7 deletions
|
@ -23,11 +23,13 @@ import java.util.Optional;
|
|||
public class BlackJackGameController {
|
||||
|
||||
private final UserService userService;
|
||||
private final BlackJackService blackJackService;
|
||||
private final BlackJackGameRepository blackJackGameRepository;
|
||||
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.blackJackService = blackJackService;
|
||||
this.blackJackGameRepository = blackJackGameRepository;
|
||||
this.userRepository = userRepository;
|
||||
}
|
||||
|
@ -60,6 +62,14 @@ public class BlackJackGameController {
|
|||
game.setUser(user);
|
||||
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));
|
||||
|
||||
userRepository.save(user);
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package de.szut.casino.blackjack;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import com.fasterxml.jackson.annotation.JsonManagedReference;
|
||||
import de.szut.casino.user.UserEntity;
|
||||
import jakarta.persistence.*;
|
||||
import lombok.AllArgsConstructor;
|
||||
|
@ -9,6 +11,7 @@ import lombok.Setter;
|
|||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
@Getter
|
||||
|
@ -19,9 +22,25 @@ public class BlackJackGameEntity {
|
|||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "user_id", nullable = false)
|
||||
@JsonIgnore
|
||||
private UserEntity user;
|
||||
|
||||
// Expose UserID to JSON output
|
||||
public Long getUserId() {
|
||||
return user != null ? user.getId() : null;
|
||||
}
|
||||
|
||||
private String state;
|
||||
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<>();
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -1,15 +1,33 @@
|
|||
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.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
@Entity
|
||||
@Getter
|
||||
@Setter
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class CardEntity {
|
||||
private final Suit suit;
|
||||
private final Rank rank;
|
||||
@Id
|
||||
@GeneratedValue
|
||||
@JsonIgnore
|
||||
private Long id;
|
||||
|
||||
public CardEntity(Suit suit, Rank rank) {
|
||||
this.suit = suit;
|
||||
this.rank = rank;
|
||||
}
|
||||
@ManyToOne
|
||||
@JoinColumn(name = "game_id", nullable = false)
|
||||
@JsonBackReference
|
||||
private BlackJackGameEntity game;
|
||||
|
||||
@Enumerated(EnumType.STRING)
|
||||
private Suit suit;
|
||||
|
||||
@Enumerated(EnumType.STRING)
|
||||
private Rank rank;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue