Skip to content

Commit

Permalink
seccion 7 Spring MVC + Data JPA + JQuery agregado CRUD Factura
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaiasMorochi committed Sep 1, 2019
1 parent 38b6a63 commit 9a9ac3f
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,18 @@ public String guardar(@Valid Factura factura,

return "redirect:/ver/" + factura.getCliente().getId();
}

@GetMapping("/eliminar/{id}")
public String eliminar(@PathVariable(value = "id") Long id, RedirectAttributes flash){

Factura factura = clienteService.findFacturaById(id);
if (factura != null){
clienteService.deleteFactura(id);
flash.addFlashAttribute("success", "Factura eliminada con éxito!");
return "redirect:/ver/" + factura.getCliente().getId();
}
flash.addFlashAttribute("error", "No existe la factura en la base de datos, no se puede eliminar!");
return "redirect:/listar";
}

}
5 changes: 5 additions & 0 deletions src/main/java/com/springboot/app/models/entity/Cliente.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,9 @@ public void setFacturas(List<Factura> facturas) {
public void addFactura(Factura factura){
facturas.add(factura);
}

@Override
public String toString() {
return nombre + ' ' + apellido ;
}
}
3 changes: 3 additions & 0 deletions src/main/java/com/springboot/app/models/entity/Factura.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.springboot.app.models.entity;

