/
sessions.go
81 lines (74 loc) · 1.66 KB
/
sessions.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
package db
import (
"context"
"github.com/amallick86/country-api/models"
"github.com/google/uuid"
"time"
)
// save user session in database
const createSession = `
INSERT INTO sessions (
id,
user_id,
refresh_token,
user_agent,
client_ip,
is_blocked,
expires_at
) VALUES (
$1, $2, $3, $4, $5, $6, $7
)
RETURNING id, user_id, refresh_token, user_agent, client_ip, is_blocked, expires_at, created_at
`
type CreateSessionParams struct {
ID uuid.UUID `json:"id"`
UserId int32 `json:"userId"`
RefreshToken string `json:"refreshToken"`
UserAgent string `json:"userAgent"`
ClientIp string `json:"clientIp"`
IsBlocked bool `json:"isBlocked"`
ExpiresAt time.Time `json:"expiresAt"`
}
func (q *Queries) CreateSession(ctx context.Context, arg CreateSessionParams) (models.Sessions, error) {
row := q.db.QueryRowContext(ctx, createSession,
arg.ID,
arg.UserId,
arg.RefreshToken,
arg.UserAgent,
arg.ClientIp,
arg.IsBlocked,
arg.ExpiresAt,
)
var i models.Sessions
err := row.Scan(
&i.ID,
&i.UserId,
&i.RefreshToken,
&i.UserAgent,
&i.ClientIp,
&i.IsBlocked,
&i.ExpiresAt,
&i.CreatedAt,
)
return i, err
}
// Get user session
const getSession = `
SELECT id, user_id, refresh_token, user_agent, client_ip, is_blocked, expires_at, created_at FROM sessions
WHERE id = $1 LIMIT 1
`
func (q *Queries) GetSession(ctx context.Context, id uuid.UUID) (models.Sessions, error) {
row := q.db.QueryRowContext(ctx, getSession, id)
var i models.Sessions
err := row.Scan(
&i.ID,
&i.UserId,
&i.RefreshToken,
&i.UserAgent,
&i.ClientIp,
&i.IsBlocked,
&i.ExpiresAt,
&i.CreatedAt,
)
return i, err
}