Skip to content

Commit 38b9df5

Browse files
committed
Stripe-style json list objects, with urls and pagination attributes
1 parent 6a30110 commit 38b9df5

File tree

6 files changed

+48
-40
lines changed

6 files changed

+48
-40
lines changed

dao/user_dao.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111
var users []models.User
1212

1313
func InitDb() {
14-
users = append(users, models.User{Id: "abc", AuthToken: "userA", Email: "[email protected]", DOB: "01/23/1984", FavoriteCity: "Dallas"})
15-
users = append(users, models.User{Id: "xyz", AuthToken: "userB", Email: "[email protected]", DOB: "02/27/1991", FavoriteCity: "Portland", Admin: true})
14+
users = append(users, models.User{DefaultModel: models.DefaultModel{Id: "abc", ObjectType: "user"}, AuthToken: "userA", Email: "[email protected]", DOB: "01/23/1984", FavoriteCity: "Dallas"})
15+
users = append(users, models.User{DefaultModel: models.DefaultModel{Id: "xyz", ObjectType: "user"}, AuthToken: "userB", Email: "[email protected]", DOB: "02/27/1991", FavoriteCity: "Portland", Admin: true})
1616
}
1717

1818
func GetAllUsers(currentUser models.User) []models.User {

models/default_model.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package models
2+
3+
type DefaultModel struct {
4+
Id string `json:"id"`
5+
ObjectType string `json:"object"`
6+
}

models/user.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package models
22

33
type User struct {
4-
Id string `json:"id"`
4+
DefaultModel
55
Email string `json:"email"`
66
DOB string `json:"dob"`
77
FavoriteCity string `json:"favorite_city"`

resources/user_resource.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
func GetAllUsers(c *gin.Context) {
1010
currentUser := getCurrentUser(c)
1111
users := dao.GetAllUsers(currentUser)
12-
c.JSON(200, serializers.SerializeUsers(users, currentUser))
12+
c.JSON(200, serializers.SerializeUsers(users, currentUser, "/users"))
1313
}
1414

1515
func GetUserById(c *gin.Context) {

serializers/common.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package serializers
2+
3+
type ListMeta struct {
4+
ObjectType string `json:"object"`
5+
Count uint16 `json:"total_count"`
6+
More bool `json:"has_more"`
7+
URL string `json:"url"`
8+
}

serializers/user_serializers.go

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,73 +3,67 @@ package serializers
33
import "github.com/sbecker/gin-api-demo/models"
44

55
type UsersJSON struct {
6-
Users []models.User `json:"users"`
7-
}
8-
9-
type UserJSON struct {
10-
User models.User `json:"user"`
6+
ListMeta
7+
Values []models.User `json:"data"`
118
}
129

1310
type UsersSubsetJSON struct {
14-
Users []UserSubset `json:"users"`
15-
}
16-
17-
type UserSubsetJSON struct {
18-
User UserSubset `json:"user"`
11+
ListMeta
12+
Values []UserSubset `json:"data"`
1913
}
2014

2115
type UserSubset struct {
22-
Id string `json:"id"`
16+
models.DefaultModel
2317
Email string `json:"email"`
2418
DOB string `json:"dob"`
2519
}
2620

2721
func NewUserSubset(user models.User) UserSubset {
2822
return UserSubset{
29-
Id: user.Id,
30-
Email: user.Email,
31-
DOB: user.DOB,
23+
DefaultModel: user.DefaultModel,
24+
Email: user.Email,
25+
DOB: user.DOB,
3226
}
3327
}
3428

35-
func NewUserSubsetJSON(user models.User) UserSubsetJSON {
36-
return UserSubsetJSON{
37-
User: UserSubset{
38-
Id: user.Id,
39-
Email: user.Email,
40-
DOB: user.DOB,
29+
func NewUsersJSON(users []models.User, URL string) UsersJSON {
30+
return UsersJSON{
31+
Values: users,
32+
ListMeta: ListMeta{
33+
ObjectType: "list",
34+
URL: URL,
35+
Count: uint16(len(users)),
4136
},
4237
}
4338
}
4439

45-
func NewUserJSON(user models.User) UserJSON {
46-
return UserJSON{User: user}
47-
}
48-
49-
func NewUsersJSON(users []models.User) UsersJSON {
50-
return UsersJSON{Users: users}
51-
}
52-
53-
func NewUsersSubsetJSON(users []models.User) UsersSubsetJSON {
54-
json := UsersSubsetJSON{Users: []UserSubset{}}
40+
func NewUsersSubsetJSON(users []models.User, URL string) UsersSubsetJSON {
41+
json := UsersSubsetJSON{
42+
Values: []UserSubset{},
43+
ListMeta: ListMeta{
44+
ObjectType: "list",
45+
URL: URL,
46+
Count: uint16(len(users)),
47+
},
48+
}
5549
for _, user := range users {
56-
json.Users = append(json.Users, NewUserSubset(user))
50+
json.Values = append(json.Values, NewUserSubset(user))
5751
}
5852
return json
5953
}
6054

61-
func SerializeUsers(users []models.User, currentUser models.User) interface{} {
55+
func SerializeUsers(users []models.User, currentUser models.User, URL string) interface{} {
6256
if currentUser.Admin {
63-
return NewUsersJSON(users)
57+
return NewUsersJSON(users, URL)
6458
} else {
65-
return NewUsersSubsetJSON(users)
59+
return NewUsersSubsetJSON(users, URL)
6660
}
6761
}
6862

6963
func SerializeUser(user models.User, currentUser models.User) interface{} {
7064
if currentUser.Admin {
71-
return NewUserJSON(user)
65+
return user
7266
} else {
73-
return NewUserSubsetJSON(user)
67+
return NewUserSubset(user)
7468
}
7569
}

0 commit comments

Comments
 (0)