-
Notifications
You must be signed in to change notification settings - Fork 2
/
search_type_model.go
106 lines (90 loc) · 2.69 KB
/
search_type_model.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
package bloomdb
import (
"time"
"database/sql"
)
type SearchTypeModel struct {
Name string
LastUpdated time.Time
LastChecked time.Time
Public bool
}
func (bdb *BloomDatabase) PublicSearchTypes() ([]SearchTypeModel, error) {
var searchTypes []SearchTypeModel
conn, err := bdb.SqlConnection()
if err != nil {
return nil, err
}
rows, err := conn.Query(`
SELECT search_types.name, search_types.last_updated, search_types.last_checked, search_types.public
FROM search_types
WHERE search_types.public = true`)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
searchType := SearchTypeModel{}
if err := rows.Scan(&searchType.Name, &searchType.LastUpdated, &searchType.LastChecked, &searchType.Public); err != nil {
return nil, err
}
searchTypes = append(searchTypes, searchType)
}
if err = rows.Err(); err != nil {
return nil, err
}
return searchTypes, nil
}
func (bdb *BloomDatabase) SearchTypesWithKey(apiKey string) ([]SearchTypeModel, error) {
var searchTypes []SearchTypeModel
conn, err := bdb.SqlConnection()
if err != nil {
return nil, err
}
rows, err := conn.Query(`
SELECT search_types.name, search_types.last_updated, search_types.last_checked, search_types.public
FROM search_types
LEFT JOIN keys_to_search_types
ON search_types.id = keys_to_search_types.search_type_id
LEFT JOIN api_keys
ON keys_to_search_types.api_key_id = api_keys.id
WHERE public = true OR key = $1
`, apiKey)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
searchType := SearchTypeModel{}
if err := rows.Scan(&searchType.Name, &searchType.LastUpdated, &searchType.LastChecked, &searchType.Public); err != nil {
return nil, err
}
searchTypes = append(searchTypes, searchType)
}
if err = rows.Err(); err != nil {
return nil, err
}
return searchTypes, nil
}
func (bdb *BloomDatabase) SearchTypeWithNameAndKey(name string, apiKey string) (*SearchTypeModel, bool, error) {
searchType := SearchTypeModel{}
conn, err := bdb.SqlConnection()
if err != nil {
return nil, false, err
}
err = conn.QueryRow(`
SELECT search_types.name, search_types.last_updated, search_types.last_checked, search_types.public
FROM search_types
LEFT JOIN keys_to_search_types
ON search_types.id = keys_to_search_types.search_type_id
LEFT JOIN api_keys
ON keys_to_search_types.api_key_id = api_keys.id
WHERE (key = $1 OR search_types.public = true) AND search_types.name = $2
`, apiKey, name).Scan(&searchType.Name, &searchType.LastUpdated, &searchType.LastUpdated, &searchType.Public)
if err == sql.ErrNoRows {
return nil, false, nil
} else if err != nil {
return nil, false, err
}
return &searchType, true, nil
}