Compare commits
23 Commits
feature/se
...
main
Author | SHA1 | Date |
---|---|---|
|
6eab0ddb8c | 2 years ago |
|
d0080463ee | 2 years ago |
|
c1b07bbecb | 2 years ago |
|
bb4f0d8977 | 2 years ago |
|
d4eae7767a | 2 years ago |
|
1a27d055d5 | 2 years ago |
|
2e4b91f35b | 2 years ago |
|
716e54cff5 | 2 years ago |
|
18fe18265d | 2 years ago |
|
5cea50049c | 2 years ago |
|
7f652033e7 | 2 years ago |
|
75f633f38f | 2 years ago |
|
addc450e19 | 2 years ago |
|
c95b3a1562 | 2 years ago |
|
b4732245df | 2 years ago |
|
68dc23a204 | 2 years ago |
|
6083a1b650 | 2 years ago |
|
034963599e | 2 years ago |
|
84670bf75f | 2 years ago |
|
30d9594113 | 2 years ago |
|
5f7179408e | 2 years ago |
|
a8dd0a50cc | 2 years ago |
|
d023288574 | 2 years ago |
@ -1,6 +1,4 @@
|
||||
FROM debian:11
|
||||
|
||||
RUN apt update && apt install -y openjdk-17-jre
|
||||
FROM openjdk:17-bullseye
|
||||
|
||||
COPY pedidos.core-0.0.1-SNAPSHOT.jar /app.jar
|
||||
|
||||
|
Binary file not shown.
Binary file not shown.
@ -0,0 +1,24 @@
|
||||
package pedidos.pedidos.core.pedidos;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.GeneratedValue;
|
||||
import jakarta.persistence.GenerationType;
|
||||
import jakarta.persistence.Id;
|
||||
import jakarta.persistence.ManyToOne;
|
||||
import lombok.Data;
|
||||
import pedidos.pedidos.core.producto.Producto;
|
||||
|
||||
@Data
|
||||
@Entity
|
||||
public class DetallePedido {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO )
|
||||
private long id;
|
||||
|
||||
@ManyToOne
|
||||
private Producto producto;
|
||||
private Integer cantidad;
|
||||
private BigDecimal precio;
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package pedidos.pedidos.core.pedidos;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
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;
|
||||
import pedidos.pedidos.core.cliente.Cliente;
|
||||
|
||||
@Data
|
||||
@Entity
|
||||
public class Pedido {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.SEQUENCE )
|
||||
private long id;
|
||||
|
||||
private String numero;
|
||||
@ManyToOne
|
||||
private Cliente cliente;
|
||||
|
||||
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = false)
|
||||
@JoinColumn(name="pedido_id")
|
||||
private List<DetallePedido> detalle = new ArrayList<>();
|
||||
}
|
@ -0,0 +1,135 @@
|
||||
package pedidos.pedidos.core.pedidos;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.math.BigDecimal;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
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;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
|
||||
|
||||
import jakarta.persistence.ManyToOne;
|
||||
import jakarta.persistence.OneToMany;
|
||||
import pedidos.pedidos.core.cliente.ClienteService;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/pedido")
|
||||
@CrossOrigin({"*"})
|
||||
public class PedidoController {
|
||||
|
||||
@Autowired
|
||||
PedidoService service;
|
||||
@Autowired ClienteService clienteService;
|
||||
|
||||
@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 long id, @RequestBody Map<String, Object> fields){
|
||||
|
||||
Pedido entity = service.findById(id);
|
||||
|
||||
// itera sobre los campos que se desean actualizar
|
||||
for (Map.Entry<String, Object> field : fields.entrySet()) {
|
||||
String fieldName = field.getKey();
|
||||
Object fieldValue = null;
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.registerModule(new JavaTimeModule());
|
||||
java.lang.reflect.Field campoEntidad = org.springframework.util.ReflectionUtils.findField(Pedido.class, fieldName);
|
||||
campoEntidad.setAccessible(true);
|
||||
if (fieldName.equals("precio")){
|
||||
fieldValue= new BigDecimal (field.getValue().toString());
|
||||
}else if (fieldName.equals("creado")){
|
||||
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm");
|
||||
fieldValue = LocalDateTime.parse(field.getValue().toString(),formatter);
|
||||
}else if (fieldName.equals("detalle")){
|
||||
List<?> originalList = mapper.convertValue(fields.get(fieldName), new TypeReference<List<?>>() {});
|
||||
try {
|
||||
Class<?> type = Class.forName("pedidos.pedidos.core.pedidos.DetallePedido");
|
||||
List<Object> newList = new java.util.ArrayList<>();
|
||||
for (Object o : originalList) {
|
||||
newList.add(mapper.convertValue(o, type));
|
||||
}
|
||||
campoEntidad.set(entity,newList);
|
||||
} catch(ClassNotFoundException ex) {
|
||||
System.out.println("Error occuured");
|
||||
ex.printStackTrace();
|
||||
} catch (IllegalArgumentException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
} catch (IllegalAccessException e) {
|
||||
// TODO Auto-generated catch block
|
||||
e.printStackTrace();
|
||||
}
|
||||
}else {
|
||||
fieldValue =field.getValue();
|
||||
}
|
||||
|
||||
// utiliza reflection para establecer el valor del campo en la entidad
|
||||
try {
|
||||
//Field campoEntidad = Pedido.class.getDeclaredField(fieldName);
|
||||
|
||||
|
||||
if (campoEntidad.isAnnotationPresent(ManyToOne.class)) {
|
||||
java.util.LinkedHashMap<String, Long> keyValue = mapper.convertValue(fieldValue, new TypeReference<java.util.LinkedHashMap<String, Long>>(){});
|
||||
Object relatedEntity = clienteService.findById(keyValue.get("id"));
|
||||
campoEntidad.set(entity, relatedEntity);
|
||||
} if (campoEntidad.isAnnotationPresent(OneToMany.class)) {
|
||||
//java.util.LinkedHashMap<String, Long> keyValue = mapper.convertValue(fieldValue, new TypeReference<java.util.LinkedHashMap<String, Long>>(){});
|
||||
//Object relatedEntity = entidadService.findById(keyValue.get("id"));
|
||||
//campoEntidad.set(entity, relatedEntity);
|
||||
}
|
||||
else{
|
||||
//campoEntidad.set(entity, fieldValue);
|
||||
campoEntidad.set(entity, mapper.convertValue(fieldValue, campoEntidad.getType()));
|
||||
}
|
||||
|
||||
|
||||
} catch ( IllegalAccessException ex) {
|
||||
// maneja la excepción si ocurre algún error al acceder al campo
|
||||
}
|
||||
}
|
||||
return update(entity);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package pedidos.pedidos.core.pedidos;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
public interface PedidoRepository extends CrudRepository <Pedido, Long> {
|
||||
|
||||
List<Pedido> findAll();
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package pedidos.pedidos.core.pedidos;
|
||||
|
||||
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(long id){
|
||||
repository.deleteById(id);
|
||||
}
|
||||
|
||||
public Pedido findById(long id){
|
||||
return repository.findById(id).orElse(null);
|
||||
}
|
||||
|
||||
public List<Pedido> findAll(){
|
||||
return repository.findAll();
|
||||
}
|
||||
}
|
@ -1,7 +1,6 @@
|
||||
spring.datasource.url=jdbc:postgresql://db:5432/pedidosdb
|
||||
spring.datasource.url=jdbc:postgresql://dbclases:5432/pedidosdb
|
||||
spring.datasource.username=postgres
|
||||
spring.datasource.password=postgres
|
||||
spring.jpa.hibernate.ddl-auto=update
|
||||
#spring.jpa.show-sql=true
|
||||
#spring.sql.init.mode=always
|
||||
server.port=8081
|
||||
server.port=8080
|
Loading…
Reference in new issue