Commit inicial
This commit is contained in:
parent
5de37d63bb
commit
f3e16798d0
2
pom.xml
2
pom.xml
@ -1,7 +1,7 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>org.lapaloma.examen.aaee</groupId>
|
||||
<artifactId>aaee_gobierno</artifactId>
|
||||
<artifactId>aaee_concesionario</artifactId>
|
||||
<version>0.0.2</version>
|
||||
|
||||
<name>Prueba de Springboot</name>
|
||||
|
||||
@ -3,8 +3,6 @@ package org.lapaloma.examen.aaee.controller;
|
||||
import java.net.URI;
|
||||
import java.util.List;
|
||||
|
||||
import javax.validation.Valid;
|
||||
|
||||
import org.lapaloma.examen.aaee.service.CocheService;
|
||||
import org.lapaloma.examen.aaee.vo.Coche;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
@ -16,7 +14,7 @@ import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/coches")
|
||||
@RequestMapping("/concesionario/coches")
|
||||
public class CocheController {
|
||||
|
||||
private final CocheService cocheService;
|
||||
@ -25,13 +23,19 @@ public class CocheController {
|
||||
this.cocheService = cocheService;
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /concesionario/coches - Obtiene la lista de todos los coches
|
||||
* GET /concesionario/coches?marca=Renault - Obtiene la lista de coches filtrada por marca (opcional)
|
||||
*
|
||||
* @return List<Miembro> lista de todos los miembros en formato JSON
|
||||
*/
|
||||
@GetMapping
|
||||
public List<Coche> consultarCochesPorMarca(@RequestParam(required = false) String marca) {
|
||||
return cocheService.consultarCochesPorMarca(marca);
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public ResponseEntity<Coche> crearNuevoCoche(@Valid @RequestBody Coche coche) {
|
||||
public ResponseEntity<Coche> crearNuevoCoche(@RequestBody Coche coche) {
|
||||
Coche creado = cocheService.crearNuevoCoche(coche);
|
||||
URI location = URI.create("/coches/" + creado.getId());
|
||||
return ResponseEntity.created(location).body(creado);
|
||||
|
||||
@ -27,12 +27,12 @@ public class CocheDaoJDBC implements ICocheDAO {
|
||||
public List<Coche> obtenerCochesPorMarca(String marca) {
|
||||
List<Coche> lista = new ArrayList<>();
|
||||
|
||||
String sqlAll = "SELECT id, marca, modelo, cilindrada FROM coche";
|
||||
String sqlByMarca = "SELECT id, marca, modelo, cilindrada FROM coche WHERE LOWER(marca) = LOWER(?)";
|
||||
String sqlConsultaTodosCoches = "SELECT identificador, marca, modelo, cilindrada FROM T_COCHE";
|
||||
String sqlConsultaCochesPorMarca = "SELECT identificador, marca, modelo, cilindrada FROM T_COCHE WHERE LOWER(marca) = LOWER(?)";
|
||||
|
||||
try (Connection conexion = dataSource.getConnection();
|
||||
PreparedStatement ps = (marca == null || marca.isBlank()) ? conexion.prepareStatement(sqlAll)
|
||||
: conexion.prepareStatement(sqlByMarca);) {
|
||||
PreparedStatement ps = (marca == null || marca.isBlank()) ? conexion.prepareStatement(sqlConsultaTodosCoches)
|
||||
: conexion.prepareStatement(sqlConsultaCochesPorMarca);) {
|
||||
|
||||
if (marca != null && !marca.isBlank()) {
|
||||
ps.setString(1, marca);
|
||||
@ -41,7 +41,7 @@ public class CocheDaoJDBC implements ICocheDAO {
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
Coche c = new Coche();
|
||||
c.setId(rs.getInt("id"));
|
||||
c.setId(rs.getInt("identificador"));
|
||||
c.setMarca(rs.getString("marca"));
|
||||
c.setModelo(rs.getString("modelo"));
|
||||
c.setCilindrada(rs.getInt("cilindrada"));
|
||||
@ -58,7 +58,7 @@ public class CocheDaoJDBC implements ICocheDAO {
|
||||
|
||||
@Override
|
||||
public Coche crearCoche(Coche coche) {
|
||||
String insertSQL = "INSERT INTO coche (marca, modelo, cilindrada) VALUES (?, ?, ?)";
|
||||
String insertSQL = "INSERT INTO T_COCHE (marca, modelo, cilindrada) VALUES (?, ?, ?)";
|
||||
|
||||
try (Connection conexion = dataSource.getConnection();
|
||||
PreparedStatement ps = conexion.prepareStatement(insertSQL, Statement.RETURN_GENERATED_KEYS);) {
|
||||
|
||||
@ -3,7 +3,6 @@ package org.lapaloma.examen.aaee.service;
|
||||
import java.util.List;
|
||||
|
||||
import org.lapaloma.examen.aaee.dao.ICocheDAO;
|
||||
import org.lapaloma.examen.aaee.excepcion.CocheNoEncontradoException;
|
||||
import org.lapaloma.examen.aaee.vo.Coche;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
@ -17,21 +16,31 @@ public class CocheService {
|
||||
this.cocheDAO = cocheDAO;
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<Coche> consultarCochesPorMarca(String marca) {
|
||||
return cocheDAO.obtenerCochesPorMarca(marca);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Coche crearNuevoCoche(Coche coche) {
|
||||
if (coche.getCilindrada() == null || coche.getCilindrada() <= 0) {
|
||||
|
||||
// Asignamos cilindada un valor negativo para probar la excepción
|
||||
coche.setCilindrada(-5);
|
||||
|
||||
if (coche.getCilindrada() == null || coche.getCilindrada() <= 0) {
|
||||
throw new IllegalArgumentException("Cilindrada debe ser mayor que 0");
|
||||
}
|
||||
return cocheDAO.crearCoche(coche);
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public Coche obtenerPorId(Integer id) {
|
||||
return cocheRepository.findById(id).orElseThrow(() -> new CocheNoEncontradoException("Coche con id '" + id + "' no encontrado"));
|
||||
public List<Coche> consultarCochesPorMarca(String marca) {
|
||||
List<Coche> listaCoches = cocheDAO.obtenerCochesPorMarca(marca);
|
||||
|
||||
// Simulamos el caso de lista vacía para probar la excepción
|
||||
listaCoches=null;
|
||||
|
||||
if (listaCoches == null || listaCoches.isEmpty()) {
|
||||
throw new RuntimeException("No hay miembros disponibles");
|
||||
}
|
||||
|
||||
return listaCoches;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -1,21 +1,11 @@
|
||||
package org.lapaloma.examen.aaee.vo;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import javax.validation.constraints.Positive;
|
||||
|
||||
public class Coche {
|
||||
|
||||
private Integer id;
|
||||
|
||||
@NotBlank(message = "Marca es obligatoria")
|
||||
private String marca;
|
||||
|
||||
@NotBlank(message = "Modelo es obligatorio")
|
||||
private String modelo;
|
||||
|
||||
@NotNull(message = "Cilindrada es obligatoria")
|
||||
@Positive(message = "Cilindrada debe ser mayor que 0")
|
||||
private Integer cilindrada;
|
||||
|
||||
public Coche() {}
|
||||
|
||||
@ -1,11 +1,11 @@
|
||||
|
||||
# Puerto en que escucha Spring Boot
|
||||
server.port=8080
|
||||
server.port=8081
|
||||
|
||||
# Servicio de la base de datos.
|
||||
# Ejemplo url base dator:
|
||||
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
|
||||
spring.datasource.url=jdbc:mysql://${DB_HOST:192.168.1.36}:${DB_PORT:3306}/${DB_NAME:Gobierno}
|
||||
spring.datasource.url=jdbc:mysql://${DB_HOST:192.168.1.36}:${DB_PORT:3306}/${DB_NAME:concesionario}
|
||||
spring.datasource.username=${DB_USER:root}
|
||||
spring.datasource.password=${DB_PASSWORD:mysql_123}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user