diff options
author | Matthew Lemon <y@yulqen.org> | 2024-02-10 12:19:10 +0000 |
---|---|---|
committer | Matthew Lemon <y@yulqen.org> | 2024-02-10 12:19:10 +0000 |
commit | c3df229e32a0a50ce4c7b6994e2ed01d6be1e13c (patch) | |
tree | 210c5b468b2cb0c9f8c067085c0f2442f28540f8 | |
parent | 7c4a913645ba1e347ab94097de44eb04d7df4e47 (diff) |
Have added Insert handler for Organisation model
This is a straight forward wrapper around INSERT.
Also added a clientError into a new helpers.go file.
-rw-r--r-- | cmd/web/handlers.go | 32 | ||||
-rw-r--r-- | cmd/web/helpers.go | 26 | ||||
-rw-r--r-- | internal/models/organisations.go | 14 | ||||
-rw-r--r-- | ui/static/css/main.css | 39 |
4 files changed, 58 insertions, 53 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) +} diff --git a/internal/models/organisations.go b/internal/models/organisations.go index 7a8898c..291562b 100644 --- a/internal/models/organisations.go +++ b/internal/models/organisations.go @@ -16,7 +16,19 @@ type OrganisationModel struct { } func (m *OrganisationModel) Insert(name string) (int, error) { - return 0, nil + stmt := `INSERT INTO organisations (name, created) + VALUEs (?, UTC_TIMESTAMP())` + + result, err := m.DB.Exec(stmt, name) + if err != nil { + return 0, err + } + + id, err := result.LastInsertId() + if err != nil { + return 0, err + } + return int(id), nil } func (m *OrganisationModel) Get(id int) (Organisation, error) { diff --git a/ui/static/css/main.css b/ui/static/css/main.css index 3d85763..8be748b 100644 --- a/ui/static/css/main.css +++ b/ui/static/css/main.css @@ -5,42 +5,3 @@ font-size: 18px; /* font-family: "Ubuntu Mono", monospace; */ } - -/* By default, these elements only expand to the height of their content. However, by setting their height to 100%, they will take up the entire height of the viewport, ensuring that they fill the entire visible area of the browser window regardless of the content size. This can be useful for creating layouts where elements need to stretch to the full height of the screen, such as in a full-page background or a layout with a sticky footer. */ -html, body { - height: 100%; -} - -body { - line-height: 1.2; - background-color: #e3f8fc; - color: #34495E; - overflow-y: scroll; -} - -header, nav, main, footer { - padding: 2px 10px 0; -} - -main { - margin-top: 54px; - margin-bottom: 54px; - min-height: calc(100vh - 345px); - overflow: auto; -} - -h1 a { - font-size: 36px; - font-weight: bold; - /* background-image: url("/static/img/logo.png"); */ - background-repeat: no-repeat; - background-position: 0px 0px; - height: 36px; - padding-left: 10px; - position: relative; -} - -h1 a:hover { - text-decoration: none; - color: #34495E; -} |