From 0ea0cecfc320a18971f6efb792a94666a660f64e Mon Sep 17 00:00:00 2001 From: ekzyis Date: Mon, 4 Dec 2023 02:25:41 +0100 Subject: [PATCH] Show market outcome --- db/init.sql | 1 + db/market.go | 4 ++-- db/types.go | 1 + server/router/handler/market.go | 5 +++++ vue/src/components/Market.vue | 10 ++++++++-- 5 files changed, 17 insertions(+), 4 deletions(-) diff --git a/db/init.sql b/db/init.sql index e13c5c2..8cf0a2b 100644 --- a/db/init.sql +++ b/db/init.sql @@ -39,6 +39,7 @@ CREATE EXTENSION "uuid-ossp"; CREATE TABLE shares( id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), market_id INTEGER REFERENCES markets(id), + win BOOLEAN, description TEXT NOT NULL ); CREATE TYPE order_side AS ENUM ('BUY', 'SELL'); diff --git a/db/market.go b/db/market.go index 803fc21..333d821 100644 --- a/db/market.go +++ b/db/market.go @@ -54,14 +54,14 @@ func (db *DB) FetchActiveMarkets(markets *[]Market) error { } func (db *DB) FetchShares(marketId int, shares *[]Share) error { - rows, err := db.Query("SELECT id, market_id, description FROM shares WHERE market_id = $1 ORDER BY description DESC", marketId) + rows, err := db.Query("SELECT id, market_id, description, win FROM shares WHERE market_id = $1 ORDER BY description DESC", marketId) if err != nil { return err } defer rows.Close() for rows.Next() { var share Share - rows.Scan(&share.Id, &share.MarketId, &share.Description) + rows.Scan(&share.Id, &share.MarketId, &share.Description, &share.Win) *shares = append(*shares, share) } return nil diff --git a/db/types.go b/db/types.go index 0bfd449..261b5be 100644 --- a/db/types.go +++ b/db/types.go @@ -37,6 +37,7 @@ type ( Id UUID `json:"sid"` MarketId int Description string + Win bool } Invoice struct { Id UUID diff --git a/server/router/handler/market.go b/server/router/handler/market.go index 87ecff8..784a37c 100644 --- a/server/router/handler/market.go +++ b/server/router/handler/market.go @@ -457,6 +457,11 @@ func HandleMarketSettlement(sc context.ServerContext) echo.HandlerFunc { return err } + if _, err = tx.ExecContext(ctx, "UPDATE shares SET win = (id = $1) WHERE market_id = $2", s.Id, marketId); err != nil { + tx.Rollback() + return err + } + tx.Commit() return c.JSON(http.StatusOK, nil) diff --git a/vue/src/components/Market.vue b/vue/src/components/Market.vue index e16be7f..201534b 100644 --- a/vue/src/components/Market.vue +++ b/vue/src/components/Market.vue @@ -9,8 +9,8 @@ |_| |_| |_|\__,_|_| |_|\_\___|\__|
{{ market.Description }}
-
-
Settled
+
+
Settled: {{ winShareDescription }}
@@ -38,6 +38,7 @@ const marketId = route.params.id const market = ref(null) const mine = ref(false) +const winShareDescription = ref(null) const url = '/api/market/' + marketId await fetch(url) .then(r => r.json()) @@ -45,6 +46,11 @@ await fetch(url) market.value = body mine.value = market.value.Pubkey === session.pubkey }) + .then(() => { + if (market.value.SettledAt) { + winShareDescription.value = market.value.Shares.find(({ Win }) => Win).Description + } + }) .catch(console.error)