refactor: direction is now revsered boolean

This commit is contained in:
Tigor Hutasuhut 2024-06-05 11:12:20 +07:00
parent f71cda7c92
commit bc005f8b30
2 changed files with 15 additions and 15 deletions

View file

@ -2,6 +2,7 @@ package api
import ( import (
"context" "context"
"slices"
"strconv" "strconv"
"time" "time"
@ -16,15 +17,14 @@ import (
type ScheduleHistoryListParams struct { type ScheduleHistoryListParams struct {
Subreddit string Subreddit string
Time time.Time Time time.Time
Direction string Reversed bool
Limit int64 Limit int64
Offset int64
} }
func (params *ScheduleHistoryListParams) FillFromQuery(query Queryable) { func (params *ScheduleHistoryListParams) FillFromQuery(query Queryable) {
params.Subreddit = query.Get("subreddit") 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) params.Limit, _ = strconv.ParseInt(query.Get("limit"), 10, 64)
if params.Limit < 1 { if params.Limit < 1 {
params.Limit = 100 params.Limit = 100
@ -33,11 +33,6 @@ func (params *ScheduleHistoryListParams) FillFromQuery(query Queryable) {
params.Limit = 1000 params.Limit = 1000
} }
params.Offset, _ = strconv.ParseInt(query.Get("offset"), 10, 64)
if params.Offset < 0 {
params.Offset = 0
}
now := time.Now() now := time.Now()
timeInt, _ := strconv.ParseInt(query.Get("time"), 10, 64) 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)) expr = append(expr, models.SelectWhere.ScheduleHistories.Subreddit.EQ(params.Subreddit))
} }
if !params.Time.IsZero() { if !params.Time.IsZero() {
if params.Direction == "before" { if params.Reversed {
expr = append(expr, expr = append(expr,
models.SelectWhere.ScheduleHistories.CreatedAt.GTE(params.Time.Unix()), 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 { if params.Limit > 0 {
expr = append(expr, sm.Limit(params.Limit)) expr = append(expr, sm.Limit(params.Limit))
} }
if params.Offset > 0 { if params.Reversed {
expr = append(expr, sm.Offset(params.Offset)) 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 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) return result, errs.Wrapw(err, "failed to count schedule histories", "query", params)
} }
if params.Reversed {
slices.Reverse(result.Schedules)
}
return result, nil return result, nil
} }

View file

@ -130,7 +130,7 @@ func actionButtonItems(data Data) []templ.Component {
if !data.IsCurrent { if !data.IsCurrent {
out = append(out, actionButtonPrev(data)) 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)) out = append(out, actionButtonNext(data))
} }
if data.IsCurrent { if data.IsCurrent {
@ -166,7 +166,7 @@ templ dateBar(data Data) {
</a> </a>
</div> </div>
} }
if len(data.ScheduleHistories.Schedules) >= int(data.Params.Limit) { if len(data.ScheduleHistories.Schedules) >= int(data.Params.Limit) || data.Params.Reversed {
<div class="tooltip" data-tip="Next"> <div class="tooltip" data-tip="Next">
<a <a
href={ templ.SafeURL(fmt.Sprintf("/history?time=%d", data.ScheduleHistories.GetLastTime().Unix())) } href={ templ.SafeURL(fmt.Sprintf("/history?time=%d", data.ScheduleHistories.GetLastTime().Unix())) }