Properties y Clase Controller
Properties y Clase Controller
properties
spring.datasource.url=jdbc:mysql://localhost/db_springboot_backend?
useSSL=false
spring.datasource.username=root
spring.datasource.password=12345
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL57Dialect
spring.jpa.hibernate.ddl-auto=create-drop
logging.level.org.hibernate.SQL=debug
________________________________________________________________
CLASE: ClienteRestController
@CrossOrigin(origins = { "http://localhost:4200" })
@RestController
@RequestMapping("/api")
public class ClienteRestController {
@Autowired
private IClienteService clienteService;
@GetMapping("/clientes")
public List<Cliente> index() {
return clienteService.findAll();
}
@GetMapping("/clientes/{id}")
public ResponseEntity<?> show(@PathVariable Long id) {
try {
cliente = clienteService.findById(id);
} catch(DataAccessException e) {
response.put("mensaje", "Error al realizar la consulta en la
base de datos");
response.put("error", e.getMessage().concat(":
").concat(e.getMostSpecificCause().getMessage()));
return new ResponseEntity<Map<String,
Object>>(response, HttpStatus.INTERNAL_SERVER_ERROR);
}
if(cliente == null) {
response.put("mensaje", "El cliente ID:
".concat(id.toString().concat(" no existe en la base de datos!")));
return new ResponseEntity<Map<String,
Object>>(response, HttpStatus.NOT_FOUND);
}
@PostMapping("/clientes")
public ResponseEntity<?> create(@Valid @RequestBody Cliente
cliente, BindingResult result) {
if(result.hasErrors()) {
response.put("errors", errors);
return new ResponseEntity<Map<String,
Object>>(response, HttpStatus.BAD_REQUEST);
}
try {
clienteNew = clienteService.save(cliente);
} catch(DataAccessException e) {
response.put("mensaje", "Error al realizar el insert en la
base de datos");
response.put("error", e.getMessage().concat(":
").concat(e.getMostSpecificCause().getMessage()));
return new ResponseEntity<Map<String,
Object>>(response, HttpStatus.INTERNAL_SERVER_ERROR);
}
@PutMapping("/clientes/{id}")
public ResponseEntity<?> update(@Valid @RequestBody Cliente
cliente, BindingResult result, @PathVariable Long id) {
if(result.hasErrors()) {
response.put("errors", errors);
return new ResponseEntity<Map<String,
Object>>(response, HttpStatus.BAD_REQUEST);
}
if (clienteActual == null) {
response.put("mensaje", "Error: no se pudo editar, el
cliente ID: "
.concat(id.toString().concat(" no existe en la
base de datos!")));
return new ResponseEntity<Map<String,
Object>>(response, HttpStatus.NOT_FOUND);
}
try {
clienteActual.setApellido(cliente.getApellido());
clienteActual.setNombre(cliente.getNombre());
clienteActual.setEmail(cliente.getEmail());
clienteActual.setCreateAt(cliente.getCreateAt());
clienteUpdated = clienteService.save(clienteActual);
} catch (DataAccessException e) {
response.put("mensaje", "Error al actualizar el cliente en
la base de datos");
response.put("error", e.getMessage().concat(":
").concat(e.getMostSpecificCause().getMessage()));
return new ResponseEntity<Map<String,
Object>>(response, HttpStatus.INTERNAL_SERVER_ERROR);
}
@DeleteMapping("/clientes/{id}")
public ResponseEntity<?> delete(@PathVariable Long id) {
try {
clienteService.delete(id);
} catch (DataAccessException e) {
response.put("mensaje", "Error al eliminar el cliente de la
base de datos");
response.put("error", e.getMessage().concat(":
").concat(e.getMostSpecificCause().getMessage()));
return new ResponseEntity<Map<String,
Object>>(response, HttpStatus.INTERNAL_SERVER_ERROR);
}