agrega control de fecha en el contralor del producto

main
Freddy Heredia 2 years ago
parent c1b07bbecb
commit d0080463ee

@ -1,11 +1,14 @@
package pedidos.pedidos.core.compania;
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;
@ -13,6 +16,13 @@ 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 pedidos.pedidos.core.cliente.Cliente;
@RestController
@RequestMapping("api/compania")
@CrossOrigin({"*"})
@ -50,4 +60,37 @@ public class CompaniaController {
companiaService.deleteById(id);
}
@PatchMapping("/{id}/")
public Compania partialUpdate(@PathVariable long id, @RequestBody Map<String, Object> fields){
Compania 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 {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
Field campoEntidad = Compania.class.getDeclaredField(fieldName);
campoEntidad.setAccessible(true);
if (campoEntidad.isAnnotationPresent(ManyToOne.class)) {
java.util.LinkedHashMap<String, Long> keyValue = mapper.convertValue(fieldValue, new TypeReference<java.util.LinkedHashMap<String, Long>>(){});
Object relatedEntity = companiaService.findById(keyValue.get("id"));
campoEntidad.set(entity, relatedEntity);
}else{
campoEntidad.set(entity, fieldValue);
}
} catch (NoSuchFieldException | IllegalAccessException ex) {
// maneja la excepción si ocurre algún error al acceder al campo
}
}
return update(entity);
}
}

@ -69,8 +69,14 @@ public class ProductoController {
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);
DateTimeFormatter formatter;
String dateString = field.getValue().toString();
if (field.getValue().toString().length()>16){
dateString = dateString.substring(0, 16);
}
formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm");
fieldValue = LocalDateTime.parse(dateString,formatter);
}else {
fieldValue =field.getValue();
}

Loading…
Cancel
Save