Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import com.gutotech.loteriasapi.model.Resultado;
import com.gutotech.loteriasapi.service.ResultadoService;

import static java.util.Objects.nonNull;

@Component
public class LoteriasUpdate {

Expand Down Expand Up @@ -48,8 +50,9 @@ public void checkForUpdates(String loteria) throws Exception {
Resultado latestResultado = consumer.getResultado(loteria, null);

Resultado myLatestResultado = resultadoService.findLatest(loteria);
int myLatestConcurso = nonNull(myLatestResultado) ? myLatestResultado.getConcurso() : 0;

if (myLatestResultado.getConcurso() == latestResultado.getConcurso()) {
if (myLatestConcurso == latestResultado.getConcurso()) {
myLatestResultado.setData(latestResultado.getData());
myLatestResultado.setLocal(latestResultado.getLocal());

Expand All @@ -64,8 +67,7 @@ public void checkForUpdates(String loteria) throws Exception {

Map<String, Integer> tentativasMap = new HashMap<>();

for (int concurso = myLatestResultado.getConcurso() + 1; concurso <= latestResultado
.getConcurso(); concurso++) {
for (int concurso = myLatestConcurso + 1; concurso <= latestResultado.getConcurso(); concurso++) {
try {
Resultado resultado = consumer.getResultado(loteria, String.valueOf(concurso));
resultadoService.save(resultado);
Expand Down
62 changes: 34 additions & 28 deletions src/main/java/com/gutotech/loteriasapi/rest/ApiRestController.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,35 @@
package com.gutotech.loteriasapi.rest;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.gutotech.loteriasapi.model.Loteria;
import com.gutotech.loteriasapi.model.Resultado;
import com.gutotech.loteriasapi.model.exception.ResourceNotFoundException;
import com.gutotech.loteriasapi.service.ResultadoService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("api")
@Api(tags = "Loterias")
public class ApiRestController {

private static final String INVALID_LOTTERY_MESSAGE = "'%s' não é o id de nenhuma das loterias suportadas. Loterias suportadas: %s";
private static final String RESULT_NOT_FOUND_MESSAGE = "Resultado não encontrado para \"%s\", concurso \"%d\"";
private static final String NO_RESULTS_FOUND_MESSAGE = "Nenhum resultado encontrado para a loteria %s";

private final List<String> lotteries = Loteria.asList();

private final String ALLOWABLE_VALUES = "maismilionaria, megasena, lotofacil, quina,"
+ " lotomania, timemania, duplasena, federal, diadesorte, supersete";

private final String invalidLotteryMessageFormat = "'%s' não é o id de nenhuma das loterias suportadas. Loterias suportadas: "
+ lotteries;

@Autowired
private ResultadoService resultadoService;

Expand All @@ -43,35 +42,42 @@ public ResponseEntity<List<String>> getLotteries() {
@GetMapping("{loteria}")
@ApiOperation(value = "Retorna todos os resultados já realizados da loteria especificada.")
public ResponseEntity<List<Resultado>> getResultsByLottery(
@ApiParam(allowableValues = ALLOWABLE_VALUES, required = true) @PathVariable("loteria") String loteria) {
if (!lotteries.contains(loteria)) {
throw new ResourceNotFoundException(String.format(invalidLotteryMessageFormat, loteria));
}

@PathVariable @ApiParam(allowableValues = ALLOWABLE_VALUES, required = true) String loteria) {
validateLottery(loteria);
return ResponseEntity.ok(resultadoService.findByLoteria(loteria));
}

@GetMapping("{loteria}/{concurso}")
@ApiOperation(value = "Retorna o resultado da loteria e concurso especificado.")
public ResponseEntity<Resultado> getResultById(
@ApiParam(allowableValues = ALLOWABLE_VALUES, required = true) @PathVariable("loteria") String loteria,
@PathVariable("concurso") Integer concurso) {
if (!lotteries.contains(loteria)) {
throw new ResourceNotFoundException(String.format(invalidLotteryMessageFormat, loteria));
}
@PathVariable @ApiParam(allowableValues = ALLOWABLE_VALUES, required = true) String loteria,
@PathVariable Integer concurso) {
validateLottery(loteria);

return ResponseEntity.ok(resultadoService.findByLoteriaAndConcurso(loteria, concurso));
Resultado resultado = resultadoService.findByLoteriaAndConcurso(loteria, concurso);
if (resultado == null) {
throw new ResourceNotFoundException(RESULT_NOT_FOUND_MESSAGE.formatted(loteria, concurso));
}
return ResponseEntity.ok(resultado);
}

@GetMapping("{loteria}/latest")
@ApiOperation(value = "Retorna o resultado mais recente da loteria especificada.")
public ResponseEntity<Resultado> getLatestResult(
@ApiParam(allowableValues = ALLOWABLE_VALUES, required = true) @PathVariable("loteria") String loteria) {
if (!lotteries.contains(loteria)) {
throw new ResourceNotFoundException(String.format(invalidLotteryMessageFormat, loteria));
@PathVariable @ApiParam(allowableValues = ALLOWABLE_VALUES, required = true) String loteria) {
validateLottery(loteria);

Resultado resultado = resultadoService.findLatest(loteria);
if (resultado == null) {
throw new ResourceNotFoundException(NO_RESULTS_FOUND_MESSAGE.formatted(loteria));
}
return ResponseEntity.ok(resultado);
}

return ResponseEntity.ok(resultadoService.findLatest(loteria));
private void validateLottery(String loteria) {
if (!lotteries.contains(loteria)) {
throw new ResourceNotFoundException(INVALID_LOTTERY_MESSAGE.formatted(loteria, lotteries));
}
}

}
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package com.gutotech.loteriasapi.service;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import com.gutotech.loteriasapi.model.Resultado;
import com.gutotech.loteriasapi.model.ResultadoId;
import com.gutotech.loteriasapi.repository.ResultadoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.gutotech.loteriasapi.model.Resultado;
import com.gutotech.loteriasapi.model.ResultadoId;
import com.gutotech.loteriasapi.repository.ResultadoRepository;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ResultadoService {
Expand All @@ -31,7 +30,7 @@ public Resultado findByLoteriaAndConcurso(String loteria, int concurso) {
}

public Resultado findLatest(String loteria) {
return repository.findTopById_Loteria(loteria).orElse(new Resultado());
return repository.findTopById_Loteria(loteria).orElse(null);
}

public void save(Resultado resultado) {
Expand Down
Loading