From fdfde8636033ed56977a5395e97bb36acf0ee14f Mon Sep 17 00:00:00 2001 From: Araozu Date: Fri, 18 Oct 2024 21:51:45 -0500 Subject: [PATCH] feat: search albums --- package.json | 2 +- src/modules/album/album.go | 26 ++++++- src/modules/album/album.service.go | 25 +++++++ src/modules/album/album.templ | 29 +++++++- src/modules/album/album_templ.go | 111 ++++++++++++++++++++--------- src/utils/utils.templ | 2 +- src/utils/utils_templ.go | 2 +- 7 files changed, 157 insertions(+), 40 deletions(-) diff --git a/package.json b/package.json index 245bd95..4f50fb9 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "scripts": { "air": "air", "build": "tailwindcss -i ./public/css/input.css -o ./public/css/output.css --minify", - "dev": "concurrently 'air' 'tailwindcss -i ./public/css/input.css -o ./public/css/output.css --watch' 'templ generate --watch'", + "dev": "concurrently -c green,cyan,yellow 'air' 'tailwindcss -i ./public/css/input.css -o ./public/css/output.css --watch' 'templ generate --watch'", "tailwind:watch": "tailwindcss -i ./public/css/input.css -o ./public/css/output.css --watch", "tailwind:build": "tailwindcss -i ./public/css/input.css -o ./public/css/output.css --minify", "temple:watch": "templ generate --watch" diff --git a/src/modules/album/album.go b/src/modules/album/album.go index 222f0ae..99c0f73 100644 --- a/src/modules/album/album.go +++ b/src/modules/album/album.go @@ -27,14 +27,36 @@ func Setup(g *echo.Group) { } func allAlbumsPage(c echo.Context) error { + // if there's a search query, do that + searchQuery := c.QueryParam("s") + isHtmxRequest := c.Request().Header.Get("HX-Request") == "true" + // get the first 10 albums token, server := utils.Credentials(c) - albums, err := loadAlbums(token, server, 0, 30) + + var ( + albums []utils.Album + err error + ) + if searchQuery != "" { + // search for the requested albums + albums, err = searchAlbums(token, searchQuery, server, 0, 20) + } else { + // get 10 random albums + albums, err = loadAlbums(token, server, 0, 30) + } + if err != nil { return err } - return utils.RenderTempl(c, http.StatusOK, allAlbumsTempl(albums)) + if isHtmxRequest { + // return just a fragment + return utils.RenderTempl(c, http.StatusOK, albumsFragment(albums)) + } else { + // return a full-blown html page + return utils.RenderTempl(c, http.StatusOK, allAlbumsTempl(albums)) + } } func albumPage(c echo.Context) error { diff --git a/src/modules/album/album.service.go b/src/modules/album/album.service.go index 276d3be..1a0f9ea 100644 --- a/src/modules/album/album.service.go +++ b/src/modules/album/album.service.go @@ -4,10 +4,35 @@ import ( "acide/src/utils" "errors" "fmt" + "net/url" "github.com/go-resty/resty/v2" ) +func searchAlbums(token, query, server string, start, end int) ([]utils.Album, error) { + var albums []utils.Album + var error utils.NavError + + encodedQuery := url.QueryEscape(query) + + client := resty.New() + response, err := client.R(). + SetHeader("x-nd-authorization", fmt.Sprintf("Bearer %s", token)). + SetResult(&albums). + SetError(&error). + Get(fmt.Sprintf("%s/api/album?_start=%d&_end=%d&_sort=name&_order=ASC&name=%s", server, start, end, encodedQuery)) + + if err != nil { + return nil, err + } + + if !response.IsSuccess() { + return nil, errors.New(error.Error) + } + + return albums, nil +} + func loadAlbums(token, server string, start, end int) ([]utils.Album, error) { var albums []utils.Album var error utils.NavError diff --git a/src/modules/album/album.templ b/src/modules/album/album.templ index 1a3f744..64f6415 100644 --- a/src/modules/album/album.templ +++ b/src/modules/album/album.templ @@ -14,15 +14,42 @@ templ allAlbumsTempl(albums []utils.Album) {

Albums

-
+
+ +
+
+
for _, album := range albums { @index.AlbumCard(album) } + if len(albums) == 0 { +

+ No albums found +

+ }
} } +templ albumsFragment(albums []utils.Album) { + for _, album := range albums { + @index.AlbumCard(album) + } + if len(albums) == 0 { +

+ No albums found +

+ } +} + // Renders the page of a single Album templ albumTempl(albumId string, album *utils.Album, songs []utils.Song, songsJson string) { @utils.SkeletonTempl() { diff --git a/src/modules/album/album_templ.go b/src/modules/album/album_templ.go index d661430..61663d7 100644 --- a/src/modules/album/album_templ.go +++ b/src/modules/album/album_templ.go @@ -49,7 +49,7 @@ func allAlbumsTempl(albums []utils.Album) templ.Component { }() } ctx = templ.InitializeContext(ctx) - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("

