From bc005f8b305988140b489c7be05f2a9fb4d8d60d Mon Sep 17 00:00:00 2001 From: Tigor Hutasuhut Date: Wed, 5 Jun 2024 11:12:20 +0700 Subject: [PATCH] refactor: direction is now revsered boolean --- api/schedule_history_list.go | 26 +++++++++++++------------- views/schedulehistories/view.templ | 4 ++-- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/api/schedule_history_list.go b/api/schedule_history_list.go index cf6f8b6..4a78145 100644 --- a/api/schedule_history_list.go +++ b/api/schedule_history_list.go @@ -2,6 +2,7 @@ package api import ( "context" + "slices" "strconv" "time" @@ -16,15 +17,14 @@ import ( type ScheduleHistoryListParams struct { Subreddit string Time time.Time - Direction string + Reversed bool - Limit int64 - Offset int64 + Limit int64 } func (params *ScheduleHistoryListParams) FillFromQuery(query Queryable) { params.Subreddit = query.Get("subreddit") - params.Direction = query.Get("direction") + params.Reversed = query.Get("direction") == "before" params.Limit, _ = strconv.ParseInt(query.Get("limit"), 10, 64) if params.Limit < 1 { params.Limit = 100 @@ -33,11 +33,6 @@ func (params *ScheduleHistoryListParams) FillFromQuery(query Queryable) { params.Limit = 1000 } - params.Offset, _ = strconv.ParseInt(query.Get("offset"), 10, 64) - if params.Offset < 0 { - params.Offset = 0 - } - now := time.Now() timeInt, _ := strconv.ParseInt(query.Get("time"), 10, 64) @@ -56,7 +51,7 @@ func (params ScheduleHistoryListParams) CountQuery() (expr []bob.Mod[*dialect.Se expr = append(expr, models.SelectWhere.ScheduleHistories.Subreddit.EQ(params.Subreddit)) } if !params.Time.IsZero() { - if params.Direction == "before" { + if params.Reversed { expr = append(expr, models.SelectWhere.ScheduleHistories.CreatedAt.GTE(params.Time.Unix()), ) @@ -73,10 +68,11 @@ func (params ScheduleHistoryListParams) Query() (expr []bob.Mod[*dialect.SelectQ if params.Limit > 0 { expr = append(expr, sm.Limit(params.Limit)) } - if params.Offset > 0 { - expr = append(expr, sm.Offset(params.Offset)) + if params.Reversed { + expr = append(expr, sm.OrderBy(models.ScheduleHistoryColumns.CreatedAt).Asc()) + } else { + expr = append(expr, sm.OrderBy(models.ScheduleHistoryColumns.CreatedAt).Desc()) } - expr = append(expr, sm.OrderBy(models.ScheduleHistoryColumns.CreatedAt).Desc()) return expr } @@ -165,5 +161,9 @@ func (api *API) ScheduleHistoryList(ctx context.Context, params ScheduleHistoryL return result, errs.Wrapw(err, "failed to count schedule histories", "query", params) } + if params.Reversed { + slices.Reverse(result.Schedules) + } + return result, nil } diff --git a/views/schedulehistories/view.templ b/views/schedulehistories/view.templ index 8288974..efd7112 100644 --- a/views/schedulehistories/view.templ +++ b/views/schedulehistories/view.templ @@ -130,7 +130,7 @@ func actionButtonItems(data Data) []templ.Component { if !data.IsCurrent { out = append(out, actionButtonPrev(data)) } - if len(data.ScheduleHistories.Schedules) >= int(data.Params.Limit) { + if len(data.ScheduleHistories.Schedules) >= int(data.Params.Limit) || data.Params.Reversed { out = append(out, actionButtonNext(data)) } if data.IsCurrent { @@ -166,7 +166,7 @@ templ dateBar(data Data) { } - if len(data.ScheduleHistories.Schedules) >= int(data.Params.Limit) { + if len(data.ScheduleHistories.Schedules) >= int(data.Params.Limit) || data.Params.Reversed {