From f41f2c3a25391e82454fbd0de5cd39e915242bab Mon Sep 17 00:00:00 2001 From: akuma06 Date: Fri, 2 Jun 2017 16:10:31 +0200 Subject: [PATCH 1/2] Field Date Filter * Search can be filtered out with ?fromDate=2017-05-01 and ?toDate=2017-05-12 --- common/search.go | 2 ++ common/torrent.go | 16 ++++++++++++++++ util/search/search.go | 12 ++++++++++++ 3 files changed, 30 insertions(+) diff --git a/common/search.go b/common/search.go index dadc8bbf..bd85462b 100644 --- a/common/search.go +++ b/common/search.go @@ -162,6 +162,8 @@ type SearchParam struct { Status Status Sort SortMode Category Category + FromDate string + ToDate string Page int UserID uint Max uint diff --git a/common/torrent.go b/common/torrent.go index 930e3147..b2d28c9b 100644 --- a/common/torrent.go +++ b/common/torrent.go @@ -29,6 +29,8 @@ type TorrentParam struct { UserID uint32 TorrentID uint32 FromID uint32 + FromDate string + ToDate string NotNull string // csv Null string // csv NameLike string // csv @@ -69,6 +71,9 @@ func (p *TorrentParam) FromRequest(r *http.Request) { var status Status status.Parse(r.URL.Query().Get("s")) + p.FromDate = r.URL.Query().Get("fromDate") + p.ToDate = r.URL.Query().Get("toDate") + var category Category category.Parse(r.URL.Query().Get("c")) @@ -129,6 +134,15 @@ func (p *TorrentParam) ToFilterQuery() string { if p.FromID != 0 { query += " id:>" + strconv.FormatInt(int64(p.FromID), 10) } + + if p.FromDate != "" && p.ToDate != "" { + query += " date: [" + p.FromDate + " " + p.ToDate + "]" + } else if p.FromDate != "" { + query += " date: [" + p.FromDate + " *]" + } else if p.ToDate != "" { + query += " date: [* " + p.ToDate + "]" + } + return query } @@ -229,6 +243,8 @@ func (p *TorrentParam) Clone() TorrentParam { UserID: p.UserID, TorrentID: p.TorrentID, FromID: p.FromID, + FromDate: p.FromDate, + ToDate: p.ToDate, NotNull: p.NotNull, Null: p.Null, NameLike: p.NameLike, diff --git a/util/search/search.go b/util/search/search.go index c370fca7..0ba142b6 100644 --- a/util/search/search.go +++ b/util/search/search.go @@ -86,6 +86,8 @@ func searchByQuery(r *http.Request, pagenum int, countAll bool, withUser bool, d searchParam := common.SearchParam{ TorrentID: uint(torrentParam.TorrentID), FromID: uint(torrentParam.FromID), + FromDate: torrentParam.FromDate, + ToDate: torrentParam.ToDate, Order: torrentParam.Order, Status: torrentParam.Status, Sort: torrentParam.Sort, @@ -121,6 +123,8 @@ func searchByQueryPostgres(r *http.Request, pagenum int, countAll bool, withUser search.UserID = uint(userID) fromID, _ := strconv.Atoi(r.URL.Query().Get("fromID")) search.FromID = uint(fromID) + search.FromDate = r.URL.Query().Get("fromDate") + search.ToDate = r.URL.Query().Get("toDate") switch s := r.URL.Query().Get("s"); s { case "1": @@ -224,6 +228,14 @@ func searchByQueryPostgres(r *http.Request, pagenum int, countAll bool, withUser conditions = append(conditions, "torrent_id > ?") parameters.Params = append(parameters.Params, search.FromID) } + if search.FromDate != "" { + conditions = append(conditions, "date >= ?") + parameters.Params = append(parameters.Params, search.FromDate) + } + if search.ToDate != "" { + conditions = append(conditions, "date <= ?") + parameters.Params = append(parameters.Params, search.ToDate) + } if search.Category.Sub != 0 { conditions = append(conditions, "sub_category = ?") parameters.Params = append(parameters.Params, search.Category.Sub) From 61a0ab0bc3e88cef4b0f2bd54459ef87d8c557ed Mon Sep 17 00:00:00 2001 From: akuma06 Date: Fri, 2 Jun 2017 16:25:45 +0200 Subject: [PATCH 2/2] Added also a maxage field Now you can provide ?maxage=nb_days to get the torrents from the last nb_days days --- common/torrent.go | 10 ++++++++-- util/search/search.go | 11 +++++++++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/common/torrent.go b/common/torrent.go index b2d28c9b..99216a1a 100644 --- a/common/torrent.go +++ b/common/torrent.go @@ -6,6 +6,7 @@ import ( "net/http" "strconv" "strings" + "time" "github.com/gorilla/mux" elastic "gopkg.in/olivere/elastic.v5" @@ -71,8 +72,13 @@ func (p *TorrentParam) FromRequest(r *http.Request) { var status Status status.Parse(r.URL.Query().Get("s")) - p.FromDate = r.URL.Query().Get("fromDate") - p.ToDate = r.URL.Query().Get("toDate") + maxage, err := strconv.Atoi(r.URL.Query().Get("maxage")) + if err != nil { + p.FromDate = r.URL.Query().Get("fromDate") + p.ToDate = r.URL.Query().Get("toDate") + } else { + p.FromDate = time.Now().AddDate(0, 0, -maxage).Format("2006-01-02") + } var category Category category.Parse(r.URL.Query().Get("c")) diff --git a/util/search/search.go b/util/search/search.go index 0ba142b6..af6d8997 100644 --- a/util/search/search.go +++ b/util/search/search.go @@ -4,6 +4,7 @@ import ( "net/http" "strconv" "strings" + "time" "unicode" "unicode/utf8" @@ -123,8 +124,14 @@ func searchByQueryPostgres(r *http.Request, pagenum int, countAll bool, withUser search.UserID = uint(userID) fromID, _ := strconv.Atoi(r.URL.Query().Get("fromID")) search.FromID = uint(fromID) - search.FromDate = r.URL.Query().Get("fromDate") - search.ToDate = r.URL.Query().Get("toDate") + + maxage, err := strconv.Atoi(r.URL.Query().Get("maxage")) + if err != nil { + search.FromDate = r.URL.Query().Get("fromDate") + search.ToDate = r.URL.Query().Get("toDate") + } else { + search.FromDate = time.Now().AddDate(0, 0, -maxage).Format("2006-01-02") + } switch s := r.URL.Query().Get("s"); s { case "1":