~sircmpwn/meta.sr.ht

ref: 0.56.17 meta.sr.ht/api/graph/model/oauthpersonaltoken.go -rw-r--r-- 1.5 KiB
b1deacf0Nicolas Schodet Fix field name when validating the PGP key in RegisterAccount 11 months ago
                                                                                
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
package model

import (
	"context"
	"database/sql"
	"time"

	sq "github.com/Masterminds/squirrel"

	"git.sr.ht/~sircmpwn/core-go/database"
)

type OAuthPersonalToken struct {
	ID      int       `json:"id"`
	Issued  time.Time `json:"issued"`
	Expires time.Time `json:"expires"`
	Comment *string   `json:"comment"`

	alias  string
	fields *database.ModelFields
}

func (tok *OAuthPersonalToken) As(alias string) *OAuthPersonalToken {
	tok.alias = alias
	return tok
}

func (tok *OAuthPersonalToken) Alias() string {
	return tok.alias
}

func (tok *OAuthPersonalToken) Table() string {
	return "oauth2_grant"
}

func (tok *OAuthPersonalToken) Fields() *database.ModelFields {
	if tok.fields != nil {
		return tok.fields
	}
	tok.fields = &database.ModelFields{
		Fields: []*database.FieldMap{
			{ "id", "id", &tok.ID },
			{ "issued", "issued", &tok.Issued },
			{ "expires", "expires", &tok.Expires },
			{ "comment", "comment", &tok.Comment },

			// Always fetch:
			{ "id", "", &tok.ID },
		},
	}
	return tok.fields
}

func (tok *OAuthPersonalToken) Query(ctx context.Context, runner sq.BaseRunner,
	q sq.SelectBuilder) []*OAuthPersonalToken {

	var (
		err  error
		rows *sql.Rows
	)

	if rows, err = q.RunWith(runner).QueryContext(ctx); err != nil {
		panic(err)
	}
	defer rows.Close()

	var tokens []*OAuthPersonalToken
	for rows.Next() {
		var tok OAuthPersonalToken
		if err := rows.Scan(database.Scan(ctx, &tok)...); err != nil {
			panic(err)
		}
		tokens = append(tokens, &tok)
	}

	return tokens
}