139 lines
4.0 KiB
Java
139 lines
4.0 KiB
Java
package com.pmu.betengine.service;
|
|
|
|
import com.pmu.betengine.model.Course;
|
|
import com.pmu.betengine.model.Resultat;
|
|
import com.pmu.betengine.model.SearchParam;
|
|
import com.pmu.betengine.repository.ResultatRepository;
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
import org.springframework.web.bind.annotation.PathVariable;
|
|
|
|
import java.util.Optional;
|
|
import java.util.Map;
|
|
import java.time.LocalDate;
|
|
import java.time.LocalDateTime;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
|
|
@Service
|
|
@RequiredArgsConstructor
|
|
public class ResultatService {
|
|
|
|
private final ResultatRepository resultatRepository;
|
|
private final CourseService courseService;
|
|
|
|
// CREATE / UPDATE
|
|
public Resultat save(Resultat resultat) {
|
|
return resultatRepository.save(resultat);
|
|
}
|
|
|
|
// GET ALL
|
|
public List<Resultat> getAll() {
|
|
return resultatRepository.findAll();
|
|
}
|
|
|
|
// GET BY ID
|
|
public Resultat getById(Long id) {
|
|
return resultatRepository.findById(id).orElse(null);
|
|
}
|
|
|
|
// GET BY COURSE
|
|
public Resultat getByCourseId(Long courseId) {
|
|
return resultatRepository.findByCourseId(courseId).orElse(null);
|
|
}
|
|
|
|
|
|
// DELETE BY ID
|
|
public void delete(Long id) {
|
|
resultatRepository.deleteById(id);
|
|
}
|
|
|
|
// DELETE BY COURSE
|
|
public void deleteByCourseId(Long courseId) {
|
|
resultatRepository.deleteByCourseId(courseId);
|
|
}
|
|
|
|
public Resultat saveAndUpdateCourse(Resultat resultat) {
|
|
// Save the resultat
|
|
Resultat savedResultat = resultatRepository.save(resultat);
|
|
|
|
// Update the course statut
|
|
Course course = savedResultat.getCourse();
|
|
if (course != null) {
|
|
course.setStatut("RESULT_PUBLISHED"); // or whatever statut you want
|
|
courseService.updateCourse(course.getId(), course);
|
|
}
|
|
|
|
return savedResultat;
|
|
}
|
|
|
|
public List<Resultat> search(SearchParam searchParam) {
|
|
System.out.println("******************** SEARCH PARAM: " + searchParam);
|
|
|
|
if (searchParam.getCriteria() == null) {
|
|
throw new IllegalArgumentException("Le paramètre 'criteria' est obligatoire et ne peut pas être null");
|
|
}
|
|
|
|
try {
|
|
String criteria = searchParam.getCriteria();
|
|
|
|
if ("1".equals(criteria)) {
|
|
// Retourne tous les résultats
|
|
return resultatRepository.findAll();
|
|
} else if ("2".equals(criteria) && searchParam.getDate() != null) {
|
|
LocalDate date = searchParam.getDate();
|
|
LocalDateTime startOfDay = date.atStartOfDay();
|
|
LocalDateTime endOfDay = startOfDay.plusDays(1);
|
|
|
|
return resultatRepository.findByCourseDate(startOfDay, endOfDay);
|
|
} else if ("3".equals(criteria) && searchParam.getId() != null) {
|
|
return resultatRepository.findByCourseId(searchParam.getId())
|
|
.map(List::of)
|
|
.orElse(List.of());
|
|
}
|
|
|
|
// Cas par défaut
|
|
return resultatRepository.findAll();
|
|
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
throw new RuntimeException("Erreur lors de la recherche des résultats");
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// package com.pmu.betengine.service;
|
|
|
|
// import com.pmu.betengine.model.Resultat;
|
|
// import com.pmu.betengine.repository.ResultatRepository;
|
|
// import lombok.RequiredArgsConstructor;
|
|
// import org.springframework.stereotype.Service;
|
|
|
|
// import java.util.Optional;
|
|
|
|
// @Service
|
|
// @RequiredArgsConstructor
|
|
// public class ResultatService {
|
|
|
|
// private final ResultatRepository resultatCourseRepository;
|
|
|
|
// public Optional<Resultat> findByCourseId(Long courseId) {
|
|
// return resultatCourseRepository.findByCourseId(courseId);
|
|
// }
|
|
|
|
// public Resultat save(Resultat resultatCourse) {
|
|
// return resultatCourseRepository.save(resultatCourse);
|
|
// }
|
|
|
|
// public void deleteByCourseId(Long courseId) {
|
|
// resultatCourseRepository.deleteByCourseId(courseId);
|
|
// }
|
|
// } |