mirror of
https://github.com/hyperledger/fabric-samples.git
synced 2026-06-17 07:25:10 +00:00
fix recetas y vacunas
This commit is contained in:
parent
6d422441ff
commit
09be144ced
7 changed files with 89 additions and 34 deletions
|
|
@ -13,7 +13,7 @@ type SmartContract struct {
|
|||
|
||||
type Receta struct {
|
||||
ID string `json:"id"`
|
||||
Identificador string `json:"identificador"`
|
||||
Identifier string `json:"identifier"`
|
||||
Owner string `json:"owner"`
|
||||
PrescripcionAnteriorId string `json:"prescripcionAnteriorId"`
|
||||
Status string `json:"status"`
|
||||
|
|
@ -51,7 +51,7 @@ func (s *SmartContract) InitLedger(ctx contractapi.TransactionContextInterface)
|
|||
recetas := []Receta{
|
||||
{
|
||||
ID: "receta1",
|
||||
Identificador: "rece1234",
|
||||
Identifier: "rece1234",
|
||||
Owner: "Tomoko",
|
||||
PrescripcionAnteriorId: "presc123",
|
||||
Status: "active",
|
||||
|
|
@ -70,7 +70,7 @@ func (s *SmartContract) InitLedger(ctx contractapi.TransactionContextInterface)
|
|||
},
|
||||
{
|
||||
ID: "receta2",
|
||||
Identificador: "rece1235",
|
||||
Identifier: "rece1235",
|
||||
Owner: "Alice",
|
||||
PrescripcionAnteriorId: "presc456",
|
||||
Status: "completed",
|
||||
|
|
|
|||
|
|
@ -5,6 +5,9 @@ import com.code.hyperledger.models.AssetIdDto;
|
|||
import com.code.hyperledger.models.Receta;
|
||||
import com.code.hyperledger.models.RecetaDto;
|
||||
import com.code.hyperledger.services.RecetaService;
|
||||
|
||||
import main.java.com.code.hyperledger.models.RecetaRequestDto;
|
||||
|
||||
import org.hyperledger.fabric.client.*;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
|
|
@ -17,47 +20,86 @@ import java.util.ArrayList;
|
|||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/recetas")
|
||||
public class RecetaController {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(RecetaController.class);
|
||||
|
||||
@Autowired
|
||||
private RecetaService recetaService;
|
||||
|
||||
@PostMapping("/crear")
|
||||
public ResponseEntity<AssetIdDto> crear(@RequestBody Receta receta) {
|
||||
System.out.println("\n--> Submit Transaction: CrearReceta");
|
||||
|
||||
|
||||
// Log para verificar los valores iniciales
|
||||
System.out.println("Receta recibida: " + receta);
|
||||
|
||||
String now = LocalDateTime.now().toString();
|
||||
String dni = receta.getPatientDocumentNumber();
|
||||
|
||||
// Log para ver el DNI y el timestamp
|
||||
System.out.println("DNI del paciente: " + dni);
|
||||
System.out.println("Timestamp actual: " + now);
|
||||
|
||||
String id = dni + now;
|
||||
String assetId = Hashing.sha256(id);
|
||||
|
||||
// Log para ver el ID generado
|
||||
System.out.println("ID generado para el asset: " + assetId);
|
||||
|
||||
receta.setId(assetId);
|
||||
|
||||
|
||||
AssetIdDto assetIdDto = new AssetIdDto();
|
||||
assetIdDto.setDni(dni);
|
||||
assetIdDto.setTimeStamp(now);
|
||||
|
||||
|
||||
try {
|
||||
// Log antes de intentar cargar la receta
|
||||
System.out.println("Intentando cargar la receta...");
|
||||
|
||||
recetaService.cargarReceta(receta);
|
||||
|
||||
// Log después de que la receta fue cargada
|
||||
System.out.println("Receta cargada correctamente.");
|
||||
|
||||
return new ResponseEntity<>(assetIdDto, HttpStatus.OK);
|
||||
} catch (CommitStatusException | EndorseException | CommitException | SubmitException e) {
|
||||
// Log de error
|
||||
System.err.println("Error al cargar la receta: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping("/obtener")
|
||||
public ResponseEntity<RecetaDto> find(@RequestBody Map<String, String> requestBody) {
|
||||
public ResponseEntity<RecetaDto> find(@RequestBody RecetaRequestDto requestBody) {
|
||||
logger.info("Received request to obtain receta with ID: {}", requestBody.getId()); // Log de entrada
|
||||
|
||||
try {
|
||||
String id = requestBody.get("id");
|
||||
String id = requestBody.getId();
|
||||
logger.debug("Searching for receta with ID: {}", id); // Log de búsqueda
|
||||
|
||||
Receta receta = recetaService.obtenerReceta(id);
|
||||
logger.debug("Receta found: {}", receta); // Log cuando se encuentra la receta
|
||||
|
||||
RecetaDto recetaDto = mapToDto(receta);
|
||||
logger.info("Receta DTO created successfully for ID: {}", id); // Log de éxito
|
||||
|
||||
return new ResponseEntity<>(recetaDto, HttpStatus.OK);
|
||||
} catch (IOException | GatewayException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
logger.error("IOException occurred while obtaining receta with ID: {}", requestBody.getId(), e); // Log de excepción específica
|
||||
return new ResponseEntity<>(HttpStatus.NOT_ACCEPTABLE);
|
||||
} catch (GatewayException e) {
|
||||
logger.error("GatewayException occurred while obtaining receta with ID: {}", requestBody.getId(), e); // Log de excepción Gateway
|
||||
return new ResponseEntity<>(HttpStatus.NOT_ACCEPTABLE);
|
||||
} catch (Exception e) {
|
||||
logger.error("Unexpected error occurred while obtaining receta with ID: {}", requestBody.getId(), e); // Log de error inesperado
|
||||
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ public class Receta {
|
|||
private String patientDocumentNumber;
|
||||
//@JsonFormat(pattern = "yyyy-MM-dd")
|
||||
private String fechaDeAutorizacion;
|
||||
private int cantidad;
|
||||
private String cantidad;
|
||||
//@JsonFormat(pattern = "yyyy-MM-dd")
|
||||
private String expectedSupplyDuration;
|
||||
}
|
||||
|
|
@ -24,7 +24,7 @@ public class RecetaDto {
|
|||
private String patientDocumentNumber;
|
||||
//@JsonFormat(pattern = "yyyy-MM-dd")
|
||||
private String fechaDeAutorizacion;
|
||||
private int cantidad;
|
||||
private String cantidad;
|
||||
//@JsonFormat(pattern = "yyyy-MM-dd")
|
||||
private String expectedSupplyDuration;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,13 @@
|
|||
package main.java.com.code.hyperledger.models;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class RecetaRequestDto {
|
||||
private String id;
|
||||
}
|
||||
|
||||
|
|
@ -11,7 +11,6 @@ public class Vacuna {
|
|||
private String id;
|
||||
private String identificador;
|
||||
private String status;
|
||||
|
||||
private String statusChange;
|
||||
private String statusReason;
|
||||
private String vaccinateCode;
|
||||
|
|
|
|||
|
|
@ -11,16 +11,18 @@ import lombok.SneakyThrows;
|
|||
import org.hyperledger.fabric.client.*;
|
||||
import org.hyperledger.fabric.client.identity.*;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.hyperledger.fabric.client.identity.Identity;
|
||||
import org.hyperledger.fabric.client.identity.Signer;
|
||||
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import org.hyperledger.fabric.client.identity.Identity;
|
||||
import java.security.InvalidKeyException;
|
||||
import org.hyperledger.fabric.client.identity.Signer;
|
||||
import java.security.cert.CertificateException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
|
|
@ -103,26 +105,19 @@ public class RecetaService {
|
|||
|
||||
public void cargarReceta(Receta receta)
|
||||
throws CommitStatusException, EndorseException, CommitException, SubmitException {
|
||||
contract.submitTransaction(
|
||||
"CreateReceta",
|
||||
receta.getId(),
|
||||
receta.getOwner(),
|
||||
receta.getPrescripcionAnteriorId(),
|
||||
receta.getStatus(),
|
||||
receta.getStatusChange(),
|
||||
receta.getPrioridad(),
|
||||
receta.getMedicacion(),
|
||||
receta.getRazon(),
|
||||
receta.getNotas(),
|
||||
receta.getPeriodoDeTratamiento(),
|
||||
receta.getInstruccionesTratamiento(),
|
||||
receta.getPeriodoDeValidez(),
|
||||
receta.getPatientDocumentNumber(),
|
||||
receta.getFechaDeAutorizacion(),
|
||||
Integer.toString(receta.getCantidad()),
|
||||
receta.getExpectedSupplyDuration());
|
||||
try {
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
String recetaJson = objectMapper.writeValueAsString(receta);
|
||||
|
||||
contract.submitTransaction("CreateReceta", recetaJson);
|
||||
System.out.println("Receta creada correctamente");
|
||||
} catch (Exception e) {
|
||||
System.err.println("Error en submitTransaction: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public Receta obtenerReceta(String recetaId) throws GatewayException, IOException {
|
||||
var evaluateResult = contract.evaluateTransaction("ReadReceta", recetaId);
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
|
@ -140,6 +135,12 @@ public class RecetaService {
|
|||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
String idsJson = objectMapper.writeValueAsString(recetaIds);
|
||||
var evaluateResult = contract.evaluateTransaction("GetMultipleRecetas", idsJson);
|
||||
|
||||
if (evaluateResult == null || evaluateResult.length == 0) {
|
||||
System.err.println("GetMultipleRecetas devolvió una respuesta vacía.");
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
return objectMapper.readValue(evaluateResult,
|
||||
objectMapper.getTypeFactory().constructCollectionType(List.class, Receta.class));
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue