Add changed_after and changed_before options to /v1/entries endpoint

This commit is contained in:
Frédéric Guillot 2023-10-05 20:28:36 -07:00
parent 67eb574fd4
commit fccc25f7a3
5 changed files with 88 additions and 44 deletions

View file

@ -540,14 +540,30 @@ func buildFilterQueryString(path string, filter *Filter) string {
values.Set("after", strconv.FormatInt(filter.After, 10)) values.Set("after", strconv.FormatInt(filter.After, 10))
} }
if filter.AfterEntryID > 0 {
values.Set("after_entry_id", strconv.FormatInt(filter.AfterEntryID, 10))
}
if filter.Before > 0 { if filter.Before > 0 {
values.Set("before", strconv.FormatInt(filter.Before, 10)) values.Set("before", strconv.FormatInt(filter.Before, 10))
} }
if filter.PublishedAfter > 0 {
values.Set("published_after", strconv.FormatInt(filter.PublishedAfter, 10))
}
if filter.PublishedBefore > 0 {
values.Set("published_before", strconv.FormatInt(filter.PublishedBefore, 10))
}
if filter.ChangedAfter > 0 {
values.Set("changed_after", strconv.FormatInt(filter.ChangedAfter, 10))
}
if filter.ChangedBefore > 0 {
values.Set("changed_before", strconv.FormatInt(filter.ChangedBefore, 10))
}
if filter.AfterEntryID > 0 {
values.Set("after_entry_id", strconv.FormatInt(filter.AfterEntryID, 10))
}
if filter.BeforeEntryID > 0 { if filter.BeforeEntryID > 0 {
values.Set("before_entry_id", strconv.FormatInt(filter.BeforeEntryID, 10)) values.Set("before_entry_id", strconv.FormatInt(filter.BeforeEntryID, 10))
} }

View file

@ -245,20 +245,24 @@ const (
// Filter is used to filter entries. // Filter is used to filter entries.
type Filter struct { type Filter struct {
Status string Status string
Offset int Offset int
Limit int Limit int
Order string Order string
Direction string Direction string
Starred string Starred string
Before int64 Before int64
After int64 After int64
BeforeEntryID int64 PublishedBefore int64
AfterEntryID int64 PublishedAfter int64
Search string ChangedBefore int64
CategoryID int64 ChangedAfter int64
FeedID int64 BeforeEntryID int64
Statuses []string AfterEntryID int64
Search string
CategoryID int64
FeedID int64
Statuses []string
} }
// EntryResultSet represents the response when fetching entries. // EntryResultSet represents the response when fetching entries.

View file

@ -283,28 +283,39 @@ func (h *handler) fetchContent(w http.ResponseWriter, r *http.Request) {
} }
func configureFilters(builder *storage.EntryQueryBuilder, r *http.Request) { func configureFilters(builder *storage.EntryQueryBuilder, r *http.Request) {
beforeEntryID := request.QueryInt64Param(r, "before_entry_id", 0) if beforeEntryID := request.QueryInt64Param(r, "before_entry_id", 0); beforeEntryID > 0 {
if beforeEntryID > 0 {
builder.BeforeEntryID(beforeEntryID) builder.BeforeEntryID(beforeEntryID)
} }
afterEntryID := request.QueryInt64Param(r, "after_entry_id", 0) if afterEntryID := request.QueryInt64Param(r, "after_entry_id", 0); afterEntryID > 0 {
if afterEntryID > 0 {
builder.AfterEntryID(afterEntryID) builder.AfterEntryID(afterEntryID)
} }
beforeTimestamp := request.QueryInt64Param(r, "before", 0) if beforePublishedTimestamp := request.QueryInt64Param(r, "before", 0); beforePublishedTimestamp > 0 {
if beforeTimestamp > 0 { builder.BeforePublishedDate(time.Unix(beforePublishedTimestamp, 0))
builder.BeforeDate(time.Unix(beforeTimestamp, 0))
} }
afterTimestamp := request.QueryInt64Param(r, "after", 0) if afterPublishedTimestamp := request.QueryInt64Param(r, "after", 0); afterPublishedTimestamp > 0 {
if afterTimestamp > 0 { builder.AfterPublishedDate(time.Unix(afterPublishedTimestamp, 0))
builder.AfterDate(time.Unix(afterTimestamp, 0))
} }
categoryID := request.QueryInt64Param(r, "category_id", 0) if beforePublishedTimestamp := request.QueryInt64Param(r, "published_before", 0); beforePublishedTimestamp > 0 {
if categoryID > 0 { builder.BeforePublishedDate(time.Unix(beforePublishedTimestamp, 0))
}
if afterPublishedTimestamp := request.QueryInt64Param(r, "published_after", 0); afterPublishedTimestamp > 0 {
builder.AfterPublishedDate(time.Unix(afterPublishedTimestamp, 0))
}
if beforeChangedTimestamp := request.QueryInt64Param(r, "changed_before", 0); beforeChangedTimestamp > 0 {
builder.BeforeChangedDate(time.Unix(beforeChangedTimestamp, 0))
}
if afterChangedTimestamp := request.QueryInt64Param(r, "changed_after", 0); afterChangedTimestamp > 0 {
builder.AfterChangedDate(time.Unix(afterChangedTimestamp, 0))
}
if categoryID := request.QueryInt64Param(r, "category_id", 0); categoryID > 0 {
builder.WithCategoryID(categoryID) builder.WithCategoryID(categoryID)
} }
@ -315,8 +326,7 @@ func configureFilters(builder *storage.EntryQueryBuilder, r *http.Request) {
} }
} }
searchQuery := request.QueryStringParam(r, "search", "") if searchQuery := request.QueryStringParam(r, "search", ""); searchQuery != "" {
if searchQuery != "" {
builder.WithSearchQuery(searchQuery) builder.WithSearchQuery(searchQuery)
} }
} }

