Compare commits
1 Commits
main
...
feature/se
Author | SHA1 | Date |
---|---|---|
|
238ebe231a | 2 years ago |
@ -1,25 +0,0 @@
|
||||
package erp.pedidos.pedido;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import erp.pedidos.producto.Producto;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import lombok.Data;
|
||||
import jakarta.persistence.ManyToOne;
|
||||
|
||||
@Data
|
||||
@Entity
|
||||
public class DetallePedido {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO )
|
||||
private Integer id;
|
||||
|
||||
@ManyToOne
|
||||
private Producto producto;
|
||||
private BigDecimal precio;
|
||||
private Integer cantidad;
|
||||
|
||||
}
|
@ -1,34 +0,0 @@
|
||||
package erp.pedidos.pedido;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import erp.pedidos.cliente.Cliente;
|
||||
import jakarta.persistence.CascadeType;
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.JoinColumn;
|
||||
import jakarta.persistence.ManyToOne;
|
||||
import jakarta.persistence.OneToMany;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@Entity
|
||||
public class Pedido {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO )
|
||||
private Integer id;
|
||||
|
||||
private Integer numero;
|
||||
private LocalDate fecha;
|
||||
@ManyToOne
|
||||
private Cliente cliente;
|
||||
|
||||
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
|
||||
@JoinColumn(name="pedido_id")
|
||||
private List<DetallePedido> detalle = new ArrayList<>();
|
||||
|
||||
}
|
@ -1,76 +0,0 @@
|
||||
package erp.pedidos.pedido;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.CrossOrigin;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PatchMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/pedido")
|
||||
@CrossOrigin({"*"})
|
||||
public class PedidoController {
|
||||
|
||||
@Autowired
|
||||
PedidoService service;
|
||||
|
||||
@GetMapping("/{id}/")
|
||||
public Pedido findById(@PathVariable Integer id){
|
||||
return service.findById(id);
|
||||
}
|
||||
|
||||
@GetMapping("/")
|
||||
public List<Pedido> findAll(){
|
||||
return service.findAll();
|
||||
}
|
||||
|
||||
//Create
|
||||
//Delimitador de acceso (public, private), tipo de dato de retorno, nombre del método, parametros de entrada { Sentencias }
|
||||
@PostMapping("/")
|
||||
public Pedido save (@RequestBody Pedido entity ){
|
||||
return service.save(entity);
|
||||
}
|
||||
|
||||
@PutMapping("/")
|
||||
public Pedido update (@RequestBody Pedido entity){
|
||||
return service.save(entity);
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}/")
|
||||
public void deleteById(@PathVariable Integer id){
|
||||
service.deleteById(id);
|
||||
}
|
||||
|
||||
@PatchMapping("/{id}/")
|
||||
public Pedido partialUpdate(@PathVariable Integer id, @RequestBody Map<String, Object> fields){
|
||||
|
||||
Pedido entity = findById(id);
|
||||
|
||||
// itera sobre los campos que se desean actualizar
|
||||
for (Map.Entry<String, Object> field : fields.entrySet()) {
|
||||
String fieldName = field.getKey();
|
||||
Object fieldValue = field.getValue();
|
||||
|
||||
// utiliza reflection para establecer el valor del campo en la entidad
|
||||
try {
|
||||
Field campoEntidad = Pedido.class.getDeclaredField(fieldName);
|
||||
campoEntidad.setAccessible(true);
|
||||
campoEntidad.set(entity, fieldValue);
|
||||
} catch (NoSuchFieldException | IllegalAccessException ex) {
|
||||
// maneja la excepción si ocurre algún error al acceder al campo
|
||||
}
|
||||
}
|
||||
return update(entity);
|
||||
}
|
||||
|
||||
}
|
@ -1,10 +0,0 @@
|
||||
package erp.pedidos.pedido;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
public interface PedidoRepository extends CrudRepository <Pedido, Integer> {
|
||||
|
||||
List<Pedido> findAll();
|
||||
}
|
@ -1,28 +0,0 @@
|
||||
package erp.pedidos.pedido;
|
||||
|
||||
import java.util.List;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class PedidoService {
|
||||
|
||||
@Autowired
|
||||
PedidoRepository repository;
|
||||
|
||||
public Pedido save( Pedido entity){
|
||||
return repository.save(entity);
|
||||
}
|
||||
|
||||
public void deleteById(Integer id){
|
||||
repository.deleteById(id);
|
||||
}
|
||||
|
||||
public Pedido findById(Integer id){
|
||||
return repository.findById(id).orElse(null);
|
||||
}
|
||||
|
||||
public List<Pedido> findAll(){
|
||||
return repository.findAll();
|
||||
}
|
||||
}
|
Loading…
Reference in new issue