0
I am creating a REST API to generate random number for lottery. These are the requirement endpointes.
In the first step, we need to build an endpoint that will receive the person’s email and return a response object with the numbers drawn for the bet.
We should also build a second endpoint to list all the bets of a requester, passing your email as parameter.
Problematic:
My project has three main classes, the Bet Class, which is abstract and contains the abstract method generating Put( ), the Apostasimplesmegasena class that extends the Bet class and implements the generating method Put( ) and finally the Bettor class, which contains the attributes ID, email and a list of the bets associated with it.
I need to check if the e-mail already exists in the database, if it already exists, I also need to check if there is already a combination of numbers (bet) associated with that email before saving the new bet in the bank. If there is no record of the email at the bank, just save the new bet.
Follow the codes of the classes:
package com.apostas.api.apostas.api.model;
import Lombok. Date;
import javax.persistence. *; import java.time.Localdatetime;
@Date @Entity @Table(name = "bets")
public Abstract class Bet {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "aposta", nullable = false)
private Integer numerosaposta = gerarNumeros();
@Column(name = "criadaEm", nullable = false)
private LocalDateTime criadaEm = LocalDateTime.now();
public abstract Integer gerarNumeros();
}
@Equalsandhashcode(callSuper = true) @Data @Noargsconstructor @Allargsconstructor public class Apostasimplesmegasena extends Bet{ @Override public Integer gerarNumeros() { Random generator = new Random(); Generator Return.nextInt(1-60); }
}
@Date @Accessors (chain = true)
@Entity @Table(name = "bettors") public class Bettor {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name ="email", nullable = false, unique = true)
private String email;
@OneToMany(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE})
private List<ApostaSimplesMegasena> apostaSimplesMegasena;
}
My difficulty is in making these checks before saving the bet on the bank. I know it’s a dumb question, but it’s the first API I’m developing. If anyone can help me with an example, I’ll be very grateful.