se agrega el endpoint para obtener recetas por DNI y estado

This commit is contained in:
luc662 2025-04-05 21:04:00 +00:00
parent 866a2ecb03
commit a70ef3b1cd
25 changed files with 148 additions and 8 deletions

View file

@ -0,0 +1,3 @@
.idea/
.gradle/
bin/

View file

@ -13,6 +13,7 @@ type SmartContract struct {
type Receta struct {
ID string `json:"id"`
Identificador string `json:"identificador"`
Owner string `json:"owner"`
PrescripcionAnteriorId string `json:"prescripcionAnteriorId"`
Status string `json:"status"`
@ -34,6 +35,7 @@ func (s *SmartContract) InitLedger(ctx contractapi.TransactionContextInterface)
recetas := []Receta{
{
ID: "receta1",
Identificador "rece1234"
Owner: "Tomoko",
PrescripcionAnteriorId: "presc123",
Status: "active",
@ -52,6 +54,7 @@ func (s *SmartContract) InitLedger(ctx contractapi.TransactionContextInterface)
},
{
ID: "receta2",
Identificador "rece1235"
Owner: "Alice",
PrescripcionAnteriorId: "presc456",
Status: "completed",
@ -224,3 +227,35 @@ func (s *SmartContract) GetMultipleRecetas(ctx contractapi.TransactionContextInt
}
return recetas, nil
}
// TODO: adaptar los campos para que se tengan un identificar de usuarios ademas del DNI
func (s *SmartContract) GetRecetasPorDniYEstado(ctx contractapi.TransactionContextInterface, dni string, estado string) ([]*Receta, error) {
if dni == "" || estado == "" {
return nil, fmt.Errorf("el dni y el estado son obligatorios")
}
resultsIterator, err := ctx.GetStub().GetStateByRange("", "")
if err != nil {
return nil, fmt.Errorf("error al obtener datos del ledger: %v", err)
}
defer resultsIterator.Close()
var recetasFiltradas []*Receta
for resultsIterator.HasNext() {
queryResponse, err := resultsIterator.Next()
if err != nil {
return nil, err
}
var receta Receta
err = json.Unmarshal(queryResponse.Value, &receta)
if err != nil {
return nil, err
}
if receta.DniPaciente == dni && receta.Status == estado {
recetasFiltradas = append(recetasFiltradas, &receta)
}
}
return recetasFiltradas, nil
}

View file

@ -1,2 +1,3 @@
.idea/
.gradle/
bin/

View file

@ -1,9 +1,10 @@
package com.code.hyperledger;
import com.code.hyperledger.coso.Receta;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.code.hyperledger.models.Receta;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParser;

View file

@ -1,9 +1,10 @@
package com.code.hyperledger.controllers;
import com.code.hyperledger.coso.Receta;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.code.hyperledger.models.Receta;
@RestController
public class Example {

View file

@ -1,9 +1,9 @@
package com.code.hyperledger.controllers;
import com.code.hyperledger.coso.Receta;
import com.code.hyperledger.coso.RecetaDto;
import com.code.hyperledger.coso.AssetIdDto;
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.services.RecetaService;
import org.hyperledger.fabric.client.*;
import org.springframework.beans.factory.annotation.Autowired;
@ -85,6 +85,7 @@ public class RecetaController {
private RecetaDto mapToDto(Receta receta) {
RecetaDto dto = new RecetaDto();
dto.setIdentifier(receta.getIdentifier());
dto.setOwner(receta.getOwner());
dto.setPrescripcionAnteriorId(receta.getPrescripcionAnteriorId());
dto.setStatus(receta.getStatus());

View file

@ -1,4 +1,4 @@
package com.code.hyperledger.coso;
package com.code.hyperledger.models;
import lombok.AllArgsConstructor;
import lombok.Data;

View file

@ -1,4 +1,4 @@
package com.code.hyperledger.coso;
package com.code.hyperledger.models;
import lombok.AllArgsConstructor;
import lombok.Data;
@ -9,6 +9,7 @@ import lombok.NoArgsConstructor;
@NoArgsConstructor
public class Receta {
private String id;
private String identifier;
private String owner;
private String prescripcionAnteriorId;
private String status;

View file

@ -1,4 +1,4 @@
package com.code.hyperledger.coso;
package com.code.hyperledger.models;
import lombok.AllArgsConstructor;
import lombok.Data;
@ -8,6 +8,7 @@ import lombok.NoArgsConstructor;
@AllArgsConstructor
@NoArgsConstructor
public class RecetaDto {
private String identifier;
private String owner;
private String prescripcionAnteriorId;
private String status;

View file

@ -0,0 +1,14 @@
package com.code.hyperledger.models;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class AssetIdDto {
private String Dni;
private String TimeStamp;
}

View file

@ -0,0 +1,31 @@
package com.code.hyperledger.models;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Receta {
private String id;
private String identifier;
private String owner;
private String prescripcionAnteriorId;
private String status;
//@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss")
private String statusChange;
private String prioridad;
private String medicacion;
private String razon;
private String notas;
private String periodoDeTratamiento;
private String instruccionesTratamiento;
private String periodoDeValidez;
private String dniPaciente;
//@JsonFormat(pattern = "yyyy-MM-dd")
private String fechaDeAutorizacion;
private int cantidad;
//@JsonFormat(pattern = "yyyy-MM-dd")
private String expectedSupplyDuration;
}

View file

@ -0,0 +1,30 @@
package com.code.hyperledger.models;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class RecetaDto {
private String identifier;
private String owner;
private String prescripcionAnteriorId;
private String status;
//@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss")
private String statusChange;
private String prioridad;
private String medicacion;
private String razon;
private String notas;
private String periodoDeTratamiento;
private String instruccionesTratamiento;
private String periodoDeValidez;
private String dniPaciente;
//@JsonFormat(pattern = "yyyy-MM-dd")
private String fechaDeAutorizacion;
private int cantidad;
//@JsonFormat(pattern = "yyyy-MM-dd")
private String expectedSupplyDuration;
}

View file

@ -141,3 +141,15 @@ public class RecetaService {
objectMapper.getTypeFactory().constructCollectionType(List.class, Receta.class));
}
}
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");
}
var evaluateResult = contract.evaluateTransaction("GetRecetasPorDniYEstado", dni, estado);
ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.readValue(evaluateResult,
objectMapper.getTypeFactory().constructCollectionType(List.class, Receta.class));
}

View file

@ -0,0 +1,3 @@
.idea/
.gradle/
bin/

View file

@ -0,0 +1,3 @@
.idea/
.gradle/
bin/

View file

@ -0,0 +1,3 @@
.idea/
.gradle/
bin/