Albums

") + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("

Albums


") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -59,6 +59,12 @@ func allAlbumsTempl(albums []utils.Album) templ.Component { return templ_7745c5c3_Err } } + if len(albums) == 0 { + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("

No albums found

") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + } _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err @@ -73,6 +79,43 @@ func allAlbumsTempl(albums []utils.Album) templ.Component { }) } +func albumsFragment(albums []utils.Album) templ.Component { + return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { + templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context + if templ_7745c5c3_CtxErr := ctx.Err(); templ_7745c5c3_CtxErr != nil { + return templ_7745c5c3_CtxErr + } + templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) + if !templ_7745c5c3_IsBuffer { + defer func() { + templ_7745c5c3_BufErr := templruntime.ReleaseBuffer(templ_7745c5c3_Buffer) + if templ_7745c5c3_Err == nil { + templ_7745c5c3_Err = templ_7745c5c3_BufErr + } + }() + } + ctx = templ.InitializeContext(ctx) + templ_7745c5c3_Var3 := templ.GetChildren(ctx) + if templ_7745c5c3_Var3 == nil { + templ_7745c5c3_Var3 = templ.NopComponent + } + ctx = templ.ClearChildren(ctx) + for _, album := range albums { + templ_7745c5c3_Err = index.AlbumCard(album).Render(ctx, templ_7745c5c3_Buffer) + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + } + if len(albums) == 0 { + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("

No albums found

") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + } + return templ_7745c5c3_Err + }) +} + // Renders the page of a single Album func albumTempl(albumId string, album *utils.Album, songs []utils.Song, songsJson string) templ.Component { return templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { @@ -90,12 +133,12 @@ func albumTempl(albumId string, album *utils.Album, songs []utils.Song, songsJso }() } ctx = templ.InitializeContext(ctx) - templ_7745c5c3_Var3 := templ.GetChildren(ctx) - if templ_7745c5c3_Var3 == nil { - templ_7745c5c3_Var3 = templ.NopComponent + templ_7745c5c3_Var4 := templ.GetChildren(ctx) + if templ_7745c5c3_Var4 == nil { + templ_7745c5c3_Var4 = templ.NopComponent } ctx = templ.ClearChildren(ctx) - templ_7745c5c3_Var4 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { + templ_7745c5c3_Var5 := templruntime.GeneratedTemplate(func(templ_7745c5c3_Input templruntime.GeneratedComponentInput) (templ_7745c5c3_Err error) { templ_7745c5c3_W, ctx := templ_7745c5c3_Input.Writer, templ_7745c5c3_Input.Context templ_7745c5c3_Buffer, templ_7745c5c3_IsBuffer := templruntime.GetBuffer(templ_7745c5c3_W) if !templ_7745c5c3_IsBuffer { @@ -111,12 +154,12 @@ func albumTempl(albumId string, album *utils.Album, songs []utils.Song, songsJso if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - var templ_7745c5c3_Var5 string - templ_7745c5c3_Var5, templ_7745c5c3_Err = templ.JoinStringErrs(fmt.Sprintf("init set $songsJson to %s", songsJson)) + var templ_7745c5c3_Var6 string + templ_7745c5c3_Var6, templ_7745c5c3_Err = templ.JoinStringErrs(fmt.Sprintf("init set $songsJson to %s", songsJson)) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `src/modules/album/album.templ`, Line: 31, Col: 58} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `src/modules/album/album.templ`, Line: 58, Col: 58} } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var5)) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var6)) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -124,12 +167,12 @@ func albumTempl(albumId string, album *utils.Album, songs []utils.Song, songsJso if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - var templ_7745c5c3_Var6 string - templ_7745c5c3_Var6, templ_7745c5c3_Err = templ.JoinStringErrs(fmt.Sprintf("/covers/%s", albumId)) + var templ_7745c5c3_Var7 string + templ_7745c5c3_Var7, templ_7745c5c3_Err = templ.JoinStringErrs(fmt.Sprintf("/covers/%s", albumId)) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `src/modules/album/album.templ`, Line: 33, Col: 89} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `src/modules/album/album.templ`, Line: 60, Col: 89} } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var6)) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var7)) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -137,12 +180,12 @@ func albumTempl(albumId string, album *utils.Album, songs []utils.Song, songsJso if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - var templ_7745c5c3_Var7 string - templ_7745c5c3_Var7, templ_7745c5c3_Err = templ.JoinStringErrs(album.Name) + var templ_7745c5c3_Var8 string + templ_7745c5c3_Var8, templ_7745c5c3_Err = templ.JoinStringErrs(album.Name) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `src/modules/album/album.templ`, Line: 35, Col: 16} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `src/modules/album/album.templ`, Line: 62, Col: 16} } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var7)) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var8)) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -150,12 +193,12 @@ func albumTempl(albumId string, album *utils.Album, songs []utils.Song, songsJso if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - var templ_7745c5c3_Var8 string - templ_7745c5c3_Var8, templ_7745c5c3_Err = templ.JoinStringErrs(album.Artist) + var templ_7745c5c3_Var9 string + templ_7745c5c3_Var9, templ_7745c5c3_Err = templ.JoinStringErrs(album.Artist) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `src/modules/album/album.templ`, Line: 38, Col: 18} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `src/modules/album/album.templ`, Line: 65, Col: 18} } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var8)) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var9)) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -168,12 +211,12 @@ func albumTempl(albumId string, album *utils.Album, songs []utils.Song, songsJso if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - var templ_7745c5c3_Var9 string - templ_7745c5c3_Var9, templ_7745c5c3_Err = templ.JoinStringErrs(fmt.Sprintf("on click replaceQueueAndPlayAt($songsJson, %d)", i)) + var templ_7745c5c3_Var10 string + templ_7745c5c3_Var10, templ_7745c5c3_Err = templ.JoinStringErrs(fmt.Sprintf("on click replaceQueueAndPlayAt($songsJson, %d)", i)) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `src/modules/album/album.templ`, Line: 44, Col: 74} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `src/modules/album/album.templ`, Line: 71, Col: 74} } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var9)) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var10)) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -181,12 +224,12 @@ func albumTempl(albumId string, album *utils.Album, songs []utils.Song, songsJso if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - var templ_7745c5c3_Var10 string - templ_7745c5c3_Var10, templ_7745c5c3_Err = templ.JoinStringErrs(strconv.Itoa(song.TrackNumber)) + var templ_7745c5c3_Var11 string + templ_7745c5c3_Var11, templ_7745c5c3_Err = templ.JoinStringErrs(strconv.Itoa(song.TrackNumber)) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `src/modules/album/album.templ`, Line: 47, Col: 39} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `src/modules/album/album.templ`, Line: 74, Col: 39} } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var10)) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var11)) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -194,12 +237,12 @@ func albumTempl(albumId string, album *utils.Album, songs []utils.Song, songsJso if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } - var templ_7745c5c3_Var11 string - templ_7745c5c3_Var11, templ_7745c5c3_Err = templ.JoinStringErrs(song.Title) + var templ_7745c5c3_Var12 string + templ_7745c5c3_Var12, templ_7745c5c3_Err = templ.JoinStringErrs(song.Title) if templ_7745c5c3_Err != nil { - return templ.Error{Err: templ_7745c5c3_Err, FileName: `src/modules/album/album.templ`, Line: 49, Col: 18} + return templ.Error{Err: templ_7745c5c3_Err, FileName: `src/modules/album/album.templ`, Line: 76, Col: 18} } - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var11)) + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var12)) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } @@ -218,7 +261,7 @@ func albumTempl(albumId string, album *utils.Album, songs []utils.Song, songsJso } return templ_7745c5c3_Err }) - templ_7745c5c3_Err = utils.SkeletonTempl().Render(templ.WithChildren(ctx, templ_7745c5c3_Var4), templ_7745c5c3_Buffer) + templ_7745c5c3_Err = utils.SkeletonTempl().Render(templ.WithChildren(ctx, templ_7745c5c3_Var5), templ_7745c5c3_Buffer) if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } diff --git a/src/utils/utils.templ b/src/utils/utils.templ index c383154..727ea32 100644 --- a/src/utils/utils.templ +++ b/src/utils/utils.templ @@ -7,7 +7,7 @@ templ SkeletonTempl() { - Acide Template + Music-to-go diff --git a/src/utils/utils_templ.go b/src/utils/utils_templ.go index 63d51ec..ff2453b 100644 --- a/src/utils/utils_templ.go +++ b/src/utils/utils_templ.go @@ -31,7 +31,7 @@ func SkeletonTempl() templ.Component { templ_7745c5c3_Var1 = templ.NopComponent } ctx = templ.ClearChildren(ctx) - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("Acide Template
") + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("Music-to-go
") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err }