2024-01-22 21:25:34 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-01-25 20:41:08 +00:00
|
|
|
"errors"
|
2024-01-22 23:07:14 +00:00
|
|
|
"fmt"
|
2024-01-25 20:41:08 +00:00
|
|
|
//"html/template"
|
2024-01-22 21:25:34 +00:00
|
|
|
"net/http"
|
2024-01-22 23:07:14 +00:00
|
|
|
"strconv"
|
2024-01-25 20:41:08 +00:00
|
|
|
|
|
|
|
"snippetbox.chaosfem.tw/internal/models"
|
2024-01-22 21:25:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// home ...
|
2024-01-24 21:32:07 +00:00
|
|
|
func (app *application) home(w http.ResponseWriter, r *http.Request) {
|
2024-01-22 21:25:34 +00:00
|
|
|
if r.URL.Path != "/" {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
2024-01-23 19:19:53 +00:00
|
|
|
|
2024-01-25 20:41:08 +00:00
|
|
|
snippets, err := app.snippets.Latest()
|
2024-01-23 19:19:53 +00:00
|
|
|
if err != nil {
|
2024-01-25 00:14:33 +00:00
|
|
|
app.serverError(w, r, err)
|
2024-01-23 19:19:53 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-25 20:41:08 +00:00
|
|
|
for _, snippet := range snippets {
|
|
|
|
fmt.Fprintf(w, "%+v", snippet)
|
2024-01-23 19:19:53 +00:00
|
|
|
}
|
2024-01-25 20:41:08 +00:00
|
|
|
|
|
|
|
// files := []string{
|
|
|
|
// "./ui/html/base.tmpl",
|
|
|
|
// "./ui/html/partials/nav.tmpl",
|
|
|
|
// "./ui/html/pages/home.tmpl",
|
|
|
|
// }
|
|
|
|
|
|
|
|
// ts, err := template.ParseFiles(files...)
|
|
|
|
// if err != nil {
|
|
|
|
// app.serverError(w, r, err)
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
|
|
|
|
// err = ts.ExecuteTemplate(w, "base", nil)
|
|
|
|
// if err != nil {
|
|
|
|
// app.serverError(w, r, err)
|
|
|
|
// }
|
2024-01-22 21:25:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// snippetView ...
|
2024-01-24 21:32:07 +00:00
|
|
|
func (app *application) snippetView(w http.ResponseWriter, r *http.Request) {
|
2024-01-22 23:07:14 +00:00
|
|
|
id, err := strconv.Atoi(r.URL.Query().Get("id"))
|
|
|
|
if err != nil || id < 1 {
|
2024-01-25 00:14:33 +00:00
|
|
|
app.notFound(w)
|
2024-01-22 23:07:14 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-25 20:41:08 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintf(w, "%+v", snippet)
|
2024-01-22 21:25:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// snippetCreate ...
|
2024-01-24 21:32:07 +00:00
|
|
|
func (app *application) snippetCreate(w http.ResponseWriter, r *http.Request) {
|
2024-01-22 21:25:34 +00:00
|
|
|
w.Header().Set("Allow", "POST")
|
|
|
|
|
|
|
|
if r.Method != http.MethodPost {
|
2024-01-25 00:14:33 +00:00
|
|
|
app.clientError(w, http.StatusMethodNotAllowed)
|
2024-01-22 21:25:34 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-25 20:41:08 +00:00
|
|
|
title := "0 snail"
|
|
|
|
content := "0 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, r, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
http.Redirect(w, r, fmt.Sprintf("/snippet/view?id=%d", id), http.StatusSeeOther)
|
2024-01-22 21:25:34 +00:00
|
|
|
}
|