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