Agrego RecetaDto y AssetIdDto para que devuelva el controller

This commit is contained in:
FrancoPandolfo 2024-08-27 20:41:59 -03:00
parent de9e40a101
commit 1d5d09175f
5 changed files with 110 additions and 6 deletions

View file

@ -0,0 +1,33 @@
package com.code.hyperledger.Utils;
import java.security.NoSuchAlgorithmException;
import java.security.MessageDigest;
public class Hashing {
public static String sha256(String input) {
try {
// Crear una instancia de MessageDigest para SHA-256
MessageDigest digest = MessageDigest.getInstance("SHA-256");
// Calcular el hash
byte[] encodedhash = digest.digest(input.getBytes());
// Convertir el hash a una representación hexadecimal
return bytesToHex(encodedhash);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
private static String bytesToHex(byte[] hash) {
StringBuilder hexString = new StringBuilder();
for (byte b : hash) {
String hex = Integer.toHexString(0xff & b);
if (hex.length() == 1) {
hexString.append('0');
}
hexString.append(hex);
}
return hexString.toString();
}
}

View file

@ -1,6 +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.services.RecetaService;
import org.hyperledger.fabric.client.*;
import org.springframework.beans.factory.annotation.Autowired;
@ -13,6 +16,7 @@ import org.springframework.web.bind.annotation.RestController;
import java.io.IOException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.util.Map;
@RestController
@ -24,11 +28,17 @@ public class RecetaController {
private RecetaService recetaService;
@PostMapping("/crear")
public ResponseEntity<Receta> crear(@RequestBody Receta receta) {
public ResponseEntity<AssetIdDto> crear(@RequestBody Receta receta) {
System.out.println("\n--> Submit Transaction: CreateAsset, creates new asset with all arguments");
String assetId = "asset" + Instant.now().toEpochMilli();
var now = LocalDateTime.now().toString();
var dni = receta.getDniPaciente();
var id = dni + now;
String assetId = Hashing.sha256(id);
receta.setId(assetId);
var assetIdDto = new AssetIdDto();
assetIdDto.setDni(dni);
assetIdDto.setTimeStamp(now);
try {
recetaService.cargarReceta(receta);
} catch (CommitStatusException | EndorseException | CommitException | SubmitException e) {
@ -36,17 +46,35 @@ public class RecetaController {
}
return new ResponseEntity<>(receta, HttpStatus.OK);
return new ResponseEntity<>(assetIdDto, HttpStatus.OK);
}
@PostMapping("/obtener")
public ResponseEntity<Receta> find(@RequestBody Map<String, String> requestBody) {
public ResponseEntity<RecetaDto> find(@RequestBody Map<String, String> requestBody) {
try {
System.out.println("requestbody: " + requestBody);
String id = requestBody.get("id");
System.out.println("id: " + id);
Receta receta = recetaService.obtenerReceta(id);
return new ResponseEntity<>(receta, HttpStatus.OK);
RecetaDto recetaDto = new RecetaDto();
recetaDto.setOwner(receta.getOwner());
recetaDto.setPrescripcionAnteriorId(receta.getPrescripcionAnteriorId());
recetaDto.setStatus(receta.getStatus());
recetaDto.setStatusChange(receta.getStatusChange());
recetaDto.setPrioridad(receta.getPrioridad());
recetaDto.setMedicacion(receta.getMedicacion());
recetaDto.setRazon(receta.getRazon());
recetaDto.setNotas(receta.getNotas());
recetaDto.setPeriodoDeTratamiento(receta.getPeriodoDeTratamiento());
recetaDto.setInstruccionesTratamiento(receta.getInstruccionesTratamiento());
recetaDto.setPeriodoDeValidez(receta.getPeriodoDeValidez());
recetaDto.setDniPaciente(receta.getDniPaciente());
recetaDto.setFechaDeAutorizacion(receta.getFechaDeAutorizacion());
recetaDto.setCantidad(receta.getCantidad());
recetaDto.setExpectedSupplyDuration(receta.getExpectedSupplyDuration());
return new ResponseEntity<>(recetaDto, HttpStatus.OK);
} catch (IOException | GatewayException e) {
e.printStackTrace();
return new ResponseEntity<>(HttpStatus.NOT_ACCEPTABLE);

View file

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

View file

@ -27,4 +27,4 @@ public class Receta {
private int cantidad;
//@JsonFormat(pattern = "yyyy-MM-dd")
private String expectedSupplyDuration;
}
}

View file

@ -0,0 +1,29 @@
package com.code.hyperledger.coso;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class RecetaDto {
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;
}