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 5, 2025
1 parent 4558cc3 commit 828f234
Show file tree
Hide file tree
Showing 14 changed files with 131 additions and 108 deletions.
3 changes: 1 addition & 2 deletions handlers/artist/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/blackfyre/wga/utils"
"github.com/blackfyre/wga/utils/jsonld"
"github.com/blackfyre/wga/utils/url"
"github.com/labstack/echo/v5"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/core"
)
Expand Down Expand Up @@ -334,7 +333,7 @@ func processArtwork(c *core.RequestEvent, app *pocketbase.PocketBase) error {
return c.HTML(http.StatusOK, buff.String())
}

func RenderArtworkContent(app *pocketbase.PocketBase, c echo.Context, artwork *core.Record, hxTarget string) (dto.Artwork, error) {
func RenderArtworkContent(app *pocketbase.PocketBase, c *core.RequestEvent, artwork *core.Record, hxTarget string) (dto.Artwork, error) {

artistId := cmp.Or(artwork.GetStringSlice("author")[0], "")

Expand Down
2 changes: 1 addition & 1 deletion handlers/artists/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func processArtists(app *pocketbase.PocketBase, c *core.RequestEvent) error {

if err != nil {
app.Logger().Error("Failed to marshal Artist JSON-LD", "error", err.Error())
return apis.NewBadRequestError("Invalid page", err)
return utils.BadRequestError(c)
}

content.Jsonld = fmt.Sprintf(`<script type="application/ld+json">%s</script>`, marshalledJsonLd)
Expand Down
17 changes: 11 additions & 6 deletions handlers/artworks/filters.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package artworks

import (
"cmp"

"github.com/pocketbase/dbx"
"github.com/pocketbase/pocketbase/core"
)
Expand Down Expand Up @@ -95,13 +97,16 @@ func (f *filters) BuildFilterString() string {
}

func buildFilters(c *core.RequestEvent) *filters {

q := c.Request.URL.Query()

f := &filters{
Title: c.Quer("title", ""),
SchoolString: c.QueryParamDefault("art_school", ""),
ArtFormString: c.QueryParamDefault("art_form", ""),
ArtTypeString: c.QueryParamDefault("art_type", ""),
ArtistString: c.QueryParamDefault("artist", ""),
Page: c.QueryParamDefault("page", ""),
Title: cmp.Or(q.Get("title"), ""),
SchoolString: cmp.Or(q.Get("art_school"), ""),
ArtFormString: cmp.Or(q.Get("art_form"), ""),
ArtTypeString: cmp.Or(q.Get("art_type"), ""),
ArtistString: cmp.Or(q.Get("artist"), ""),
Page: cmp.Or(q.Get("page"), ""),
}

return f
Expand Down
67 changes: 38 additions & 29 deletions handlers/artworks/main.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package artworks

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

"github.com/blackfyre/wga/assets/templ/dto"
"github.com/blackfyre/wga/assets/templ/pages"
tmplUtils "github.com/blackfyre/wga/assets/templ/utils"
"github.com/blackfyre/wga/models"
"github.com/blackfyre/wga/utils"
"github.com/blackfyre/wga/utils/url"
"github.com/labstack/echo/v5"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/apis"
"github.com/pocketbase/pocketbase/core"
)

Expand Down Expand Up @@ -47,30 +45,36 @@ func searchPage(app *pocketbase.PocketBase, c *core.RequestEvent) error {
ctx = tmplUtils.DecorateContext(ctx, tmplUtils.DescriptionKey, "On this page you can search for artworks by title, artist, art form, art type and art school!")
ctx = tmplUtils.DecorateContext(ctx, tmplUtils.OgUrlKey, fullUrl)

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

var buff bytes.Buffer

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

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

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

}

func search(app *pocketbase.PocketBase, c echo.Context) error {
func search(app *pocketbase.PocketBase, c *core.RequestEvent) error {

limit := 16
page := 1
offset := 0

if c.QueryParam("page") != "" {
queryParams := c.Request.URL.Query()

if queryParams.Has("page") {
err := error(nil)
page, err = strconv.Atoi(c.QueryParam("page"))
page, err = strconv.Atoi(queryParams.Get("page"))

if err != nil {
return apis.NewBadRequestError("Invalid page", err)
app.Logger().Error("Failed to parse page number", "error", err.Error())
return utils.BadRequestError(c)
}
}

Expand All @@ -81,7 +85,7 @@ func search(app *pocketbase.PocketBase, c echo.Context) error {

filterString, filterParams := filters.BuildFilter()

records, err := app.Dao().FindRecordsByFilter(
records, err := app.FindRecordsByFilter(
"artworks",
filterString,
"+title",
Expand All @@ -91,11 +95,12 @@ func search(app *pocketbase.PocketBase, c echo.Context) error {
)

if err != nil {
return apis.NewBadRequestError("Invalid page", err)
app.Logger().Error("Failed to get artwork records", "error", err.Error())
return utils.ServerFaultError(c)
}

// this could be replaced with a dedicated sql query, but this is more convinient
totalRecords, err := app.Dao().FindRecordsByFilter(
// this could be replaced with a dedicated SQL query, but this is more convinient
totalRecords, err := app.FindRecordsByFilter(
"artworks",
filterString,
"",
Expand All @@ -105,7 +110,8 @@ func search(app *pocketbase.PocketBase, c echo.Context) error {
)

if err != nil {
return apis.NewBadRequestError("Invalid page", err)
app.Logger().Error("Failed to count artwork records", "error", err.Error())
return utils.ServerFaultError(c)
}

recordsCount := len(totalRecords)
Expand Down Expand Up @@ -139,7 +145,7 @@ func search(app *pocketbase.PocketBase, c echo.Context) error {
continue
}

artist, err := models.GetArtistById(app.Dao(), artistIds[0])
artist, err := app.FindRecordById("artists", artistIds[0])

if err != nil {
// waiting for the promised logging system by @pocketbase
Expand All @@ -148,25 +154,25 @@ func search(app *pocketbase.PocketBase, c echo.Context) error {

content.Results.Artworks = append(content.Results.Artworks, dto.Image{
Url: url.GenerateFullArtworkUrl(url.ArtworkUrlDTO{
ArtistName: artist.Name,
ArtistId: artist.Id,
ArtistName: artist.GetString("name"),
ArtistId: artist.GetString("id"),
ArtworkTitle: v.GetString("title"),
ArtworkId: v.GetId(),
ArtworkId: v.GetString("id"),
}),
Image: url.GenerateFileUrl("artworks", v.GetString("id"), v.GetString("image"), ""),
Thumb: url.GenerateThumbUrl("artworks", v.GetString("id"), v.GetString("image"), "320x240", ""),
Comment: v.GetString("comment"),
Title: v.GetString("title"),
Technique: v.GetString("technique"),
Id: v.GetId(),
Id: v.GetString("id"),
Artist: dto.Artist{
Id: artist.Id,
Name: artist.Name,
Id: artist.GetString("id"),
Name: artist.GetString("name"),
Url: url.GenerateArtistUrl(url.ArtistUrlDTO{
ArtistId: artist.Id,
ArtistName: artist.Name,
ArtistName: artist.GetString("name"),
}),
Profession: artist.Profession,
Profession: artist.GetString("profession"),
},
})
}
Expand All @@ -182,25 +188,28 @@ func search(app *pocketbase.PocketBase, c echo.Context) error {
ctx = tmplUtils.DecorateContext(ctx, tmplUtils.DescriptionKey, "On this page you can search for artworks by title, artist, art form, art type and art school!")
ctx = tmplUtils.DecorateContext(ctx, tmplUtils.OgUrlKey, pHtmxUrl)

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

var buff bytes.Buffer

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

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

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

// RegisterArtworksHandlers registers search handlers to the given PocketBase app.
func RegisterArtworksHandlers(app *pocketbase.PocketBase) {
app.OnServe().BindFunc(func(e *core.ServeEvent) error {
e.Router.GET("/artworks", func(*core.RequestEvent) error {
e.Router.GET("/artworks", func(c *core.RequestEvent) error {
return searchPage(app, c)
})

e.Router.GET("/artworks/results", func(*core.RequestEvent) error {
e.Router.GET("/artworks/results", func(c *core.RequestEvent) error {
return search(app, c)
})
return nil
Expand Down
32 changes: 18 additions & 14 deletions handlers/dual/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/blackfyre/wga/handlers/artworks"
"github.com/blackfyre/wga/utils"
"github.com/blackfyre/wga/utils/url"
"github.com/labstack/echo/v5"
"github.com/pocketbase/pocketbase"
"github.com/pocketbase/pocketbase/core"
)
Expand Down Expand Up @@ -42,7 +41,7 @@ type renderPaneDto struct {
// It renders the dual page using the contentDto and writes the response to the context's writer.
// If there is an error during the rendering process, it logs the error and returns a server fault error.
// Finally, it returns nil if the rendering process is successful.
func renderDualModePage(app *pocketbase.PocketBase, c echo.Context) error {
func renderDualModePage(app *pocketbase.PocketBase, c *core.RequestEvent) error {
ctx := tmplUtils.DecorateContext(context.Background(), tmplUtils.TitleKey, "Dual View")
ctx = tmplUtils.DecorateContext(ctx, tmplUtils.DescriptionKey, "On this page you can search for artworks by title, artist, art form, art type and art school!")
// ctx = tmplUtils.DecorateContext(ctx, tmplUtils.OgUrlKey, pHtmxUrl)
Expand Down Expand Up @@ -79,15 +78,18 @@ func renderDualModePage(app *pocketbase.PocketBase, c echo.Context) error {
relPath.Query().Add("left_render_to", rightPane.Side)
relPath.Query().Add("right_render_to", leftPane.Side)

c.Response().Header().Set("HX-Push-Url", relPath.String())
err = pages.DualPage(contentDto).Render(ctx, c.Response().Writer)
c.Response.Header().Set("HX-Push-Url", relPath.String())

var buff bytes.Buffer

err = pages.DualPage(contentDto).Render(ctx, &buff)

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

return nil
return c.HTML(200, buff.String())
}

// formatArtistNameList formats the artist name list.
Expand Down Expand Up @@ -117,10 +119,12 @@ func reverseSide(side string) string {
return ""
}

func renderPane(side string, app *pocketbase.PocketBase, c echo.Context) (renderPaneDto, error) {
func renderPane(side string, app *pocketbase.PocketBase, c *core.RequestEvent) (renderPaneDto, error) {

rawQParams := c.Request.URL.Query()

queryParam := cmp.Or(c.QueryParam(side), "default")
renderTo := cmp.Or(c.QueryParam(side+"_render_to"), reverseSide(side))
queryParam := cmp.Or(rawQParams.Get(side), "default")
renderTo := cmp.Or(rawQParams.Get(side+"_render_to"), reverseSide(side))

pane := renderPaneDto{
Side: side,
Expand Down Expand Up @@ -195,8 +199,8 @@ func renderPane(side string, app *pocketbase.PocketBase, c echo.Context) (render
return pane, nil
}

func renderArtistPane(app *pocketbase.PocketBase, c echo.Context, artistId string, renderTo string, buf *bytes.Buffer) (dto.Artist, error) {
artistModel, err := app.Dao().FindRecordById("artists", artistId)
func renderArtistPane(app *pocketbase.PocketBase, c *core.RequestEvent, artistId string, renderTo string, buf *bytes.Buffer) (dto.Artist, error) {
artistModel, err := app.FindRecordById("artists", artistId)

if err != nil {
app.Logger().Error("Error finding artist", "error", err.Error())
Expand All @@ -220,8 +224,8 @@ func renderArtistPane(app *pocketbase.PocketBase, c echo.Context, artistId strin
return artistDto, nil
}

func renderArtworkPane(app *pocketbase.PocketBase, c echo.Context, artworkId string, renderTo string, buf *bytes.Buffer) (dto.Artwork, error) {
artworkModel, err := app.Dao().FindRecordById("artworks", artworkId)
func renderArtworkPane(app *pocketbase.PocketBase, c *core.RequestEvent, artworkId string, renderTo string, buf *bytes.Buffer) (dto.Artwork, error) {
artworkModel, err := app.FindRecordById("artworks", artworkId)

if err != nil {
app.Logger().Error("Error finding artwork", "error", err.Error())
Expand All @@ -246,8 +250,8 @@ func renderArtworkPane(app *pocketbase.PocketBase, c echo.Context, artworkId str
}

func RegisterHandlers(app *pocketbase.PocketBase) {
app.OnBeforeServe().Add(func(e *core.ServeEvent) error {
e.Router.GET("/dual-mode", func(c echo.Context) error {
app.OnServe().BindFunc(func(e *core.ServeEvent) error {
e.Router.GET("/dual-mode", func(c *core.RequestEvent) error {
return renderDualModePage(app, c)
})
return nil
Expand Down
Loading

0 comments on commit 828f234

Please sign in to comment.