I try to make an api RESTfull with Angular 6 and Java, which must perform the CRUD operations, until now everything seems to work correctly, except to edit and insert in Employee and Auto.
The model is composed of 3 tables, Branch, Employee and Auto, each with its attributes and the foreign key.
Branch (id, name, description) Employee (id, first name, last name, ..., id_sucursal) Auto (id, name, description, ..., employee_id)
These are the methods by which I enter and edit on the backend
@Override
public String insertar(Auto auto) {
String resultado = null;
String sql = "INSERT INTO auto(nombre, descripcion, precio, placa, fecha_ingreso, marca, id_empleado) VALUES (?, ?, ?, ?, ?, ?, ?)";
try (Connection cn = con.conectar();
PreparedStatement pst = cn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);) {
pst.setString (1, auto.getNombre());
pst.setString (2, auto.getDescripcion());
pst.setFloat (3, auto.getPrecio());
pst.setString (4, auto.getPlaca());
pst.setString (5, auto.getFechaIngreso());
pst.setString (6, auto.getMarca());
pst.setInt (7, auto.getEmpleado().getId());
pst.executeUpdate();
ResultSet gk = pst.getGeneratedKeys();
if (gk.next())
{
auto.setId(gk.getInt(1));
}
Gson gson = new Gson();
resultado = gson.toJson(auto);
} catch (Exception ex) {
resultado = ex.getMessage();
}
return resultado;
}
@Override
public String editar(Auto auto) {
String resultado = null;
String sql = "UPDATE auto SET nombre= ?, descripcion = ?, precio = ?, placa = ?, fecha_ingreso = ?, marca = ?, id_empleado = ? WHERE id = ?";
try (Connection cn = con.conectar(); PreparedStatement pst = cn.prepareStatement(sql);) {
pst.setString(1, auto.getNombre());
pst.setString(2, auto.getDescripcion());
pst.setFloat(3, auto.getPrecio());
pst.setString(4, auto.getPlaca());
pst.setString(5, auto.getFechaIngreso());
pst.setString(6, auto.getMarca());
pst.setInt(7, auto.getEmpleado().getId());
pst.setInt(8, auto.getId());
pst.executeUpdate();
JsonObject json = new JsonObject();
json.addProperty("status", "exito");
resultado = json.toString();
} catch (Exception ex) {
JsonObject json = new JsonObject();
json.addProperty("status", ex.getMessage());
resultado = json.toString();
}
return resultado;
}
And this method is responsible for doing both in the component
createNewBranchOffice(){
if(this.validateNotEmptyField())
{
this.refresh=false;
if(!this.isUpdate)
{
this._branchOfficeService.insertSuc( { nombre : this.bOffice.nombre , descripcion : this.bOffice.descripcion } ).subscribe
(
(response :any) =>
{
this.bOffices.push(response);
this.refresh=true;
console.log(response);
this.clear();
},
error => {
console.log(error);
}
)
}
else
{
this.isUpdate = false;
this._branchOfficeService.editSuc( this.bOffice.id, { nombre: this.bOffice.nombre, descripcion: this.bOffice.descripcion } ).subscribe
(
(response : any) =>
{
this.bOffices[this.index] = JSON.parse(JSON.stringify(this.bOffice));
this.refresh=true;
console.log(response);
this.clear();
},
error =>
{
console.log(error);
}
)
}
this.refresh=false;
}
}
And here is how I get them in the table
<div>
<div class="form-group"> <label>Name</label> <input type="text" class="form-control input-group-sm input-sm" [(ngModel)]="aut.nombre"></div>
<div class="form-group"> <label>Descripcion</label> <input type="text" class="form-control input-group-sm input-sm" [(ngModel)]="aut.descripcion"></div>
<div class="form-group"> <label>Price</label> <input type="text" class="form-control input-group-sm input-sm" [(ngModel)]="aut.precio"></div>
<div class="form-group"> <label>Mat</label> <input type="text" class="form-control input-group-sm input-sm" [(ngModel)]="aut.placa"></div>
<div class="form-group"> <label>Admision Date</label> <input type="text" class="form-control input-group-sm input-sm" [(ngModel)]="aut.fechaIngreso"></div>
<div class="form-group"> <label>Mark Trade</label> <input type="text" class="form-control input-xs" [(ngModel)]="aut.marca"></div>
<div class="form-group">
<label for="">Employees</label>
<select [(ngModel)]="aut.empleado.id" class="custom-select">
<option selected >Select, please.....</option>
<option *ngFor="let emp of emps" [ngValue]=" emp.id" > {{emp.nombre}} </option>
</select>
</div>
<div class="form-group" style="display: none;"> <input type="text" class="form-control input-xs" [(ngModel)]="aut.empleado.nombre"></div>
<div class="form-group">
<button (click)="createAuto()" class="btn btn-sm btn-success"><i class="fas fa-plus-square"></i></button>
</div>
</div>
This is a method when clicking on edit, which loads the data in the registration form
editAuto(index){
this.isUpdate=true;
this.aut = JSON.parse(JSON.stringify(this.auts[index]));
this.index = index;
}
The truth is that I am new to all this in both java, angular and use of json so I do not know what I could do to solve it without altering the model of the database And here below is the view, that when editing load in a form, and in the form I update the data and the changes are reflected except in the employee column that only shows them when reloading the page