Let the client do the limit / sort
This commit is contained in:
parent
582c8c99ec
commit
6b93ee5900
@ -5,8 +5,6 @@ import (
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"sort"
|
||||
"strconv"
|
||||
|
||||
"github.com/odwrtw/papi"
|
||||
polochon "github.com/odwrtw/polochon/lib"
|
||||
@ -22,13 +20,6 @@ import (
|
||||
// ErrPolochonUnavailable is an error returned if the polochon server is not available
|
||||
var ErrPolochonUnavailable = fmt.Errorf("Invalid polochon address")
|
||||
|
||||
// SortByTitle helps sort the movies
|
||||
type SortByTitle []*Movie
|
||||
|
||||
func (a SortByTitle) Len() int { return len(a) }
|
||||
func (a SortByTitle) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
||||
func (a SortByTitle) Less(i, j int) bool { return a[i].Title < a[j].Title }
|
||||
|
||||
func getPolochonMovies(user *users.User) ([]*Movie, error) {
|
||||
movies := []*Movie{}
|
||||
|
||||
@ -73,45 +64,6 @@ func getPolochonMovies(user *users.User) ([]*Movie, error) {
|
||||
|
||||
func FromPolochon(env *web.Env, w http.ResponseWriter, r *http.Request) error {
|
||||
v := auth.GetCurrentUser(r, env.Log)
|
||||
params := struct {
|
||||
Sort string
|
||||
Order string
|
||||
Start int
|
||||
Limit int
|
||||
}{
|
||||
Sort: "title",
|
||||
Order: "asc",
|
||||
Start: 0,
|
||||
Limit: 50,
|
||||
}
|
||||
|
||||
err := r.ParseForm()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if sort := r.FormValue("sort"); sort != "" {
|
||||
params.Sort = sort
|
||||
}
|
||||
|
||||
if order := r.FormValue("order"); order != "" {
|
||||
params.Order = order
|
||||
}
|
||||
if start := r.FormValue("start"); start != "" {
|
||||
n, err := strconv.Atoi(start)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
params.Start = n
|
||||
}
|
||||
if limit := r.FormValue("limit"); limit != "" {
|
||||
n, err := strconv.Atoi(limit)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
params.Limit = n
|
||||
}
|
||||
|
||||
user, ok := v.(*users.User)
|
||||
if !ok {
|
||||
return fmt.Errorf("invalid user type")
|
||||
@ -141,26 +93,6 @@ func FromPolochon(env *web.Env, w http.ResponseWriter, r *http.Request) error {
|
||||
}
|
||||
}
|
||||
|
||||
var smovies sort.Interface
|
||||
|
||||
switch params.Sort {
|
||||
case "title":
|
||||
smovies = SortByTitle(movies)
|
||||
default:
|
||||
return fmt.Errorf("invalid sort param")
|
||||
}
|
||||
|
||||
switch params.Order {
|
||||
case "asc":
|
||||
break
|
||||
case "desc":
|
||||
smovies = sort.Reverse(smovies)
|
||||
default:
|
||||
return fmt.Errorf("invalid order param")
|
||||
}
|
||||
|
||||
sort.Sort(smovies)
|
||||
|
||||
return env.RenderJSON(w, movies)
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user