diff --git a/init.sql b/init.sql index e5953c9..033d0ab 100644 --- a/init.sql +++ b/init.sql @@ -40,12 +40,17 @@ CREATE TABLE invoices( CREATE TYPE order_side AS ENUM ('BUY', 'SELL'); CREATE TABLE orders( id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), + created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP, share_id UUID NOT NULL REFERENCES shares(id), pubkey TEXT NOT NULL REFERENCES users(pubkey), side ORDER_SIDE NOT NULL, quantity BIGINT NOT NULL, price BIGINT NOT NULL, - invoice_id UUID NOT NULL REFERENCES invoices(id), - order_id UUID REFERENCES orders(id) + invoice_id UUID NOT NULL REFERENCES invoices(id) ); ALTER TABLE orders ADD CONSTRAINT order_price CHECK(price > 0 AND price < 100); +CREATE TABLE matches( + id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), + oid1 UUID NOT NULL REFERENCES orders(id), + oid2 UUID NOT NULL REFERENCES orders(id) +); diff --git a/src/db.go b/src/db.go index 7064796..4cf3228 100644 --- a/src/db.go +++ b/src/db.go @@ -77,7 +77,7 @@ type FetchOrdersWhere struct { func (db *DB) FetchOrders(where *FetchOrdersWhere, orders *[]Order) error { query := "" + - "SELECT o.id, share_id, o.pubkey, o.side, o.quantity, o.price, o.invoice_id, s.description, s.market_id, i.confirmed_at, o.order_id " + + "SELECT o.id, share_id, o.pubkey, o.side, o.quantity, o.price, o.invoice_id, o.created_at, s.description, s.market_id, i.confirmed_at " + "FROM orders o " + "JOIN invoices i ON o.invoice_id = i.id " + "JOIN shares s ON o.share_id = s.id " + @@ -102,7 +102,7 @@ func (db *DB) FetchOrders(where *FetchOrdersWhere, orders *[]Order) error { defer rows.Close() for rows.Next() { var order Order - rows.Scan(&order.Id, &order.ShareId, &order.Pubkey, &order.Side, &order.Quantity, &order.Price, &order.InvoiceId, &order.Share.Description, &order.Share.MarketId, &order.Invoice.ConfirmedAt, &order.OrderId) + rows.Scan(&order.Id, &order.ShareId, &order.Pubkey, &order.Side, &order.Quantity, &order.Price, &order.InvoiceId, &order.CreatedAt, &order.Share.Description, &order.Share.MarketId, &order.Invoice.ConfirmedAt) *orders = append(*orders, order) } return nil diff --git a/src/market.go b/src/market.go index 9600e05..a7663ae 100644 --- a/src/market.go +++ b/src/market.go @@ -34,8 +34,8 @@ type Order struct { Side string `form:"side"` Price int `form:"price"` Quantity int `form:"quantity"` - OrderId string InvoiceId string + CreatedAt time.Time } type Invoice struct {