From 5f375f654a766e6a8a21643eada07f233c2f23a9 Mon Sep 17 00:00:00 2001 From: nathan Date: Thu, 17 Aug 2023 12:09:26 +0000 Subject: [PATCH] working the get statement --- snippetbox/cmd/web/handlers.go | 10 +++++++++- snippetbox/internal/models/snippets.go | 12 +++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/snippetbox/cmd/web/handlers.go b/snippetbox/cmd/web/handlers.go index 88de467..d546a94 100644 --- a/snippetbox/cmd/web/handlers.go +++ b/snippetbox/cmd/web/handlers.go @@ -42,5 +42,13 @@ func (app *application) snippetCreate(w http.ResponseWriter, r *http.Request) { app.clientError(w, http.StatusMethodNotAllowed) return } - w.Write([]byte("Create a new snippet")) + title := "0 Snail" + content := "O snail\nClimb Mount Fuji,\nBut slowly, slowly!\n\n– Kobayashi Issa" + expires := 7 + id, err := app.snippets.Insert(title, content, expires) + if err != nil { + app.serverError(w, err) + return + } + http.Redirect(w, r, fmt.Sprintf("/snippet/view?id=%d", id), http.StatusSeeOther) } diff --git a/snippetbox/internal/models/snippets.go b/snippetbox/internal/models/snippets.go index 1f1ca71..d5819bc 100644 --- a/snippetbox/internal/models/snippets.go +++ b/snippetbox/internal/models/snippets.go @@ -18,10 +18,20 @@ type SnippetModel struct { } func (m *SnippetModel) Insert(title string, content string, expires int) (int, error) { - return 0, nil + stmt := `INSERT INTO snippets (title, content,created,expires) VALUES( ?,?, UTC_TIMESTAMP(), DATE_ADD(UTC_TIMESTAMP(), INTERVAL ? DAY))` + result, err := m.DB.Exec(stmt, title, content, expires) + if err != nil { + return 0, err + } + id, err := result.LastInsertId() + if err != nil { + return 0, err + } + return int(id), nil } func (m *SnippetModel) Get(id int) (*Snippet, error) { + stmt := `SELECT` return nil, nil }