-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
235 lines (184 loc) · 5.03 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package main
import (
"database/sql"
"fmt"
"strings"
_ "github.com/lib/pq"
"github.com/newtoallofthis123/noob_text/utils"
)
type Store interface {
createTable() error
InsertIntoDB(req *utils.CreateDocumentRequest) error
GetByHash(hash string) (utils.Document, error)
GetAll() ([]utils.Document, error)
CreateUser(req utils.CreateUserRequest) error
GetUser(username string) (utils.User, error)
GetUserDocs(username string) ([]utils.Document, error)
UpdateDoc(req *utils.UpdateDocumentRequest) error
DeleteDoc(hash string) error
DeleteUser(username string) error
SearchDoc(query string) ([]utils.Document, error)
}
type DBInstance struct {
db *sql.DB
}
func (pq *DBInstance) createTable() error {
user_query := `
CREATE TABLE IF NOT EXISTS users(
username TEXT PRIMARY KEY,
password TEXT NOT NULL,
created_at TIMESTAMP DEFAULT NOW()
)
`
_, err := pq.db.Exec(user_query)
if err != nil {
return err
}
query := `
CREATE TABLE IF NOT EXISTS content(
hash TEXT UNIQUE PRIMARY KEY,
author TEXT REFERENCES users(username),
title TEXT NOT NULL DEFAULT 'Untitled',
content TEXT,
created_at TIMESTAMP DEFAULT NOW(),
updated_at TIMESTAMP DEFAULT NOW()
)
`
_, err = pq.db.Exec(query)
return err
}
func NewStoreInstance() (*DBInstance, error) {
env := utils.GetEnv()
db, err := sql.Open("postgres", env.DatabaseUrl)
if err != nil {
return nil, err
}
return &DBInstance{db}, nil
}
func (pq *DBInstance) InsertIntoDB(req *utils.CreateDocumentRequest) error {
query := `
INSERT INTO content (hash, author, title, content)
VALUES ($1, $2, $3, $4)
`
_, err := pq.db.Exec(query, req.Hash, req.Author, req.Title, req.Content)
return err
}
func (pq *DBInstance) GetByHash(hash string) (utils.Document, error) {
query := `
SELECT * from content WHERE hash=$1
`
var document utils.Document
rows := pq.db.QueryRow(query, hash)
err := rows.Scan(&document.Hash, &document.Author, &document.Title, &document.Content, &document.CreatedAt, &document.UpdatedAt)
if err != nil {
return document, err
}
return document, nil
}
func (pq *DBInstance) GetAll() ([]utils.Document, error) {
query := `
SELECT * from content
`
var documents []utils.Document
rows, err := pq.db.Query(query)
if err != nil {
return documents, err
}
for rows.Next() {
var document utils.Document
err := rows.Scan(&document.Hash, &document.Author, &document.Title, &document.Content, &document.CreatedAt, &document.UpdatedAt)
if err != nil {
return documents, err
}
documents = append(documents, document)
}
return documents, nil
}
func (pq *DBInstance) CreateUser(req utils.CreateUserRequest) error {
query := `
INSERT INTO users (username, password)
VALUES ($1, $2)
`
hashedPassword, err := utils.HashPassword(req.Password)
if err != nil {
return err
}
_, err = pq.db.Exec(query, req.Username, hashedPassword)
return err
}
func (pq *DBInstance) GetUser(username string) (utils.User, error) {
query := `
SELECT * from users WHERE username=$1
`
var user utils.User
rows := pq.db.QueryRow(query, username)
err := rows.Scan(&user.Username, &user.Password, &user.CreatedAt)
if err != nil {
return user, err
}
return user, nil
}
func (pq *DBInstance) GetUserDocs(username string) ([]utils.Document, error) {
query := `
SELECT * from content WHERE author=$1
`
var documents []utils.Document
rows, err := pq.db.Query(query, username)
if err != nil {
return documents, err
}
for rows.Next() {
var document utils.Document
err := rows.Scan(&document.Hash, &document.Author, &document.Title, &document.Content, &document.CreatedAt, &document.UpdatedAt)
if err != nil {
return documents, err
}
documents = append(documents, document)
}
return documents, nil
}
func (pq *DBInstance) UpdateDoc(req *utils.UpdateDocumentRequest) error {
query := `
UPDATE content SET title=$1, content=$2, updated_at=NOW() WHERE hash=$3
`
_, err := pq.db.Exec(query, req.Title, req.Content, req.Hash)
return err
}
func (pq *DBInstance) DeleteDoc(hash string) error {
query := `
DELETE FROM content WHERE hash=$1
`
_, err := pq.db.Exec(query, hash)
return err
}
func (pq *DBInstance) DeleteUser(username string) error {
query := `
DELETE FROM users WHERE username=$1
`
_, err := pq.db.Exec(query, username)
return err
}
func (pq *DBInstance) SearchDoc(query string) ([]utils.Document, error) {
sqlQuery := `
select * from content c where to_tsvector(CONCAT(c.title, ' ', c.content, ' ', c.author))
@@ to_tsquery('english', '%s');
`
formattedQuery := strings.Split(query, " ")
actualQuery := strings.Join(formattedQuery, " & ")
sqlQuery = fmt.Sprintf(sqlQuery, actualQuery)
fmt.Println(sqlQuery)
var documents []utils.Document
rows, err := pq.db.Query(sqlQuery)
if err != nil {
return documents, err
}
for rows.Next() {
var document utils.Document
err := rows.Scan(&document.Hash, &document.Author, &document.Title, &document.Content, &document.CreatedAt, &document.UpdatedAt)
if err != nil {
return documents, err
}
documents = append(documents, document)
}
return documents, nil
}