Skip to content

Commit

Permalink
Combine enable and disable handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
mtlynch committed Jan 11, 2025
1 parent 19c19c3 commit 4987b4c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 29 deletions.
38 changes: 11 additions & 27 deletions handlers/guest_links.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"log"
"net/http"
"strings"

"github.com/gorilla/mux"
"github.com/mtlynch/picoshare/v2/handlers/parse"
Expand Down Expand Up @@ -63,7 +64,7 @@ func (s Server) guestLinksDelete() http.HandlerFunc {
}
}

func (s *Server) guestLinksDisable() http.HandlerFunc {
func (s *Server) guestLinksEnableDisable() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id, err := parseGuestLinkID(mux.Vars(r)["id"])
if err != nil {
Expand All @@ -78,34 +79,17 @@ func (s *Server) guestLinksDisable() http.HandlerFunc {
return
}

if err := s.getDB(r).DisableGuestLink(id); err != nil {
log.Printf("failed to disable guest link: %v", err)
http.Error(w, fmt.Sprintf("Failed to disable guest link: %v", err), http.StatusInternalServerError)
return
}

w.WriteHeader(http.StatusNoContent)
}
}

func (s *Server) guestLinksEnable() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id, err := parseGuestLinkID(mux.Vars(r)["id"])
if err != nil {
log.Printf("failed to parse guest link ID %s: %v", mux.Vars(r)["id"], err)
http.Error(w, fmt.Sprintf("Invalid guest link ID: %v", err), http.StatusBadRequest)
return
}

if _, err := s.getDB(r).GetGuestLink(id); err != nil {
log.Printf("failed to get guest link ID %s: %v", mux.Vars(r)["id"], err)
http.Error(w, fmt.Sprintf("Guest link with ID %s not found: %v", mux.Vars(r)["id"], err), http.StatusNotFound)
return
// Determine if client is enabling or disabling link.
var dbFn func(picoshare.GuestLinkID) error
if strings.HasSuffix(r.URL.Path, "/enable") {
dbFn = s.getDB(r).EnableGuestLink
} else {
dbFn = s.getDB(r).DisableGuestLink
}

if err := s.getDB(r).EnableGuestLink(id); err != nil {
log.Printf("failed to enable guest link: %v", err)
http.Error(w, fmt.Sprintf("Failed to enable guest link: %v", err), http.StatusInternalServerError)
if err := dbFn(id); err != nil {
log.Printf("failed to change guest link enabled state: %v", err)
http.Error(w, fmt.Sprintf("Failed to change guest link enabled state: %v", err), http.StatusInternalServerError)
return
}

Expand Down
4 changes: 2 additions & 2 deletions handlers/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ func (s *Server) routes() {
authenticatedApis.HandleFunc("/entry/{id}", s.entryDelete()).Methods(http.MethodDelete)
authenticatedApis.HandleFunc("/guest-links", s.guestLinksPost()).Methods(http.MethodPost)
authenticatedApis.HandleFunc("/guest-links/{id}", s.guestLinksDelete()).Methods(http.MethodDelete)
authenticatedApis.HandleFunc("/guest-links/{id}/enable", s.guestLinksEnable()).Methods(http.MethodPut)
authenticatedApis.HandleFunc("/guest-links/{id}/disable", s.guestLinksDisable()).Methods(http.MethodPut)
authenticatedApis.HandleFunc("/guest-links/{id}/enable", s.guestLinksEnableDisable()).Methods(http.MethodPut)
authenticatedApis.HandleFunc("/guest-links/{id}/disable", s.guestLinksEnableDisable()).Methods(http.MethodPut)
authenticatedApis.HandleFunc("/settings", s.settingsPut()).Methods(http.MethodPut)

publicApis := s.router.PathPrefix("/api").Subrouter()
Expand Down

0 comments on commit 4987b4c

Please sign in to comment.