Skip to content

Commit

Permalink
Add changed_after and changed_before options to /v1/entries endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
fguillot committed Oct 6, 2023
1 parent 67eb574 commit 3f2421a
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 44 deletions.
24 changes: 20 additions & 4 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -540,14 +540,30 @@ func buildFilterQueryString(path string, filter *Filter) string {
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 {
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 {
values.Set("before_entry_id", strconv.FormatInt(filter.BeforeEntryID, 10))
}
Expand Down
32 changes: 18 additions & 14 deletions client/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,20 +245,24 @@ const (

// Filter is used to filter entries.
type Filter struct {
Status string
Offset int
Limit int
Order string
Direction string
Starred string
Before int64
After int64
BeforeEntryID int64
AfterEntryID int64
Search string
CategoryID int64
FeedID int64
Statuses []string
Status string
Offset int
Limit int
Order string
Direction string
Starred string
Before int64
After int64
PublishedBefore int64
PublishedAfter int64
ChangedBefore int64
ChangedAfter int64
BeforeEntryID int64
AfterEntryID int64
Search string
CategoryID int64
FeedID int64
Statuses []string
}

// EntryResultSet represents the response when fetching entries.
Expand Down
38 changes: 24 additions & 14 deletions internal/api/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,28 +283,39 @@ func (h *handler) fetchContent(w http.ResponseWriter, r *http.Request) {
}

func configureFilters(builder *storage.EntryQueryBuilder, r *http.Request) {
beforeEntryID := request.QueryInt64Param(r, "before_entry_id", 0)
if beforeEntryID > 0 {
if beforeEntryID := request.QueryInt64Param(r, "before_entry_id", 0); beforeEntryID > 0 {
builder.BeforeEntryID(beforeEntryID)
}

afterEntryID := request.QueryInt64Param(r, "after_entry_id", 0)
if afterEntryID > 0 {
if afterEntryID := request.QueryInt64Param(r, "after_entry_id", 0); afterEntryID > 0 {
builder.AfterEntryID(afterEntryID)
}

beforeTimestamp := request.QueryInt64Param(r, "before", 0)
if beforeTimestamp > 0 {
builder.BeforeDate(time.Unix(beforeTimestamp, 0))
if beforePublishedTimestamp := request.QueryInt64Param(r, "before", 0); beforePublishedTimestamp > 0 {
builder.BeforePublishedDate(time.Unix(beforePublishedTimestamp, 0))
}

afterTimestamp := request.QueryInt64Param(r, "after", 0)
if afterTimestamp > 0 {
builder.AfterDate(time.Unix(afterTimestamp, 0))
if afterPublishedTimestamp := request.QueryInt64Param(r, "after", 0); afterPublishedTimestamp > 0 {
builder.AfterPublishedDate(time.Unix(afterPublishedTimestamp, 0))
}

categoryID := request.QueryInt64Param(r, "category_id", 0)
if categoryID > 0 {
if beforePublishedTimestamp := request.QueryInt64Param(r, "published_before", 0); beforePublishedTimestamp > 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)
}

Expand All @@ -315,8 +326,7 @@ func configureFilters(builder *storage.EntryQueryBuilder, r *http.Request) {
}
}

searchQuery := request.QueryStringParam(r, "search", "")
if searchQuery != "" {
if searchQuery := request.QueryStringParam(r, "search", ""); searchQuery != "" {
builder.WithSearchQuery(searchQuery)
}
}
16 changes: 8 additions & 8 deletions internal/googlereader/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -1329,10 +1329,10 @@ func (h *handler) handleReadingListStreamHandler(w http.ResponseWriter, r *http.
builder.WithOffset(rm.Offset)
builder.WithSorting(model.DefaultSortingOrder, rm.SortDirection)
if rm.StartTime > 0 {
builder.AfterDate(time.Unix(rm.StartTime, 0))
builder.AfterPublishedDate(time.Unix(rm.StartTime, 0))
}
if rm.StopTime > 0 {
builder.BeforeDate(time.Unix(rm.StopTime, 0))
builder.BeforePublishedDate(time.Unix(rm.StopTime, 0))
}

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

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

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

rawEntryIDs, err := builder.GetEntryIDs()
Expand Down
22 changes: 18 additions & 4 deletions internal/storage/entry_query_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,29 @@ func (e *EntryQueryBuilder) WithStarred(starred bool) *EntryQueryBuilder {
return e
}

// BeforeDate adds a condition < published_at
func (e *EntryQueryBuilder) BeforeDate(date time.Time) *EntryQueryBuilder {
// BeforeChangedDate adds a condition < changed_at
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.args = append(e.args, date)
return e
}

// AfterDate adds a condition > published_at
func (e *EntryQueryBuilder) AfterDate(date time.Time) *EntryQueryBuilder {
// AfterPublishedDate adds a condition > published_at
func (e *EntryQueryBuilder) AfterPublishedDate(date time.Time) *EntryQueryBuilder {
e.conditions = append(e.conditions, fmt.Sprintf("e.published_at > $%d", len(e.args)+1))
e.args = append(e.args, date)
return e
Expand Down

0 comments on commit 3f2421a

Please sign in to comment.