diff options
author | Matthew Lemon <y@yulqen.org> | 2024-02-10 19:22:56 +0000 |
---|---|---|
committer | Matthew Lemon <y@yulqen.org> | 2024-02-10 19:22:56 +0000 |
commit | 58967839c646c717b5345f3a3d2924be5c6d5a4b (patch) | |
tree | 3c537852823fb90c374eae385e660b18e05a2ced | |
parent | 030806353ca9b329bfb0102ebc10518b641511ee (diff) |
Adds the Get handler for organisation model and fixes
Also fixes the NotAuthorised response I was mistakenly getting when it
should have been a 404.
-rw-r--r-- | cmd/web/handlers.go | 62 | ||||
-rw-r--r-- | cmd/web/helpers.go | 11 | ||||
-rw-r--r-- | internal/models/errors.go | 7 | ||||
-rw-r--r-- | internal/models/organisations.go | 18 |
4 files changed, 70 insertions, 28 deletions
diff --git a/cmd/web/handlers.go b/cmd/web/handlers.go index d4ac91c..1f3a58f 100644 --- a/cmd/web/handlers.go +++ b/cmd/web/handlers.go @@ -1,15 +1,14 @@ package main import ( + "errors" "fmt" + "html/template" "net/http" - "text/template" -) - -func (app *application) notFound(w http.ResponseWriter) { - http.Error(w, http.StatusText(401), 401) -} + "strconv" + "github.com/yulqen/ded-go-core/internal/models" +) func (app *application) listOrganisation(w http.ResponseWriter, r *http.Request) { files := []string{ @@ -31,10 +30,10 @@ func (app *application) listOrganisation(w http.ResponseWriter, r *http.Request) } 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. + // 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 @@ -59,24 +58,39 @@ func (app *application) home(w http.ResponseWriter, r *http.Request) { } func (app *application) organisationView(w http.ResponseWriter, r *http.Request) { - w.Write([]byte("This is an organisation")) + id, err := strconv.Atoi(r.URL.Query().Get("id")) + if err != nil || id < 1 { + app.notFound(w) + return + } + + o, err := app.organisations.Get(id) + if err != nil { + if errors.Is(err, models.ErrNoRecord) { + app.notFound(w) + } else { + app.serverError(w, r, err) + } + return + } + fmt.Fprintf(w, "%+v", o) } 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 - } + if r.Method != http.MethodPost { + w.Header().Set("Allow", http.MethodPost) + app.clientError(w, http.StatusMethodNotAllowed) + return + } - // Dummy data - name := "Test Organisation" + // Dummy data + name := "Test Organisation" - id, err := app.organisations.Insert(name) - if err != nil { - app.serverError(w, r, err) - return - } + 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) + 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 index ea09979..5f7e4d3 100644 --- a/cmd/web/helpers.go +++ b/cmd/web/helpers.go @@ -1,9 +1,9 @@ package main import ( - "log" - "runtime/debug" + "log" "net/http" + "runtime/debug" ) func (app *application) serverError(w http.ResponseWriter, r *http.Request, err error) { @@ -22,5 +22,10 @@ func (app *application) serverError(w http.ResponseWriter, r *http.Request, err // 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) + http.Error(w, http.StatusText(status), status) +} + +// notFound helper is a convenience wrapper around clientError which sends a 404 Not Found response +func (app *application) notFound(w http.ResponseWriter) { + app.clientError(w, http.StatusNotFound) } diff --git a/internal/models/errors.go b/internal/models/errors.go new file mode 100644 index 0000000..a70c7dc --- /dev/null +++ b/internal/models/errors.go @@ -0,0 +1,7 @@ +package models + +import ( + "errors" +) + +var ErrNoRecord = errors.New("models: no matching record found") diff --git a/internal/models/organisations.go b/internal/models/organisations.go index 291562b..edaa992 100644 --- a/internal/models/organisations.go +++ b/internal/models/organisations.go @@ -2,6 +2,7 @@ package models import ( "database/sql" + "errors" "time" ) @@ -32,7 +33,22 @@ func (m *OrganisationModel) Insert(name string) (int, error) { } func (m *OrganisationModel) Get(id int) (Organisation, error) { - return Organisation{}, nil + stmt := `SELECT id, name, created FROM organisations + WHERE id = ?` + + row := m.DB.QueryRow(stmt, id) + + var o Organisation + + err := row.Scan(&o.ID, &o.Name, &o.Created) + if err != nil { + if errors.Is(err, sql.ErrNoRows) { + return Organisation{}, ErrNoRecord + } else { + return Organisation{}, err + } + } + return o, nil } // Ten most recent... |