You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
backend-pw/src/main/java/erp/pedidos/provincia/ProvinciaController.java

80 lines
2.8 KiB

package erp.pedidos.provincia;
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;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
@RestController
@RequestMapping("/api/provincia")
@CrossOrigin({"*"})
public class ProvinciaController {
@Autowired
ProvinciaService service;
@GetMapping("/{id}/")
public Provincia findById(@PathVariable long id){
return service.findById(id);
}
@GetMapping("/")
public List<Provincia> 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 Provincia save (@RequestBody Provincia entity ){
return service.save(entity);
}
@PutMapping("/")
public Provincia update (@RequestBody Provincia entity){
return service.save(entity);
}
@DeleteMapping("/{id}/")
public void deleteById(@PathVariable long id){
service.deleteById(id);
}
@PatchMapping("/{id}/")
public Provincia partialUpdate(@PathVariable long id, @RequestBody Map<String, Object> fields){
Provincia 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 = Provincia.class.getDeclaredField(fieldName);
campoEntidad.setAccessible(true);
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
campoEntidad.set(entity, mapper.convertValue(fieldValue, campoEntidad.getType()));
} catch (NoSuchFieldException | IllegalAccessException ex) {
// maneja la excepción si ocurre algún error al acceder al campo
}
}
return update(entity);
}
}