diff --git a/db/market.go b/db/market.go index 9396be1..dcd788d 100644 --- a/db/market.go +++ b/db/market.go @@ -265,3 +265,31 @@ func (db *DB) FetchMarketStats(marketId int64, stats *MarketStats) error { } return nil } + +func (db *DB) FetchUserBalance(marketId int64, pubkey string, balance *map[string]any) error { + query := "" + + "SELECT s.description, " + + "SUM(CASE WHEN o.side = 'BUY' THEN o.quantity ELSE -o.quantity END) " + + "FROM orders o " + + "JOIN invoices i ON i.id = o.invoice_id " + + "JOIN shares s ON s.id = o.share_id " + + "WHERE o.pubkey = $1 AND s.market_id = $2 AND i.confirmed_at IS NOT NULL AND o.order_id IS NOT NULL " + + "GROUP BY o.pubkey, s.description" + rows, err := db.Query(query, pubkey, marketId) + if err != nil { + return err + } + + defer rows.Close() + for rows.Next() { + var ( + sdesc string + val int + ) + if err = rows.Scan(&sdesc, &val); err != nil { + return err + } + (*balance)[sdesc] = val + } + return nil +} diff --git a/server/router/handler/market.go b/server/router/handler/market.go index 52fff47..3cec240 100644 --- a/server/router/handler/market.go +++ b/server/router/handler/market.go @@ -25,6 +25,7 @@ func HandleMarket(sc context.ServerContext) echo.HandlerFunc { orders []db.Order err error data map[string]any + u db.User ) if marketId, err = strconv.ParseInt(c.Param("id"), 10, 64); err != nil { return echo.NewHTTPError(http.StatusBadRequest, "Bad Request") @@ -45,6 +46,14 @@ func HandleMarket(sc context.ServerContext) echo.HandlerFunc { "Description": market.Description, "Shares": shares, } + if session := c.Get("session"); session != nil { + u = session.(db.User) + uBalance := make(map[string]any) + if err = sc.Db.FetchUserBalance(marketId, u.Pubkey, &uBalance); err != nil { + return err + } + lib.Merge(&data, &map[string]any{"user": uBalance}) + } return c.JSON(http.StatusOK, data) } } diff --git a/vue/src/components/BuyOrderForm.vue b/vue/src/components/BuyOrderForm.vue new file mode 100644 index 0000000..4af46e2 --- /dev/null +++ b/vue/src/components/BuyOrderForm.vue @@ -0,0 +1,132 @@ + + + + + diff --git a/vue/src/components/Market.vue b/vue/src/components/Market.vue index b1d765a..9ad3c05 100644 --- a/vue/src/components/Market.vue +++ b/vue/src/components/Market.vue @@ -18,7 +18,7 @@ - + diff --git a/vue/src/components/MarketList.vue b/vue/src/components/MarketList.vue index a3668f6..3653f77 100644 --- a/vue/src/components/MarketList.vue +++ b/vue/src/components/MarketList.vue @@ -1,7 +1,7 @@