43 lines
1.4 KiB
Java
Executable File
43 lines
1.4 KiB
Java
Executable File
package es.recursoscatolicos.controller;
|
|
|
|
import es.recursoscatolicos.model.VersoBiblico;
|
|
import es.recursoscatolicos.repository.VersoBiblicoRepository;
|
|
import lombok.RequiredArgsConstructor;
|
|
import org.springframework.data.domain.PageRequest;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
import java.time.LocalDate;
|
|
|
|
@RestController
|
|
@RequestMapping("/versos")
|
|
@RequiredArgsConstructor
|
|
public class VersoBiblicoController {
|
|
|
|
private final VersoBiblicoRepository repo;
|
|
|
|
/**
|
|
* Devuelve el verso bíblico del día.
|
|
* La selección se basa en el día del año (1-365) módulo el total de versos,
|
|
* lo que garantiza que cada día salga un verso diferente rotando el ciclo.
|
|
*/
|
|
@GetMapping("/hoy")
|
|
public ResponseEntity<VersoBiblico> hoy() {
|
|
long total = repo.count();
|
|
if (total == 0) return ResponseEntity.notFound().build();
|
|
|
|
LocalDate hoy = LocalDate.now();
|
|
int diaDelAnio = hoy.getDayOfYear();
|
|
int indice = (int) (diaDelAnio % total);
|
|
|
|
return repo.findAllByOrderByIdAsc(PageRequest.of(indice, 1))
|
|
.getContent()
|
|
.stream()
|
|
.findFirst()
|
|
.map(ResponseEntity::ok)
|
|
.orElse(ResponseEntity.notFound().build());
|
|
}
|
|
}
|