mirror of
https://github.com/hyperledger/fabric-samples.git
synced 2026-06-17 07:25:10 +00:00
se agrega el endpoint y el smart contract de validar recetas
This commit is contained in:
parent
a70ef3b1cd
commit
07d5d9e5f2
3 changed files with 61 additions and 11 deletions
|
|
@ -105,21 +105,44 @@ func (s *SmartContract) CreateReceta(ctx contractapi.TransactionContextInterface
|
|||
return ctx.GetStub().PutState(receta.ID, recetaJSON)
|
||||
}
|
||||
|
||||
func (s *SmartContract) UpdateReceta(ctx contractapi.TransactionContextInterface, receta Receta) error {
|
||||
exists, err := s.RecetaExists(ctx, receta.ID)
|
||||
func (s *SmartContract) EntregarReceta(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", receta.ID)
|
||||
return fmt.Errorf("la receta %s no existe", recetaID)
|
||||
}
|
||||
|
||||
recetaJSON, err := json.Marshal(receta)
|
||||
// Obtener la receta actual
|
||||
recetaActualJSON, err := ctx.GetStub().GetState(recetaID)
|
||||
if err != nil {
|
||||
return err
|
||||
return fmt.Errorf("error al obtener la receta actual: %v", err)
|
||||
}
|
||||
if recetaActualJSON == nil {
|
||||
return fmt.Errorf("la receta %s no fue encontrada en el ledger", recetaID)
|
||||
}
|
||||
|
||||
return ctx.GetStub().PutState(receta.ID, recetaJSON)
|
||||
var recetaActual Receta
|
||||
if err := json.Unmarshal(recetaActualJSON, &recetaActual); err != nil {
|
||||
return fmt.Errorf("error al parsear la receta actual: %v", err)
|
||||
}
|
||||
|
||||
// Validar que el estado actual sea ACTIVO
|
||||
if recetaActual.Status != "ACTIVO" {
|
||||
return fmt.Errorf("solo se puede entregar la receta si está en estado 'ACTIVO'")
|
||||
}
|
||||
|
||||
// Cambiar el estado a ENTREGADO
|
||||
recetaActual.Status = "ENTREGADO"
|
||||
|
||||
// Guardar la receta modificada
|
||||
updatedRecetaJSON, err := json.Marshal(recetaActual)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error al serializar la receta modificada: %v", err)
|
||||
}
|
||||
|
||||
return ctx.GetStub().PutState(recetaID, updatedRecetaJSON)
|
||||
}
|
||||
|
||||
func (s *SmartContract) ReadReceta(ctx contractapi.TransactionContextInterface, id string) (*Receta, error) {
|
||||
|
|
|
|||
|
|
@ -83,6 +83,25 @@ public class RecetaController {
|
|||
}
|
||||
}
|
||||
|
||||
@PutMapping("/entregar")
|
||||
public ResponseEntity<Void> entregarReceta(@RequestBody Map<String, String> requestBody) {
|
||||
try {
|
||||
String id = requestBody.get("id");
|
||||
|
||||
if (id == null || id.isEmpty()) {
|
||||
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
|
||||
System.out.println("\n--> Submit Transaction: EntregarReceta");
|
||||
|
||||
recetaService.entregarReceta(id);
|
||||
return new ResponseEntity<>(HttpStatus.OK);
|
||||
} catch (EndorseException | SubmitException | CommitStatusException | CommitException | IOException | GatewayException e) {
|
||||
e.printStackTrace();
|
||||
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
private RecetaDto mapToDto(Receta receta) {
|
||||
RecetaDto dto = new RecetaDto();
|
||||
dto.setIdentifier(receta.getIdentifier());
|
||||
|
|
|
|||
|
|
@ -17,7 +17,9 @@ import java.io.IOException;
|
|||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.security.Identity;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.Signer;
|
||||
import java.security.cert.CertificateException;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
|
@ -29,7 +31,8 @@ public class RecetaService {
|
|||
private static final String CHANNEL_NAME = System.getenv().getOrDefault("CHANNEL_NAME", "mychannel");
|
||||
private static final String CHAINCODE_NAME = System.getenv().getOrDefault("CHAINCODE_NAME", "basic");
|
||||
|
||||
private static final Path CRYPTO_PATH = Paths.get("../../test-network/organizations/peerOrganizations/org1.example.com");
|
||||
private static final Path CRYPTO_PATH = Paths
|
||||
.get("../../test-network/organizations/peerOrganizations/org1.example.com");
|
||||
private static final Path CERT_DIR_PATH = CRYPTO_PATH.resolve("users/User1@org1.example.com/msp/signcerts");
|
||||
private static final Path KEY_DIR_PATH = CRYPTO_PATH.resolve("users/User1@org1.example.com/msp/keystore");
|
||||
private static final Path TLS_CERT_PATH = CRYPTO_PATH.resolve("peers/peer0.org1.example.com/tls/ca.crt");
|
||||
|
|
@ -98,7 +101,8 @@ public class RecetaService {
|
|||
contract.submitTransaction("InitLedger");
|
||||
}
|
||||
|
||||
public void cargarReceta(Receta receta) throws CommitStatusException, EndorseException, CommitException, SubmitException {
|
||||
public void cargarReceta(Receta receta)
|
||||
throws CommitStatusException, EndorseException, CommitException, SubmitException {
|
||||
contract.submitTransaction(
|
||||
"CreateReceta",
|
||||
receta.getId(),
|
||||
|
|
@ -116,8 +120,7 @@ public class RecetaService {
|
|||
receta.getDniPaciente(),
|
||||
receta.getFechaDeAutorizacion(),
|
||||
Integer.toString(receta.getCantidad()),
|
||||
receta.getExpectedSupplyDuration()
|
||||
);
|
||||
receta.getExpectedSupplyDuration());
|
||||
}
|
||||
|
||||
public Receta obtenerReceta(String recetaId) throws GatewayException, IOException {
|
||||
|
|
@ -140,8 +143,14 @@ public class RecetaService {
|
|||
return objectMapper.readValue(evaluateResult,
|
||||
objectMapper.getTypeFactory().constructCollectionType(List.class, Receta.class));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void entregarReceta(String recetaId)
|
||||
throws CommitStatusException, EndorseException, CommitException, SubmitException {
|
||||
contract.submitTransaction("EntregarReceta", recetaId);
|
||||
}
|
||||
|
||||
public List<Receta> obtenerRecetasPorDniYEstado(String dni, String estado) throws GatewayException, IOException {
|
||||
if (dni == null || dni.isBlank() || estado == null || estado.isBlank()) {
|
||||
throw new IllegalArgumentException("DNI y estado son obligatorios");
|
||||
|
|
@ -152,4 +161,3 @@ public List<Receta> obtenerRecetasPorDniYEstado(String dni, String estado) throw
|
|||
return objectMapper.readValue(evaluateResult,
|
||||
objectMapper.getTypeFactory().constructCollectionType(List.class, Receta.class));
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue