1.How to Store multiple assests for participant in hyperledger composer - hyperledger

Hi iam new to Blockchain .. please help with this.
1.I have customer participant should able to store multiple assests.
2. How to query all products of a customer
namespace demo
participant Customer identified by customerId {
o String customerId
o String firstName
o String lastName
o String email
o String phone
o Address address optional
--> Product[] product optional // here i need to store multiple products that belongs to this customer
}
asset Product identified by productId{
o String productId
o productType producttype
o String productName
o String model
--> Customer owner optional
}

1-i think in your business case there is no meaning to add owner to each product because you already have array of assets(Products) which belongs to each Customer.
1.1-to save multiple assets in your product array this will be done in Transaction Processing Function in logic.js file
in the documentation link below you will find very useful information about how to implement it:
https://hyperledger.github.io/composer/latest/reference/js_scripts
1.2-as solution for query you are going to query this participant registry by the customerId and in the results you will find your array of products. as the following.
in the queries.qry file
query CurrentCustomer {
description: "Returns Certain Customer Details in This Registry"
statement:
SELECT demo.Customer
WHERE (customerId == _$customerId)
}
for better understanding about queries you can check the documentation link below:
https://hyperledger.github.io/composer/latest/tutorials/queries
2-the question here are you going to add new products to this array each time the customer will buy Product ?
so imagine your customer have bought 10,000 products and with multiple Customer with this rate your application performance will not be the best because retrieving this much of data and mutate it then update your participant(Customer).
you can instead build relation between customer and invoice and each invoice contains enum of Product will be represented as concept which will contains all product properties and in this case each invoice will have owner so you can query all invoices for certain customer.

Related

Hyperledger Composer query not working

I'm developing an application using Hyperledger composer and its related part of the model file as bellows.
abstract participant Stakeholder {
o String name
o Address address
o String email optional
o String telephone optional
o Certification certification optional
o String[] images optional
o Company company
o String username
o String password
}
participant Farmer identified by stakeholderId extends Stakeholder {
o String stakeholderId
o String description optional
--> Farm[] farms
}
I'm trying to retrieve specific farmers by their username using below query.
query getUserFromUsername{
description:"get user from username"
statement:
SELECT org.ucsc.agriblockchain.Stakeholder
WHERE (username == _$username)
}
But it does not work as expected. Here, since Farmer is not the only stakeholder in the system, the Stakeholder abstract participant is used.
Any suggestions?
You can move the identified by stakeholderId to the abstract participation as mentioned below.
abstract participant Stakeholder identified by stakeholderId{
o String stakeholderId
o String name
o Address address
o String email optional
o String telephone optional
o Certification certification optional
o String[] images optional
o Company company
o String username
o String password
}
participant Farmer extends Stakeholder {
o String description optional
--> Farm[] farms
}
To query for getUserFromUsername
query getUserFromUsername{
description:"get user from username"
statement:
SELECT org.ucsc.agriblockchain.Farmer
WHERE (username == _$username)
}
Because it is an abstract type, there is no data for the "Stakeholder" registry. You need to query the Farmer registry ... SELECT org.ucsc.agriblockchain.Farmer ...
Changing Stakeholkder from abstract to concrete won't help either if Farmer extends it because the Farmer will still be part of the Farmer registry.
I'm not sure exactly what you are wanting to achieve from the model, but you could revert to a single Participant type with optional fields for the different Stakeholder types, otherwise just write separate queries for the different Participant Types, and collate the results yourself in your code.
Update Following Comment
That Query against the Farmer registry should work.
Couple of hints ...
If you have changed the model or the Query, remember to stop and restart the REST server so that it can Discover the changes.
If you have changed the model, create some new test data.
If you are testing with the REST server, when you enter the parameter for the username, do not use quotes round the value.
(If you still have problems, please post the whole model, the latest query, and some example data that you are using in JSON format.)

Ecommerce (Spree) print count of products with completed orders on Index page. (postgres)

