https://github.com/gothinkster/golang-gin-realworld-example-app/blob/master/users/serializers.go

序列化定义

package users

import (
"gopkg.in/gin-gonic/gin.v1" "github.com/wangzitian0/golang-gin-starter-kit/common"
) // 用户简介序列化结构体
type ProfileSerializer struct {
C *gin.Context
UserModel
} // 用户简介响应json结构体
type ProfileResponse struct {
ID uint `json:"-"`
Username string `json:"username"`
Bio string `json:"bio"`
Image *string `json:"image"`
Following bool `json:"following"`
} // 用户简介的内置方法Response
func (self *ProfileSerializer) Response() ProfileResponse {
//中间件请求c.MustGet
myUserModel := self.C.MustGet("my_user_model").(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
} // 用户信息响应json结构体
type UserResponse struct {
Username string `json:"username"`
Email string `json:"email"`
Bio string `json:"bio"`
Image *string `json:"image"`
Token string `json:"token"`
} // 用户信息的内置方法Response
func (self *UserSerializer) Response() UserResponse {
//中间件请求c.MustGet
myUserModel := self.c.MustGet("my_user_model").(UserModel)
user := UserResponse{
Username: myUserModel.Username,
Email: myUserModel.Email,
Bio: myUserModel.Bio,
Image: myUserModel.Image,
Token: common.GenToken(myUserModel.ID),
}
return user
}

从golang-gin-realworld-example-app项目学写httpapi (三)的更多相关文章

  1. 从golang-gin-realworld-example-app项目学写httpapi (八)

    https://github.com/gothinkster/golang-gin-realworld-example-app/blob/master/common/unit_test.go 单元测试 ...

  2. 从golang-gin-realworld-example-app项目学写httpapi (七)

    https://github.com/gothinkster/golang-gin-realworld-example-app/blob/master/hello.go main调用 package ...

  3. 从golang-gin-realworld-example-app项目学写httpapi (六)

    https://github.com/gothinkster/golang-gin-realworld-example-app/blob/master/users/validators.go 验证器 ...

  4. 从golang-gin-realworld-example-app项目学写httpapi (五)

    https://github.com/gothinkster/golang-gin-realworld-example-app/blob/master/users/middlewares.go 中间件 ...

  5. 从golang-gin-realworld-example-app项目学写httpapi (四)

    https://github.com/gothinkster/golang-gin-realworld-example-app/blob/master/users/routers.go 路由定义 pa ...

  6. 从golang-gin-realworld-example-app项目学写httpapi (二)

    https://github.com/gothinkster/golang-gin-realworld-example-app/blob/master/users/models.go 模型定义 use ...

  7. 从golang-gin-realworld-example-app项目学写httpapi (一)

    https://wangzitian0.github.io/2013/06/29/zero-to-one-1/ https://github.com/gothinkster/golang-gin-re ...

  8. 【饿了么】—— Vue2.0高仿饿了么核心模块&移动端Web App项目爬坑(三)

    前言:接着上一篇项目总结,这一篇是学习过程记录的最后一篇,这里会梳理:评论组件.商家组件.优化.打包.相关资料链接.项目github地址:https://github.com/66Web/ljq_el ...

  9. Golang Gin 项目包依赖管理 godep 使用

    Golang Gin 项目包依赖管理 godep 使用 标签(空格分隔): Go 在按照github.com/tools/godep文档go get完包以后,调整项目结构为$GOPATH/src/$P ...

随机推荐

  1. 【ORACLE】oracle 日志文件管理

    修改Oracle重做日志文件大小 创建新的日志组1 删除旧的日志组0(旧的日志组状态需要是INACTIVE) 创建新的日志组2,组名为旧的日志组0的组名 删除日志组1 ---------------- ...

  2. MySQL 重命名数据库

    首先创建目标库 create database trgdb; 获取所有源库的表名 use information_schema; select table_name from TABLES where ...

  3. TortoiseGit学习系列之Windows上本地代码如何通过TortoiserGit提交到GitHub详解(图文)

    不多说,直接上干货! 前面博客 TortoiseGit学习系列之Windows上TortoiseGit的安装详解(图文) 上面博文给大家讲解了一下如何本地安装TortoiseGit. 这篇为大家讲一下 ...

  4. 布局xml里面所有元素详解

    被坑惨了,为了去掉一个元素,被各种莫名其妙的问题坑惨了.把所有常用到的都记录下来,不要再被坑到了 tools:context:http://blog.csdn.net/xiabing082/artic ...

  5. LINQ-let子句、join子句

    1.Let子句 let子句用于在LINQ表达式中存储子表达式的计算结果,既let子句创建一个范围变量来存储结果,变量被创建后,不能修改或把其他表达式的结果重新赋值给它.此范围变量可以在后续的LINQ中 ...

  6. MVC-AOP(面向切面编程)思想-Filter 三种注册方式

    在ASP.NET MVC框架中,为我们提供了四种类型的Filter类型包括:IAuthorizationFilter.IActionFilter.IResultFilter.IExceptionFil ...

  7. 第9天:原型、继承、函数使用推荐以及this的指向

    原型 javascript原型指向改变如何添加方法和访问 <!DOCTYPE html> <html lang="en"> <head> < ...

  8. 撩课-Python-每天5道面试题-第5天

    一. 给定一个圆心和半径, 以及一个点坐标, 判定该点是否在圆内; 例如: 用户输入圆心: (1, 2) 半径: 2.5 测试点为(2, 2)结果: 判定测试点是在圆内 思路: 结合勾股定理, 计算测 ...

  9. ChannelSftp.chmod方法赋权文件夹

    最近对接分账工作需要上传分账文件到指定sftp服务器,在给sftp上文件夹赋权是遇到一个问题: 具体是ChannelSftp的chmod(int permissions, String path)方法 ...

  10. 7. Reverse Integer 反向输出整数 easy 9. Palindrome Number 判断是否是水仙花数 easy

    Given a 32-bit signed integer, reverse digits of an integer. 将32位整数反向输出. Note:Assume we are dealing ...