View file

@ -1329,10 +1329,10 @@ func (h *handler) handleReadingListStreamHandler(w http.ResponseWriter, r *http.
builder.WithOffset(rm.Offset) builder.WithOffset(rm.Offset)
builder.WithSorting(model.DefaultSortingOrder, rm.SortDirection) builder.WithSorting(model.DefaultSortingOrder, rm.SortDirection)
if rm.StartTime > 0 { if rm.StartTime > 0 {
builder.AfterDate(time.Unix(rm.StartTime, 0)) builder.AfterPublishedDate(time.Unix(rm.StartTime, 0))
} }
if rm.StopTime > 0 { if rm.StopTime > 0 {
builder.BeforeDate(time.Unix(rm.StopTime, 0)) builder.BeforePublishedDate(time.Unix(rm.StopTime, 0))
} }
rawEntryIDs, err := builder.GetEntryIDs() rawEntryIDs, err := builder.GetEntryIDs()
@ -1367,10 +1367,10 @@ func (h *handler) handleStarredStreamHandler(w http.ResponseWriter, r *http.Requ
builder.WithOffset(rm.Offset) builder.WithOffset(rm.Offset)
builder.WithSorting(model.DefaultSortingOrder, rm.SortDirection) builder.WithSorting(model.DefaultSortingOrder, rm.SortDirection)
if rm.StartTime > 0 { if rm.StartTime > 0 {
builder.AfterDate(time.Unix(rm.StartTime, 0)) builder.AfterPublishedDate(time.Unix(rm.StartTime, 0))
} }
if rm.StopTime > 0 { if rm.StopTime > 0 {
builder.BeforeDate(time.Unix(rm.StopTime, 0)) builder.BeforePublishedDate(time.Unix(rm.StopTime, 0))
} }
rawEntryIDs, err := builder.GetEntryIDs() rawEntryIDs, err := builder.GetEntryIDs()
@ -1405,10 +1405,10 @@ func (h *handler) handleReadStreamHandler(w http.ResponseWriter, r *http.Request
builder.WithOffset(rm.Offset) builder.WithOffset(rm.Offset)
builder.WithSorting(model.DefaultSortingOrder, rm.SortDirection) builder.WithSorting(model.DefaultSortingOrder, rm.SortDirection)
if rm.StartTime > 0 { if rm.StartTime > 0 {
builder.AfterDate(time.Unix(rm.StartTime, 0)) builder.AfterPublishedDate(time.Unix(rm.StartTime, 0))
} }
if rm.StopTime > 0 { if rm.StopTime > 0 {
builder.BeforeDate(time.Unix(rm.StopTime, 0)) builder.BeforePublishedDate(time.Unix(rm.StopTime, 0))
} }
rawEntryIDs, err := builder.GetEntryIDs() rawEntryIDs, err := builder.GetEntryIDs()
@ -1449,10 +1449,10 @@ func (h *handler) handleFeedStreamHandler(w http.ResponseWriter, r *http.Request
builder.WithOffset(rm.Offset) builder.WithOffset(rm.Offset)
builder.WithSorting(model.DefaultSortingOrder, rm.SortDirection) builder.WithSorting(model.DefaultSortingOrder, rm.SortDirection)
if rm.StartTime > 0 { if rm.StartTime > 0 {
builder.AfterDate(time.Unix(rm.StartTime, 0)) builder.AfterPublishedDate(time.Unix(rm.StartTime, 0))
} }
if rm.StopTime > 0 { if rm.StopTime > 0 {
builder.BeforeDate(time.Unix(rm.StopTime, 0)) builder.BeforePublishedDate(time.Unix(rm.StopTime, 0))
} }
rawEntryIDs, err := builder.GetEntryIDs() rawEntryIDs, err := builder.GetEntryIDs()

View file

@ -51,15 +51,29 @@ func (e *EntryQueryBuilder) WithStarred(starred bool) *EntryQueryBuilder {
return e return e
} }
// BeforeDate adds a condition < published_at // BeforeChangedDate adds a condition < changed_at
func (e *EntryQueryBuilder) BeforeDate(date time.Time) *EntryQueryBuilder { func (e *EntryQueryBuilder) BeforeChangedDate(date time.Time) *EntryQueryBuilder {
e.conditions = append(e.conditions, fmt.Sprintf("e.changed_at < $%d", len(e.args)+1))
e.args = append(e.args, date)
return e
}
// AfterChangedDate adds a condition > changed_at
func (e *EntryQueryBuilder) AfterChangedDate(date time.Time) *EntryQueryBuilder {
e.conditions = append(e.conditions, fmt.Sprintf("e.changed_at > $%d", len(e.args)+1))
e.args = append(e.args, date)
return e
}
// BeforePublishedDate adds a condition < published_at
func (e *EntryQueryBuilder) BeforePublishedDate(date time.Time) *EntryQueryBuilder {
e.conditions = append(e.conditions, fmt.Sprintf("e.published_at < $%d", len(e.args)+1)) e.conditions = append(e.conditions, fmt.Sprintf("e.published_at < $%d", len(e.args)+1))
e.args = append(e.args, date) e.args = append(e.args, date)
return e return e
} }
// AfterDate adds a condition > published_at // AfterPublishedDate adds a condition > published_at
func (e *EntryQueryBuilder) AfterDate(date time.Time) *EntryQueryBuilder { func (e *EntryQueryBuilder) AfterPublishedDate(date time.Time) *EntryQueryBuilder {
e.conditions = append(e.conditions, fmt.Sprintf("e.published_at > $%d", len(e.args)+1)) e.conditions = append(e.conditions, fmt.Sprintf("e.published_at > $%d", len(e.args)+1))
e.args = append(e.args, date) e.args = append(e.args, date)
return e return e