This commit is contained in:
lbenedar
2026-03-04 16:44:25 +03:00
parent e392f24256
commit e5288f0d33
2 changed files with 22 additions and 2 deletions

View File

@@ -43,5 +43,14 @@ 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 := "0 snail\nClimb Mount Fuji,\nBut slowly, slowly!\n\n-Kobayshi 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)
}

View File

@@ -18,7 +18,18 @@ 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) {