Cleaning up main a bit
This commit is contained in:
@@ -101,52 +101,50 @@ func adminIDHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func adminMembersHandler(w http.ResponseWriter, r *http.Request) {
|
func adminMembersHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method == "GET" {
|
if r.Method != http.MethodPost {
|
||||||
// Redirect to client's AdminMembersPage instead of serving members.html
|
|
||||||
w.Header().Set("Location", "/admin-members")
|
|
||||||
w.WriteHeader(http.StatusFound)
|
|
||||||
return
|
|
||||||
} else if r.Method == "POST" {
|
|
||||||
var year int
|
|
||||||
var err error
|
|
||||||
|
|
||||||
if err = r.ParseMultipartForm(10 << 20); err != nil {
|
|
||||||
http.Error(w, "Failed to parse multipart form", http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if y := r.FormValue("year"); y != "" {
|
|
||||||
year, err = strconv.Atoi(y)
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, "Invalid year", http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
file, _, err := r.FormFile("members.csv")
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, "Failed to upload file", http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer file.Close()
|
|
||||||
|
|
||||||
fileBytes, err := ioutil.ReadAll(file)
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, "Failed to read file", http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if err = services.ParseMembersFromBytes(year, fileBytes); err != nil {
|
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
|
||||||
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
json.NewEncoder(w).Encode(map[string]bool{"success": true})
|
|
||||||
} else {
|
|
||||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var year int
|
||||||
|
var err error
|
||||||
|
|
||||||
|
if err = r.ParseMultipartForm(10 << 20); err != nil {
|
||||||
|
http.Error(w, "Failed to parse multipart form", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
year_from_form := r.FormValue("year")
|
||||||
|
if year_from_form == "" {
|
||||||
|
http.Error(w, "Year is required", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
year, err = strconv.Atoi(year_from_form)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, "Invalid year", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if file, _, err := r.FormFile("members.csv"); err != nil {
|
||||||
|
http.Error(w, "Failed to upload file", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
if fileBytes, err := ioutil.ReadAll(file); err != nil {
|
||||||
|
http.Error(w, "Failed to read file", http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = services.ParseMembersFromBytes(year, fileBytes); err != nil {
|
||||||
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
|
json.NewEncoder(w).Encode(map[string]string{"error": err.Error()})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
json.NewEncoder(w).Encode(map[string]bool{"success": true})
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|||||||
Reference in New Issue
Block a user