use categories get list instead of admin list

This commit is contained in:
Celogeek 2021-12-18 23:13:03 +01:00
parent 2889e94dd9
commit d8e82a7c37
Signed by: celogeek
GPG Key ID: E6B7BDCFC446233A

View File

@ -1,10 +1,8 @@
package piwigocli
import (
"encoding/json"
"net/url"
"os"
"strconv"
"github.com/celogeek/piwigo-cli/internal/piwigo"
"github.com/jedib0t/go-pretty/v6/table"
@ -14,33 +12,9 @@ type CategoriesListCommand struct {
}
type Category struct {
Id int `json:"id,string"`
Id int `json:"id"`
Name string `json:"name"`
FullName string `json:"fullname"`
ImagesCount int `json:"nb_images,string"`
}
func getInt(n interface{}) (r int) {
switch n := n.(type) {
case string:
r, _ = strconv.Atoi(n)
case int:
r = n
}
return
}
func (b *Category) UnmarshalJSON(data []byte) error {
var v map[string]interface{}
if err := json.Unmarshal(data, &v); err != nil {
return err
}
b.Id = getInt(v["id"])
b.Name = v["name"].(string)
b.FullName = v["fullname"].(string)
b.ImagesCount = getInt(v["nb_images"])
return nil
ImagesCount int `json:"nb_images"`
}
type GetCategoriesListResponse struct {
@ -59,17 +33,21 @@ func (c *CategoriesListCommand) Execute(args []string) error {
}
var resp GetCategoriesListResponse
if err := p.Post("pwg.categories.getAdminList", &url.Values{}, &resp); err != nil {
if err := p.Post("pwg.categories.getList", &url.Values{
"recursive": []string{"true"},
"fullname": []string{"true"},
}, &resp); err != nil {
return err
}
t := table.NewWriter()
t.AppendHeader(table.Row{"Id", "FullName", "ImagesCount"})
t.AppendHeader(table.Row{"Id", "Name", "Images"})
for _, category := range resp.Categories {
t.AppendRow(table.Row{
category.Id,
category.FullName,
category.Name,
category.ImagesCount,
})
}