Add limit and sort in library display

This commit is contained in:
Nicolas Duhamel 2016-06-28 23:26:02 +02:00 committed by Grégoire Delattre
parent db7f11c473
commit 787aaea2b0
3 changed files with 74 additions and 3 deletions

View File

@ -5,6 +5,8 @@ import (
"net"
"net/http"
"net/url"
"sort"
"strconv"
"github.com/odwrtw/papi"
polochon "github.com/odwrtw/polochon/lib"
@ -19,6 +21,12 @@ import (
var ErrPolochonUnavailable = fmt.Errorf("Invalid polochon address")
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{}
@ -63,6 +71,44 @@ 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 {
@ -101,7 +147,27 @@ func FromPolochon(env *web.Env, w http.ResponseWriter, r *http.Request) error {
}
}
web.SetData(r, "movies", movies)
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)
web.SetData(r, "movies", movies[params.Start:params.Start+params.Limit])
return env.Rends(w, r, "movies/library")
}

View File

@ -43,7 +43,12 @@
<p class="movie-plot">{{ .Plot }}</p>
<div class="movie-details-buttons">
<a id="imdb-link" type="button" class="btn btn-warning" href="http://www.imdb.com/title/{{ .ImdbID }}">
{{ if .PolochonURL }}
<a type="button" class="btn btn-success btn-sm" href="{{ .PolochonURL }}">
<i class="fa fa-download"></i> Download
</a>
{{ end }}
<a id="imdb-link" type="button" class="btn btn-sm btn-warning" href="http://www.imdb.com/title/{{ .ImdbID }}">
<i class="fa fa-external-link"></i> IMDB
</a>
</div>

View File

@ -16,7 +16,7 @@
<ul class="nav navbar-nav">
<li><p class="navbar-text">Library: </p></li>
<li {{ if eq $.Route "movies.polochon" }}class="active"{{ end }}>
<a href="{{ URL "movies.polochon" }}">Movies{{ if eq $.Route "movies.polochon" }}<span class="sr-only">(current)</span>{{ end }}</a>
<a href="{{ URL "movies.polochon" }}?limit=10">Movies{{ if eq $.Route "movies.polochon" }}<span class="sr-only">(current)</span>{{ end }}</a>
</li>
<li><a href="#">TV Shows</a></li>