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 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 fields){ Pedido entity = findById(id); // itera sobre los campos que se desean actualizar for (Map.Entry 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); } }