mirror of
https://github.com/acaloiaro/garbagespeak.com
synced 2026-07-21 10:12:19 +00:00
Add 'show' garbage route
This commit is contained in:
parent
f838799ac9
commit
1fd47cd8a7
3 changed files with 94 additions and 17 deletions
48
partials/garbage/list.html
Normal file
48
partials/garbage/list.html
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
<div class="posts">
|
||||
{{range .Posts}}
|
||||
<article class="post on-list">
|
||||
<h1 class="post-title">{{ .Title }}</h1>
|
||||
<div class="post-meta">
|
||||
{{- with .Username -}}
|
||||
<span>Submitter: {{ . }}</span>
|
||||
{{- end -}}
|
||||
{{- with .CreatedAt -}}
|
||||
<time class="post-date">
|
||||
{{- .Format "2006-01-02" -}}
|
||||
</time>
|
||||
{{- end -}}
|
||||
|
||||
<a href="#"
|
||||
hx-get="{{ $.ApiBaseUrl }}/garbage/{{ .ID }}"
|
||||
hx-target="#content">Link</a>
|
||||
|
||||
{{ if eq .OwnerID.String $.UserID }}
|
||||
<a href="#"
|
||||
hx-get="{{ $.ApiBaseUrl }}/garbage/{{ .ID }}/edit"
|
||||
hx-target="#content"
|
||||
hx-swap="outerHTML">Edit</a>
|
||||
{{ end }}
|
||||
</div>
|
||||
|
||||
<div class="post-content">
|
||||
{{ .Content }}
|
||||
</div>
|
||||
|
||||
<br/>
|
||||
<div class="post-meta">
|
||||
{{ if .Metadata.tags }}
|
||||
<span>tags</span>
|
||||
<span>
|
||||
{{ range .Metadata.tags }}
|
||||
{{- . -}},
|
||||
{{ end }}
|
||||
</span>
|
||||
{{ end }}
|
||||
</div>
|
||||
</article>
|
||||
{{end}}
|
||||
</div>
|
||||
|
||||
{{- with .Alert -}}
|
||||
<div id="alert" remove-me="5s" class="alert">{{ . }}</div>
|
||||
{{ end }}
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
<div class="posts">
|
||||
{{ $apiBaseURL := .ApiBaseUrl }}
|
||||
{{range .Posts}}
|
||||
{{ with .Garbage }}
|
||||
<article class="post on-list">
|
||||
<h1 class="post-title">{{ .Title }}</h1>
|
||||
<div class="post-meta">
|
||||
|
|
@ -21,7 +21,6 @@
|
|||
{{ end }}
|
||||
</div>
|
||||
|
||||
|
||||
<div class="post-content">
|
||||
{{ .Content }}
|
||||
</div>
|
||||
60
server.go
60
server.go
|
|
@ -204,6 +204,7 @@ func main() {
|
|||
garbage.Post("/new", createGarbageHandler)
|
||||
garbage.Get("/{garbage_id}/edit", editGarbageHandler)
|
||||
garbage.Put("/{garbage_id}", editGarbageUpdateHandler)
|
||||
garbage.Get("/{garbage_id}", showGarbageHandler)
|
||||
})
|
||||
})
|
||||
|
||||
|
|
@ -310,7 +311,7 @@ func editGarbageHandler(w http.ResponseWriter, r *http.Request) {
|
|||
"SelectedTags": selectedTags,
|
||||
"AvailableTags": availableTags,
|
||||
}
|
||||
tmpl := template.Must(template.ParseFS(partials, "partials/garbage/*"))
|
||||
tmpl := template.Must(template.ParseFS(partials, "partials/garbage/edit.html"))
|
||||
err = tmpl.ExecuteTemplate(w, "edit.html", tmplVars)
|
||||
if err != nil {
|
||||
ise(err, w)
|
||||
|
|
@ -324,7 +325,7 @@ func editGarbageHandler(w http.ResponseWriter, r *http.Request) {
|
|||
// newUserValidationHandler checks whether a username is available during account creation
|
||||
func newUserValidationHandler(w http.ResponseWriter, r *http.Request) {
|
||||
tmplVars := map[string]any{"ApiBaseUrl": apiURL()}
|
||||
tmpl := template.Must(template.ParseFS(partials, "partials/users/*"))
|
||||
tmpl := template.Must(template.ParseFS(partials, "partials/users/new_user_validation.html"))
|
||||
|
||||
errCnt := 0
|
||||
|
||||
|
|
@ -409,7 +410,7 @@ func loginHandler(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
tmpl := template.Must(template.ParseFS(partials, "partials/users/*"))
|
||||
tmpl := template.Must(template.ParseFS(partials, "partials/users/login.html"))
|
||||
err = tmpl.ExecuteTemplate(w, "login.html", map[string]any{
|
||||
"ApiBaseURL": apiURL(),
|
||||
"LoginError": "Incorrect username or password",
|
||||
|
|
@ -436,7 +437,7 @@ func navUserItems(w http.ResponseWriter, r *http.Request) {
|
|||
var tmpl *template.Template
|
||||
|
||||
var err error
|
||||
tmpl = template.Must(template.ParseFS(partials, "partials/nav/*"))
|
||||
tmpl = template.Must(template.ParseFS(partials, "partials/nav/*.html"))
|
||||
if isLoggedIn(r) {
|
||||
err = tmpl.ExecuteTemplate(w, "user_nav_items.html", map[string]any{"ApiURL": apiURL()})
|
||||
} else {
|
||||
|
|
@ -630,19 +631,13 @@ func createAccountHandler(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
// listGarbageHandler returns the latest garbage
|
||||
func listGarbageHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
userID := sessions.GetString(r.Context(), "userID")
|
||||
name := r.URL.Query().Get("name")
|
||||
if name == "null" || name == "" {
|
||||
name = "World"
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
posts := []*Garbage{}
|
||||
garbage := []*Garbage{}
|
||||
err := pgxscan.Select(
|
||||
ctx,
|
||||
db,
|
||||
&posts,
|
||||
&garbage,
|
||||
`SELECT garbages.id, owner_id, username, title, content, metadata, url, garbages.created_at
|
||||
FROM garbages
|
||||
JOIN users ON garbages.owner_id = users.id
|
||||
|
|
@ -652,9 +647,44 @@ func listGarbageHandler(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
tmpl := template.Must(template.ParseFS(partials, "partials/posts.html"))
|
||||
err = tmpl.ExecuteTemplate(w, "posts.html", map[string]any{
|
||||
"Posts": posts,
|
||||
tmpl := template.Must(template.ParseFS(partials, "partials/garbage/list.html"))
|
||||
err = tmpl.ExecuteTemplate(w, "list.html", map[string]any{
|
||||
"Posts": garbage,
|
||||
"ApiBaseUrl": apiURL(),
|
||||
"LoggedIn": isLoggedIn(r),
|
||||
"UserID": userID,
|
||||
})
|
||||
if err != nil {
|
||||
ise(err, w)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
// showGarbageHandler returns the latest garbage
|
||||
func showGarbageHandler(w http.ResponseWriter, r *http.Request) {
|
||||
garbageID := chi.URLParam(r, "garbage_id")
|
||||
userID := sessions.GetString(r.Context(), "userID")
|
||||
ctx := context.Background()
|
||||
garbage := Garbage{}
|
||||
err := pgxscan.Get(
|
||||
ctx,
|
||||
db,
|
||||
&garbage,
|
||||
`SELECT garbages.id, owner_id, username, title, content, metadata, url, garbages.created_at
|
||||
FROM garbages
|
||||
JOIN users ON garbages.owner_id = users.id
|
||||
WHERE garbages.id = $1
|
||||
ORDER BY created_at DESC`, garbageID)
|
||||
if err != nil {
|
||||
ise(err, w)
|
||||
return
|
||||
}
|
||||
|
||||
tmpl := template.Must(template.ParseFS(partials, "partials/garbage/show.html"))
|
||||
err = tmpl.ExecuteTemplate(w, "show.html", map[string]any{
|
||||
"Garbage": garbage,
|
||||
"ApiBaseUrl": apiURL(),
|
||||
"LoggedIn": isLoggedIn(r),
|
||||
"UserID": userID,
|
||||
|
|
|
|||
Loading…
Reference in a new issue