/
api.go
97 lines (80 loc) · 2.32 KB
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package server
import (
"context"
"encoding/json"
"net/http"
"time"
"github.com/cockroachdb/cockroach-go/crdb"
"github.com/cockroachdb/errors"
"github.com/go-chi/chi"
"github.com/jmoiron/sqlx"
"github.com/opentracing-contrib/go-zap/log"
"go.uber.org/zap"
)
const (
newRoomQuery = `INSERT INTO rooms(code) VALUES (gen_random_uuid()::string) RETURNING *`
)
type Room struct {
ID string `db:"id" json:"id"`
Code string `db:"code" json:"code"`
State string `db:"state" json:"state"`
CreatedAt time.Time `db:"created_at" json:"createdAt"`
UpdatedAt time.Time `db:"updated_at" json:"updatedAt"`
}
type API struct {
db *sqlx.DB
}
func ExecuteTx(ctx context.Context, db *sqlx.DB, f func(*sqlx.Tx) error) error {
tx, err := db.BeginTxx(ctx, nil)
if err != nil {
return errors.Wrap(err, "opening tx")
}
return crdb.ExecuteInTx(ctx, tx, func() error {
return f(tx)
})
}
func (s *API) newRoom(rw http.ResponseWriter, req *http.Request) {
var room Room
ctx := req.Context()
if err := ExecuteTx(ctx, s.db, func(tx *sqlx.Tx) error {
row := tx.QueryRowxContext(ctx, newRoomQuery)
if err := row.Err(); err != nil {
return err
}
return row.StructScan(&room)
}); err != nil {
log.ErrorWithContext(ctx, "writing response", zap.Error(err))
rw.WriteHeader(http.StatusInternalServerError)
return
}
rw.Header().Add("Content-Type", "application/json")
rw.WriteHeader(http.StatusCreated)
enc := json.NewEncoder(rw)
if err := enc.Encode(&room); err != nil {
log.ErrorWithContext(ctx, "writing response", zap.Error(err))
}
}
func (s *API) getRoom(rw http.ResponseWriter, req *http.Request) {
id := chi.URLParam(req, "id")
var room Room
ctx := req.Context()
if err := ExecuteTx(ctx, s.db, func(tx *sqlx.Tx) error {
row := tx.QueryRowxContext(ctx, `SELECT * FROM rooms WHERE id = $1`, id)
if err := row.Err(); err != nil {
return err
}
return row.StructScan(&room)
}); err != nil {
log.ErrorWithContext(ctx, "writing response", zap.Error(err))
rw.WriteHeader(http.StatusInternalServerError)
return
}
rw.Header().Add("Content-Type", "application/json")
enc := json.NewEncoder(rw)
if err := enc.Encode(&room); err != nil {
log.ErrorWithContext(ctx, "writing response", zap.Error(err))
}
}
func (s *API) joinRoom(rw http.ResponseWriter, req *http.Request) {
// ctx := req.Context()
}