-
Notifications
You must be signed in to change notification settings - Fork 0
/
tx.go
41 lines (32 loc) · 914 Bytes
/
tx.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
package tx
import (
"net/http"
"github.com/diamondburned/facechat/backend/db"
"github.com/diamondburned/facechat/backend/facechat"
"github.com/diamondburned/facechat/backend/http/auth"
"github.com/diamondburned/facechat/backend/http/tx/dbctx"
"github.com/diamondburned/facechat/backend/internal/httperr"
)
var ErrMissingDB = httperr.New(500, "missing database middleware")
func Acquire(r *http.Request, fn func(*db.Tx) error) error {
var db = dbctx.Database(r)
if db == nil {
return ErrMissingDB
}
var selfID facechat.ID
if s := auth.Session(r); s != nil {
selfID = s.UserID
}
return db.Acquire(r.Context(), selfID, fn)
}
func RAcquire(r *http.Request, fn func(*db.ReadTx) error) error {
var db = dbctx.Database(r)
if db == nil {
return ErrMissingDB
}
var selfID facechat.ID
if s := auth.Session(r); s != nil {
selfID = s.UserID
}
return db.RAcquire(r.Context(), selfID, fn)
}