Skip to content

Commit

Permalink
plantilla autocomplete
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaiasMorochi committed Aug 30, 2019
1 parent 79d7a62 commit 38b6a63
Show file tree
Hide file tree
Showing 17 changed files with 541 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ public ResponseEntity<Resource> verFoto(@PathVariable String filename) {

@GetMapping(value = "/ver/{id}")
public String ver(@PathVariable(value = "id") Long id, Map<String, Object> model, RedirectAttributes flash) {
Optional<Cliente> cl = clienteService.findById(id);
Cliente cliente = cl.get();
// Optional<Cliente> cl = clienteService.findById(id);
Cliente cliente = clienteService.findById(id);//cl.get();

if (cliente == null) {
flash.addFlashAttribute("error", "El cliente no existe en la base de datos");
Expand Down Expand Up @@ -93,10 +93,10 @@ public String crear(Map<String, Object> model) {
@RequestMapping(value="/form/{id}")
public String editar(@PathVariable(value="id") Long id, Map<String, Object> model, RedirectAttributes flash) {

Optional<Cliente> cliente = null;

// Optional<Cliente> cliente = null;
Cliente cliente = clienteService.findById(id);
if(id > 0) {
cliente = clienteService.findById(id);

if(cliente == null) {
flash.addFlashAttribute("error", "El ID del cliente no existe en la BBDD!");
return "redirect:/listar";
Expand Down Expand Up @@ -144,9 +144,9 @@ public String guardar(@Valid Cliente cliente, BindingResult result, Model model,

@RequestMapping(value="/eliminar/{id}")
public String eliminar(@PathVariable(value="id") Long id, RedirectAttributes flash) {
Optional<Cliente> cl = clienteService.findById(id);
Cliente cliente = cl.get();

Cliente cliente = clienteService.findById(id);
// Cliente cliente = cl.get();

if (id > 0) {
clienteService.delete(id);
Expand Down
112 changes: 112 additions & 0 deletions src/main/java/com/springboot/app/controllers/FacturaController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package com.springboot.app.controllers;

import com.springboot.app.models.entity.Cliente;
import com.springboot.app.models.entity.Factura;
import com.springboot.app.models.entity.ItemFactura;
import com.springboot.app.models.entity.Producto;
import com.springboot.app.models.service.IClienteService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/factura")
@SessionAttributes("factura")
public class FacturaController {

@Autowired
private IClienteService clienteService;

private final Logger log = LoggerFactory.getLogger(getClass());

@GetMapping("/ver/{id}")
public String ver(@PathVariable(value="id") Long id,
Model model,
RedirectAttributes flash) {
Factura factura = clienteService.findFacturaById(id);

if(factura == null) {
flash.addFlashAttribute("error", "La factura no existe en la base de datos!");
return "redirect:/listar";
}

model.addAttribute("factura", factura);
model.addAttribute("titulo", "Factura: ".concat(factura.getDescripcion()));

return "factura/ver";
}

@GetMapping("/form/{clienteId}")
public String crear(@PathVariable(value = "clienteId") Long clienteId, Map<String, Object> model,
RedirectAttributes flash) {

Cliente cliente = clienteService.findById(clienteId);

if (cliente == null) {
flash.addFlashAttribute("error", "El cliente no existe en la base de datos");
return "redirect:/listar";
}

Factura factura = new Factura();
factura.setCliente(cliente);

model.put("factura", factura);
model.put("titulo", "Crear Factura");

return "factura/form";
}

@GetMapping(value = "/cargar-productos/{term}", produces = { "application/json" })
public @ResponseBody
List<Producto> cargarProductos(@PathVariable String term) {
return clienteService.findByNombre(term);
}

@PostMapping("/form")
public String guardar(@Valid Factura factura,
BindingResult result, Model model,
@RequestParam(name = "item_id[]", required = false) Long[] itemId,
@RequestParam(name = "cantidad[]", required = false) Integer[] cantidad,
RedirectAttributes flash,
SessionStatus status) {

if (result.hasErrors()) {
model.addAttribute("titulo", "Crear Factura");
return "factura/form";
}

if (itemId == null || itemId.length == 0) {
model.addAttribute("titulo", "Crear Factura");
model.addAttribute("error", "Error: La factura NO puede no tener líneas!");
return "factura/form";
}

for (int i = 0; i < itemId.length; i++) {
Producto producto = clienteService.findProductoById(itemId[i]);

ItemFactura linea = new ItemFactura();
linea.setCantidad(cantidad[i]);
linea.setProducto(producto);
factura.addItemFactura(linea);

log.info("ID: " + itemId[i].toString() + ", cantidad: " + cantidad[i].toString());
}

clienteService.saveFactura(factura);
status.setComplete();

flash.addFlashAttribute("success", "Factura creada con éxito!");

return "redirect:/ver/" + factura.getCliente().getId();
}
}
7 changes: 7 additions & 0 deletions src/main/java/com/springboot/app/models/dao/IFacturaDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.springboot.app.models.dao;

import com.springboot.app.models.entity.Factura;
import org.springframework.data.repository.CrudRepository;

public interface IFacturaDao extends CrudRepository<Factura, Long> {
}
15 changes: 15 additions & 0 deletions src/main/java/com/springboot/app/models/dao/IProductoDao.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.springboot.app.models.dao;

import com.springboot.app.models.entity.Producto;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

import java.util.List;

public interface IProductoDao extends CrudRepository<Producto,Long> {

@Query("select p from Producto p where p.nombre like %?1%")
public List<Producto> findByNombre(String term);

public List<Producto> findByNombreLikeIgnoreCase(String term);
}
4 changes: 4 additions & 0 deletions src/main/java/com/springboot/app/models/entity/Factura.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ public void setItems(List<ItemFactura> items) {
this.items = items;
}

public void addItemFactura(ItemFactura item) {
this.items.add(item);
}

public Double getTotal() {
Double total = 0.0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ public void setCantidad(Integer cantidad) {
this.cantidad = cantidad;
}

public Producto getProducto() {
return producto;
}

public void setProducto(Producto producto) {
this.producto = producto;
}


public Double calcularImporte() {
return cantidad.doubleValue() * producto.getPrecio();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,29 @@
import java.util.List;
import java.util.Optional;

import com.springboot.app.models.entity.Factura;
import com.springboot.app.models.entity.Producto;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.springboot.app.models.entity.Cliente;

public interface IClienteService {
List<Cliente> findAll();
Page<Cliente> findAll(Pageable pageable);
void save(Cliente cliente);
Optional<Cliente> findById(Long id);
void delete(Long id);
public List<Cliente> findAll();

public Page<Cliente> findAll(Pageable pageable);

public void save(Cliente cliente);

public Cliente findById(Long id);

public void delete(Long id);

public List<Producto> findByNombre(String term);

public void saveFactura(Factura factura);

public Producto findProductoById(Long id);

public Factura findFacturaById(Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import java.util.List;
import java.util.Optional;

import com.springboot.app.models.dao.IFacturaDao;
import com.springboot.app.models.dao.IProductoDao;
import com.springboot.app.models.entity.Factura;
import com.springboot.app.models.entity.Producto;
import com.springboot.app.models.service.IClienteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
Expand All @@ -15,10 +19,15 @@

@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() {
Expand All @@ -29,24 +38,49 @@ public List<Cliente> findAll() {
@Transactional
public void save(Cliente cliente) {
clienteDao.save(cliente);

}

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

@Override
@Transactional
public void delete(Long id) {
clienteDao.deleteById(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);
}
}
7 changes: 7 additions & 0 deletions src/main/resources/static/css/jquery-ui.min.css

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions src/main/resources/static/js/jquery-ui.min.js

Large diffs are not rendered by default.

Loading

0 comments on commit 38b6a63

Please sign in to comment.