mirror of
https://github.com/hyperledger/fabric-samples.git
synced 2026-06-22 17:45:10 +00:00
fix borrar receta
This commit is contained in:
parent
3b67b83d49
commit
d95af3b0f7
3 changed files with 30 additions and 53 deletions
|
|
@ -231,42 +231,37 @@ func (s *SmartContract) ReadReceta(ctx contractapi.TransactionContextInterface,
|
||||||
return &receta, nil
|
return &receta, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SmartContract) DeleteReceta(ctx contractapi.TransactionContextInterface, id string) error {
|
func (s *SmartContract) DeleteReceta(ctx contractapi.TransactionContextInterface, recetaID string) error {
|
||||||
exists, err := s.RecetaExists(ctx, id)
|
exists, err := s.RecetaExists(ctx, recetaID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if !exists {
|
if !exists {
|
||||||
return fmt.Errorf("la receta %s no existe", id)
|
return fmt.Errorf("la receta %s no existe", recetaID)
|
||||||
}
|
}
|
||||||
|
recetaJSON, err := ctx.GetStub().GetState(recetaID)
|
||||||
// Obtener receta
|
|
||||||
recetaJSON, err := ctx.GetStub().GetState(id)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error al obtener la receta: %v", err)
|
return fmt.Errorf("error al obtener la receta: %v", err)
|
||||||
}
|
}
|
||||||
|
if recetaJSON == nil {
|
||||||
|
return fmt.Errorf("la receta %s no fue encontrada en el ledger", recetaID)
|
||||||
|
}
|
||||||
var receta Receta
|
var receta Receta
|
||||||
err = json.Unmarshal(recetaJSON, &receta)
|
if err := json.Unmarshal(recetaJSON, &receta); err != nil {
|
||||||
if err != nil {
|
return fmt.Errorf("error al parsear la receta: %v", err)
|
||||||
return fmt.Errorf("error al deserializar la receta: %v", err)
|
}
|
||||||
|
if receta.Status != string(EstadoDraft) {
|
||||||
|
return fmt.Errorf("la receta %s no puede ser firmada porque no está en estado 'draft'", recetaID)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cambiar el estado a "cancelled"
|
|
||||||
receta.Status = string(EstadoCancelled)
|
receta.Status = string(EstadoCancelled)
|
||||||
|
|
||||||
// Volver a guardar la receta modificada
|
updatedRecetaJSON, err := json.Marshal(receta)
|
||||||
recetaActualizadaJSON, err := json.Marshal(receta)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error al serializar la receta actualizada: %v", err)
|
return fmt.Errorf("error al serializar la receta firmada: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = ctx.GetStub().PutState(id, recetaActualizadaJSON)
|
return ctx.GetStub().PutState(recetaID, updatedRecetaJSON)
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error al guardar la receta actualizada: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SmartContract) RecetaExists(ctx contractapi.TransactionContextInterface, id string) (bool, error) {
|
func (s *SmartContract) RecetaExists(ctx contractapi.TransactionContextInterface, id string) (bool, error) {
|
||||||
|
|
|
||||||
|
|
@ -181,32 +181,22 @@ public class RecetaController {
|
||||||
|
|
||||||
@PostMapping("/borrar")
|
@PostMapping("/borrar")
|
||||||
public ResponseEntity<RecetaDto> delete(@RequestBody Map<String, String> requestBody) {
|
public ResponseEntity<RecetaDto> delete(@RequestBody Map<String, String> requestBody) {
|
||||||
logger.info("Received request to obtain receta with ID: {}", requestBody.get("id")); // Log de entrada
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
String id = requestBody.get("id");
|
String id = requestBody.get("id");
|
||||||
logger.debug("Searching for receta with ID: {}", id); // Log de búsqueda
|
|
||||||
|
if (id == null || id.isEmpty()) {
|
||||||
|
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("\n--> Submit Transaction: BorrarReceta");
|
||||||
|
|
||||||
recetaService.borrarReceta(id);
|
recetaService.borrarReceta(id);
|
||||||
logger.debug("Receta deleted: {}", id); // Log cuando se encuentra la receta
|
|
||||||
|
|
||||||
return new ResponseEntity<>(HttpStatus.OK);
|
return new ResponseEntity<>(HttpStatus.OK);
|
||||||
} catch (IOException e) {
|
} catch (EndorseException | SubmitException | CommitStatusException | CommitException e) {
|
||||||
logger.error("IOException occurred while deleting receta with ID: {}", requestBody.get("id"), e); // Log de
|
e.printStackTrace(); // o algún log específico
|
||||||
// excepción
|
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||||
// específica
|
|
||||||
return new ResponseEntity<>(HttpStatus.NOT_ACCEPTABLE);
|
|
||||||
} catch (GatewayException e) {
|
} catch (GatewayException e) {
|
||||||
logger.error("GatewayException occurred while deleting receta with ID: {}", requestBody.get("id"), e); // Log
|
e.printStackTrace(); // este bloque rara vez se ejecutaría si ya atrapás las anteriores
|
||||||
// de
|
|
||||||
// excepción
|
|
||||||
// Gateway
|
|
||||||
return new ResponseEntity<>(HttpStatus.NOT_ACCEPTABLE);
|
|
||||||
} catch (Exception e) {
|
|
||||||
logger.error("Unexpected error occurred while deleting receta with ID: {}", requestBody.get("id"), e); // Log
|
|
||||||
// de
|
|
||||||
// error
|
|
||||||
// inesperado
|
|
||||||
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
|
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -181,24 +181,16 @@ public class RecetaService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void borrarReceta(String recetaId) throws GatewayException, IOException {
|
public void borrarReceta(String recetaId)
|
||||||
|
throws CommitStatusException, EndorseException, CommitException, SubmitException {
|
||||||
System.out.println("[INFO] Iniciando borrado de receta con ID: " + recetaId);
|
System.out.println("[INFO] Iniciando borrado de receta con ID: " + recetaId);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
System.out.println("[DEBUG] Ejecutando transacción 'DeleteReceta' con ID: " + recetaId);
|
var evaluateResult = contract.submitTransaction("DeleteReceta", recetaId);
|
||||||
var evaluateResult = contract.evaluateTransaction("DeleteReceta", recetaId);
|
|
||||||
System.out.println("[DEBUG] Resultado de transacción recibido: " + new String(evaluateResult));
|
System.out.println("[DEBUG] Resultado de transacción recibido: " + new String(evaluateResult));
|
||||||
|
|
||||||
System.out.println("[INFO] Receta borrada exitosamente para ID: " + recetaId);
|
System.out.println("[INFO] Receta borrada exitosamente para ID: " + recetaId);
|
||||||
|
|
||||||
} catch (GatewayException e) {
|
|
||||||
System.err.println("[ERROR] GatewayException al borrar receta con ID: " + recetaId);
|
|
||||||
e.printStackTrace(System.err);
|
|
||||||
throw e;
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
System.err.println("[ERROR] Error inesperado al borrar receta con ID: " + recetaId);
|
System.err.println("[ERROR] Error al borrar receta: " + e.getMessage());
|
||||||
e.printStackTrace(System.err);
|
e.printStackTrace();
|
||||||
throw new RuntimeException("Error inesperado al borrar la receta", e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue