Making smaller functions to make switch simpler
This commit is contained in:
parent
a0786860e2
commit
d431f34e72
1 changed files with 81 additions and 72 deletions
153
controller.go
153
controller.go
|
|
@ -32,6 +32,85 @@ func getStaticFile(relPath string, contentType string, w http.ResponseWriter) {
|
|||
w.Write(file)
|
||||
}
|
||||
|
||||
func isAuthorized(r *http.Request) bool {
|
||||
auth := r.Header.Get("Authorization")
|
||||
|
||||
if auth == "" {
|
||||
return false
|
||||
}
|
||||
|
||||
return auth == fmt.Sprintf("Basic %s", token)
|
||||
}
|
||||
|
||||
func (h *Handler) getRsvps(w http.ResponseWriter, r *http.Request) {
|
||||
if !isAuthorized(r) {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
rsvps, err := GetRsvps(h.db)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
fmt.Fprintf(w, "%s", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
fmt.Fprintf(w, "%#v", rsvps)
|
||||
}
|
||||
|
||||
func (h *Handler) createRsvp(w http.ResponseWriter, r *http.Request) {
|
||||
err := r.ParseForm()
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
attending := r.Form.Get("attending") == "true"
|
||||
partySize, err := strconv.ParseInt(r.Form.Get("party-size"), 10, 64)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
partyMembers := make([]Member, partySize)
|
||||
for i := range partySize {
|
||||
name := r.Form.Get(fmt.Sprintf("name-%d", i))
|
||||
child := r.Form.Has(fmt.Sprintf("child-%d", i)) && r.Form.Get(fmt.Sprintf("child-%d", i)) == "true"
|
||||
dietaryPreferences := r.Form.Get(fmt.Sprintf("diet-%d", i))
|
||||
member := Member{
|
||||
Name: name,
|
||||
Child: child,
|
||||
DietaryPreferences: dietaryPreferences,
|
||||
}
|
||||
partyMembers[i] = member
|
||||
}
|
||||
|
||||
rsvp := Rsvp{
|
||||
Attending: attending,
|
||||
PartySize: partySize,
|
||||
PartyMembers: partyMembers,
|
||||
}
|
||||
|
||||
_, err = rsvp.CreateRsvp(h.db)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if h.ntfy != nil {
|
||||
SendRsvpNotification(h.ntfy, &rsvp)
|
||||
}
|
||||
w.Header().Add("Location", "/rsvp_confirmed")
|
||||
w.WriteHeader(http.StatusSeeOther)
|
||||
}
|
||||
|
||||
func SetupAuth() {
|
||||
var username string
|
||||
var password string
|
||||
|
|
@ -63,16 +142,6 @@ func SetupAuth() {
|
|||
fmt.Println("auth ready")
|
||||
}
|
||||
|
||||
func isAuthorized(r *http.Request) bool {
|
||||
auth := r.Header.Get("Authorization")
|
||||
|
||||
if auth == "" {
|
||||
return false
|
||||
}
|
||||
|
||||
return auth == fmt.Sprintf("Basic %s", token)
|
||||
}
|
||||
|
||||
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Printf("%s - [%s] (%s) %s\n", time.Now().Format(time.RFC3339), r.RemoteAddr, r.Method, r.URL)
|
||||
|
||||
|
|
@ -102,71 +171,11 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
getStaticFile("./client/style.css", "text/css", w)
|
||||
|
||||
case r.Method == "GET" && r.URL.Path == "/api/rsvps":
|
||||
if !isAuthorized(r) {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
rsvps, err := GetRsvps(h.db)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
fmt.Fprintf(w, "%s", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
fmt.Fprintf(w, "%#v", rsvps)
|
||||
h.getRsvps(w, r)
|
||||
|
||||
case r.Method == "POST" && r.URL.Path == "/api/rsvps":
|
||||
err := r.ParseForm()
|
||||
h.createRsvp(w, r)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
attending := r.Form.Get("attending") == "true"
|
||||
partySize, err := strconv.ParseInt(r.Form.Get("party-size"), 10, 64)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
partyMembers := make([]Member, partySize)
|
||||
for i := range partySize {
|
||||
name := r.Form.Get(fmt.Sprintf("name-%d", i))
|
||||
child := r.Form.Has(fmt.Sprintf("child-%d", i)) && r.Form.Get(fmt.Sprintf("child-%d", i)) == "true"
|
||||
dietaryPreferences := r.Form.Get(fmt.Sprintf("diet-%d", i))
|
||||
member := Member{
|
||||
Name: name,
|
||||
Child: child,
|
||||
DietaryPreferences: dietaryPreferences,
|
||||
}
|
||||
partyMembers[i] = member
|
||||
}
|
||||
|
||||
rsvp := Rsvp{
|
||||
Attending: attending,
|
||||
PartySize: partySize,
|
||||
PartyMembers: partyMembers,
|
||||
}
|
||||
|
||||
_, err = rsvp.CreateRsvp(h.db)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if h.ntfy != nil {
|
||||
SendRsvpNotification(h.ntfy, &rsvp)
|
||||
}
|
||||
w.Header().Add("Location", "/rsvp_confirmed")
|
||||
w.WriteHeader(http.StatusSeeOther)
|
||||
default:
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue