Creating a handler that return JSON
This commit is contained in:
parent
76bee566cd
commit
a8b56b50db
@ -24,6 +24,7 @@ func routes(app *config.AppConfig) http.Handler {
|
|||||||
mux.Get("/majors-suite", handlers.Repo.Majors)
|
mux.Get("/majors-suite", handlers.Repo.Majors)
|
||||||
mux.Get("/reservation", handlers.Repo.Reservation)
|
mux.Get("/reservation", handlers.Repo.Reservation)
|
||||||
mux.Post("/reservation", handlers.Repo.PostReservation)
|
mux.Post("/reservation", handlers.Repo.PostReservation)
|
||||||
|
mux.Get("/reservation-json", handlers.Repo.ReservationJSON)
|
||||||
mux.Get("/make-reservation", handlers.Repo.MakeReservation)
|
mux.Get("/make-reservation", handlers.Repo.MakeReservation)
|
||||||
|
|
||||||
fileServer := http.FileServer(http.Dir("./static/"))
|
fileServer := http.FileServer(http.Dir("./static/"))
|
||||||
|
@ -1,10 +1,12 @@
|
|||||||
package handlers
|
package handlers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"go-udemy-web-1/pkg/config"
|
"go-udemy-web-1/pkg/config"
|
||||||
"go-udemy-web-1/pkg/models"
|
"go-udemy-web-1/pkg/models"
|
||||||
"go-udemy-web-1/pkg/render"
|
"go-udemy-web-1/pkg/render"
|
||||||
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -80,3 +82,25 @@ func (m *Repository) PostReservation(w http.ResponseWriter, r *http.Request) {
|
|||||||
end := r.Form.Get("end")
|
end := r.Form.Get("end")
|
||||||
fmt.Fprintf(w, "Posted to search availability from %s to %s", start, end)
|
fmt.Fprintf(w, "Posted to search availability from %s to %s", start, end)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type responseJSON struct {
|
||||||
|
OK string `json:"ok"`
|
||||||
|
Message string `json:"message"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// MakeReservation is the make reservation page handler
|
||||||
|
func (m *Repository) ReservationJSON(w http.ResponseWriter, r *http.Request) {
|
||||||
|
resp := responseJSON{
|
||||||
|
OK: "true",
|
||||||
|
Message: "Available!",
|
||||||
|
}
|
||||||
|
|
||||||
|
out, err := json.MarshalIndent(resp, "", " ")
|
||||||
|
if err != nil {
|
||||||
|
log.Println(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
|
||||||
|
w.Write(out)
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user