|
|
|
@ -10,11 +10,12 @@ import (
|
|
|
|
|
|
|
|
|
|
"git.runcible.io/learning/ratchet/internal/model"
|
|
|
|
|
"git.runcible.io/learning/ratchet/internal/validator"
|
|
|
|
|
"github.com/alexedwards/scs/v2"
|
|
|
|
|
"github.com/go-playground/form/v4"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// TODO function should accept and a pointer to an interface allowing for mocking in tests.
|
|
|
|
|
func handleHome(logger *slog.Logger, tc *TemplateCache, snippetService *model.SnippetService) http.Handler {
|
|
|
|
|
func handleHome(logger *slog.Logger, tc *TemplateCache, sm *scs.SessionManager, snippetService *model.SnippetService) http.Handler {
|
|
|
|
|
return http.HandlerFunc(
|
|
|
|
|
func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
// Retrieve Snippets from DB
|
|
|
|
@ -30,7 +31,7 @@ func handleHome(logger *slog.Logger, tc *TemplateCache, snippetService *model.Sn
|
|
|
|
|
// data := templateData{
|
|
|
|
|
// Snippets: snippets,
|
|
|
|
|
// }
|
|
|
|
|
data := newTemplateData()
|
|
|
|
|
data := newTemplateData(r, sm)
|
|
|
|
|
data.Snippets = snippets
|
|
|
|
|
|
|
|
|
|
renderTemplate(w, r, tc, http.StatusOK, "home.go.tmpl", data)
|
|
|
|
@ -62,7 +63,7 @@ func handleHome(logger *slog.Logger, tc *TemplateCache, snippetService *model.Sn
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func handleSnippetView(logger *slog.Logger, tc *TemplateCache, snippetService *model.SnippetService) http.Handler {
|
|
|
|
|
func handleSnippetView(logger *slog.Logger, tc *TemplateCache, sm *scs.SessionManager, snippetService *model.SnippetService) http.Handler {
|
|
|
|
|
return http.HandlerFunc(
|
|
|
|
|
func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
id, err := strconv.Atoi(r.PathValue("id"))
|
|
|
|
@ -85,6 +86,15 @@ func handleSnippetView(logger *slog.Logger, tc *TemplateCache, snippetService *m
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Use the PopString() method to retrieve the value for the "flash" key.
|
|
|
|
|
// PopString() also deletes the key and value from the session data, so it
|
|
|
|
|
// acts like a one-time fetch. If there is no matching key in the session
|
|
|
|
|
// data this will return the empty string.
|
|
|
|
|
|
|
|
|
|
// See also GetInt, GetBool, GetBytes, GetTime etc.
|
|
|
|
|
// NOW DONE IN TEMPLATE DATA FUNC
|
|
|
|
|
// flash := sm.PopString(r.Context(), "flash")
|
|
|
|
|
|
|
|
|
|
// files := []string{
|
|
|
|
|
// "./ui/html/base.go.tmpl",
|
|
|
|
|
// "./ui/html/partials/nav.go.tmpl",
|
|
|
|
@ -117,15 +127,16 @@ func handleSnippetView(logger *slog.Logger, tc *TemplateCache, snippetService *m
|
|
|
|
|
// data := templateData{
|
|
|
|
|
// Snippet: snippet,
|
|
|
|
|
// }
|
|
|
|
|
data := newTemplateData()
|
|
|
|
|
data := newTemplateData(r, sm)
|
|
|
|
|
data.Snippet = snippet
|
|
|
|
|
data.Flash = flash
|
|
|
|
|
renderTemplate(w, r, tc, http.StatusOK, "view.go.tmpl", data)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func handleSnippetCreateGet(tc *TemplateCache) http.Handler {
|
|
|
|
|
func handleSnippetCreateGet(tc *TemplateCache, sm *scs.SessionManager) http.Handler {
|
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
data := newTemplateData()
|
|
|
|
|
data := newTemplateData(r, sm)
|
|
|
|
|
// Initialize a new snippetCreateForm instance and pass it to the template.
|
|
|
|
|
// Notice how this is also a great opportunity to set any default or
|
|
|
|
|
// 'initial' values for the form --- here we set the initial value for the
|
|
|
|
@ -140,7 +151,7 @@ func handleSnippetCreateGet(tc *TemplateCache) http.Handler {
|
|
|
|
|
// snippetCreate handles display of the form used to create snippets
|
|
|
|
|
//
|
|
|
|
|
// curl -iL -d "" http://localhost:5001/snippet/create
|
|
|
|
|
func handleSnippetCreatePost(logger *slog.Logger, tc *TemplateCache, formDecoder *form.Decoder, snippetService *model.SnippetService) http.Handler {
|
|
|
|
|
func handleSnippetCreatePost(logger *slog.Logger, tc *TemplateCache, formDecoder *form.Decoder, sm *scs.SessionManager, snippetService *model.SnippetService) http.Handler {
|
|
|
|
|
return http.HandlerFunc(
|
|
|
|
|
func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
// example of a custom header. Must be done before calling WriteHeader
|
|
|
|
@ -242,7 +253,7 @@ func handleSnippetCreatePost(logger *slog.Logger, tc *TemplateCache, formDecoder
|
|
|
|
|
form.CheckField(validator.PermittedValue(form.Expires, 1, 7, 365), "expires", "this field cannot be blank")
|
|
|
|
|
|
|
|
|
|
if !form.Valid() {
|
|
|
|
|
data := newTemplateData()
|
|
|
|
|
data := newTemplateData(r, sm)
|
|
|
|
|
data.Form = form
|
|
|
|
|
renderTemplate(w, r, tc, http.StatusUnprocessableEntity, "create.go.tmpl", data)
|
|
|
|
|
return
|
|
|
|
@ -264,6 +275,10 @@ func handleSnippetCreatePost(logger *slog.Logger, tc *TemplateCache, formDecoder
|
|
|
|
|
}
|
|
|
|
|
logger.Info(fmt.Sprintf("Inserted record. id: %d", id))
|
|
|
|
|
|
|
|
|
|
// Use the Put() method to add a string value ("Snippet successfully
|
|
|
|
|
// created!") and the corresponding key ("flash") to the session data.
|
|
|
|
|
sm.Put(r.Context(), "flash", "Snippet successfully created!")
|
|
|
|
|
|
|
|
|
|
http.Redirect(w, r, fmt.Sprintf("/snippet/view/%d", id), http.StatusSeeOther)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|