Skip to content

Commit 1f0976a

Browse files
committed
refactor
1 parent 7fa0341 commit 1f0976a

File tree

2 files changed

+48
-39
lines changed

2 files changed

+48
-39
lines changed

internal/rest/api_user.go

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

97
"github.com/danicc097/openapi-go-gin-postgres-sqlc/internal/repos/postgresql/gen/models"
108
"github.com/gin-gonic/gin"
@@ -91,43 +89,6 @@ func (h *StrictHandlers) UpdateUserAuthorization(c *gin.Context, request UpdateU
9189
return UpdateUserAuthorization204Response{}, nil
9290
}
9391

94-
func formatCursorValue(value interface{}) (string, error) {
95-
switch v := value.(type) {
96-
case time.Time:
97-
return v.Format(time.RFC3339Nano), nil
98-
case string:
99-
return v, nil
100-
case int, int8, int16, int32, int64:
101-
return fmt.Sprintf("%d", v), nil
102-
default:
103-
return "", fmt.Errorf("unhandled cursor type: %v", v)
104-
}
105-
}
106-
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-
13192
func (h *StrictHandlers) GetPaginatedUsers(c *gin.Context, request GetPaginatedUsersRequestObject) (GetPaginatedUsersResponseObject, error) {
13293
users, err := h.svc.User.Paginated(c, h.pool, request.Params)
13394
if err != nil {

internal/rest/pagination.go

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

0 commit comments

Comments
 (0)