idk random shit

This commit is contained in:
2024-04-16 23:45:34 +02:00
parent cac8554b0b
commit db00bfb79a
4 changed files with 14 additions and 8 deletions

View File

@ -438,7 +438,16 @@ func (server *MyServer) handleDeleteTimer(w http.ResponseWriter, r *http.Request
}
}
func (server *MyServer) handlePutTimer(w http.ResponseWriter, r *http.Request) {
func parseNumber(s string) (int64, error) {
s = strings.TrimSpace(s)
if len(s) == 0 {
s = "0"
}
return strconv.ParseInt(s, 10, 64)
}
func (server *MyServer) handleCreateTimer(w http.ResponseWriter, r *http.Request) {
timerName := strings.TrimSpace(r.FormValue("timerName"))
user := server.findCurrentUser(w, r)
@ -448,14 +457,14 @@ func (server *MyServer) handlePutTimer(w http.ResponseWriter, r *http.Request) {
return
}
days, err := strconv.ParseInt(strings.TrimSpace(r.FormValue("days")), 10, 32)
days, err := parseNumber(r.FormValue("days"))
if err != nil {
w.WriteHeader(http.StatusBadRequest)
view.TimerCreateForm(timerName, "Error parsing days").Render(context.Background(), w)
return
}
hours, err := strconv.ParseInt(strings.TrimSpace(r.FormValue("hours")), 10, 32)
hours, err := parseNumber(r.FormValue("hours"))
if err != nil {
w.WriteHeader(http.StatusBadRequest)
view.TimerCreateForm(timerName, "Error parsing hours").Render(context.Background(), w)
@ -563,7 +572,7 @@ func main() {
http.HandleFunc("DELETE /timer/{timerId}", myServer.handleDeleteTimer)
http.HandleFunc("POST /timer/{timerId}/resetToken", myServer.handleResetTimerToken)
http.HandleFunc("GET /timer/{timerId}/token", myServer.handleGetTimerToken)
http.HandleFunc("PUT /timer", myServer.handlePutTimer)
http.HandleFunc("PUT /timer", myServer.handleCreateTimer)
http.HandleFunc("GET /", myServer.handleMain)
log.Println("Started!")