package main import ( "errors" "fmt" "net/http" "strconv" "strings" "unicode/utf8" "github.com/julienschmidt/httprouter" "snippetbox.chaosfem.tw/internal/models" ) // home ... func (app *application) home(w http.ResponseWriter, r *http.Request) { snippets, err := app.snippets.Latest() if err != nil { app.serverError(w, r, err) return } data := app.newTemplateData(r) data.Snippets = snippets app.render(w, r, http.StatusOK, "home.tmpl", data) } // snippetView ... func (app *application) snippetView(w http.ResponseWriter, r *http.Request) { params := httprouter.ParamsFromContext(r.Context()) id, err := strconv.Atoi(params.ByName("id")) if err != nil || id < 1 { app.notFound(w) return } snippet, err := app.snippets.Get(id) if err != nil || id < 1 { if errors.Is(err, models.ErrNoRecord) { app.notFound(w) } else { app.serverError(w, r, err) } return } data := app.newTemplateData(r) data.Snippet = snippet app.render(w, r, http.StatusOK, "view.tmpl", data) } // (app *application) snippetCreate ... func (app *application) snippetCreate(w http.ResponseWriter, r *http.Request) { data := app.newTemplateData(r) app.render(w, r, http.StatusOK, "create.tmpl", data) } // snippetCreatePost ... func (app *application) snippetCreatePost(w http.ResponseWriter, r *http.Request) { err := r.ParseForm() if err != nil { app.clientError(w, http.StatusBadRequest) return } title := r.PostForm.Get("title") content := r.PostForm.Get("content") expires, err := strconv.Atoi(r.PostForm.Get("expires")) if err != nil { app.clientError(w, http.StatusBadRequest) return } fieldErrors := make(map[string]string) if strings.TrimSpace(title) == "" { fieldErrors["title"] = "This field cannot be blank" } else if utf8.RuneCountInString(title) > 100 { fieldErrors["title"] = "This field cannot contain more than 100 characters" } if strings.TrimSpace(content) == "" { fieldErrors["content"] = "This field cannot be blank" } if expires != 1 && expires != 7 && expires != 365 { fieldErrors["expires"] = "This field must equal 1, 7 or 365" } if len(fieldErrors) > 0 { fmt.Fprint(w, fieldErrors) return } id, err := app.snippets.Insert(title, content, expires) if err != nil { app.serverError(w, r, err) return } http.Redirect(w, r, fmt.Sprintf("/snippet/view/%d", id), http.StatusSeeOther) }