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..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" @@ -29,6 +30,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 +72,14 @@ func (p *TorrentParam) FromRequest(r *http.Request) { var status Status status.Parse(r.URL.Query().Get("s")) + 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")) @@ -129,6 +140,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 +249,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..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" @@ -86,6 +87,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, @@ -122,6 +125,14 @@ func searchByQueryPostgres(r *http.Request, pagenum int, countAll bool, withUser fromID, _ := strconv.Atoi(r.URL.Query().Get("fromID")) search.FromID = uint(fromID) + 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": search.Status = common.FilterRemakes @@ -224,6 +235,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)