package main
import (
"fmt"
"net/http"
"text/template"
)
func (app *application) notFound(w http.ResponseWriter) {
http.Error(w, http.StatusText(401), 401)
}
func (app *application) listOrganisation(w http.ResponseWriter, r *http.Request) {
files := []string{
"./ui/html/base.tmpl.html",
"./ui/html/pages/organisations/list.tmpl.html",
"./ui/html/partials/nav.tmpl.html",
}
ts, err := template.ParseFiles(files...)
if err != nil {
app.serverError(w, r, err) // Use the serverError() helper
return
}
err = ts.ExecuteTemplate(w, "base", nil)
if err != nil {
app.serverError(w, r, err) // Use the serverError() helper
}
}
func (app *application) home(w http.ResponseWriter, r *http.Request) {
// Check if the current request URL path exactly matches "/". If it doesn't, use
// the http.NotFound() function to send a 404 response to the client.
// Importantly, we then return from the handler. If we don't return the handler
// would keep executing.
if r.URL.Path != "/" {
app.notFound(w)
return
}
files := []string{
"./ui/html/base.tmpl.html",
"./ui/html/pages/home.tmpl.html",
"./ui/html/partials/nav.tmpl.html",
}
ts, err := template.ParseFiles(files...)
if err != nil {
app.serverError(w, r, err) // Use the serverError() helper
return
}
err = ts.ExecuteTemplate(w, "base", nil)
if err != nil {
app.serverError(w, r, err) // Use the serverError() helper
}
}
func (app *application) organisationView(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("This is an organisation"))
}
func (app *application) organisationCreate(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
w.Header().Set("Allow", http.MethodPost)
app.clientError(w, http.StatusMethodNotAllowed)
return
}
// Dummy data
name := "Test Organisation"
id, err := app.organisations.Insert(name)
if err != nil {
app.serverError(w, r, err)
return
}
http.Redirect(w, r, fmt.Sprintf("/organisation/view?id=%d", id), http.StatusSeeOther)
}