Skip to content

Commit

Permalink
chore: dependency update fallout
Browse files Browse the repository at this point in the history
  • Loading branch information
blackfyre committed Feb 6, 2025
1 parent dc079d1 commit 7c690ca
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 31 deletions.
44 changes: 34 additions & 10 deletions handlers/guestbook/main.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package guestbook

import (
"bytes"
"cmp"
"context"
"fmt"
"net/http"
"time"

"github.com/blackfyre/wga/assets/templ/pages"
wgaModels "github.com/blackfyre/wga/models"
"github.com/blackfyre/wga/utils"
"github.com/labstack/echo/v5"
"github.com/blackfyre/wga/utils/url"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/apis"
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/forms"
"github.com/pocketbase/pocketbase/models"

Expand All @@ -36,12 +40,29 @@ func yearOptions() []string {
return years
}

func convertRawEntriesToGuestbookEntries(entries []*core.Record) []wgaModels.GuestbookEntry {
var guestbookEntries []wgaModels.GuestbookEntry

for _, entry := range entries {
guestbookEntries = append(guestbookEntries, wgaModels.GuestbookEntry{
Name: entry.GetString("name"),
Email: entry.GetString("email"),
Location: entry.GetString("location"),
Message: entry.GetString("message"),
Year: entry.GetString("year"),
})
}

return guestbookEntries
}

func EntriesHandler(app *pocketbase.PocketBase, c *core.RequestEvent) error {

fullUrl := c.Scheme() + "://" + c.Request().Host + c.Request().URL.String()
year := c.QueryParamDefault("year", fmt.Sprintf("%d", time.Now().Year()))
fullUrl := url.GenerateCurrentPageUrl(c)
year := cmp.Or(c.Request.URL.Query().Get("year"), fmt.Sprintf("%d", time.Now().Year()))

entries, err := wgaModels.FindEntriesForYear(app.Dao(), year)
// entries, err := wgaModels.FindEntriesForYear(app.Dao(), year)
entries, err := app.FindRecordsByFilter("Guestbook", "year", year, 0, 0)

if err != nil {
app.Logger().Error("Failed to get guestbook entries", "error", err)
Expand All @@ -51,21 +72,24 @@ func EntriesHandler(app *pocketbase.PocketBase, c *core.RequestEvent) error {
content := pages.GuestbookView{
SelectedYear: year,
YearOptions: yearOptions(),
Entries: entries,
Entries: convertRawEntriesToGuestbookEntries(entries),
}

ctx := tmplUtils.DecorateContext(context.Background(), tmplUtils.TitleKey, "Guestbook")
ctx = tmplUtils.DecorateContext(ctx, tmplUtils.DescriptionKey, "This is the guestbook of the Web Gallery of Art. Please feel free to leave a message.")
ctx = tmplUtils.DecorateContext(ctx, tmplUtils.CanonicalUrlKey, fullUrl)

c.Response().Header().Set("HX-Push-Url", fullUrl)
err = pages.GuestbookPage(content).Render(ctx, c.Response().Writer)
c.Response.Header().Set("HX-Push-Url", fullUrl)

var buff bytes.Buffer

err = pages.GuestbookPage(content).Render(ctx, &buff)

if err != nil {
return utils.ServerFaultError(c)
}

return nil
return c.HTML(http.StatusOK, buff.String())
}

func StoreEntryViewHandler(app *pocketbase.PocketBase, c *core.RequestEvent) error {
Expand Down Expand Up @@ -104,7 +128,7 @@ func StoreEntryHandler(app *pocketbase.PocketBase, c *core.RequestEvent) error {
return c.NoContent(204)
}

collection, err := app.Dao().FindCollectionByNameOrId("Guestbook")
collection, err := app.FindCollectionByNameOrId("Guestbook")
if err != nil {
app.Logger().Error("Database table not found", "error", err.Error())
utils.SendToastMessage("Something went wrong!", "error", true, c, "")
Expand All @@ -117,7 +141,7 @@ func StoreEntryHandler(app *pocketbase.PocketBase, c *core.RequestEvent) error {

form.LoadData(map[string]any{
"email": postData.Email,
"name": postData.GetString("name")
"name": postData.GetString("name"),
"message": postData.Message,
"location": postData.Location,
})
Expand Down
25 changes: 16 additions & 9 deletions handlers/home.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package handlers

import (
"bytes"
"context"
"fmt"
"net/http"

"github.com/blackfyre/wga/assets/templ/pages"
tmplUtils "github.com/blackfyre/wga/assets/templ/utils"
Expand Down Expand Up @@ -34,7 +36,7 @@ func getWelcomeContent(app *pocketbase.PocketBase) (string, error) {
return app.Store().Get("strings:welcome").(string), nil
}

record, err := app.Dao().FindFirstRecordByData("strings", "name", "welcome")
record, err := app.FindFirstRecordByData("strings", "name", "welcome")

if err != nil {
app.Logger().Error("Error getting welcome content", "error", err.Error())
Expand Down Expand Up @@ -66,7 +68,7 @@ func getArtistCount(app *pocketbase.PocketBase) (string, error) {

c := counter{}

err := app.Dao().DB().NewQuery("SELECT COUNT(*) as c FROM artists WHERE published IS true").One(&c)
err := app.DB().NewQuery("SELECT COUNT(*) as c FROM artists WHERE published IS true").One(&c)

if err != nil {
app.Logger().Error("Error getting artist count", "error", err.Error())
Expand Down Expand Up @@ -98,7 +100,7 @@ func getArtworkCount(app *pocketbase.PocketBase) (string, error) {

c := counter{}

err := app.Dao().DB().NewQuery("SELECT COUNT(*) as c FROM artworks WHERE published IS true").One(&c)
err := app.DB().NewQuery("SELECT COUNT(*) as c FROM artworks WHERE published IS true").One(&c)

if err != nil {
app.Logger().Error("Error getting artwork count", "error", err.Error())
Expand All @@ -114,8 +116,8 @@ func getArtworkCount(app *pocketbase.PocketBase) (string, error) {
}

func registerHome(app *pocketbase.PocketBase) {
app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
// this is safe to be used by multiple goroutines
app.OnServe().BindFunc(func(e *core.ServeEvent) error {
// This is safe to be used by multiple goroutines
// (it acts as store for the parsed templates)

e.Router.GET("/", func(c *core.RequestEvent) error {
Expand Down Expand Up @@ -149,17 +151,22 @@ func registerHome(app *pocketbase.PocketBase) {

ctx := tmplUtils.DecorateContext(context.Background(), tmplUtils.TitleKey, "Welcome to the Gallery")
ctx = tmplUtils.DecorateContext(ctx, tmplUtils.DescriptionKey, "Welcome to the Gallery")
ctx = tmplUtils.DecorateContext(ctx, tmplUtils.OgUrlKey, c.Scheme()+"://"+c.Request().Host+c.Request().URL.String())

c.Response().Header().Set("HX-Push-Url", "/")
err = pages.HomePageWrapped(content).Render(ctx, c.Response().Writer)
//TODO: Fix this
// ctx = tmplUtils.DecorateContext(ctx, tmplUtils.OgUrlKey, c.Scheme()+"://"+c.Request().Host+c.Request().URL.String())

c.Response.Header().Set("HX-Push-Url", "/")

var buff bytes.Buffer

err = pages.HomePageWrapped(content).Render(ctx, &buff)

if err != nil {
app.Logger().Error("Error rendering home page", "error", err.Error())
return utils.ServerFaultError(c)
}

return nil
return c.HTML(http.StatusOK, buff.String())
})

return nil
Expand Down
3 changes: 2 additions & 1 deletion handlers/inspire/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package inspire
import (
"bytes"
"context"
"net/http"

"github.com/blackfyre/wga/assets/templ/dto"
"github.com/blackfyre/wga/assets/templ/pages"
Expand Down Expand Up @@ -72,7 +73,7 @@ func inspirationHandler(app *pocketbase.PocketBase, c *core.RequestEvent) error
return utils.ServerFaultError(c)
}

return nil
return c.HTML(http.StatusOK, buff.String())
}

// RegisterHandlers registers the HTTP handlers for the PocketBase application.
Expand Down
6 changes: 3 additions & 3 deletions handlers/musics.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func GetParsedMusics() []Composer_seed {

composers = append(composers, Composer_seed{
ID: id,
Name: composer.GetString("name")
Name: composer.GetString("name"),
Date: composer.Date,
Language: composer.Language,
Century: century.Century,
Expand All @@ -220,7 +220,7 @@ func GetParsedMusics() []Composer_seed {

func getComposers(app *pocketbase.PocketBase) ([]shape.Music_composer, error) {
composers := []shape.Music_composer{}
err := app.Dao().DB().NewQuery("SELECT * FROM music_composer").All(&composers)
err := app.DB().NewQuery("SELECT * FROM music_composer").All(&composers)
if err != nil {
app.Logger().Error("failed to get music composers", "error", err.Error())
return nil, fmt.Errorf("failed to get music composers: %w", err)
Expand All @@ -230,7 +230,7 @@ func getComposers(app *pocketbase.PocketBase) ([]shape.Music_composer, error) {
songs := []shape.Music_song{}

query := "SELECT * FROM music_song WHERE composer_id = {:id}"
err := app.Dao().DB().NewQuery(query).Bind(dbx.Params{"id": composer.ID}).All(&songs)
err := app.DB().NewQuery(query).Bind(dbx.Params{"id": composer.ID}).All(&songs)
if err != nil {
app.Logger().Error("failed to get music song by composer", "error", err.Error())
return nil, fmt.Errorf("failed to get music song by composer: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion handlers/postcards/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func sendPostcard(app *pocketbase.PocketBase, c echo.Context) error {
func renderForm(artworkId string, app *pocketbase.PocketBase, c echo.Context) error {
ctx := context.Background()

r, err := app.Dao().FindRecordById("artworks", artworkId)
r, err := app.FindRecordById("artworks", artworkId)

if err != nil {
app.Logger().Error("Failed to find artwork "+artworkId, "error", err.Error())
Expand Down
7 changes: 0 additions & 7 deletions handlers/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,3 @@ func generateArtistSlug(artist *core.Record) string {
}
return artist.GetString("slug") + "-" + artist.GetString("id")
}

func generateCurrentPageUrl(c *core.RequestEvent) string {
if c == nil || c.Request() == nil {
return ""
}
return c.Scheme() + "://" + c.Request().Host + c.Request().URL.String()
}
7 changes: 7 additions & 0 deletions utils/url/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,10 @@ func GetRequiredQueryParam(c echo.Context, param string) (string, error) {

return p, nil
}

func GenerateCurrentPageUrl(c *core.RequestEvent) string {
if c == nil || c.Request == nil {
return ""
}
return c.Request.URL.Scheme + "://" + c.Request.URL.Host + c.Request.URL.String()
}

0 comments on commit 7c690ca

Please sign in to comment.