summaryrefslogtreecommitdiffstats
path: root/cmd/web
diff options
context:
space:
mode:
Diffstat (limited to 'cmd/web')
-rw-r--r--cmd/web/handlers.go32
-rw-r--r--cmd/web/helpers.go26
2 files changed, 45 insertions, 13 deletions
diff --git a/cmd/web/handlers.go b/cmd/web/handlers.go
index 4b76103..6456e96 100644
--- a/cmd/web/handlers.go
+++ b/cmd/web/handlers.go
@@ -1,9 +1,9 @@
package main
import (
+ "fmt"
"log"
"net/http"
- "runtime/debug"
"text/template"
)
@@ -11,17 +11,6 @@ func (app *application) notFound(w http.ResponseWriter) {
http.Error(w, http.StatusText(401), 401)
}
-func (app *application) serverError(w http.ResponseWriter, r *http.Request, err error) {
- var (
- method = r.Method
- uri = r.URL.RequestURI()
- trace = string(debug.Stack())
- )
-
- app.logger.Error(err.Error(), "method", method, "uri", uri, "trace", trace)
- log.Printf("Crash! %s %s %s", method, uri, trace)
- http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
-}
func (app *application) listOrganisation(w http.ResponseWriter, r *http.Request) {
files := []string{
@@ -64,6 +53,23 @@ func (app *application) home(w http.ResponseWriter, r *http.Request) {
if err != nil {
app.serverError(w, r, err) // Use the serverError() helper
}
+}
+
+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
+ }
- // w.Write([]byte("Hello from DED"))
+ http.Redirect(w, r, fmt.Sprintf("/organisation/view?id=%d", id), http.StatusSeeOther)
}
diff --git a/cmd/web/helpers.go b/cmd/web/helpers.go
new file mode 100644
index 0000000..ea09979
--- /dev/null
+++ b/cmd/web/helpers.go
@@ -0,0 +1,26 @@
+package main
+
+import (
+ "log"
+ "runtime/debug"
+ "net/http"
+)
+
+func (app *application) serverError(w http.ResponseWriter, r *http.Request, err error) {
+ var (
+ method = r.Method
+ uri = r.URL.RequestURI()
+ trace = string(debug.Stack())
+ )
+
+ app.logger.Error(err.Error(), "method", method, "uri", uri, "trace", trace)
+ log.Printf("Crash! %s %s %s", method, uri, trace)
+ http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
+}
+
+// The clientError helper sends a specific status code and corresponding description
+// to the user. We'll use this later in the book to send responses like 400 "Bad
+// Request" when there's a problem with the request that the user sent.
+func (app *application) clientError(w http.ResponseWriter, status int) {
+ http.Error(w, http.StatusText(status), status)
+}