World-Three-Technologies / Masxaro-Prototype

Other
8 stars 4 forks source link

Backend pull request #5

Closed yaxing closed 13 years ago

yaxing commented 13 years ago

@Jimmy, check userGetAllReceipt function

This function will return receipts in following format:

[ {"basicInfo": {"receipt_id":"1","receipt_time":"2011-06-15 09:08:42","tax":"0.1","total_cost":"14","store_name":"McDonalds(NYU)"}, "items": [{"item_id":"10","item_name":"fries-mid","item_qty":"2","item_discount":"1","item_price":"2.25"},{"item_id":"11","item_name":"cheese burg","item_qty":"1","item_discount":"1","item_price":"2.5"},{"item_id":"12","item_name":"coke","item_qty":"2","item_discount":"1","item_price":"1.25"},{"item_id":"23","item_name":"Coffee","item_qty":"1","item_discount":"1","item_price":"1"},{"item_id":"29","item_name":"Salad","item_qty":"1","item_discount":"1","item_price":"3"}]},

{"basicInfo...", "items"...} ]

Rafe commented 13 years ago

Well, I mean get all full receipts by user at once, and if able, sort by time sequence.

yaxing commented 13 years ago

Yes it is all full receipts of one user, ok I'll make them sorted by time sequence. Any other issues about this one?

Rafe commented 13 years ago

sorry, its good, thanks.

Rafe commented 13 years ago

Thanks a lot, and last call, can you make value as class member but not array, to avoid basicInfo prefix in response?

yaxing commented 13 years ago

But items have to be built into an array, or else an object cannot have multiple items, right?

yaxing commented 13 years ago

I assumed that using 'basicInfo', 'items' in the response would make you to distinct different modules of one receipt more efficiently.

yaxing commented 13 years ago

For example, you can use ajax() to retrieve all receipts and use getEachReceiptId() to get receipt_id of each receipt after ajax is finished.

var receipts = null; function ajax(){ $.post('receiptOperation', {'opcode' : 'user_get_all_receipt', 'acc' : 'new'}, function(data){ receipts = $.parseJSON(data); } ); }

function getEachReceiptId(){ for(var rec in receipts){ var id = rec['basicInfo']['receipt_id']; } }

Rafe commented 13 years ago

here's what I do before, I can add another layer to accommodate new format, but...... you know what I mean.

function getEachReceiptId(){
    for(var receipt in receipts){
        var id = receipt['receipt_id'];
    }
}
yaxing commented 13 years ago

k...I think I understand what do u mean. However, I think this method may be more flexible. As we don't want to retrieve all fields from database every time, we may need different fields from receipt table for different situations. For example, when selecting all receipt for users, we don't want to retrieve store_account and we don't need to retrieve user_account, but we need to retrieve store_name from store table. But for stores, we may not want to select user_account. In this way, we can control those data only using different SQL statements. And php function just needs to push all selected elements from the result set into corresponding arrays. However, if those attributes are class members, we should use more complex logics to handle them for different situations so that they would not be sent through http requests to front end.

What do you think?

Rafe commented 13 years ago

You can still dynamically attach member to it. If that's the case. the only change is to make

$this->basicInfo[$key] = $value;

to

$this->$key = $value;
yaxing commented 13 years ago

Sure, the thing is I suppose if the classes member is dynamically changed, the encapsulation would be meaningless. How about this method, I'll encapsulate receipt.* and store.store_name as Receipt member. If any of them is not selected from database, then I'll left corresponding member as NULL.

In this case, the JSON data would be like:

[ { "receipt_id":"1", "receipt_time":"2011-06-15 09:08:42", "tax":"0.1", "total_cost":"14", "store_name":"McDonalds(NYU)", "items": [{"item_id":"10","item_name":"fries-mid","item_qty":"2","item_discount":"1","item_price":"2.25"},{"item_id":"11","item_name":"cheese burg","item_qty":"1","item_discount":"1","item_price":"2.5"},{"item_id":"12","item_name":"coke","item_qty":"2","item_discount":"1","item_price":"1.25"},{"item_id":"23","item_name":"Coffee","item_qty":"1","item_discount":"1","item_price":"1"},{"item_id":"29","item_name":"Salad","item_qty":"1","item_discount":"1","item_price":"3"}]},

{"receipt_id...", "items"...} ]

Is this what you need?

Rafe commented 13 years ago

YES!

yaxing commented 13 years ago

Ok I changed it, now the format is:

[{"receipt_id":"11","store_account":null,"store_name":"McDonalds(NYU)","user_account":null,"receipt_time":"2011-06-15 09:09:51","tax":"0.1","total_cost":"0","img":null,"deleted":0,"items":[]},{"receipt_id":"10","store_account":null,"store_name":"McDonalds(NYU)","user_account":null,"receipt_time":"2011-06-15 09:09:50","tax":"0.1","total_cost":"4","img":null,"deleted":0,"items":[{"item_id":"23","item_name":"Coffee","item_qty":"1","item_discount":"1","item_price":"1"},{"item_id":"29","item_name":"Salad","item_qty":"1","item_discount":"1","item_price":"3"}]},{"receipt_id":"1","store_account":null,"store_name":"McDonalds(NYU)","user_account":null,"receipt_time":"2011-06-15 09:08:42","tax":"0.1","total_cost":"14","img":null,"deleted":0,"items":[{"item_id":"10","item_name":"fries-mid","item_qty":"2","item_discount":"1","item_price":"2.25"},{"item_id":"11","item_name":"cheese burg","item_qty":"1","item_discount":"1","item_price":"2.5"},{"item_id":"12","item_name":"coke","item_qty":"2","item_discount":"1","item_price":"1.25"},{"item_id":"23","item_name":"Coffee","item_qty":"1","item_discount":"1","item_price":"1"},{"item_id":"29","item_name":"Salad","item_qty":"1","item_discount":"1","item_price":"3"}]},{"receipt_id":"9","store_account":null,"store_name":"McDonalds(NYU)","user_account":null,"receipt_time":"2011-06-15 09:08:37","tax":"0.1","total_cost":"0","img":null,"deleted":0,"items":[]},{"receipt_id":"8","store_account":null,"store_name":"McDonalds(NYU)","user_account":null,"receipt_time":"2011-06-15 09:08:36","tax":"0.1","total_cost":"4","img":null,"deleted":0,"items":[{"item_id":"23","item_name":"Coffee","item_qty":"1","item_discount":"1","item_price":"1"},{"item_id":"29","item_name":"Salad","item_qty":"1","item_discount":"1","item_price":"3"}]},{"receipt_id":"7","store_account":null,"store_name":"McDonalds(NYU)","user_account":null,"receipt_time":"2011-06-15 08:43:46","tax":"0.1","total_cost":"4","img":null,"deleted":0,"items":[{"item_id":"23","item_name":"Coffee","item_qty":"1","item_discount":"1","item_price":"1"},{"item_id":"29","item_name":"Salad","item_qty":"1","item_discount":"1","item_price":"3"}]},{"receipt_id":"5","store_account":null,"store_name":"McDonalds(NYU)","user_account":null,"receipt_time":"2011-06-12 09:04:26","tax":"0","total_cost":"0","img":null,"deleted":0,"items":[]},{"receipt_id":"2","store_account":null,"store_name":"McDonalds(NYU)","user_account":null,"receipt_time":"2011-06-10 15:24:01","tax":"0.1","total_cost":"0","img":null,"deleted":0,"items":[]}]