Skip to content

Commit 5b37bb6

Browse files
author
Buco7854
committed
feat: add limit
1 parent 85d0ba8 commit 5b37bb6

File tree

7 files changed

+51
-13
lines changed

7 files changed

+51
-13
lines changed

api/controllers/image/binding.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func QueryParamsBinder(
6060
gif *string,
6161
orderBy *string,
6262
orientation *string,
63-
many *bool,
63+
limit *int,
6464
full *bool,
6565
width *string,
6666
height *string,
@@ -189,7 +189,7 @@ func QueryParamsBinder(
189189
}
190190
return []error{echo.NewBindingError("byte_size", values[0:1], regexError(ComparatorRegexRule), nil)}
191191
}).
192-
Bool("many", many).
192+
Int("limit", limit).
193193
Bool("full", full).
194194
BindError()
195195
}

api/controllers/image/image_controller.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func (controller Controller) RouteSelector(favRoute bool) echo.HandlerFunc {
8686
var gif = constants.Null
8787
var orderBy = constants.Random
8888
var orientation = constants.Random
89-
var many = false
89+
var limit = 0
9090
var full = false
9191
var width string
9292
var height string
@@ -115,14 +115,21 @@ func (controller Controller) RouteSelector(favRoute bool) echo.HandlerFunc {
115115
&gif,
116116
&orderBy,
117117
&orientation,
118-
&many,
118+
&limit,
119119
&full,
120120
&width,
121121
&height,
122122
&byteSize,
123123
); err != nil {
124124
return err
125125
}
126+
if limit != 0 && limit < 2 {
127+
return &echo.HTTPError{
128+
Code: http.StatusBadRequest,
129+
Message: "If provided limit should be greater than 1",
130+
Internal: nil,
131+
}
132+
}
126133
rows, execTime, err := controller.Globals.Database.FetchImages(
127134
isNsfw,
128135
includedTags,
@@ -132,7 +139,7 @@ func (controller Controller) RouteSelector(favRoute bool) echo.HandlerFunc {
132139
gif,
133140
orderBy,
134141
orientation,
135-
many,
142+
limit,
136143
full,
137144
width,
138145
height,

api/middlewares/skippers.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package middlewares
33
import (
44
"bytes"
55
"encoding/json"
6+
"github.com/Waifu-im/waifu-api/constants"
67
"github.com/Waifu-im/waifu-api/serializers"
78
"github.com/labstack/echo/v4"
89
"io"
@@ -45,6 +46,24 @@ func Int64ParamsSkipper(sourceParam string, contextKey string, skipAfter bool) f
4546
}
4647
}
4748

49+
// LimitParamsSkipper return true when skipAfter and the param provided is not superior to MaxLimit (only for int param).
50+
// If a param is provided it can assign it to a context 'variable' if contextKey is passed
51+
func LimitParamsSkipper(sourceParam string, contextKey string, skipAfter bool) func(echo.Context) (bool, error) {
52+
return func(c echo.Context) (bool, error) {
53+
var param int
54+
if err := echo.QueryParamsBinder(c).Int(sourceParam, &param).BindError(); err != nil {
55+
return false, err
56+
}
57+
if param <= constants.MaxLimit && skipAfter {
58+
return true, nil
59+
}
60+
if contextKey != "" {
61+
(c).Set(contextKey, param)
62+
}
63+
return false, nil
64+
}
65+
}
66+
4867
// SkipOrSetUser I use this 'skipper' has a function to set the user id present in the body so that permissions can check for it later
4968
func SkipOrSetUser(isSkipper bool) func(echo.Context) (bool, error) {
5069
return func(c echo.Context) (bool, error) {

api/routes/image.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"github.com/Waifu-im/waifu-api/api/controllers/image"
55
"github.com/Waifu-im/waifu-api/api/middlewares"
66
"github.com/Waifu-im/waifu-api/api/utils"
7+
"github.com/Waifu-im/waifu-api/constants"
78
"github.com/labstack/echo/v4"
89
)
910

@@ -19,14 +20,23 @@ func AddImageRouter(globals utils.Globals, app *echo.Echo) error {
1920
globals,
2021
func(c echo.Context) (bool, error) {
2122
var full bool
23+
var limit int
2224
_ = echo.QueryParamsBinder(c).Bool("full", &full)
25+
_ = echo.QueryParamsBinder(c).Int("limit", &limit)
2326
// when new release will be out this middleware will be on the whole group with that condition in addition
2427
// c.Request().URL.Path == "/search"
25-
return !full, nil
28+
skip := !full && limit <= constants.MaxLimit
29+
return skip, nil
2630
}),
27-
middlewares.PermissionsVerification(globals, []string{"admin"}, middlewares.BoolParamsSkipper("full", "", true)),
31+
middlewares.PermissionsVerification(
32+
globals, []string{"admin"},
33+
middlewares.BoolParamsSkipper("full", "", true),
34+
),
35+
middlewares.PermissionsVerification(
36+
globals, []string{"admin"},
37+
middlewares.LimitParamsSkipper("limit", "", true),
38+
),
2839
)
29-
3040
app.GET(
3141
"/fav",
3242
controller.Fav(),

constants/database.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ const (
2121
Portrait = "PORTRAIT"
2222
)
2323

24-
const ManyLimit = "30"
24+
const MaxLimit = 30

database/queries.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,14 @@ func (database Database) FetchImages(
2525
gif string,
2626
orderBy string,
2727
orientation string,
28-
many bool,
28+
limit int,
2929
full bool,
3030
width string,
3131
height string,
3232
byteSize string,
3333
userId int64,
3434
) (ImageRows, time.Duration, error) {
35+
limitString := strconv.Itoa(limit)
3536
var parameters []any
3637

3738
// Select some information about the images
@@ -85,9 +86,10 @@ func (database Database) FetchImages(
8586
if len(includedTags) > 0 {
8687
query += "HAVING COUNT(*)=" + strconv.FormatInt(int64(len(includedTags)), 10) + " "
8788
}
89+
// If it's not in full mode or files has been provided, and it's not in fav mode and limit is provided then add the limit.
8890
query += FormatOrderBy(orderBy, "", true)
89-
if !(full || len(includedFiles) > 0) && userId == 0 && many {
90-
query += "LIMIT " + constants.ManyLimit + " "
91+
if !(full || len(includedFiles) > 0) && userId == 0 && limit > 0 {
92+
query += "LIMIT " + limitString + " "
9193
} else if !(full || len(includedFiles) > 0) && userId == 0 {
9294
query += "LIMIT 1 "
9395
}
@@ -163,7 +165,7 @@ func (database Database) FetchImages(
163165
if err = rows.Err(); err != nil {
164166
return imageRows, time.Since(start), err
165167
}
166-
if orderBy == constants.Random && (full || many || len(includedFiles) > 0 || userId != 0) {
168+
if orderBy == constants.Random && len(imageRows.Rows) > 1 {
167169
rand.Seed(time.Now().UnixNano())
168170
rand.Shuffle(
169171
len(imageRows.Rows),

waifu-api

-2.64 KB
Binary file not shown.

0 commit comments

Comments
 (0)