improve output formatting
This commit is contained in:
@@ -1,17 +0,0 @@
|
||||
package api
|
||||
|
||||
type ListResult[T any] struct {
|
||||
Items []T `json:"items"`
|
||||
Count map[string]int `json:"count"`
|
||||
Params map[string]any `json:"params"`
|
||||
}
|
||||
|
||||
func NewListResult[T any](items ...T) *ListResult[T] {
|
||||
r := &ListResult[T]{
|
||||
Items: items,
|
||||
Count: map[string]int{"response": len(items)},
|
||||
Params: map[string]any{},
|
||||
}
|
||||
|
||||
return r
|
||||
}
|
||||
+12
-23
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
"code.aneur.in/go/rest"
|
||||
"code.aneur.in/go/validate"
|
||||
"code.aneur.in/zampler/zampler/internal/dto"
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
@@ -60,9 +61,7 @@ func (api *API) ListFiles(w http.ResponseWriter, req *http.Request) {
|
||||
}
|
||||
|
||||
switch uq.Get("direction") {
|
||||
case "":
|
||||
break
|
||||
case "asc":
|
||||
case "", "asc":
|
||||
asc = true
|
||||
case "desc":
|
||||
asc = false
|
||||
@@ -72,12 +71,10 @@ func (api *API) ListFiles(w http.ResponseWriter, req *http.Request) {
|
||||
}
|
||||
|
||||
switch uq.Get("sort") {
|
||||
case "":
|
||||
break
|
||||
case "", "path":
|
||||
sort = "path"
|
||||
case "filename":
|
||||
sort = "filename"
|
||||
case "path":
|
||||
sort = "path"
|
||||
default:
|
||||
rest.ErrBadRequest.WithMessage("invalid sort").WriteJSON(w)
|
||||
return
|
||||
@@ -131,22 +128,14 @@ func (api *API) ListFiles(w http.ResponseWriter, req *http.Request) {
|
||||
q.Offset(offset).Limit(limit)
|
||||
|
||||
// Output result
|
||||
output := NewListResult(q.ResultDTOs())
|
||||
output.Count["total"] = totalCount
|
||||
if filename != "" {
|
||||
output.Params["filename"] = filename
|
||||
}
|
||||
if path != "" {
|
||||
output.Params["path"] = path
|
||||
}
|
||||
output.Params["sort"] = sort
|
||||
if asc {
|
||||
output.Params["direction"] = "asc"
|
||||
} else {
|
||||
output.Params["direction"] = "desc"
|
||||
}
|
||||
output.Params["offset"] = offset
|
||||
output.Params["limit"] = limit
|
||||
output := dto.NewList(q.ResultDTOs()).
|
||||
WithTotalCount(totalCount).
|
||||
WithParam("sort", sort).
|
||||
WithDirection(asc).
|
||||
WithParam("offset", offset).
|
||||
WithParam("limit", limit).
|
||||
MaybeWithParam("filename", filename).
|
||||
MaybeWithParam("path", path)
|
||||
|
||||
rest.WriteResponseJSON(w, http.StatusOK, output)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
package dto
|
||||
|
||||
type List[T any] struct {
|
||||
Items []T `json:"items"`
|
||||
Count map[string]int `json:"count"`
|
||||
Params map[string]any `json:"params"`
|
||||
}
|
||||
|
||||
func (l *List[T]) MaybeWithParam(name string, value any) *List[T] {
|
||||
switch value.(type) {
|
||||
case bool:
|
||||
if value == true {
|
||||
return l.WithParam(name, value)
|
||||
}
|
||||
case int:
|
||||
if value != 0 {
|
||||
return l.WithParam(name, value)
|
||||
}
|
||||
case string:
|
||||
if value != "" {
|
||||
return l.WithParam(name, value)
|
||||
}
|
||||
}
|
||||
|
||||
// Ignore other types for now
|
||||
return l
|
||||
}
|
||||
|
||||
func (l *List[T]) WithCount(name string, value int) *List[T] {
|
||||
l.Count[name] = value
|
||||
return l
|
||||
}
|
||||
|
||||
func (l *List[T]) WithDirection(asc bool) *List[T] {
|
||||
if asc {
|
||||
return l.WithParam("direction", "asc")
|
||||
}
|
||||
return l.WithParam("direction", "desc")
|
||||
}
|
||||
|
||||
func (l *List[T]) WithParam(name string, value any) *List[T] {
|
||||
l.Params[name] = value
|
||||
return l
|
||||
}
|
||||
|
||||
func (l *List[T]) WithSort(value string) *List[T] {
|
||||
return l.WithParam("sort", value)
|
||||
}
|
||||
|
||||
func (l *List[T]) WithTotalCount(value int) *List[T] {
|
||||
return l.WithCount("total", value)
|
||||
}
|
||||
|
||||
func NewList[T any](items ...T) *List[T] {
|
||||
r := &List[T]{
|
||||
Items: items,
|
||||
Count: map[string]int{"items": len(items), "total": len(items)},
|
||||
Params: map[string]any{},
|
||||
}
|
||||
|
||||
return r
|
||||
}
|
||||
Reference in New Issue
Block a user