From 6b93ee59000a5d48533c0d84f30f7b075bd36a89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Delattre?= Date: Thu, 17 Nov 2016 12:58:35 +0100 Subject: [PATCH] Let the client do the limit / sort --- src/internal/movies/handlers.go | 68 --------------------------------- 1 file changed, 68 deletions(-) diff --git a/src/internal/movies/handlers.go b/src/internal/movies/handlers.go index b6f270c..8fe2a4d 100644 --- a/src/internal/movies/handlers.go +++ b/src/internal/movies/handlers.go @@ -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) }