txdefs

package
v0.0.0-...-9de20ef Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 7, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CreateNewLibrary = tx.Transaction{
	Tag:         "createNewLibrary",
	Label:       "Create New Library",
	Description: "Create a New Library",
	Method:      "POST",
	Callers: []accesscontrol.Caller{
		{
			MSP: "org3MSP",
			OU:  "admin",
		},
		{
			MSP: "orgMSP",
			OU:  "admin",
		},
	},

	Args: []tx.Argument{
		{
			Tag:         "name",
			Label:       "Name",
			Description: "Name of the library",
			DataType:    "string",
			Required:    true,
		},
	},
	Routine: func(stub *sw.StubWrapper, req map[string]interface{}) ([]byte, errors.ICCError) {
		name, _ := req["name"].(string)

		libraryMap := make(map[string]interface{})
		libraryMap["@assetType"] = "library"
		libraryMap["name"] = name

		libraryAsset, err := assets.NewAsset(libraryMap)
		if err != nil {
			return nil, errors.WrapError(err, "Failed to create a new asset")
		}

		_, err = libraryAsset.PutNew(stub)
		if err != nil {
			return nil, errors.WrapErrorWithStatus(err, "Error saving asset on blockchain", err.Status())
		}

		libraryJSON, nerr := json.Marshal(libraryAsset)
		if nerr != nil {
			return nil, errors.WrapError(nil, "failed to encode asset to JSON format")
		}

		logMsg, ok := json.Marshal(fmt.Sprintf("New library name: %s", name))
		if ok != nil {
			return nil, errors.WrapError(nil, "failed to encode asset to JSON format")
		}

		events.CallEvent(stub, "createLibraryLog", logMsg)

		return libraryJSON, nil
	},
}

Create a new Library on channel POST Method

View Source
var GetBooksByAuthor = tx.Transaction{
	Tag:         "getBooksByAuthor",
	Label:       "Get Books by the Author Name",
	Description: "Return all the books from an author",
	Method:      "GET",
	Callers: []accesscontrol.Caller{
		{MSP: "org1MSP"},
		{MSP: "org2MSP"},
		{MSP: "orgMSP"},
	},

	Args: []tx.Argument{
		{
			Tag:         "authorName",
			Label:       "Author Name",
			Description: "Author Name",
			DataType:    "string",
			Required:    true,
		},
		{
			Tag:         "limit",
			Label:       "Limit",
			Description: "Limit",
			DataType:    "number",
		},
	},
	Routine: func(stub *sw.StubWrapper, req map[string]interface{}) ([]byte, errors.ICCError) {
		authorName, _ := req["authorName"].(string)
		limit, hasLimit := req["limit"].(float64)

		if hasLimit && limit <= 0 {
			return nil, errors.NewCCError("limit must be greater than 0", 400)
		}

		query := map[string]interface{}{
			"selector": map[string]interface{}{
				"@assetType": "book",
				"author":     authorName,
			},
		}

		if hasLimit {
			query["limit"] = limit
		}

		var err error
		response, err := assets.Search(stub, query, "", true)
		if err != nil {
			return nil, errors.WrapErrorWithStatus(err, "error searching for book's author", 500)
		}

		responseJSON, err := json.Marshal(response)
		if err != nil {
			return nil, errors.WrapErrorWithStatus(err, "error marshaling response", 500)
		}

		return responseJSON, nil
	},
}

Return the all books from an specific author GET method

View Source
var GetNumberOfBooksFromLibrary = tx.Transaction{
	Tag:         "getNumberOfBooksFromLibrary",
	Label:       "Get Number Of Books From Library",
	Description: "Return the number of books of a library",
	Method:      "GET",
	Callers: []accesscontrol.Caller{
		{MSP: "org2MSP"},
		{MSP: "orgMSP"},
	},

	Args: []tx.Argument{
		{
			Tag:         "library",
			Label:       "Library",
			Description: "Library",
			DataType:    "->library",
			Required:    true,
		},
	},
	Routine: func(stub *sw.StubWrapper, req map[string]interface{}) ([]byte, errors.ICCError) {
		libraryKey, _ := req["library"].(assets.Key)

		libraryMap, err := libraryKey.GetMap(stub)
		if err != nil {
			return nil, errors.WrapErrorWithStatus(err, "failed to get asset from the ledger", err.Status())
		}

		numberOfBooks := 0
		books, ok := libraryMap["books"].([]interface{})
		if ok {
			numberOfBooks = len(books)
		}

		returnMap := make(map[string]interface{})
		returnMap["numberOfBooks"] = numberOfBooks

		returnJSON, nerr := json.Marshal(returnMap)
		if nerr != nil {
			return nil, errors.WrapError(err, "failed to marshal response")
		}

		return returnJSON, nil
	},
}

Return the number of books of a library GET method

View Source
var UpdateBookTenant = tx.Transaction{
	Tag:         "updateBookTenant",
	Label:       "Update Book Tenant",
	Description: "Change the tenant of a book",
	Method:      "PUT",
	Callers: []accesscontrol.Caller{
		{MSP: `$org\dMSP`},
		{MSP: "orgMSP"},
	},

	Args: []tx.Argument{
		{
			Tag:         "book",
			Label:       "Book",
			Description: "Book",
			DataType:    "->book",
			Required:    true,
		},
		{
			Tag:         "tenant",
			Label:       "tenant",
			Description: "New tenant of the book",
			DataType:    "->person",
		},
	},
	Routine: func(stub *sw.StubWrapper, req map[string]interface{}) ([]byte, errors.ICCError) {
		bookKey, ok := req["book"].(assets.Key)
		if !ok {
			return nil, errors.WrapError(nil, "Parameter book must be an asset")
		}
		tenantKey, ok := req["tenant"].(assets.Key)
		if !ok {
			return nil, errors.WrapError(nil, "Parameter tenant must be an asset")
		}

		bookAsset, err := bookKey.Get(stub)
		if err != nil {
			return nil, errors.WrapErrorWithStatus(err, "failed to get asset from the ledger", err.Status())
		}
		bookMap := (map[string]interface{})(*bookAsset)

		tenantAsset, err := tenantKey.Get(stub)
		if err != nil {
			return nil, errors.WrapErrorWithStatus(err, "failed to get asset from the ledger", err.Status())
		}
		tenantMap := (map[string]interface{})(*tenantAsset)

		updatedTenantKey := make(map[string]interface{})
		updatedTenantKey["@assetType"] = "person"
		updatedTenantKey["@key"] = tenantMap["@key"]

		bookMap["currentTenant"] = updatedTenantKey

		bookMap, err = bookAsset.Update(stub, bookMap)
		if err != nil {
			return nil, errors.WrapError(err, "failed to update asset")
		}

		bookJSON, nerr := json.Marshal(bookMap)
		if nerr != nil {
			return nil, errors.WrapError(err, "failed to marshal response")
		}

		return bookJSON, nil
	},
}

Updates the tenant of a Book POST Method

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL