-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
315 lines (253 loc) · 6.68 KB
/
main.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package main
import (
"database/sql"
"fmt"
"log"
"net/http"
"os"
"html/template"
"reflect"
fb "github.com/huandu/facebook"
_ "github.com/lib/pq"
)
type guccomments struct {
Comments string
}
type guccommentspageData struct {
Course string
Guccommentss []guccomments
}
var (
dbname = os.Getenv("DATABASE_NAME")
dbpassword = os.Getenv("DATABASE_PASSWORD")
dbuser = os.Getenv("DATABASE_USER")
dbhost = os.Getenv("DATABASE_HOST")
ac = os.Getenv("ACCESS_TOKEN")
db *sql.DB
tpl *template.Template
)
func init() {
var err error
dbInfo := fmt.Sprintf("host=%s user=%s password=%s dbname=%s sslmode=disable",
dbhost, dbuser, dbpassword, dbname)
db, err = sql.Open("postgres", dbInfo)
if err != nil {
panic(err)
}
if err = db.Ping(); err != nil {
panic(err)
}
fmt.Println("You connected to your database.")
tpl = template.Must(template.ParseGlob("guc_comments.html"))
}
func main() {
q := ` SELECT table_name
FROM information_schema.tables
WHERE table_schema='public' AND table_type='BASE TABLE'`
q1 := `SELECT CourseId,CourseName
FROM Courses `
qcreate := `CREATE TABLE Courses
(
CourseId SERIAL PRIMARY KEY,
CourseName TEXT
);
CREATE TABLE Comments
(
CourseId INTEGER REFERENCES Courses(CourseId),
Comment TEXT
);`
qinsertCourses := `INSERT INTO Courses(CourseId ,CourseName ) VALUES
(DEFAULT,'CSEN 702 Microprocessors'),
(DEFAULT,'CSEN 703 Analysis and Design of Algorithms'),
(DEFAULT,'CSEN 704 Advanced computer lab');`
db.Exec(qcreate)
Result, err := db.Exec(qinsertCourses)
if err != nil {
log.Fatal(err)
}
fmt.Println(Result)
rows, err := db.Query(q)
if (err) != nil {
log.Fatal(err)
}
for rows.Next() {
var (
tableName string
)
if err := rows.Scan(&tableName); err != nil {
log.Fatal(err)
}
log.Printf("Table name is %s", tableName)
}
rows2, err := db.Query(q1)
for rows2.Next() {
var (
courseName string
courseid int
)
if err := rows2.Scan(&courseid, &courseName); err != nil {
log.Fatal(err)
}
log.Printf("course name is %s and id is %d", courseName, courseid)
}
res, err := fb.Get("582313518881669_582751342171220/comments", fb.Params{
"fields": "message",
"access_token": ac,
})
if (err) != nil {
log.Fatal(err)
}
s, _ := res["data"].([]interface{})
for i := 0; i < reflect.ValueOf(s).Len(); i++ {
msg := s[i].(map[string]interface{})
qinsertcomment := fmt.Sprintf(`INSERT INTO Comments(CourseId, Comment) VALUES (1 ,'%s')`,
msg["message"])
Result, err := db.Exec(qinsertcomment)
if err != nil {
log.Fatal(err)
}
fmt.Println(Result)
fmt.Println(qinsertcomment)
}
fmt.Println("")
res2, _ := fb.Get("582313518881669_582750698837951/comments", fb.Params{
"fields": "message",
"access_token": ac})
r, _ := res2["data"].([]interface{})
for i := 0; i < reflect.ValueOf(r).Len(); i++ {
msg := r[i].(map[string]interface{})
qinsertcomment := fmt.Sprintf(`INSERT INTO Comments(CourseId, Comment) VALUES (2 ,'%s')`,
msg["message"])
Result, err := db.Exec(qinsertcomment)
if err != nil {
log.Fatal(err)
}
fmt.Println(Result)
fmt.Println(qinsertcomment)
}
fmt.Println("")
res3, _ := fb.Get("582313518881669_582751015504586/comments", fb.Params{
"fields": "message",
"access_token": ac})
v, _ := res3["data"].([]interface{})
for i := 0; i < reflect.ValueOf(v).Len(); i++ {
msg := v[i].(map[string]interface{})
qinsertcomment := fmt.Sprintf(`INSERT INTO Comments(CourseId, Comment) VALUES (3 ,'%s')`,
msg["message"])
Result, err := db.Exec(qinsertcomment)
if err != nil {
log.Fatal(err)
}
fmt.Println(Result)
fmt.Println(qinsertcomment)
}
q222 := `SELECT Comment
FROM Comments
INNER JOIN Courses ON Courses.CourseId = Comments.CourseId
WHERE Courses.CourseName = 'CSEN 702 Microprocessors' `
rows5555, err := db.Query(q222)
if (err) != nil {
log.Fatal(err)
}
defer rows5555.Close()
gucs := make([]guccomments, 0)
for rows5555.Next() {
guc := guccomments{}
if err := rows5555.Scan(&guc.Comments); err != nil {
log.Fatal(err)
}
fmt.Println(guc.Comments)
gucs = append(gucs, guc)
}
http.HandleFunc("/", defaultHandler)
http.HandleFunc("/MICRO", microHandler)
http.HandleFunc("/ANALYSIS", analysisHandler)
http.HandleFunc("/ADVANCED", advancedHandler)
http.ListenAndServe(":8080", nil)
}
func defaultHandler(w http.ResponseWriter, r *http.Request) {
p := make([]guccomments, 0)
data := guccommentspageData{
Course: " ",
Guccommentss: p,
}
t, _ := template.ParseFiles("guc_comments.html")
t.Execute(w, data)
}
func microHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != "GET" {
http.Error(w, http.StatusText(405), http.StatusMethodNotAllowed)
}
q := `SELECT Comment
FROM Comments
INNER JOIN Courses ON Courses.CourseId = Comments.CourseId
WHERE Courses.CourseName = 'CSEN 702 Microprocessors' `
rows5555, err := db.Query(q)
if (err) != nil {
log.Fatal(err)
}
defer rows5555.Close()
gucs := make([]guccomments, 0)
for rows5555.Next() {
guc := guccomments{}
if err := rows5555.Scan(&guc.Comments); err != nil {
log.Fatal(err)
}
fmt.Println(guc.Comments)
gucs = append(gucs, guc)
}
data := guccommentspageData{
Course: "CSEN 702 Microprocessors",
Guccommentss: gucs,
}
t, _ := template.ParseFiles("guc_comments.html")
t.Execute(w, data)
}
func analysisHandler(w http.ResponseWriter, r *http.Request) {
q := `SELECT Comment
FROM Comments
INNER JOIN Courses ON Courses.CourseId = Comments.CourseId
WHERE Courses.CourseName = 'CSEN 703 Analysis and Design of Algorithms' `
rows, err := db.Query(q)
if (err) != nil {
log.Fatal(err)
}
gucs := make([]guccomments, 0)
for rows.Next() {
guc := guccomments{}
if err := rows.Scan(&guc.Comments); err != nil {
log.Fatal(err)
}
gucs = append(gucs, guc)
}
data := guccommentspageData{
Course: "CSEN 703 Analysis and Design of Algorithms",
Guccommentss: gucs,
}
t, _ := template.ParseFiles("guc_comments.html")
t.Execute(w, data)
}
func advancedHandler(w http.ResponseWriter, r *http.Request) {
q := `SELECT Comment
FROM Comments
INNER JOIN Courses ON Courses.CourseId = Comments.CourseId
WHERE Courses.CourseName = 'CSEN 704 Advanced computer lab' `
rows, err := db.Query(q)
if (err) != nil {
log.Fatal(err)
}
gucs := make([]guccomments, 0)
for rows.Next() {
guc := guccomments{}
if err := rows.Scan(&guc.Comments); err != nil {
log.Fatal(err)
}
gucs = append(gucs, guc)
}
data := guccommentspageData{
Course: "CSEN 704 Advanced computer lab",
Guccommentss: gucs,
}
t, _ := template.ParseFiles("guc_comments.html")
t.Execute(w, data)
}