-
Notifications
You must be signed in to change notification settings - Fork 2
/
category.go
190 lines (184 loc) · 5.79 KB
/
category.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
package handlers
import (
"encoding/json"
"fmt"
"github.com/dgrijalva/jwt-go"
"github.com/jafarlihi/symposium/api/config"
"github.com/jafarlihi/symposium/api/repositories"
"io"
"net/http"
"strings"
)
func GetCategories(w http.ResponseWriter, r *http.Request) {
categories, err := repositories.GetCategories()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
io.WriteString(w, `{"error": "Failed to get the categories"}`)
return
}
jsonResult, err := json.Marshal(categories)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
io.WriteString(w, `{"error": "Failed to marshal the result to JSON"}`)
return
}
w.WriteHeader(http.StatusOK)
io.WriteString(w, string(jsonResult))
}
type categoryDeletionRequest struct {
ID uint32 `json:"id"`
}
func DeleteCategory(w http.ResponseWriter, r *http.Request) {
var cdr categoryDeletionRequest
err := json.NewDecoder(r.Body).Decode(&cdr)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
io.WriteString(w, `{"error": "Request body couldn't be parsed as JSON"}`)
return
}
if cdr.ID == 0 {
w.WriteHeader(http.StatusBadRequest)
io.WriteString(w, `{"error": "CategoryID parameter is missing"}`)
return
}
tokenHeader := r.Header.Get("Authorization")
tokenFields := strings.Fields(tokenHeader)
if len(tokenFields) != 2 {
w.WriteHeader(http.StatusBadRequest)
io.WriteString(w, `{"error: "Token is missing"}`)
return
}
tokenString := tokenFields[1]
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
}
return []byte(config.Config.Jwt.SigningSecret), nil
})
if err != nil {
w.WriteHeader(http.StatusBadRequest)
io.WriteString(w, `{"error": "Failed to parse the token"}`)
return
}
var userID float64
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
userID = claims["userID"].(float64)
} else {
w.WriteHeader(http.StatusBadRequest)
io.WriteString(w, `{"error": "Invalid token"}`)
return
}
user, err := repositories.GetUser(uint32(userID))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
io.WriteString(w, `{"error": "Failed to fetch the user from database"}`)
return
}
if user.Access != 99 {
w.WriteHeader(http.StatusBadRequest)
io.WriteString(w, `{"error": "User lacks the necessary privileges to create a category"}`)
return
}
threads, err := repositories.GetAllThreadsByCategoryID(cdr.ID)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
io.WriteString(w, `{"error": "Failed to fetch the threads of the category"}`)
return
}
for _, thread := range threads {
err := repositories.DeleteNotificationsByThreadID(thread.ID)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
io.WriteString(w, `{"error": "Failed to delete the notifications of a thread in the category"}`)
return
}
err = repositories.DeleteFollowsByThreadID(thread.ID)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
io.WriteString(w, `{"error": "Failed to delete the follow of a thread in the category"}`)
return
}
err = repositories.DeletePostsByThreadID(thread.ID)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
io.WriteString(w, `{"error": "Failed to delete the posts of a thread in the category"}`)
return
}
err = repositories.DeleteThread(thread.ID)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
io.WriteString(w, `{"error": "Failed to delete a thread of the category"}`)
return
}
}
err = repositories.DeleteCategory(cdr.ID)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
io.WriteString(w, `{"error": "Failed to delete the category"}`)
return
}
w.WriteHeader(http.StatusOK)
}
type categoryCreationRequest struct {
Name string `json:"name"`
Color string `json:"color"`
}
func CreateCategory(w http.ResponseWriter, r *http.Request) {
var ccr categoryCreationRequest
err := json.NewDecoder(r.Body).Decode(&ccr)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
io.WriteString(w, `{"error": "Request body couldn't be parsed as JSON"}`)
return
}
if ccr.Name == "" || ccr.Color == "" {
w.WriteHeader(http.StatusBadRequest)
io.WriteString(w, `{"error": "Name, and/or color field(s) is/are missing"}`)
return
}
tokenHeader := r.Header.Get("Authorization")
tokenFields := strings.Fields(tokenHeader)
if len(tokenFields) != 2 {
w.WriteHeader(http.StatusBadRequest)
io.WriteString(w, `{"error: "Token is missing"}`)
return
}
tokenString := tokenFields[1]
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
}
return []byte(config.Config.Jwt.SigningSecret), nil
})
if err != nil {
w.WriteHeader(http.StatusBadRequest)
io.WriteString(w, `{"error": "Failed to parse the token"}`)
return
}
var userID float64
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
userID = claims["userID"].(float64)
} else {
w.WriteHeader(http.StatusBadRequest)
io.WriteString(w, `{"error": "Invalid token"}`)
return
}
user, err := repositories.GetUser(uint32(userID))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
io.WriteString(w, `{"error": "Failed to fetch the user from database"}`)
return
}
if user.Access != 99 {
w.WriteHeader(http.StatusBadRequest)
io.WriteString(w, `{"error": "User lacks the necessary privileges to create a category"}`)
return
}
_, err = repositories.CreateCategory(ccr.Name, ccr.Color)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
io.WriteString(w, `{"error": "Failed to create the category"}`)
return
}
w.WriteHeader(http.StatusOK)
}