-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.go
126 lines (95 loc) · 2.68 KB
/
db.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"context"
"database/sql"
"fmt"
"os"
"github.com/jackc/pgx/v4/pgxpool"
)
type User struct {
ID string
Secret string
}
type Binding struct {
ID string `json:"id"`
StationUser string `json:"station_user"`
StationID string `json:"station_id"`
}
type Station struct {
UserID string `json:"user_id,omitempty"`
ID string `json:"id"`
Type string `json:"type"`
Source sql.NullString `json:"-"`
}
func (server *FNRadioServer) setupDB() {
var err error
server.DB, err = pgxpool.Connect(context.Background(), os.Getenv("DATABASE_URL"))
if err != nil {
panic(err)
}
file, err := os.ReadFile("init.sql")
if err != nil {
return
}
_, err = server.DB.Exec(context.Background(), string(file))
if err != nil {
panic(err)
}
}
func (server *FNRadioServer) getUserStations(user string) ([]Station, error) {
var stations []Station
rows, err := server.DB.Query(context.TODO(), "SELECT id, type, source FROM stations WHERE user_id = $1", user)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var station Station
err = rows.Scan(&station.ID, &station.Type, &station.Source)
if err != nil {
return nil, err
}
stations = append(stations, station)
}
if stations == nil {
return make([]Station, 0), nil // Return an empty array instead of nil if there are no stations
}
return stations, nil
}
func (server *FNRadioServer) getUserBindings(user string) ([]Binding, error) {
var bindings []Binding
rows, err := server.DB.Query(context.TODO(), "SELECT id, station_user, station_id FROM bindings WHERE user_id = $1", user)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var binding Binding
err = rows.Scan(&binding.ID, &binding.StationUser, &binding.StationID)
if err != nil {
return nil, err
}
bindings = append(bindings, binding)
}
if bindings == nil {
return make([]Binding, 0), nil // Return an empty array instead of nil if there are no bindings
}
return bindings, nil
}
func (server *FNRadioServer) getUserStation(user string, stationID string) *Station {
var station Station
err := server.DB.QueryRow(context.TODO(), "SELECT type, source FROM stations WHERE user_id = $1 AND id = $2", user, stationID).Scan(&station.Type, &station.Source)
if err != nil {
return nil
}
return &station
}
func (server *FNRadioServer) getUserBinding(user string, id string) *Binding {
var binding Binding
err := server.DB.QueryRow(context.TODO(), "SELECT station_user, station_id FROM bindings WHERE user_id = $1 AND id = $2", user, id).Scan(&binding.StationUser, &binding.StationID)
if err != nil {
fmt.Println(err)
return nil
}
return &binding
}