se agrego la visualizacion de recetas y se arreglo la carga de recetas

This commit is contained in:
lucas bonfil 2024-08-24 14:45:05 -03:00
parent 1a302e2240
commit 79da291bd1
4 changed files with 40 additions and 55 deletions

View file

@ -9,8 +9,6 @@ public class Example {
@GetMapping("/hello")
public String sayHello() {
Receta receta = new Receta();
return "Hello, World!";
}
}

View file

@ -2,14 +2,16 @@ package com.code.hyperledger.controllers;
import com.code.hyperledger.coso.Receta;
import com.code.hyperledger.services.RecetaService;
import org.hyperledger.fabric.client.*;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.io.IOException;
import java.time.Instant;
@RestController
@ -19,21 +21,29 @@ public class RecetaController {
@Autowired
private RecetaService recetaService;
//usar este endpoint
@PostMapping("/crear")
public String crear() {
public ResponseEntity<Receta> crear(Receta receta) {
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")
public String sayHello() {
com.code.hyperledger.coso.Receta receta = new com.code.hyperledger.coso.Receta();
return "Hello, World!";
@PostMapping("/obtener")
public ResponseEntity<Receta> find(String id) {
try {
return new ResponseEntity<>(recetaService.obtenerReceta(id), HttpStatus.OK);
} catch (IOException | GatewayException e) {
e.printStackTrace();
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
}

View file

@ -1,12 +1,12 @@
package com.code.hyperledger.coso;
import java.time.LocalDate;
import java.time.LocalDateTime;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDate;
import java.time.LocalDateTime;
@Data
@AllArgsConstructor
@NoArgsConstructor
@ -27,5 +27,4 @@ public class Receta {
private LocalDate fechaDeAutorizacion;
private int cantidad;
private LocalDate expectedSupplyDuration;
}

View file

@ -1,7 +1,7 @@
package com.code.hyperledger.services;
import com.code.hyperledger.App;
import com.code.hyperledger.coso.Receta;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import io.grpc.Grpc;
@ -14,6 +14,7 @@ import org.springframework.stereotype.Service;
import javax.annotation.PostConstruct;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
@ -45,7 +46,7 @@ public class RecetaService {
private static final String OVERRIDE_AUTH = "peer0.org1.example.com";
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();
@ -71,9 +72,10 @@ public class RecetaService {
try (var gateway = builder.connect()) {
this.setContract(gateway);
} finally {
this.initLedger();
} /*finally {
channel.shutdownNow().awaitTermination(5, TimeUnit.SECONDS);
}
}*/
}
@ -117,29 +119,9 @@ public class RecetaService {
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");
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(
"CreateAsset",
receta.getId(),
@ -159,19 +141,15 @@ public class RecetaService {
Integer.toString(receta.getCantidad()),
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");
}
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);
}
}