init: 初始化 AssetX 项目仓库
包含 webapp(Next.js 用户端)、webapp-back(Go 后端)、 antdesign(管理后台)、landingpage(营销落地页)、 数据库 SQL 和配置文件。
This commit is contained in:
66
webapp-back/users/serializers.go
Normal file
66
webapp-back/users/serializers.go
Normal file
@@ -0,0 +1,66 @@
|
||||
package users
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/gothinkster/golang-gin-realworld-example-app/common"
|
||||
)
|
||||
|
||||
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("my_user_model").(UserModel)
|
||||
image := ""
|
||||
if self.Image != nil {
|
||||
image = *self.Image
|
||||
}
|
||||
profile := ProfileResponse{
|
||||
ID: self.ID,
|
||||
Username: self.Username,
|
||||
Bio: self.Bio,
|
||||
Image: 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("my_user_model").(UserModel)
|
||||
image := ""
|
||||
if myUserModel.Image != nil {
|
||||
image = *myUserModel.Image
|
||||
}
|
||||
user := UserResponse{
|
||||
Username: myUserModel.Username,
|
||||
Email: myUserModel.Email,
|
||||
Bio: myUserModel.Bio,
|
||||
Image: image,
|
||||
Token: common.GenToken(myUserModel.ID),
|
||||
}
|
||||
return user
|
||||
}
|
||||
Reference in New Issue
Block a user