Image Courtesy Mainpal Hospital
Id: com-sap-icn-blockchain-organTrans9
Version: 2
swagger: "2.0"
info:
description: "Organ Transplant using OpenUI5 and BlockChain"
version: "1.0"
title: "Organ Transplant using OpenUI5 and BlockChain"
consumes:
- application/x-www-form-urlencoded
produces:
- application/json
parameters:
OrganType:
name: OrganType
in: path
description: Type of Organ for Transplant
required: true
type: string
maxLength: 255
BloodGroup:
name: BloodGroup
in: formData
description: Bloodgroup
required: true
type: string
maxLength: 255
Hospital:
name: Hospital
in: formData
description: Hospital Name
required: true
type: string
maxLength: 255
City:
name: City
in: formData
description: City
required: true
type: string
maxLength: 255
State:
name: State
in: formData
description: State
required: true
type: string
maxLength: 255
paths:
/{OrganType}:
get:
operationId: organsByType
summary: Get list of Organs available for Transplant by organ type
parameters:
- $ref: '#/parameters/OrganType'
responses:
200:
description: OK
schema:
type: object
properties:
values:
type: array
items:
type: object
properties:
OrganType:
type: string
Hospital:
type: string
City :
type: string
State:
type: string
400:
description: Mismatch
404:
description: Not Found
post:
operationId: initorgan
summary: Post an organ available for Transplant
parameters:
- $ref: '#/parameters/OrganType'
- $ref: '#/parameters/BloodGroup'
- $ref: '#/parameters/Hospital'
- $ref: '#/parameters/City'
- $ref: '#/parameters/State'
responses:
200:
description: Organ for Transplant updated
201:
description: Organ for Transplant updated
400:
description: Mismatch
409:
description: Organ already posted.
package main
import (
"bytes"
"encoding/json"
"fmt"
"strings"
"time"
"github.com/hyperledger/fabric/core/chaincode/shim"
pb "github.com/hyperledger/fabric/protos/peer"
)
var logger = shim.NewLogger("chaincode")
type OrganDonorChainCode struct {
}
type organ struct {
OrganType string `json:"organType"`
BloodGroup string `json:"BloodGroup"`
Hospital string `json:"Hospital"`
City string `json:"City"`
State string `json:"State"`
}
// start of program
func main() {
err := shim.Start(new(OrganDonorChainCode))
if err != nil {
fmt.Printf("Error Occured: %s", err)
}
}
// Start a chaincode.
func (t *OrganDonorChainCode) Init(stub shim.ChaincodeStubInterface) pb.Response {
return shim.Success(nil)
}
// Decode incoming request and call relevant methods.
func (t *OrganDonorChainCode) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
function, args := stub.GetFunctionAndParameters()
switch function {
// Creating a new organ
case "initorgan":
return t.initorgan(stub, args)
// Get list of organs by type of organ
case "organsByType":
return t.organsByType(stub, args)
default:
return shim.Error("error occured ")
}
}
// FM to add the entry for the available organ
func (t *OrganDonorChainCode) initorgan(stub shim.ChaincodeStubInterface, args []string) pb.Response {
var err error
// key as combination of time and input aruments
organBloodGroup := args[0] + args[1] + args[2] + args[3] + args[4] + time.Now().UTC().String()
organBloodGroup1 := args[1]
Hospital := strings.ToLower(args[2])
State := strings.ToLower(args[4])
City := strings.ToLower(args[3])
// check if by chance any entry exists
organAsBytes, err := stub.GetState(organBloodGroup)
if err != nil {
return shim.Error("error occure " + err.Error())
} else if organAsBytes != nil {
fmt.Println("organ exists " + organBloodGroup)
return shim.Error("organ exists " + organBloodGroup)
}
// create organ as json
OrganType := args[0]
organ := &organ{OrganType, organBloodGroup1, Hospital, City, State}
logger.Errorf("Data %d ", organ)
organJSONasBytes, err := json.Marshal(organ)
if err != nil {
return shim.Error(err.Error())
}
// save organ to blockchain
err = stub.PutState(organBloodGroup, organJSONasBytes)
if err != nil {
return shim.Error(err.Error())
}
return shim.Success(nil)
}
func (t *OrganDonorChainCode) organsByType(stub shim.ChaincodeStubInterface, args []string) pb.Response {
Organ := args[0]
queryString := fmt.Sprintf("{\"selector\":{\"organType\":\"%s\"}}", Organ)
logger.Errorf("QueryString %d ", queryString)
queryResults, err := getQueryResultForQueryString(stub, queryString)
if err != nil {
return shim.Error(err.Error())
}
return shim.Success(queryResults)
}
// =========================================================================================
// getQueryResultForQueryString executes the passed in query string.
// Result set is built and returned as a byte array containing the JSON results.
// =========================================================================================
func getQueryResultForQueryString(stub shim.ChaincodeStubInterface, queryString string) ([]byte, error) {
fmt.Printf("- getQueryResultForQueryString queryString:\n%s\n", queryString)
resultsIterator, err := stub.GetQueryResult(queryString)
if err != nil {
return nil, err
}
defer resultsIterator.Close()
// buffer is a JSON array containing QueryRecords
var buffer bytes.Buffer
buffer.WriteString("[")
bArrayMemberAlreadyWritten := false
for resultsIterator.HasNext() {
queryResponse, err := resultsIterator.Next()
if err != nil {
return nil, err
}
// Add a comma before array members, suppress it for the first array member
if bArrayMemberAlreadyWritten == true {
buffer.WriteString(",")
}
buffer.WriteString("{\"Key\":")
buffer.WriteString("\"")
buffer.WriteString(queryResponse.Key)
buffer.WriteString("\"")
buffer.WriteString(", \"Record\":")
// Record is a JSON object, so we write as-is
buffer.WriteString(string(queryResponse.Value))
buffer.WriteString("}")
bArrayMemberAlreadyWritten = true
}
buffer.WriteString("]")
fmt.Printf("- getQueryResultForQueryString queryResult:\n%s\n", buffer.String())
return buffer.Bytes(), nil
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
5 | |
5 | |
5 | |
5 | |
4 | |
4 | |
4 | |
4 | |
3 | |
3 |