import javax.persistence.*;
import javax.validation.constraints.NotEmpty;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
Expand All @@ -16,7 +17,9 @@ public class Factura implements Serializable {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@NotEmpty
private String descripcion;

private String observacion;

@Temporal(TemporalType.DATE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,23 @@
import com.springboot.app.models.entity.Cliente;

public interface IClienteService {
public List<Cliente> findAll();
List<Cliente> findAll();

public Page<Cliente> findAll(Pageable pageable);
Page<Cliente> findAll(Pageable pageable);

public void save(Cliente cliente);
void save(Cliente cliente);

public Cliente findById(Long id);
Cliente findById(Long id);

public void delete(Long id);
void delete(Long id);

public List<Producto> findByNombre(String term);
List<Producto> findByNombre(String term);

public void saveFactura(Factura factura);
void saveFactura(Factura factura);

public Producto findProductoById(Long id);
Producto findProductoById(Long id);

public Factura findFacturaById(Long id);
Factura findFacturaById(Long id);

void deleteFactura(Long id);
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.springboot.app.models.service.impl;

import java.util.List;
import java.util.Optional;

import com.springboot.app.models.dao.IClienteDao;
import com.springboot.app.models.dao.IFacturaDao;
import com.springboot.app.models.dao.IProductoDao;
import com.springboot.app.models.entity.Cliente;
import com.springboot.app.models.entity.Factura;
import com.springboot.app.models.entity.Producto;
import com.springboot.app.models.service.IClienteService;
Expand All @@ -14,73 +13,75 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.springboot.app.models.dao.IClienteDao;
import com.springboot.app.models.entity.Cliente;
import java.util.List;

@Service
public class ClienteServiceImpl implements IClienteService {
@Autowired
private IClienteDao clienteDao;

@Autowired
private IProductoDao productoDao;

@Autowired
private IFacturaDao facturaDao;

@Override
@Transactional(readOnly = true)
public List<Cliente> findAll() {
return (List<Cliente>) clienteDao.findAll();
}

@Override
@Transactional
public void save(Cliente cliente) {
clienteDao.save(cliente);

}

@Override
@Transactional(readOnly = true)
public Cliente findById(Long id) {
return clienteDao.findById(id).orElse(null);
}

@Override
@Transactional
public void delete(Long id) {
clienteDao.deleteById(id);

}

@Override
@Transactional(readOnly = true)
public Page<Cliente> findAll(Pageable pageable) {
return clienteDao.findAll(pageable);
}

@Override
@Transactional(readOnly = true)
public List<Producto> findByNombre(String term) {
return productoDao.findByNombreLikeIgnoreCase("%" + term + "%");
}

@Override
@Transactional
public void saveFactura(Factura factura) {
facturaDao.save(factura);
}

@Override
@Transactional(readOnly=true)
public Producto findProductoById(Long id) {
return productoDao.findById(id).orElse(null);
}

@Override
@Transactional(readOnly=true)
public Factura findFacturaById(Long id) {
return facturaDao.findById(id).orElse(null);
}
@Autowired
private IClienteDao clienteDao;

@Autowired
private IProductoDao productoDao;

@Autowired
private IFacturaDao facturaDao;

@Override
@Transactional(readOnly = true)
public List<Cliente> findAll() {
return (List<Cliente>) clienteDao.findAll();
}

@Override
@Transactional
public void save(Cliente cliente) {
clienteDao.save(cliente);
}

@Override
@Transactional(readOnly = true)
public Cliente findById(Long id) {
return clienteDao.findById(id).orElse(null);
}

@Override
@Transactional
public void delete(Long id) {
clienteDao.deleteById(id);
}

@Override
@Transactional(readOnly = true)
public Page<Cliente> findAll(Pageable pageable) {
return clienteDao.findAll(pageable);
}

@Override
@Transactional(readOnly = true)
public List<Producto> findByNombre(String term) {
return productoDao.findByNombreLikeIgnoreCase("%" + term + "%");
}

@Override
@Transactional
public void saveFactura(Factura factura) {
facturaDao.save(factura);
}

@Override
@Transactional(readOnly = true)
public Producto findProductoById(Long id) {
return productoDao.findById(id).orElse(null);
}

@Override
@Transactional(readOnly = true)
public Factura findFacturaById(Long id) {
return facturaDao.findById(id).orElse(null);
}

@Override
public void deleteFactura(Long id) {
facturaDao.deleteById(id);
}
}
13 changes: 7 additions & 6 deletions src/main/resources/messages.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
NotEmpty.cliente.nombre= El nombre del clientes es requerido
NotEmpty.cliente.apellido= El apellido del cliente es requerido
NotEmpty.cliente.email= El email es requerido
Email.cliente.email= La direccion de correo no es valida
NotNull.cliente.createdAt= La fecha no puede ser nula
type.Mismatch.cliente.createdAt= Formato de la fecha invalido, debe ser: yyy-MM-dd
NotEmpty.cliente.nombre=El nombre del clientes es requerido
NotEmpty.cliente.apellido=El apellido del cliente es requerido
NotEmpty.cliente.email=El email es requerido
Email.cliente.email=La direccion de correo no es valida
NotNull.cliente.createdAt=La fecha no puede ser nula
type.Mismatch.cliente.createdAt=Formato de la fecha invalido, debe ser: yyy-MM-dd
NotEmpty.factura.descripcion=La descripción es requerida!
3 changes: 2 additions & 1 deletion src/main/resources/templates/factura/form.html
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ <h4 class="card-title">
<tbody>
</tbody>
</table>

<h5>Total <span class="badge badge-secondary" id="gran_total">0</span></h5>

<div class="form-group row">
Expand All @@ -78,8 +79,8 @@ <h5>Total <span class="badge badge-secondary" id="gran_total">0</span></h5>
class="btn btn-secondary" />
</div>
</div>
</form>

</form>

</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/templates/factura/ver.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ <h4 class="card-title">
</ul>
<div class="alert alert-info my-4"
th:if="${#lists.isEmpty(factura.items)}"
th:text="'No hay l�neas para la factura' + ${factura.descripcion}"></div>
th:text="'No hay lineas para la factura' + ${factura.descripcion}"></div>

<table th:if="${not #lists.isEmpty(factura.items)}"
class="table table-striped table-hover table-bordered my-3">
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/templates/layout/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<title th:text="'Spring Boot: MVC + Thymeleaf + Data JPA - ' + ${titulo}" />
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css" />
<link rel="stylesheet" th:href="@{/css/bootstrap.min.css}" />
<link rel="stylesheet" th:href="@{/css/jquery-ui.min.css}" />
<link rel="stylesheet" th:href="@{/css/styles.css}" />
</head>

Expand Down
5 changes: 5 additions & 0 deletions src/main/resources/templates/ver.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ <h4 class="card-title">
<th>Descripción</th>
<th>Fecha</th>
<th>Total</th>
<th>Ver</th>
<th>Eliminar</th>
</tr>
</thead>

Expand All @@ -59,6 +61,9 @@ <h4 class="card-title">
<td th:text="${factura.descripcion}"></td>
<td th:text="${factura.createAt}"></td>
<td th:text="${factura.total}"></td>
<td><a class="btn btn-primary btn-xs" th:href="@{'/factura/ver/' + ${factura.id}}" th:text="'detalle'"></a></td>
<td><a class="btn btn-danger btn-xs" th:href="@{'/factura/eliminar/' + ${factura.id}}" th:text="'eliminar'"
onclick="return confirm('Esta seguro que desea eliminar la factura?');"></a></td>
</tr>
</tbody>
</table>
Expand Down

0 comments on commit 9a9ac3f

Please sign in to comment.