diff --git a/db/market.go b/db/market.go index 39db574..f1b4bed 100644 --- a/db/market.go +++ b/db/market.go @@ -132,3 +132,23 @@ func (db *DB) FetchUserOrders(pubkey string, orders *[]Order) error { } return nil } + +func (db *DB) FetchMarketOrders(marketId int64, 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 " + + "FROM orders o " + + "JOIN shares s ON o.share_id = s.id " + + "WHERE s.market_id = $1 " + + "ORDER BY o.created_at DESC" + rows, err := db.Query(query, marketId) + 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) + *orders = append(*orders, order) + } + return nil +} diff --git a/server/router/handler/market.go b/server/router/handler/market.go index e170191..8b2babc 100644 --- a/server/router/handler/market.go +++ b/server/router/handler/market.go @@ -112,6 +112,23 @@ func HandleCreateMarket(sc context.ServerContext) echo.HandlerFunc { } } +func HandleMarketOrders(sc context.ServerContext) echo.HandlerFunc { + return func(c echo.Context) error { + var ( + marketId int64 + orders []db.Order + err error + ) + if marketId, err = strconv.ParseInt(c.Param("id"), 10, 64); err != nil { + return echo.NewHTTPError(http.StatusBadRequest, "Bad Request") + } + if err = sc.Db.FetchMarketOrders(marketId, &orders); err != nil { + return err + } + return c.JSON(http.StatusOK, orders) + } +} + func HandleOrder(sc context.ServerContext) echo.HandlerFunc { return func(c echo.Context) error { var ( diff --git a/server/router/router.go b/server/router/router.go index dd86dd0..c1fb901 100644 --- a/server/router/router.go +++ b/server/router/router.go @@ -46,6 +46,7 @@ func addBackendRoutes(e *echo.Echo, sc ServerContext) { middleware.SessionGuard, middleware.LNDGuard) GET(e, sc, "/api/market/:id", handler.HandleMarket) + GET(e, sc, "/api/market/:id/orders", handler.HandleMarketOrders) POST(e, sc, "/api/order", handler.HandleOrder, middleware.SessionGuard, diff --git a/vue/src/components/Market.vue b/vue/src/components/Market.vue index 31cde0b..023ad79 100644 --- a/vue/src/components/Market.vue +++ b/vue/src/components/Market.vue @@ -10,62 +10,24 @@
description | +created at | +status | + + +
---|---|---|
{{ o.side }} {{ o.quantity }} {{ o.ShareDescription }} @ {{ o.price }} sats | +{{ ago(new Date(o.CreatedAt)) }} | +PENDING | +