Skip to content

How would you implement Date Time based filters #49

Answered by benbjohnson
rugwirobaker asked this question in Q&A
Discussion options

You must be logged in to vote

Good question. I usually separate out the two filters. So if it's a filtered on a Created timestamp then I would have CreatedAfter and CreatedBefore fields on my filter:

type WidgetFilter struct {
	CreatedAfter  *time.Time
	CreatedBefore *time.Time
}

Then in the SQL construction it would look something like:

if v := filter.CreatedAfter; v != nil {
	where = append(where, "created_at >= ?")
	args = append(args, *v)
}

if v := filter.CreatedBefore; v != nil {
	where = append(where, "created_at < ?")
	args = append(args, *v)
}

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@rugwirobaker
Comment options

@benbjohnson
Comment options

Answer selected by benbjohnson
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants