-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrest.go
179 lines (154 loc) · 4.35 KB
/
rest.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
package main
import (
"encoding/json"
"net/http"
"log"
"github.com/gorilla/mux"
"fmt"
"strconv"
"strings"
)
func createRouter(initialClass []Class) *mux.Router {
router := mux.NewRouter()
router.HandleFunc("/check", func(w http.ResponseWriter, r *http.Request) {
// Parse the request body
var requestData struct {
Email string `json:"email"`
Noun string `json:"noun"`
}
err := json.NewDecoder(r.Body).Decode(&requestData)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// Check if the email and noun exist in the database
exists := false
for _, item := range initialClass {
if item.Email == requestData.Email && item.Noun == requestData.Noun {
exists = true
break
}
}
// Prepare the response
responseData := struct {
Exists bool `json:"exists"`
}{
Exists: exists,
}
// Send the response
w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(responseData)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Log the request and response
log.Printf("Request: %+v\n", requestData)
log.Printf("Response: %+v\n", responseData)
}).Methods("POST")
router.HandleFunc("/votes", func(w http.ResponseWriter, r *http.Request) {
// Count the number of true votes
votedCount := 0
for _, item := range initialClass {
if item.Voted {
votedCount++
}
}
// Prepare the response
responseData := struct {
Result string `json:"result"`
}{
Result: fmt.Sprintf("%d/%d", votedCount, len(initialClass)),
}
// Send the response
w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(responseData)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}).Methods("GET")
router.HandleFunc("/vote", func(w http.ResponseWriter, r *http.Request) {
// Parse the request body
var requestData struct {
Email string `json:"email"`
Noun string `json:"noun"`
Votes []string `json:"votes"`
Time string `json:"time"`
}
err := json.NewDecoder(r.Body).Decode(&requestData)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// Convert votes to integers
intVotes := make([]int, len(requestData.Votes))
for i, vote := range requestData.Votes {
intVote, err := strconv.Atoi(vote)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
intVotes[i] = intVote
}
timeVoted := strings.Split(requestData.Time, ",")
// Find the corresponding item in initialClass
for i, item := range initialClass {
if item.Email == requestData.Email && item.Noun == requestData.Noun {
// Update the item in initialClass
initialClass[i].Vote = intVotes
initialClass[i].TimeVoted = timeVoted
initialClass[i].Voted = true
break
}
}
// Prepare the response
responseData := struct {
Success bool `json:"success"`
}{
Success: true,
}
// Send the response
w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(responseData)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Log the request and response
log.Printf("Request: %+v\n", requestData)
log.Printf("Response: %+v\n", responseData)
}).Methods("POST")
router.HandleFunc("/eachVoted", func(w http.ResponseWriter, r *http.Request) {
// Create an array to store the vote counts
voteCounts := make([]int, 6) // Assuming the vote array can have values from 1 to 6
// Count the occurrences of each number in the Vote array
for _, item := range initialClass {
if item.Voted {
for _, vote := range item.Vote {
if vote >= 1 && vote <= 6 {
voteCounts[vote-1]++
}
}
}
}
// Prepare the response
responseData := struct {
VoteCounts []int `json:"voteCounts"`
}{
VoteCounts: voteCounts,
}
// Send the response
w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(responseData)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}).Methods("GET")
// Route for the root URL ("/")
router.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "index.html")
})
return router
}