mirror of
https://github.com/hyperledger/fabric-samples.git
synced 2026-06-22 09:35:10 +00:00
cambios para el paginado, ahora funciona
This commit is contained in:
parent
bcd903c14a
commit
15fce03a94
3 changed files with 62 additions and 19 deletions
|
|
@ -33,7 +33,7 @@ type Receta struct {
|
||||||
Practitioner string `json:"practitioner"`
|
Practitioner string `json:"practitioner"`
|
||||||
PractitionerDocumentNumber string `json:"practitionerDocumentNumber"`
|
PractitionerDocumentNumber string `json:"practitionerDocumentNumber"`
|
||||||
Signature string `json:"signature"`
|
Signature string `json:"signature"`
|
||||||
Matricula string `json:"matricula"`
|
Matricula string `json:"matricula"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Vacuna struct {
|
type Vacuna struct {
|
||||||
|
|
@ -51,10 +51,11 @@ type Vacuna struct {
|
||||||
Reactions string `json:"reactions"` // puede ser un string o una estructura si querés después
|
Reactions string `json:"reactions"` // puede ser un string o una estructura si querés después
|
||||||
Practitioner string `json:"practitioner"`
|
Practitioner string `json:"practitioner"`
|
||||||
PractitionerDocumentNumber string `json:"practitionerDocumentNumber"`
|
PractitionerDocumentNumber string `json:"practitionerDocumentNumber"`
|
||||||
Matricula string `json:"matricula"`
|
Matricula string `json:"matricula"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Estado string
|
type Estado string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
EstadoDraft Estado = "draft"
|
EstadoDraft Estado = "draft"
|
||||||
EstadoActive Estado = "active"
|
EstadoActive Estado = "active"
|
||||||
|
|
@ -361,36 +362,73 @@ func (s *SmartContract) GetMultipleRecetas(ctx contractapi.TransactionContextInt
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: adaptar los campos para que se tengan un identificar de usuarios ademas del DNI
|
// 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) {
|
type ResultadoPaginado struct {
|
||||||
|
Recetas []*Receta `json:"recetas"`
|
||||||
|
Bookmark string `json:"bookmark"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *SmartContract) GetRecetasPorDniYEstadoPaginado(
|
||||||
|
ctx contractapi.TransactionContextInterface,
|
||||||
|
dni string,
|
||||||
|
estado string,
|
||||||
|
pageSize int32,
|
||||||
|
bookmark string,
|
||||||
|
) (*ResultadoPaginado, error) {
|
||||||
|
|
||||||
if dni == "" || estado == "" {
|
if dni == "" || estado == "" {
|
||||||
return nil, fmt.Errorf("el dni y el estado son obligatorios")
|
return nil, fmt.Errorf("el dni y el estado son obligatorios")
|
||||||
}
|
}
|
||||||
|
|
||||||
resultsIterator, err := ctx.GetStub().GetStateByRange("", "")
|
// Creamos la query para CouchDB
|
||||||
|
query := map[string]interface{}{
|
||||||
|
"selector": map[string]interface{}{
|
||||||
|
"patientDocumentNumber": dni,
|
||||||
|
"status": estado,
|
||||||
|
},
|
||||||
|
"limit": pageSize,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Si hay bookmark, lo agregamos
|
||||||
|
if bookmark != "" {
|
||||||
|
query["bookmark"] = bookmark
|
||||||
|
}
|
||||||
|
|
||||||
|
queryBytes, err := json.Marshal(query)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error al obtener datos del ledger: %v", err)
|
return nil, fmt.Errorf("error al generar la query: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
resultsIterator, metadata, err := ctx.GetStub().GetQueryResultWithPagination(string(queryBytes), pageSize, bookmark)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error al ejecutar la query: %v", err)
|
||||||
}
|
}
|
||||||
defer resultsIterator.Close()
|
defer resultsIterator.Close()
|
||||||
|
|
||||||
var recetasFiltradas []*Receta
|
var recetas []*Receta
|
||||||
for resultsIterator.HasNext() {
|
for resultsIterator.HasNext() {
|
||||||
queryResponse, err := resultsIterator.Next()
|
response, iterErr := resultsIterator.Next()
|
||||||
if err != nil {
|
if iterErr != nil {
|
||||||
return nil, err
|
return nil, iterErr
|
||||||
}
|
}
|
||||||
|
|
||||||
var receta Receta
|
var receta Receta
|
||||||
err = json.Unmarshal(queryResponse.Value, &receta)
|
if err := json.Unmarshal(response.Value, &receta); err != nil {
|
||||||
if err != nil {
|
return nil, fmt.Errorf("error al parsear receta: %v", err)
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if receta.PatientDocumentNumber == dni && receta.Status == estado {
|
recetas = append(recetas, &receta)
|
||||||
recetasFiltradas = append(recetasFiltradas, &receta)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return recetasFiltradas, nil
|
if recetas == nil {
|
||||||
|
recetas = []*Receta{}
|
||||||
|
}
|
||||||
|
|
||||||
|
resultado := &ResultadoPaginado{
|
||||||
|
Recetas: recetas,
|
||||||
|
Bookmark: metadata.Bookmark,
|
||||||
|
}
|
||||||
|
|
||||||
|
return resultado, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SmartContract) CreateVacuna(ctx contractapi.TransactionContextInterface, vacuna Vacuna) error {
|
func (s *SmartContract) CreateVacuna(ctx contractapi.TransactionContextInterface, vacuna Vacuna) error {
|
||||||
|
|
|
||||||
|
|
@ -197,6 +197,11 @@ public class RecetaController {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping("/todasTest")
|
||||||
|
public List<Receta> obtenerTodasTest() throws Exception {
|
||||||
|
return recetaService.obtenerTodasLasRecetas();
|
||||||
|
}
|
||||||
|
|
||||||
@PostMapping("/borrar")
|
@PostMapping("/borrar")
|
||||||
public ResponseEntity<RecetaDto> delete(@RequestBody Map<String, String> requestBody) {
|
public ResponseEntity<RecetaDto> delete(@RequestBody Map<String, String> requestBody) {
|
||||||
try {
|
try {
|
||||||
|
|
|
||||||
|
|
@ -153,18 +153,18 @@ public class RecetaService {
|
||||||
String dni, String estado, int pageSize, String bookmark) throws Exception {
|
String dni, String estado, int pageSize, String bookmark) throws Exception {
|
||||||
|
|
||||||
byte[] result = contract.evaluateTransaction(
|
byte[] result = contract.evaluateTransaction(
|
||||||
"GetRecetasPorDniYEstado",
|
"GetRecetasPorDniYEstadoPaginado",
|
||||||
dni,
|
dni,
|
||||||
estado,
|
estado,
|
||||||
String.valueOf(pageSize),
|
String.valueOf(pageSize),
|
||||||
bookmark);
|
bookmark);
|
||||||
|
|
||||||
|
System.out.println("Respuesta del chaincode (JSON): " + new String(result));
|
||||||
ObjectMapper mapper = new ObjectMapper();
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
JavaType tipo = mapper.getTypeFactory()
|
JavaType tipo = mapper.getTypeFactory()
|
||||||
.constructParametricType(ResultadoPaginado.class, RecetaDto.class);
|
.constructParametricType(ResultadoPaginado.class, RecetaDto.class);
|
||||||
ResultadoPaginado<RecetaDto> resultado = mapper.readValue(json, tipo);
|
|
||||||
|
|
||||||
return mapper.readValue(result, type);
|
return mapper.readValue(result, tipo);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue