Show quantity of user shares

This commit is contained in:
ekzyis 2024-08-26 06:15:33 -05:00
parent 5587aeb03a
commit 215a9fb3b6
3 changed files with 24 additions and 11 deletions

View File

@ -101,18 +101,27 @@ func HandleMarket(sc context.Context) echo.HandlerFunc {
var ( var (
db = sc.Db db = sc.Db
ctx = c.Request().Context() ctx = c.Request().Context()
u = types.User{}
id = c.Param("id") id = c.Param("id")
quantity = c.QueryParam("q") quantity = c.QueryParam("q")
q int64 q int64
m = types.Market{} m = types.Market{}
u = types.User{} mU = types.User{}
l = types.LMSR{} l = types.LMSR{}
total float64 total float64
quote0 = types.MarketQuote{} quote0 = types.MarketQuote{}
quote1 = types.MarketQuote{} quote1 = types.MarketQuote{}
uQ0 int
uQ1 int
err error err error
) )
if c.Get("session") != nil {
u = c.Get("session").(types.User)
} else {
u.Id = -1
}
if quantity == "" { if quantity == "" {
q = 1 q = 1
} else if q, err = strconv.ParseInt(quantity, 10, 64); err != nil { } else if q, err = strconv.ParseInt(quantity, 10, 64); err != nil {
@ -127,18 +136,20 @@ func HandleMarket(sc context.Context) echo.HandlerFunc {
"JOIN invoices i ON m.invoice_id = i.id "+ "JOIN invoices i ON m.invoice_id = i.id "+
"WHERE m.id = $1 AND i.confirmed_at IS NOT NULL", id).Scan( "WHERE m.id = $1 AND i.confirmed_at IS NOT NULL", id).Scan(
&m.Id, &m.Question, &m.Description, &m.CreatedAt, &m.EndDate, &l.B, &m.Id, &m.Question, &m.Description, &m.CreatedAt, &m.EndDate, &l.B,
&u.Id, &u.Name, &u.CreatedAt, &u.LnPubkey, &u.NostrPubkey, &u.Msats); err != nil { &mU.Id, &mU.Name, &mU.CreatedAt, &mU.LnPubkey, &mU.NostrPubkey, &mU.Msats); err != nil {
if err == sql.ErrNoRows { if err == sql.ErrNoRows {
return echo.NewHTTPError(http.StatusNotFound) return echo.NewHTTPError(http.StatusNotFound)
} }
return err return err
} }
m.User = u m.User = mU
if err = db.QueryRowContext(ctx, ""+ if err = db.QueryRowContext(ctx, ""+
"SELECT "+ "SELECT "+
"COALESCE(SUM(o.quantity) FILTER(WHERE o.outcome = 0), 0) AS q1, "+ "COALESCE(SUM(o.quantity) FILTER(WHERE o.outcome = 0), 0) AS q1, "+
"COALESCE(SUM(o.quantity) FILTER(WHERE o.outcome = 1), 0) AS q2 "+ "COALESCE(SUM(o.quantity) FILTER(WHERE o.outcome = 1), 0) AS q2, "+
"COALESCE(SUM(o.quantity) FILTER(WHERE o.outcome = 0 AND o.user_id = $2), 0) AS uq1, "+
"COALESCE(SUM(o.quantity) FILTER(WHERE o.outcome = 1 AND o.user_id = $2), 0) AS uq2 "+
"FROM orders o "+ "FROM orders o "+
"JOIN markets m ON o.market_id = m.id "+ "JOIN markets m ON o.market_id = m.id "+
"JOIN invoices i ON o.invoice_id = i.id "+ "JOIN invoices i ON o.invoice_id = i.id "+
@ -153,8 +164,8 @@ func HandleMarket(sc context.Context) echo.HandlerFunc {
// but this isn't sybil resistant. // but this isn't sybil resistant.
// //
// For now, we will ignore pending orders. // For now, we will ignore pending orders.
"WHERE o.market_id = $1 AND i.confirmed_at IS NOT NULL", id).Scan( "WHERE o.market_id = $1 AND i.confirmed_at IS NOT NULL", id, u.Id).Scan(
&l.Q1, &l.Q2); err != nil { &l.Q1, &l.Q2, &uQ0, &uQ1); err != nil {
if err == sql.ErrNoRows { if err == sql.ErrNoRows {
return echo.NewHTTPError(http.StatusNotFound) return echo.NewHTTPError(http.StatusNotFound)
} }
@ -177,7 +188,7 @@ func HandleMarket(sc context.Context) echo.HandlerFunc {
Reward: float64(q) - total, Reward: float64(q) - total,
} }
return pages.Market(m, quote0, quote1).Render(context.RenderContext(sc, c), c.Response().Writer) return pages.Market(m, quote0, quote1, uQ0, uQ1).Render(context.RenderContext(sc, c), c.Response().Writer)
} }
} }

View File

@ -7,7 +7,7 @@ import (
"strconv" "strconv"
) )
templ MarketForm(m types.Market, outcome int, q types.MarketQuote) { templ MarketForm(m types.Market, outcome int, q types.MarketQuote, uQ int) {
<form <form
id={ formId(outcome) } id={ formId(outcome) }
autocomplete="off" autocomplete="off"
@ -42,6 +42,8 @@ templ MarketForm(m types.Market, outcome int, q types.MarketQuote) {
<div id="total">{formatPrice(q.TotalPrice)}</div> <div id="total">{formatPrice(q.TotalPrice)}</div>
<label for="reward">{ "if you win:" }</label> <label for="reward">{ "if you win:" }</label>
<div id="reward">+{formatPrice(q.Reward)}</div> <div id="reward">+{formatPrice(q.Reward)}</div>
<label for="uQ">you have:</label>
<div id="uQ">{ fmt.Sprint(uQ) }</div>
<button type="submit" class="col-span-2">submit</button> <button type="submit" class="col-span-2">submit</button>
</form> </form>
} }

View File

@ -8,7 +8,7 @@ import (
// TODO: Add countdown? Use or at least show somewhere precise timestamps? // TODO: Add countdown? Use or at least show somewhere precise timestamps?
templ Market(m types.Market, q0 types.MarketQuote, q1 types.MarketQuote) { templ Market(m types.Market, q0 types.MarketQuote, q1 types.MarketQuote, uQ0 int, uQ1 int) {
<html> <html>
@components.Head() @components.Head()
<body <body
@ -49,10 +49,10 @@ templ Market(m types.Market, q0 types.MarketQuote, q1 types.MarketQuote) {
</button> </button>
</div> </div>
<div class="mx-auto my-5" x-show="outcome === 1"> <div class="mx-auto my-5" x-show="outcome === 1">
@components.MarketForm(m, 1, q1) @components.MarketForm(m, 1, q1, uQ1)
</div> </div>
<div class="mx-auto my-5" x-show="outcome === 0"> <div class="mx-auto my-5" x-show="outcome === 0">
@components.MarketForm(m, 0, q0) @components.MarketForm(m, 0, q0, uQ0)
</div> </div>
</div> </div>