Skip to content

Commit f4c56cb

Browse files
committed
refactor
1 parent 7fa0341 commit f4c56cb

File tree

2 files changed

+34
-25
lines changed

2 files changed

+34
-25
lines changed

internal/rest/api_user.go

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package rest
33
import (
44
"errors"
55
"fmt"
6-
"reflect"
76
"time"
87

98
"github.com/danicc097/openapi-go-gin-postgres-sqlc/internal/repos/postgresql/gen/models"
@@ -104,30 +103,6 @@ func formatCursorValue(value interface{}) (string, error) {
104103
}
105104
}
106105

107-
func getNextCursor(entity interface{}, jsonFieldName string, tableEntity models.TableEntity) (string, error) {
108-
if entity == nil {
109-
return "", fmt.Errorf("no entity given")
110-
}
111-
112-
if _, ok := models.EntityFields[tableEntity]; !ok {
113-
return "", fmt.Errorf("no entity found")
114-
}
115-
116-
entityType := reflect.TypeOf(entity)
117-
entityValue := reflect.ValueOf(entity)
118-
119-
for i := 0; i < entityType.NumField(); i++ {
120-
structField := entityType.Field(i)
121-
jsonTag := structField.Tag.Get("json")
122-
if jsonTag == jsonFieldName {
123-
fieldValue := entityValue.Field(i).Interface()
124-
return formatCursorValue(fieldValue)
125-
}
126-
}
127-
128-
return "", fmt.Errorf("no json tag with value: %v", jsonFieldName)
129-
}
130-
131106
func (h *StrictHandlers) GetPaginatedUsers(c *gin.Context, request GetPaginatedUsersRequestObject) (GetPaginatedUsersResponseObject, error) {
132107
users, err := h.svc.User.Paginated(c, h.pool, request.Params)
133108
if err != nil {

internal/rest/pagination.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package rest
2+
3+
import (
4+
"fmt"
5+
"reflect"
6+
7+
"github.com/danicc097/openapi-go-gin-postgres-sqlc/internal/repos/postgresql/gen/models"
8+
)
9+
10+
// getNextCursor returns the next cursor from an entity struct by a given json tag.
11+
// This allows for dynamic pagination parameters.
12+
func getNextCursor(entity interface{}, jsonFieldName string, tableEntity models.TableEntity) (string, error) {
13+
if entity == nil {
14+
return "", fmt.Errorf("no entity given")
15+
}
16+
17+
if _, ok := models.EntityFields[tableEntity]; !ok {
18+
return "", fmt.Errorf("no entity found")
19+
}
20+
21+
entityType := reflect.TypeOf(entity)
22+
entityValue := reflect.ValueOf(entity)
23+
24+
for i := 0; i < entityType.NumField(); i++ {
25+
structField := entityType.Field(i)
26+
jsonTag := structField.Tag.Get("json")
27+
if jsonTag == jsonFieldName {
28+
fieldValue := entityValue.Field(i).Interface()
29+
return formatCursorValue(fieldValue)
30+
}
31+
}
32+
33+
return "", fmt.Errorf("no json tag with value: %v", jsonFieldName)
34+
}

0 commit comments

Comments
 (0)