• Stars
    star
    248
  • Rank 163,560 (Top 4 %)
  • Language
    Ruby
  • License
    MIT License
  • Created about 5 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

PostgreSQL shopping cart

PostgreSQL shopping cart

All functionality is in PostgreSQLā€™s PL/pgSQL functions.

SELECT * FROM ā€¦ result
items_get() show all items
cart_get(person_id) get cart (unpaid invoice)
lineitem_add(person_id, item_id, quantity) add item to cart
lineitem_delete(lineitems.id) delete lineitem in cart
lineitem_update(lineitems.id, quantity) change quantity (0=delete)
invoice_get(invoices.id) get order
invoice_update(invoices.id, country) update country
invoice_update(invoices.id, country, address) update address
invoice_delete(invoices.id) delete order
invoice_paid(invoices.id, payment info) mark order as paid
invoices_get() show all orders
invoices_get_unshipped() orders needing to be shipped
invoice_shipped(invoice_id, info) mark order as shipped
invoices_get_for(person_id) this personā€™s orders
items_get_for(person_id) items this person has paid for

Install

gem install pg
gem install json
sh init.sh
sh reset.sh
ruby test-db.rb
ruby test-api.rb

Every API function returns:

  1. ā€œokā€ = boolean success/fail
  2. ā€œjsā€ = JSON result (if !ok then {error: "explanation"})

Play around

$ irb
irbĀ» require './getdb.rb'
=> true
irbĀ» db = getdb('store')
irbĀ» ok, res = db.call('invoice_get', 4)
irbĀ» res
{:id=>4,
  :person_id=>7,
  :name=>"å·©äæ",
  :order_date=>"2019-10-02",
  :payment_date=>nil,
  :payment_info=>nil,
  :subtotal=>2,
  :shipping=>0,
  :total=>2,
  :country=>"CN",
  :address=>nil,
  :ship_date=>nil,
  :ship_info=>nil,
  :lineitems=>[{:id=>4, :item_id=>4, :name=>"JPG of Mr. Wonka", :quantity=>1, :price=>2}]}
irbĀ» ok, res = db.call('invoice_paid', 4, 'cash')
irbĀ» puts ok ? 'paid' : res[:error]
paid
irbĀ» ok, res = db.call('invoices_get')
irbĀ» res.map {|i| "%d = %s" % [i[:id], i[:name]]}
=> ["1 = Charlie Buckets", "2 = Veruca Salt", "3 = Augustus Gloop", "4 = å·©äæ"]
irbĀ» exit

$ psql -U dude dude
pgĀ» select * from store.invoices_get();
ok ā”‚ t
js ā”‚ [{"id":1,"person_id":4,"name":"Charlie Buckets","order_date":"2019-10-02","payment_date":"2019-10-02","payment_info":"PayPal #abc123","subtotal":15,"shipping":6,"total":21,"country":"US","address":"Charlie Buckets\n3 Skid Row\nHershey, PA 04141","ship_date":"2019-10-03","ship_info":"usps# a1b2","lineitems":[{"id":1,"item_id":3,"name":"Fizzy Lifting Drink","quantity":3,"price":15}]},
ā€¦
pgĀ» select * from store.invoice_shipped(4, 'posted');
ok ā”‚ t
js ā”‚ {"id":4,"person_id":7,"order_date":"2019-10-02","payment_date":"2023-03-12","payment_info":"cash","subtotal":2,"shipping":0,"total":2,"country":"CN","address":null,"ship_date":"2023-03-12","ship_info":"posted"}

Contents

  • api/ = public API functions (only use these)
  • functions/ = private functions used by API
  • getdb.rb = helper to call API
  • tables.sql = tables and indexes
  • test-api.rb = unit tests of API calls
  • test-db.rb = unit tests of private functions
  • test_data.sql = sample data for testing
  • tester.rb = helper for unit tests
  • triggers/ = triggers for data logic
  • views/ = re-usable views for JSON

Schema for functions

All functions, views, and triggers are in the "store" schema.

So any time you make a change, you can run ā€¦ "drop schema store cascade; create schema store" ā€¦ then re-load them all anytime. It won't lose your data.

Questions?

Email me at https://sive.rs/contact

Sorry I wonā€™t be watching pull-requests or issues here. Iā€™m posting this just as some example code.