Created
July 24, 2023 11:25
-
-
Save danirod/bd4bc8413f74e889e86fefe29b0f08a6 to your computer and use it in GitHub Desktop.
Código del capítulo de Quarkus donde hablo de servicios. Las notas del episodio están en https://www.makigas.es/series/fundamentos-de-uso-de-quarkus/servicios-e-inyeccion-de-dependencia y el vídeo se puede ver en https://www.youtube.com/watch?v=HhTX3qbKhEs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package es.danirod.quarkus.bookshelf.temperaturas; | |
public record Temperatura(String ciudad, int minima, int maxima) { | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package es.danirod.quarkus.bookshelf.temperaturas; | |
public class TemperaturaDuplicadaException extends RuntimeException { | |
public TemperaturaDuplicadaException(String msg) { | |
super(msg); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package es.danirod.quarkus.bookshelf.temperaturas; | |
import jakarta.inject.Inject; | |
import jakarta.ws.rs.GET; | |
import jakarta.ws.rs.POST; | |
import jakarta.ws.rs.Path; | |
import java.util.stream.Stream; | |
@Path("/temperaturas") | |
public class TemperaturaResource { | |
private TemperaturaService temperaturas; | |
@Inject | |
public TemperaturaResource(TemperaturaService temperaturas) { | |
this.temperaturas = temperaturas; | |
} | |
@POST | |
public Temperatura insertar(Temperatura t) { | |
temperaturas.insertar(t); | |
return t; | |
} | |
@GET | |
public Stream<Temperatura> listar() { | |
return temperaturas.obtener(); | |
} | |
@GET | |
@Path("/maxima") | |
public Temperatura maxima() { | |
return temperaturas.maxima(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package es.danirod.quarkus.bookshelf.temperaturas; | |
import java.util.stream.Stream; | |
public interface TemperaturaService { | |
void insertar(Temperatura t); | |
Stream<Temperatura> obtener(); | |
Temperatura maxima(); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package es.danirod.quarkus.bookshelf.temperaturas; | |
import jakarta.enterprise.context.ApplicationScoped; | |
import java.util.Comparator; | |
import java.util.HashMap; | |
import java.util.Map; | |
import java.util.NoSuchElementException; | |
import java.util.stream.Stream; | |
@ApplicationScoped | |
public class TemperaturaServiceImpl implements TemperaturaService { | |
private Map<String, Temperatura> temperaturas = new HashMap<>(); | |
@Override | |
public void insertar(Temperatura t) { | |
if (temperaturas.containsKey(t.ciudad())) { | |
var msg = "Ya existe medición para " + t.ciudad(); | |
throw new TemperaturaDuplicadaException(msg); | |
} | |
temperaturas.put(t.ciudad(), t); | |
} | |
@Override | |
public Stream<Temperatura> obtener() { | |
return temperaturas.values().stream(); | |
} | |
@Override | |
public Temperatura maxima() { | |
return temperaturas.values().stream() | |
.max(Comparator.comparingInt(Temperatura::maxima)) | |
.orElseThrow(() -> new NoSuchElementException("No hay medición")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment