diff --git a/src/modules/album/album.go b/src/modules/album/album.go new file mode 100644 index 0000000..abb7704 --- /dev/null +++ b/src/modules/album/album.go @@ -0,0 +1,60 @@ +package album + +import ( + "acide/src/modules/song" + "acide/src/utils" + "net/http" + "sync" + + "github.com/labstack/echo" + "github.com/labstack/gommon/log" +) + +func Setup(g *echo.Group) { + log.Print("Setting up the album module") + g.Use(utils.Authed) + + g.GET("/:id", albumPage) +} + +func albumPage(c echo.Context) error { + token, server := utils.Credentials(c) + albumId := c.Param("id") + + // load album info and song list on the background + var wg sync.WaitGroup + + var album *utils.Album + var songs []utils.Song + var routineErr error = nil + + wg.Add(2) + go func() { + defer wg.Done() + + res, err := loadAlbum(token, server, albumId) + if err != nil { + routineErr = err + return + } + + album = res + }() + go func() { + defer wg.Done() + + res, err := song.LoadSongs(token, server, albumId) + if err != nil { + routineErr = err + return + } + songs = res + }() + wg.Wait() + + if routineErr != nil { + return routineErr + } + + return utils.RenderTempl(c, http.StatusOK, albumTempl(albumId, album, songs)) +} diff --git a/src/modules/album/album.service.go b/src/modules/album/album.service.go new file mode 100644 index 0000000..c4a260c --- /dev/null +++ b/src/modules/album/album.service.go @@ -0,0 +1,31 @@ +package album + +import ( + "acide/src/utils" + "errors" + "fmt" + + "github.com/go-resty/resty/v2" +) + +func loadAlbum(token, server, albumId string) (*utils.Album, error) { + var album utils.Album + var error utils.NavError + + client := resty.New() + response, err := client.R(). + SetHeader("x-nd-authorization", fmt.Sprintf("Bearer %s", token)). + SetResult(&album). + SetError(&error). + Get(fmt.Sprintf("%s/api/album/%s", server, albumId)) + + if err != nil { + return nil, err + } + + if !response.IsSuccess() { + return nil, errors.New(error.Error) + } + + return &album, nil +} diff --git a/src/modules/album/album.templ b/src/modules/album/album.templ new file mode 100644 index 0000000..257c561 --- /dev/null +++ b/src/modules/album/album.templ @@ -0,0 +1,27 @@ +package album + +import ( + "acide/src/utils" + "fmt" +) + +templ albumTempl(albumId string, album *utils.Album, songs []utils.Song) { + @utils.SkeletonTempl() { +
+ { album.Artist } +
++ { song.Title } +
+ } +") + if templ_7745c5c3_Err != nil { + return templ_7745c5c3_Err + } + var templ_7745c5c3_Var5 string + templ_7745c5c3_Var5, 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: 16, Col: 18} + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var5)) + 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 + } + var templ_7745c5c3_Var6 string + templ_7745c5c3_Var6, 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: 21, Col: 18} + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString(templ.EscapeString(templ_7745c5c3_Var6)) + 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 + } + } + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("