diff --git a/db/market.go b/db/market.go index 7ba4eb0..39db574 100644 --- a/db/market.go +++ b/db/market.go @@ -111,3 +111,24 @@ func (db *DB) CreateOrder(tx *sql.Tx, ctx context.Context, order *Order) error { } return nil } + +func (db *DB) FetchUserOrders(pubkey string, orders *[]Order) error { + query := "" + + "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 " + + "WHERE o.pubkey = $1 AND i.confirmed_at IS NOT NULL " + + "ORDER BY o.created_at DESC" + rows, err := db.Query(query, pubkey) + if err != nil { + return err + } + 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.CreatedAt, &order.ShareDescription, &order.Share.MarketId, &order.Invoice.ConfirmedAt) + *orders = append(*orders, order) + } + return nil +} diff --git a/db/types.go b/db/types.go index fa8cd81..9e64896 100644 --- a/db/types.go +++ b/db/types.go @@ -50,9 +50,10 @@ type ( Status string } Order struct { - Id UUID - CreatedAt time.Time - ShareId string `json:"sid"` + Id UUID + CreatedAt time.Time + ShareId string `json:"sid"` + ShareDescription string Share Pubkey string Side string `json:"side"` diff --git a/server/router/handler/market.go b/server/router/handler/market.go index c81531a..e170191 100644 --- a/server/router/handler/market.go +++ b/server/router/handler/market.go @@ -196,3 +196,18 @@ func HandleOrder(sc context.ServerContext) echo.HandlerFunc { return c.JSON(http.StatusPaymentRequired, data) } } + +func HandleOrders(sc context.ServerContext) echo.HandlerFunc { + return func(c echo.Context) error { + var ( + u db.User + orders []db.Order + err error + ) + u = c.Get("session").(db.User) + if err = sc.Db.FetchUserOrders(u.Pubkey, &orders); err != nil { + return err + } + return c.JSON(http.StatusOK, orders) + } +} diff --git a/server/router/router.go b/server/router/router.go index bbd9569..dd86dd0 100644 --- a/server/router/router.go +++ b/server/router/router.go @@ -50,6 +50,9 @@ func addBackendRoutes(e *echo.Echo, sc ServerContext) { handler.HandleOrder, middleware.SessionGuard, middleware.LNDGuard) + GET(e, sc, "/api/orders", + handler.HandleOrders, + middleware.SessionGuard) GET(e, sc, "/api/login", handler.HandleLogin) GET(e, sc, "/api/login/callback", handler.HandleLoginCallback) POST(e, sc, "/api/logout", handler.HandleLogout) diff --git a/vue/src/components/UserInvoices.vue b/vue/src/components/UserInvoices.vue index 11f2d59..5b6659c 100644 --- a/vue/src/components/UserInvoices.vue +++ b/vue/src/components/UserInvoices.vue @@ -45,10 +45,14 @@ table { } th { - padding: 0 1rem; + padding: 0 2rem; } @media only screen and (max-width: 600px) { + th { + padding: 0 1rem; + } + .hidden-sm { display: none } diff --git a/vue/src/components/UserOrders.vue b/vue/src/components/UserOrders.vue new file mode 100644 index 0000000..51670fb --- /dev/null +++ b/vue/src/components/UserOrders.vue @@ -0,0 +1,57 @@ + + + + + market + description + created at + status + + + + {{ o.MarketId }} + {{ o.side }} {{ o.quantity }} {{ o.ShareDescription }} @ {{ o.price }} sats + {{ ago(new Date(o.CreatedAt)) }} + + + + + + + + + + diff --git a/vue/src/main.js b/vue/src/main.js index 9c06add..ba00761 100644 --- a/vue/src/main.js +++ b/vue/src/main.js @@ -12,6 +12,7 @@ import MarketView from '@/views/MarketView' import InvoiceView from '@/views/InvoiceView' import UserSettings from '@/components/UserSettings' import UserInvoices from '@/components/UserInvoices' +import UserOrders from '@/components/UserOrders' const routes = [ { @@ -25,7 +26,8 @@ const routes = [ component: UserView, children: [ { path: 'settings', name: 'user', component: UserSettings }, - { path: 'invoices', name: 'invoices', component: UserInvoices } + { path: 'invoices', name: 'invoices', component: UserInvoices }, + { path: 'orders', name: 'orders', component: UserOrders } ] }, { diff --git a/vue/src/views/UserView.vue b/vue/src/views/UserView.vue index d38593f..204dc45 100644 --- a/vue/src/views/UserView.vue +++ b/vue/src/views/UserView.vue @@ -13,6 +13,7 @@ settings invoices + orders