/* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ /* $Rev$ $Date$ */ /** * Shopping cart component implementation. */ #include "string.hpp" #include "function.hpp" #include "list.hpp" #include "value.hpp" #include "monad.hpp" namespace tuscany { namespace store { const string cartId("1234"); /** * Get the shopping cart from the cache. Return an empty * cart if not found. */ const list getcart(const value& id, const lambda&)> cache) { const value cart = cache(mklist("get", mklist(id))); cerr << "cart value: " << cart << "\n"; const failable fcart = cart; cerr << "cart fvalue: " << fcart << "\n"; cerr << "cart content: " << content(fcart) << "\n"; cerr << "cart reason: " << reason(fcart) << "\n"; if (isNil(cart)) return value(list()); return (list)cart; } /** * Post a new item to the cart. Create a new cart if necessary. */ const failable post(unused const list& collection, const value& item, const lambda&)> cache) { const value id(mkuuid()); const list newItem(mklist("entry", cadr(car(item)), mklist("id", id), cadddr(car(item)))); const list cart(cons(newItem, getcart(cartId, cache))); cache(mklist("put", mklist(cartId), cart)); return value(mklist(id)); } /** * Find an item in the cart. */ const value find(const value& id, const list& cart) { if (isNil(cart)) return mklist(mklist("entry", mklist("title", string("Item")), mklist("id", "0"))); if (id == cadr(caddr(car(cart)))) return mklist(car(cart)); return find(id, cdr(cart)); } /** * Return items from the cart. */ const failable get(const list& id, const lambda&)> cache) { if (isNil(id)) return value(mklist(append(mklist("feed", mklist("title", string("Your Cart")), mklist("id", cartId)), getcart(cartId, cache)))); return find(car(id), getcart(cartId, cache)); } /** * Delete items from the cart. */ const failable del(const list& id, unused const lambda&)> cache) { if (isNil(id)) return cache(mklist("delete", mklist(cartId))); return value(true); } /** * Return the price of an item. */ const double price(const list& item) { return cadr(assoc("price", cdr(cadr(cadddr(item))))); } /** * Sum the prices of a list of items. */ const double sum(const list& items) { if (isNil(items)) return 0; return price(car(items)) + sum(cdr(items)); } /** * Return the total price of the items in the cart. */ const failable total(const lambda&)> cache) { const list cart(getcart(cartId, cache)); return value(sum(cart)); } } } extern "C" { const tuscany::value apply(const tuscany::list& params) { const tuscany::value func(car(params)); if (func == "post") return tuscany::store::post(cadr(params), caddr(params), cadddr(params)); if (func == "get") return tuscany::store::get(cadr(params), caddr(params)); if (func == "delete") return tuscany::store::del(cadr(params), caddr(params)); if (func == "total") return tuscany::store::total(cadr(params)); return tuscany::mkfailure(); } }