-
Notifications
You must be signed in to change notification settings - Fork 2
/
admin.go
163 lines (142 loc) · 3.13 KB
/
admin.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
package main
import (
"database/sql"
"log"
"net/http"
"strconv"
"strings"
"github.com/go-chi/chi"
)
func AdminView(w http.ResponseWriter, r *http.Request) {
user := r.Context().Value("user").(User)
if !user.Admin {
http.Error(w, "Forbidden", 403)
return
}
rows, err := db.Query("select id, name, pickup, dropoff, address, phone, logo_url, user_id from charity where approved is null order by id")
if err != nil {
log.Println(err)
t.ExecuteTemplate(w, "error.tmpl", ErrorPage{
ErrorCode: 500,
Error: err.Error(),
})
return
}
defer rows.Close()
charities := []Charity{}
for rows.Next() {
logoURL := sql.NullString{}
pickup := sql.NullBool{}
dropoff := sql.NullBool{}
userID := sql.NullString{}
charity := Charity{}
err := rows.Scan(
&charity.Id,
&charity.Name,
&pickup,
&dropoff,
&charity.Address,
&charity.Phone,
&logoURL,
&userID,
)
if err != nil {
log.Println(err)
t.ExecuteTemplate(w, "error.tmpl", ErrorPage{
ErrorCode: 500,
Error: err.Error(),
})
return
}
charity.Pickup = pickup.Bool
charity.Dropoff = dropoff.Bool
charity.LogoURL = logoURL.String
charity.UserID = userID.String
charity.Phone = FormatPhone(charity.Phone)
charities = append(charities, charity)
}
ids := []string{}
for _, c := range charities {
if len(c.UserID) > 0 {
ids = append(ids, c.UserID)
}
}
if len(ids) > 0 {
idList := strings.Join(ids, ",")
stmt := "select id from user where email_verified = true and id in ('" + idList + "')"
log.Println(stmt)
rows, err = db.Query(stmt)
if err != nil {
log.Println(err)
t.ExecuteTemplate(w, "error.tmpl", ErrorPage{
ErrorCode: 500,
Error: err.Error(),
})
}
for rows.Next() {
var id string
err = rows.Scan(&id)
if err != nil {
log.Println(err)
t.ExecuteTemplate(w, "error.tmpl", ErrorPage{
ErrorCode: 500,
Error: err.Error(),
})
}
for i, c := range charities {
if c.UserID == id {
charities[i].EmailVerified = true
break
}
}
}
}
err = t.ExecuteTemplate(w, "adminView.tmpl", struct {
User User
Charities []Charity
}{
User: user,
Charities: charities,
})
if err != nil {
log.Println(err)
}
}
func AdminCharityApprove(w http.ResponseWriter, r *http.Request) {
user := r.Context().Value("user").(User)
if !user.Admin {
http.Error(w, "Forbidden", 403)
return
}
id, err := strconv.Atoi(chi.URLParam(r, "id"))
if err != nil {
log.Println(err)
http.Error(w, "id error", 400)
return
}
_, err = db.Exec("update charity set approved = true where id = ?", id)
if err != nil {
log.Println(err)
http.Error(w, "server error", 500)
return
}
}
func AdminCharityDeny(w http.ResponseWriter, r *http.Request) {
user := r.Context().Value("user").(User)
if !user.Admin {
http.Error(w, "Forbidden", 403)
return
}
id, err := strconv.Atoi(chi.URLParam(r, "id"))
if err != nil {
log.Println(err)
http.Error(w, "id error", 400)
return
}
_, err = db.Exec("update charity set approved = false where id = ?", id)
if err != nil {
log.Println(err)
http.Error(w, "server error", 500)
return
}
}