On our product gallery page, I'd really like to query the database to find how many of each product has been purchased, and print that number on the page (for admin users only). I feel like I have all the pieces, I just don't know how to put it together to work.
There is a Products table that is referenced to print out all the products.
There is an LineItems table (Spree::LineItems >> Spree::Orders) where the product can be called/identified by:
#line_items.each do |line_item|
line_item.product.id
end
& this is what defines an order as complete in relation to that line_item:
#line_item.order.state == "complete"
So...I'd like to see if #product.id and #line_item.product.id match (where the #line_item.order.state == "complete") and count how many.
Basically, we iterate each product on the gallery page, so I want to see for each product how many times it appears as a line_item in an order where the order.state is complete
I'm no engineer (as you can obviously tell), but I feel like I'm making this more complicated than it needs to be. Help?
We're using a Postgres database. Thanks in advance!!
That should give you a count of line items associated at the same time with a product with the given id and an order with 'complete' state.
LineItem.joins(:product, :order).where(orders: { state: 'complete' }).where(products: { id: your_product_id }).count
No need to iterate! Just a database query (using ActiveRecord), which also is extremely more efficient than iteration.
I assumed your tables are called orders, products (used in the where clauses) and the associations on the LineItem ActiveRecord object are called order, product (used in the join). Otherwise simply replace those values with the appropriate ones.

In Neo4J, how to retrieve nodes, along with additional information about if they are connected or not to a specific node

I have nodes with Product Information (P). Every time a user likes the product, relationship [L] is created connecting user (U)->[L]->[P]
Now I need to retrieve a set of Product nodes based on specific condition, but also need to return additional information that whether they are liked by a specific user or not.
So if the Product Structure is like
{ Product Name, Price }
If lets say, 1 out of the 3 products in question is liked by user X, the result set I need may look something like this
[{Product1, 29.00, true}, {Product2, 39.00, false}, {Product3, 25.00, false}]
Here true refers to the fact that user has liked Product1 and has not liked Product2.
I am not sure how to write such a query that includes this additional information of whether the returned nodes are liked or not
I think something like this will suit your needs.
Match all the products. You will want to scope this match down in some way.
Optionally match user likes per product.
Return collection of maps that contain name, price and like status.
match (p:Product)
optional match (u:User)-[:LIKES]->p
with {product:p.name, price:p.price, like: case when u is null then false else true end} as Product_Detail
return collect(Product_Detail)

GORM: Find by collection contents

I Have the following two classes:
class Person{
String name
static hasMany = [
items: Item
]
}
class Item{
String name
}
Multiple Persons can also have the same Item. I'm trying to get a list of all Persons that have a specific item in their collection. I.E. Both Person A and B have Item A in their list so return them both. Unfortunately their is no findAllByCollectionContains() the closest is is findAllByCollection() which requires an exact set.
I've been trying executeQuery to give me a but more control and haven't come up with anything yet.
Example of what I have tried:
Person.executeQuery("select name from Person p where ? in p.items",[item])
Any suggestions?
You have to join the items collection, then you can query on it easily
Person.executeQuery("select name from Person p join p.items as i where i = ?",[item])

Find most recent donation excluding funds beginning with X

I'm trying to grab the gift date of the most recent donation (in $USD) to my organization that has a fund ID not beginning in X (those are Canadian donations). Here's the code (in the controller) that I've used to pull out the gift date of the most recent overall donation, but it's a Canadian gift (fund_id = XP05). How do I get the most recent donation with a fund ID not beginning with an X?
#income_batch = Gift.between(Date.today - 2.weeks, Date.today, :field => :gift_date).order('gift_date DESC').first.gift_date
Assuming fund_id is a string field within your gifts table, you can use not like:
Gift.where("fund_id not like 'X%").between....
You should also avoid using an _id postfix on fields unless they're foreign keys, as Rails convention dictates that _id is specific for this purpose.
If fund_id is a foreign key, and you have a belongs_to :fund inside your Gift model, you can use joins and not like:
Gift.joins(:fund).where("funds.name not like 'X%").between....
This assumes that the field within the funds table is called name.

Resources