forked from tom.biard/ScienceQuest
parent
68e6228e4e
commit
649f9431c6
@ -0,0 +1,41 @@
|
||||
package fr.iut.sciencequest.sae.dto.partieKahoot.partie;
|
||||
|
||||
import fr.iut.sciencequest.sae.dto.difficulte.DifficulteSimpleDTO;
|
||||
import fr.iut.sciencequest.sae.dto.jeu.JeuDTO;
|
||||
import fr.iut.sciencequest.sae.dto.joueur.JoueurSimpleDTO;
|
||||
import fr.iut.sciencequest.sae.dto.thematique.ThematiqueSimpleDTO;
|
||||
import fr.iut.sciencequest.sae.entities.Question;
|
||||
import fr.iut.sciencequest.sae.entities.Status;
|
||||
import jakarta.validation.constraints.NotEmpty;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import lombok.*;
|
||||
import org.springframework.hateoas.RepresentationModel;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
public class PartieKahootDTO extends RepresentationModel<PartieKahootDTO> {
|
||||
@NotNull
|
||||
private Integer id;
|
||||
@NotEmpty
|
||||
private String codeInvitation;
|
||||
@NotEmpty
|
||||
private List<JoueurSimpleDTO> joueurs;
|
||||
@NotNull
|
||||
private JeuDTO jeu;
|
||||
@NotEmpty
|
||||
private List<ThematiqueSimpleDTO> thematiques;
|
||||
@NotNull
|
||||
private DifficulteSimpleDTO difficulte;
|
||||
@NotNull
|
||||
private Status status;
|
||||
|
||||
private Question questionActuel;
|
||||
|
||||
private List<Question> questions;
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
package fr.iut.sciencequest.sae.entities;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonManagedReference;
|
||||
import jakarta.persistence.*;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Data
|
||||
@Entity
|
||||
@Table(name="partiekahoot")
|
||||
@PrimaryKeyJoinColumn(name = "idpartie")
|
||||
public class PartieKahoot extends Partie {
|
||||
|
||||
@ManyToOne
|
||||
@JoinColumn(name="idquestionactuel")
|
||||
private Question questionActuel;
|
||||
|
||||
@JsonManagedReference
|
||||
@ManyToMany(fetch = FetchType.EAGER)
|
||||
@JoinTable(
|
||||
name = "Questionpartiekahoot",
|
||||
joinColumns = @JoinColumn(name = "idpartiekahoot"),
|
||||
inverseJoinColumns = @JoinColumn(name="idquestion")
|
||||
)
|
||||
private List<Question> questions;
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package fr.iut.sciencequest.sae.exceptions.notFound;
|
||||
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
|
||||
@ResponseStatus(HttpStatus.NOT_FOUND)
|
||||
public class PartieKahootNotFoundException extends EntityNotFoundException{
|
||||
private static String entityName = "partieKahoot";
|
||||
public PartieKahootNotFoundException(int id) {
|
||||
super(PartieKahootNotFoundException.entityName, id);
|
||||
}
|
||||
|
||||
public PartieKahootNotFoundException(String field, Object value){
|
||||
super(PartieKahootNotFoundException.entityName, field, value);
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package fr.iut.sciencequest.sae.repositories;
|
||||
|
||||
import fr.iut.sciencequest.sae.entities.PartieKahoot;
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Repository
|
||||
public interface PartieKahootRepository extends CrudRepository<PartieKahoot, Integer> {
|
||||
Optional<PartieKahoot> getPartieByCodeInvitation(String codeInvitation);
|
||||
}
|
@ -0,0 +1,61 @@
|
||||
package fr.iut.sciencequest.sae.services;
|
||||
|
||||
import fr.iut.sciencequest.sae.entities.Partie;
|
||||
import fr.iut.sciencequest.sae.entities.PartieKahoot;
|
||||
import fr.iut.sciencequest.sae.exceptions.DuplicatedIdException;
|
||||
import fr.iut.sciencequest.sae.exceptions.notFound.PartieKahootNotFoundException;
|
||||
import fr.iut.sciencequest.sae.exceptions.notFound.PartieNotFoundException;
|
||||
import fr.iut.sciencequest.sae.repositories.*;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.PersistenceContext;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@AllArgsConstructor
|
||||
@Service
|
||||
public class PartieKahootService {
|
||||
private final PartieKahootRepository partieKahootRepository;
|
||||
private final PartieRepository partieRepository;
|
||||
|
||||
public PartieKahoot findById(int id) {
|
||||
return this.partieKahootRepository.findById(id).orElseThrow(() ->
|
||||
new PartieKahootNotFoundException(id)
|
||||
);
|
||||
}
|
||||
|
||||
public PartieKahoot getPartieByCodeInvitation(String codeInvitation){
|
||||
return this.partieKahootRepository.getPartieByCodeInvitation(codeInvitation).orElseThrow(() -> new PartieKahootNotFoundException("codeInvitation", codeInvitation));
|
||||
}
|
||||
|
||||
public PartieKahoot getPartieKahootByIdOrCodeInvitation(String codeInvitation){
|
||||
//try to get invitation from codeInvitation, if not : try with id
|
||||
PartieKahoot partie;
|
||||
try{
|
||||
partie = this.getPartieByCodeInvitation(codeInvitation);
|
||||
}catch (PartieKahootNotFoundException exceptionNotFoundByCodeInvitation){
|
||||
try{
|
||||
partie = this.findById(Integer.parseInt(codeInvitation));
|
||||
}catch (PartieKahootNotFoundException | NumberFormatException e2){
|
||||
throw exceptionNotFoundByCodeInvitation;
|
||||
}
|
||||
}
|
||||
return partie;
|
||||
}
|
||||
|
||||
public PartieKahoot update(PartieKahoot partie){
|
||||
if(!this.partieRepository.existsById(partie.getId())){
|
||||
throw new PartieKahootNotFoundException(partie.getId());
|
||||
}
|
||||
PartieKahoot savedPartie = this.partieKahootRepository.save(partie);
|
||||
return this.findById(savedPartie.getId());
|
||||
}
|
||||
|
||||
public PartieKahoot create(PartieKahoot partie){
|
||||
if(partie.getId() != null && this.partieRepository.existsById(partie.getId())){
|
||||
throw new DuplicatedIdException();
|
||||
}
|
||||
PartieKahoot savedPartie = this.partieKahootRepository.save(partie);
|
||||
return this.findById(savedPartie.getId());
|
||||
}
|
||||
|
||||
}
|
@ -1,67 +0,0 @@
|
||||
package fr.iut.sciencequest.sae.services;
|
||||
|
||||
import fr.iut.sciencequest.sae.entities.*;
|
||||
import fr.iut.sciencequest.sae.exceptions.DuplicatedIdException;
|
||||
import fr.iut.sciencequest.sae.exceptions.notFound.PartieNotFoundException;
|
||||
import fr.iut.sciencequest.sae.repositories.*;
|
||||
import jakarta.persistence.EntityManager;
|
||||
import jakarta.persistence.PersistenceContext;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@AllArgsConstructor
|
||||
@Service
|
||||
public class PartieService {
|
||||
private final PartieRepository partieRepository;
|
||||
private final JeuRepository jeuRepository;
|
||||
private final JoueurRepository joueurRepository;
|
||||
private final UtilisateurRepository utilisateurRepository;
|
||||
private final ThematiqueRepository thematiqueRepository;
|
||||
private final DifficulteRepository difficulteRepository;
|
||||
private final JoueurService joueurService;
|
||||
|
||||
@PersistenceContext
|
||||
private EntityManager entityManager;
|
||||
|
||||
public Partie findById(int id) {
|
||||
return this.partieRepository.findById(id).orElseThrow(() ->
|
||||
new PartieNotFoundException(id)
|
||||
);
|
||||
}
|
||||
|
||||
public Partie getPartieByCodeInvitation(String codeInvitation){
|
||||
return this.partieRepository.getPartieByCodeInvitation(codeInvitation).orElseThrow(() -> new PartieNotFoundException("codeInvitation", codeInvitation));
|
||||
}
|
||||
|
||||
public Partie getPartieByIdOrCodeInvitation(String codeInvitation){
|
||||
//try to get invitation from codeInvitation, if not : try with id
|
||||
Partie partie;
|
||||
try{
|
||||
partie = this.getPartieByCodeInvitation(codeInvitation);
|
||||
}catch (PartieNotFoundException exceptionNotFoundByCodeInvitation){
|
||||
try{
|
||||
partie = this.findById(Integer.parseInt(codeInvitation));
|
||||
}catch (PartieNotFoundException | NumberFormatException e2){
|
||||
throw exceptionNotFoundByCodeInvitation;
|
||||
}
|
||||
}
|
||||
return partie;
|
||||
}
|
||||
|
||||
public Partie update(Partie partie){
|
||||
if(!this.partieRepository.existsById(partie.getId())){
|
||||
throw new PartieNotFoundException(partie.getId());
|
||||
}
|
||||
Partie savedPartie = this.partieRepository.save(partie);
|
||||
return this.findById(savedPartie.getId());
|
||||
}
|
||||
|
||||
public Partie create(Partie partie){
|
||||
if(partie.getId() != null && this.partieRepository.existsById(partie.getId())){
|
||||
throw new DuplicatedIdException();
|
||||
}
|
||||
Partie savedPartie = this.partieRepository.save(partie);
|
||||
return this.findById(savedPartie.getId());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue