working the get statement

This commit is contained in:
2023-08-17 12:09:26 +00:00
parent 5dd8de09de
commit dacffde1f8
3 changed files with 52 additions and 4 deletions

View File

@@ -1,10 +1,13 @@
package main
import (
"errors"
"fmt"
"html/template"
"net/http"
"strconv"
"gitea.wagshome.duckdns.org/nathan/golearn/internal/models"
)
func (app *application) home(w http.ResponseWriter, r *http.Request) {
@@ -34,7 +37,16 @@ func (app *application) snippetView(w http.ResponseWriter, r *http.Request) {
app.notFound(w)
return
}
fmt.Fprintf(w, "Display a specific snippet with ID %d...", id)
snippet, err := app.snippets.Get(id)
if err != nil {
if errors.Is(err, models.ErrNoRecord) {
app.notFound(w)
} else {
app.serverError(w, err)
}
return
}
fmt.Fprintf(w, "%+v", snippet)
}
func (app *application) snippetCreate(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
@@ -42,5 +54,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)
}