0% found this document useful (0 votes)
26 views4 pages

Properties y Clase Controller

Uploaded by

yohamanu23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views4 pages

Properties y Clase Controller

Uploaded by

yohamanu23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 4

application.

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) {

Cliente cliente = null;


Map<String, Object> response = new HashMap<>();

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);
}

return new ResponseEntity<Cliente>(cliente, HttpStatus.OK);


}

@PostMapping("/clientes")
public ResponseEntity<?> create(@Valid @RequestBody Cliente
cliente, BindingResult result) {

Cliente clienteNew = null;


Map<String, Object> response = new HashMap<>();

if(result.hasErrors()) {

List<String> errors = result.getFieldErrors()


.stream()
.map(err -> "El campo '" + err.getField() +"' "+
err.getDefaultMessage())
.collect(Collectors.toList());

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);
}

response.put("mensaje", "El cliente ha sido creado con éxito!");


response.put("cliente", clienteNew);
return new ResponseEntity<Map<String, Object>>(response,
HttpStatus.CREATED);
}

@PutMapping("/clientes/{id}")
public ResponseEntity<?> update(@Valid @RequestBody Cliente
cliente, BindingResult result, @PathVariable Long id) {

Cliente clienteActual = clienteService.findById(id);

Cliente clienteUpdated = null;

Map<String, Object> response = new HashMap<>();

if(result.hasErrors()) {

List<String> errors = result.getFieldErrors()


.stream()
.map(err -> "El campo '" + err.getField() +"' "+
err.getDefaultMessage())
.collect(Collectors.toList());

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);
}

response.put("mensaje", "El cliente ha sido actualizado con


éxito!");
response.put("cliente", clienteUpdated);

return new ResponseEntity<Map<String, Object>>(response,


HttpStatus.CREATED);
}

@DeleteMapping("/clientes/{id}")
public ResponseEntity<?> delete(@PathVariable Long id) {

Map<String, Object> response = new HashMap<>();

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);
}

response.put("mensaje", "El cliente eliminado con éxito!");

You might also like