mirror of
https://github.com/hyperledger/fabric-samples.git
synced 2026-06-21 17:15:10 +00:00
se agrego la visualizacion de recetas y se arreglo la carga de recetas
This commit is contained in:
parent
1a302e2240
commit
79da291bd1
4 changed files with 40 additions and 55 deletions
|
|
@ -9,8 +9,6 @@ public class Example {
|
||||||
|
|
||||||
@GetMapping("/hello")
|
@GetMapping("/hello")
|
||||||
public String sayHello() {
|
public String sayHello() {
|
||||||
Receta receta = new Receta();
|
|
||||||
|
|
||||||
return "Hello, World!";
|
return "Hello, World!";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,14 +2,16 @@ package com.code.hyperledger.controllers;
|
||||||
|
|
||||||
import com.code.hyperledger.coso.Receta;
|
import com.code.hyperledger.coso.Receta;
|
||||||
import com.code.hyperledger.services.RecetaService;
|
import com.code.hyperledger.services.RecetaService;
|
||||||
|
import org.hyperledger.fabric.client.*;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
import java.time.LocalDate;
|
import java.io.IOException;
|
||||||
import java.time.LocalDateTime;
|
import java.time.Instant;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
|
|
||||||
|
|
@ -19,21 +21,29 @@ public class RecetaController {
|
||||||
@Autowired
|
@Autowired
|
||||||
private RecetaService recetaService;
|
private RecetaService recetaService;
|
||||||
|
|
||||||
//usar este endpoint
|
|
||||||
@PostMapping("/crear")
|
@PostMapping("/crear")
|
||||||
public String crear() {
|
public ResponseEntity<Receta> crear(Receta receta) {
|
||||||
System.out.println("\n--> Submit Transaction: CreateAsset, creates new asset with all arguments");
|
System.out.println("\n--> Submit Transaction: CreateAsset, creates new asset with all arguments");
|
||||||
recetaService.crearReceta();
|
|
||||||
|
|
||||||
return "Hello, World!";
|
String assetId = "asset" + Instant.now().toEpochMilli();
|
||||||
|
receta.setId(assetId);
|
||||||
|
try {
|
||||||
|
recetaService.cargarReceta(receta);
|
||||||
|
} catch (CommitStatusException | EndorseException | CommitException | SubmitException e) {
|
||||||
|
return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return new ResponseEntity<>(receta, HttpStatus.OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/obtener")
|
@PostMapping("/obtener")
|
||||||
public String sayHello() {
|
public ResponseEntity<Receta> find(String id) {
|
||||||
com.code.hyperledger.coso.Receta receta = new com.code.hyperledger.coso.Receta();
|
try {
|
||||||
|
return new ResponseEntity<>(recetaService.obtenerReceta(id), HttpStatus.OK);
|
||||||
return "Hello, World!";
|
} catch (IOException | GatewayException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,12 @@
|
||||||
package com.code.hyperledger.coso;
|
package com.code.hyperledger.coso;
|
||||||
|
|
||||||
import java.time.LocalDate;
|
|
||||||
import java.time.LocalDateTime;
|
|
||||||
|
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
@AllArgsConstructor
|
@AllArgsConstructor
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
|
|
@ -27,5 +27,4 @@ public class Receta {
|
||||||
private LocalDate fechaDeAutorizacion;
|
private LocalDate fechaDeAutorizacion;
|
||||||
private int cantidad;
|
private int cantidad;
|
||||||
private LocalDate expectedSupplyDuration;
|
private LocalDate expectedSupplyDuration;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
package com.code.hyperledger.services;
|
package com.code.hyperledger.services;
|
||||||
|
|
||||||
import com.code.hyperledger.App;
|
|
||||||
import com.code.hyperledger.coso.Receta;
|
import com.code.hyperledger.coso.Receta;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.GsonBuilder;
|
import com.google.gson.GsonBuilder;
|
||||||
import io.grpc.Grpc;
|
import io.grpc.Grpc;
|
||||||
|
|
@ -14,6 +14,7 @@ import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import javax.annotation.PostConstruct;
|
import javax.annotation.PostConstruct;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
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;
|
||||||
|
|
@ -45,7 +46,7 @@ public class RecetaService {
|
||||||
private static final String OVERRIDE_AUTH = "peer0.org1.example.com";
|
private static final String OVERRIDE_AUTH = "peer0.org1.example.com";
|
||||||
|
|
||||||
private Contract contract;
|
private Contract contract;
|
||||||
private final String assetId = "asset" + Instant.now().toEpochMilli();
|
//private final String assetId = "asset" + Instant.now().toEpochMilli();
|
||||||
private final Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
private final Gson gson = new GsonBuilder().setPrettyPrinting().create();
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -71,9 +72,10 @@ public class RecetaService {
|
||||||
|
|
||||||
try (var gateway = builder.connect()) {
|
try (var gateway = builder.connect()) {
|
||||||
this.setContract(gateway);
|
this.setContract(gateway);
|
||||||
} finally {
|
this.initLedger();
|
||||||
|
} /*finally {
|
||||||
channel.shutdownNow().awaitTermination(5, TimeUnit.SECONDS);
|
channel.shutdownNow().awaitTermination(5, TimeUnit.SECONDS);
|
||||||
}
|
}*/
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -117,29 +119,9 @@ public class RecetaService {
|
||||||
System.out.println("*** Transaction committed successfully");
|
System.out.println("*** Transaction committed successfully");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void crearReceta() {
|
public void cargarReceta(Receta receta) throws CommitStatusException, EndorseException, CommitException, SubmitException {
|
||||||
System.out.println("\n--> Submit Transaction: CreateAsset, creates new asset with all arguments");
|
System.out.println("\n--> Submit Transaction: CreateAsset, creates new asset with all arguments");
|
||||||
|
|
||||||
Receta receta = new Receta(
|
|
||||||
assetId,
|
|
||||||
"Tom",
|
|
||||||
"presc456",
|
|
||||||
"active",
|
|
||||||
LocalDateTime.now(),
|
|
||||||
"high",
|
|
||||||
"Medicine XYZ",
|
|
||||||
"Condition ABC",
|
|
||||||
"Take with food",
|
|
||||||
"30 days",
|
|
||||||
"1 pill per day",
|
|
||||||
"1 year",
|
|
||||||
"12345678",
|
|
||||||
LocalDate.now(),
|
|
||||||
30,
|
|
||||||
LocalDate.now()
|
|
||||||
);
|
|
||||||
|
|
||||||
try {
|
|
||||||
contract.submitTransaction(
|
contract.submitTransaction(
|
||||||
"CreateAsset",
|
"CreateAsset",
|
||||||
receta.getId(),
|
receta.getId(),
|
||||||
|
|
@ -159,19 +141,15 @@ public class RecetaService {
|
||||||
Integer.toString(receta.getCantidad()),
|
Integer.toString(receta.getCantidad()),
|
||||||
receta.getExpectedSupplyDuration().toString()
|
receta.getExpectedSupplyDuration().toString()
|
||||||
);
|
);
|
||||||
} catch (EndorseException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
} catch (SubmitException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
} catch (CommitStatusException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
} catch (CommitException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
System.out.println("*** Transaction committed successfully");
|
System.out.println("*** Transaction committed successfully");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Receta obtenerReceta(String assetId) throws GatewayException, IOException {
|
||||||
|
System.out.println("\n--> Evaluate Transaction: ReadAsset, function returns asset attributes");
|
||||||
|
|
||||||
|
var evaluateResult = contract.evaluateTransaction("ReadAsset", assetId);
|
||||||
|
ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
return objectMapper.readValue(evaluateResult, Receta.class);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue