mirror of
https://github.com/hyperledger/fabric-samples.git
synced 2026-06-17 15:35:09 +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
|
||||
}
|
||||
|
||||
func (s *SmartContract) DeleteReceta(ctx contractapi.TransactionContextInterface, id string) error {
|
||||
exists, err := s.RecetaExists(ctx, id)
|
||||
func (s *SmartContract) DeleteReceta(ctx contractapi.TransactionContextInterface, recetaID string) error {
|
||||
exists, err := s.RecetaExists(ctx, recetaID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !exists {
|
||||
return fmt.Errorf("la receta %s no existe", id)
|
||||
return fmt.Errorf("la receta %s no existe", recetaID)
|
||||
}
|
||||
|
||||
// Obtener receta
|
||||
recetaJSON, err := ctx.GetStub().GetState(id)
|
||||
recetaJSON, err := ctx.GetStub().GetState(recetaID)
|
||||
if err != nil {
|
||||
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
|
||||
err = json.Unmarshal(recetaJSON, &receta)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error al deserializar la receta: %v", err)
|
||||
if err := json.Unmarshal(recetaJSON, &receta); err != nil {
|
||||
return fmt.Errorf("error al parsear 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)
|
||||
|
||||
// Volver a guardar la receta modificada
|
||||
recetaActualizadaJSON, err := json.Marshal(receta)
|
||||
updatedRecetaJSON, err := json.Marshal(receta)
|
||||
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)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error al guardar la receta actualizada: %v", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
return ctx.GetStub().PutState(recetaID, updatedRecetaJSON)
|
||||
}
|
||||
|
||||
func (s *SmartContract) RecetaExists(ctx contractapi.TransactionContextInterface, id string) (bool, error) {
|
||||
|
|
|
|||
|
|
@ -181,32 +181,22 @@ public class RecetaController {
|
|||
|
||||
@PostMapping("/borrar")
|
||||
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 {
|
||||
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);
|
||||
logger.debug("Receta deleted: {}", id); // Log cuando se encuentra la receta
|
||||
|
||||
return new ResponseEntity<>(HttpStatus.OK);
|
||||
} catch (IOException e) {
|
||||
logger.error("IOException occurred while deleting receta with ID: {}", requestBody.get("id"), e); // Log de
|
||||
// excepción
|
||||
// específica
|
||||
return new ResponseEntity<>(HttpStatus.NOT_ACCEPTABLE);
|
||||
} catch (EndorseException | SubmitException | CommitStatusException | CommitException e) {
|
||||
e.printStackTrace(); // o algún log específico
|
||||
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
} catch (GatewayException e) {
|
||||
logger.error("GatewayException occurred while deleting receta with ID: {}", requestBody.get("id"), e); // Log
|
||||
// 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
|
||||
e.printStackTrace(); // este bloque rara vez se ejecutaría si ya atrapás las anteriores
|
||||
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);
|
||||
|
||||
try {
|
||||
System.out.println("[DEBUG] Ejecutando transacción 'DeleteReceta' con ID: " + recetaId);
|
||||
var evaluateResult = contract.evaluateTransaction("DeleteReceta", recetaId);
|
||||
var evaluateResult = contract.submitTransaction("DeleteReceta", recetaId);
|
||||
System.out.println("[DEBUG] Resultado de transacción recibido: " + new String(evaluateResult));
|
||||
|
||||
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) {
|
||||
System.err.println("[ERROR] Error inesperado al borrar receta con ID: " + recetaId);
|
||||
e.printStackTrace(System.err);
|
||||
throw new RuntimeException("Error inesperado al borrar la receta", e);
|
||||
System.err.println("[ERROR] Error al borrar receta: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue