-
Notifications
You must be signed in to change notification settings - Fork 2
/
serializers.go
58 lines (50 loc) · 1.33 KB
/
serializers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package users
import (
"github.com/gin-gonic/gin"
"github.com/qianyan/go-gin-quickstart/infra"
)
type ProfileSerializer struct {
C *gin.Context
UserModel
}
// Declare your response schema here
type ProfileResponse struct {
ID uint `json:"-"`
Username string `json:"username"`
Bio string `json:"bio"`
Image *string `json:"image"`
Following bool `json:"following"`
}
// Put your response logic including wrap the userModel here.
func (self *ProfileSerializer) Response() ProfileResponse {
myUserModel := self.C.MustGet("currentUserModel").(UserModel)
profile := ProfileResponse{
ID: self.ID,
Username: self.Username,
Bio: self.Bio,
Image: self.Image,
Following: myUserModel.isFollowing(self.UserModel),
}
return profile
}
type UserSerializer struct {
c *gin.Context
}
type UserResponse struct {
Username string `json:"username"`
Email string `json:"email"`
Bio string `json:"bio"`
Image *string `json:"image"`
Token string `json:"token"`
}
func (self *UserSerializer) Response() UserResponse {
myUserModel := self.c.MustGet("currentUserModel").(UserModel)
user := UserResponse{
Username: myUserModel.Username,
Email: myUserModel.Email,
Bio: myUserModel.Bio,
Image: myUserModel.Image,
Token: infra.GenToken(myUserModel.ID),
}
return user
}