// Statup // Copyright (C) 2018. Hunter Long and the project contributors // Written by Hunter Long and the project contributors // // https://github.com/hunterlong/statup // // The licenses for most software and other practical works are designed // to take away your freedom to share and change the works. By contrast, // the GNU General Public License is intended to guarantee your freedom to // share and change all versions of a program--to make sure it remains free // software for all its users. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . package handlers import ( "encoding/json" "errors" "fmt" "github.com/gorilla/mux" "github.com/hunterlong/statup/core" "github.com/hunterlong/statup/types" "github.com/hunterlong/statup/utils" "net/http" "strconv" ) func usersHandler(w http.ResponseWriter, r *http.Request) { if !IsAuthenticated(r) { http.Redirect(w, r, "/", http.StatusSeeOther) return } users, _ := core.SelectAllUsers() executeResponse(w, r, "users.html", users, nil) } func usersEditHandler(w http.ResponseWriter, r *http.Request) { if !IsAuthenticated(r) { http.Redirect(w, r, "/", http.StatusSeeOther) return } vars := mux.Vars(r) id, _ := strconv.Atoi(vars["id"]) user, _ := core.SelectUser(int64(id)) executeResponse(w, r, "user.html", user, nil) } func apiUserHandler(w http.ResponseWriter, r *http.Request) { if !isAPIAuthorized(r) { sendUnauthorizedJson(w, r) return } vars := mux.Vars(r) user, err := core.SelectUser(utils.StringInt(vars["id"])) if err != nil { sendErrorJson(err, w, r) return } user.Password = "" w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(user) } func apiUserUpdateHandler(w http.ResponseWriter, r *http.Request) { if !isAPIAuthorized(r) { sendUnauthorizedJson(w, r) return } vars := mux.Vars(r) user, err := core.SelectUser(utils.StringInt(vars["id"])) if err != nil { sendErrorJson(fmt.Errorf("user #%v was not found", vars["id"]), w, r) return } decoder := json.NewDecoder(r.Body) decoder.Decode(&user) err = user.Update() if err != nil { sendErrorJson(fmt.Errorf("issue updating user #%v: %v", user.Id, err), w, r) return } sendJsonAction(user, "update", w, r) } func apiUserDeleteHandler(w http.ResponseWriter, r *http.Request) { if !isAPIAuthorized(r) { sendUnauthorizedJson(w, r) return } vars := mux.Vars(r) users := core.CountUsers() if users == 1 { sendErrorJson(errors.New("cannot delete the last user"), w, r) return } user, err := core.SelectUser(utils.StringInt(vars["id"])) if err != nil { sendErrorJson(err, w, r) return } err = user.Delete() if err != nil { sendErrorJson(err, w, r) return } sendJsonAction(user, "delete", w, r) } func apiAllUsersHandler(w http.ResponseWriter, r *http.Request) { if !isAPIAuthorized(r) { sendUnauthorizedJson(w, r) return } users, err := core.SelectAllUsers() if err != nil { sendErrorJson(err, w, r) return } w.Header().Set("Content-Type", "application/json") json.NewEncoder(w).Encode(users) } func apiCreateUsersHandler(w http.ResponseWriter, r *http.Request) { if !isAPIAuthorized(r) { sendUnauthorizedJson(w, r) return } var user *types.User decoder := json.NewDecoder(r.Body) err := decoder.Decode(&user) if err != nil { sendErrorJson(err, w, r) return } newUser := core.ReturnUser(user) _, err = newUser.Create() if err != nil { sendErrorJson(err, w, r) return } sendJsonAction(newUser, "create", w, r) }