~sircmpwn/meta.sr.ht

ref: 0.56.17 meta.sr.ht/api/graph/model/oauthclient.go -rw-r--r-- 1.6 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
78
79
80
81
82
83
package model

import (
	"context"
	"database/sql"

	sq "github.com/Masterminds/squirrel"

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

type OAuthClient struct {
	ID          int     `json:"id"`
	UUID        string  `json:"uuid"`
	RedirectURL string  `json:"redirectUrl"`
	Name        string  `json:"name"`
	Description *string `json:"description"`
	URL         *string `json:"url"`

	OwnerID int

	alias  string
	fields *database.ModelFields
}

func (oc *OAuthClient) As(alias string) *OAuthClient {
	oc.alias = alias
	return oc
}

func (o *OAuthClient) Alias() string {
	return o.alias
}

func (o *OAuthClient) Table() string {
	return "oauth2_client"
}

func (o *OAuthClient) Fields() *database.ModelFields {
	if o.fields != nil {
		return o.fields
	}
	o.fields = &database.ModelFields{
		Fields: []*database.FieldMap{
			{ "id", "id", &o.ID },
			{ "client_uuid", "uuid", &o.UUID },
			{ "redirect_url", "redirectUrl", &o.RedirectURL },
			{ "client_name", "name", &o.Name },
			{ "client_description", "description", &o.Description },
			{ "client_url", "url", &o.URL },

			// Always fetch:
			{ "id", "", &o.ID },
			{ "owner_id", "", &o.OwnerID },
		},
	}
	return o.fields
}

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

	var (
		err  error
		rows *sql.Rows
	)

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

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

	return clients
}