From 831d4f78e30bb806f6defd068b00c4b50a55c41a Mon Sep 17 00:00:00 2001 From: Nick Griffey Date: Sat, 24 Feb 2024 18:21:24 -0600 Subject: [PATCH] split limit determination into separate function --- main.go | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 0dafad7..964bcdb 100644 --- a/main.go +++ b/main.go @@ -34,17 +34,23 @@ func formula(p float64) int64 { return int64(yearsAgo * 31556926) } -func (db *TimelineDB) closestEvents(t int64, n int, after bool, before bool) ([]EventsRow, error) { - ulimit := int64(math.MaxInt64) +func limits(t int64, after bool, before bool) (int64, int64, error) { llimit := int64(math.MinInt64) + ulimit := int64(math.MaxInt64) + var err error + if after && before { - return nil, errors.New("after and before can't both be true") + err = errors.New("after and before can't both be true") } else if after { llimit = t } else if before { ulimit = t } + return llimit, ulimit, err +} + +func (db *TimelineDB) closestEvents(t int64, n int, llimit int64, ulimit int64) ([]EventsRow, error) { query := ` select description, timestamp, yearknown, monthknown, dayknown, hourknown, minuteknown, secondknown from events @@ -130,7 +136,12 @@ func main() { } defer db.Close() - events, err := db.closestEvents(t, *nevents, *after, *before) + llimit, ulimit, err := limits(t, *after, *before) + if err != nil { + log.Fatal(err) + } + + events, err := db.closestEvents(t, *nevents, llimit, ulimit) if err != nil { log.Fatal(err) }