mirror of
https://github.com/hyperledger/fabric-samples.git
synced 2026-06-18 16:05:10 +00:00
cambios para vacunas
This commit is contained in:
parent
09be144ced
commit
5008a13609
4 changed files with 116 additions and 6 deletions
|
|
@ -342,6 +342,27 @@ func (s *SmartContract) ReadVacuna(ctx contractapi.TransactionContextInterface,
|
|||
return &vacuna, nil
|
||||
}
|
||||
|
||||
func (s *SmartContract) GetMultipleVacunas(ctx contractapi.TransactionContextInterface, vacunaIDs []string) ([]*Vacuna, error) {
|
||||
var vacunas []*Vacuna
|
||||
for _, id := range vacunaIDs {
|
||||
vacunaJSON, err := ctx.GetStub().GetState(id)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error al leer del ledger: %v", err)
|
||||
}
|
||||
if vacunaJSON == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
var vacuna Vacuna
|
||||
err = json.Unmarshal(vacunaJSON, &vacuna)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
vacunas = append(vacunas, &vacuna)
|
||||
}
|
||||
return vacunas, nil
|
||||
}
|
||||
|
||||
func (s *SmartContract) GetVacunasPorDniYEstado(ctx contractapi.TransactionContextInterface, dni string, estado string) ([]*Vacuna, error) {
|
||||
if dni == "" {
|
||||
return nil, fmt.Errorf("el dni es obligatorio")
|
||||
|
|
|
|||
|
|
@ -2,6 +2,10 @@ package com.code.hyperledger.controllers;
|
|||
|
||||
import com.code.hyperledger.Utils.Hashing;
|
||||
import com.code.hyperledger.models.AssetIdDto;
|
||||
import com.code.hyperledger.models.Receta;
|
||||
import com.code.hyperledger.models.RecetaDto;
|
||||
import com.code.hyperledger.models.Vacuna;
|
||||
import com.code.hyperledger.models.VacunaDto;
|
||||
import com.code.hyperledger.models.Vacuna;
|
||||
import com.code.hyperledger.services.VacunaService;
|
||||
import org.hyperledger.fabric.client.*;
|
||||
|
|
@ -12,6 +16,7 @@ import org.springframework.web.bind.annotation.*;
|
|||
|
||||
import java.io.IOException;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
|
|
@ -25,25 +30,52 @@ public class VacunaController {
|
|||
@PostMapping("/crear")
|
||||
public ResponseEntity<AssetIdDto> crearVacuna(@RequestBody Vacuna vacuna) {
|
||||
System.out.println("\n--> Submit Transaction: CrearVacuna");
|
||||
|
||||
|
||||
// Log para verificar los valores iniciales
|
||||
System.out.println("Vacuna recibida: " + vacuna);
|
||||
|
||||
// Validación básica
|
||||
if (vacuna == null || vacuna.getPatientDocumentNumber() == null || vacuna.getPatientDocumentNumber().isEmpty()) {
|
||||
System.err.println("Datos de vacuna inválidos: faltan campos requeridos.");
|
||||
return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
|
||||
String now = LocalDateTime.now().toString();
|
||||
String dni = vacuna.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);
|
||||
|
||||
vacuna.setId(assetId);
|
||||
|
||||
|
||||
AssetIdDto assetIdDto = new AssetIdDto();
|
||||
assetIdDto.setDni(dni);
|
||||
assetIdDto.setTimeStamp(now);
|
||||
|
||||
|
||||
try {
|
||||
// Log antes de intentar registrar la vacuna
|
||||
System.out.println("Intentando registrar la vacuna...");
|
||||
|
||||
vacunaService.registrarVacuna(vacuna);
|
||||
|
||||
// Log después de que la vacuna fue registrada
|
||||
System.out.println("Vacuna registrada correctamente.");
|
||||
|
||||
return new ResponseEntity<>(assetIdDto, HttpStatus.OK);
|
||||
} catch (CommitStatusException | EndorseException | CommitException | SubmitException e) {
|
||||
// Log de error
|
||||
System.err.println("Error al registrar la vacuna: " + e.getMessage());
|
||||
e.printStackTrace();
|
||||
return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping("/obtener")
|
||||
public ResponseEntity<Vacuna> obtenerVacuna(@RequestBody Map<String, String> requestBody) {
|
||||
|
|
@ -61,7 +93,7 @@ public class VacunaController {
|
|||
}
|
||||
}
|
||||
|
||||
@GetMapping("/todas")
|
||||
@GetMapping("/all")
|
||||
public ResponseEntity<List<Vacuna>> obtenerTodasLasVacunas() {
|
||||
try {
|
||||
List<Vacuna> vacunas = vacunaService.obtenerTodasLasVacunas();
|
||||
|
|
@ -72,6 +104,28 @@ public class VacunaController {
|
|||
}
|
||||
}
|
||||
|
||||
@PostMapping("/todas")
|
||||
public ResponseEntity<List<VacunaDto>> obtenerVacunasPorIds(@RequestBody Map<String, List<String>> requestBody) {
|
||||
try {
|
||||
List<String> ids = requestBody.get("ids");
|
||||
if (ids == null || ids.isEmpty()) {
|
||||
return new ResponseEntity<>(new ArrayList<>(), HttpStatus.OK);
|
||||
}
|
||||
|
||||
List<Vacuna> vacunas = vacunaService.obtenerVacunasPorIds(ids);
|
||||
List<VacunaDto> vacunasDto = new ArrayList<>();
|
||||
|
||||
for (Vacuna vacuna : vacunas) {
|
||||
vacunasDto.add(mapToDto(vacuna));
|
||||
}
|
||||
|
||||
return new ResponseEntity<>(vacunasDto, HttpStatus.OK);
|
||||
} catch (IOException | GatewayException e) {
|
||||
e.printStackTrace();
|
||||
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
@PostMapping("/filtrar")
|
||||
public ResponseEntity<List<Vacuna>> obtenerVacunasPorDniYEstado(@RequestBody Map<String, String> filtros) {
|
||||
try {
|
||||
|
|
@ -89,4 +143,22 @@ public class VacunaController {
|
|||
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
private VacunaDto mapToDto(Vacuna vacuna) {
|
||||
VacunaDto dto = new VacunaDto();
|
||||
dto.setIdentificador(vacuna.getIdentificador());
|
||||
dto.setStatus(vacuna.getStatus());
|
||||
dto.setStatusChange(vacuna.getStatusChange());
|
||||
dto.setStatusReason(vacuna.getStatusReason());
|
||||
dto.setVaccinateCode(vacuna.getVaccinateCode());
|
||||
dto.setAdministradedProduct(vacuna.getAdministradedProduct());
|
||||
dto.setManufacturer(vacuna.getManufacturer());
|
||||
dto.setLotNumber(vacuna.getLotNumber());
|
||||
dto.setExpirationDate(vacuna.getExpirationDate());
|
||||
dto.setPatientDocumentNumber(vacuna.getPatientDocumentNumber());
|
||||
dto.setReactions(vacuna.getReactions());
|
||||
|
||||
return dto;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -117,7 +117,6 @@ public class RecetaService {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
public Receta obtenerReceta(String recetaId) throws GatewayException, IOException {
|
||||
var evaluateResult = contract.evaluateTransaction("ReadReceta", recetaId);
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
package com.code.hyperledger.services;
|
||||
|
||||
import com.code.hyperledger.models.Receta;
|
||||
import com.code.hyperledger.models.Vacuna;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.google.gson.Gson;
|
||||
|
|
@ -11,6 +12,8 @@ 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;
|
||||
|
|
@ -19,6 +22,7 @@ import java.nio.file.Path;
|
|||
import java.nio.file.Paths;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.cert.CertificateException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
|
|
@ -126,6 +130,20 @@ public class VacunaService {
|
|||
objectMapper.getTypeFactory().constructCollectionType(List.class, Vacuna.class));
|
||||
}
|
||||
|
||||
public List<Vacuna> obtenerVacunasPorIds(List<String> vacunaIds) throws GatewayException, IOException {
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
String idsJson = objectMapper.writeValueAsString(vacunaIds);
|
||||
var evaluateResult = contract.evaluateTransaction("GetMultipleVacunas", idsJson);
|
||||
|
||||
if (evaluateResult == null || evaluateResult.length == 0) {
|
||||
System.err.println("GetMultipleVacunas devolvió una respuesta vacía.");
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
return objectMapper.readValue(evaluateResult,
|
||||
objectMapper.getTypeFactory().constructCollectionType(List.class, Vacuna.class));
|
||||
}
|
||||
|
||||
public List<Vacuna> obtenerVacunasPorDniYEstado(String dni, String estado) throws GatewayException, IOException {
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
byte[] evaluateResult;
|
||||
|
|
|
|||
Loading…
Reference in a new issue