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. sql geography类型(地理坐标) 赋值

    sql 语句赋值 update [lishui].[dbo].[t_NearbyService] ,, ) ,[locationbaidu]=geography::Point(,, ) where [ ...

  2. DB2 体系结构 (进程模型)

    DB2 是众多关系型数据库中的一种, 关系型数据库还包括比较火的Oracle,MySQL 实例 数据库 DB2 进程模型 DB2 通过 db2start 命令启动数据库实例,即启动相应的进程和线程,并 ...

  3. Java类文件结构及javac的ClassReader类解读

    首先来看一下ClassFile,类注释如下: A JVM class file. Generic Java classfiles have one additional attribute for c ...

  4. 3-0 js基础 语言特性及性能优化

    1.语言特性: 内存泄露:内存没有释放,越堆越多. 垃圾回收(生命周期): 1.局部 很短 在局部中当函数完成时.已经释放了.全局变量在页面关闭的时候才被回收. 2.全局 很长 3.闭包.可长可短,只 ...

  5. java.lang.NoClassDefFoundError: org/springframework/ui/jasperreports/JasperReportsUtils原因

    在springMVC结合jasperReporter中发现的问题 java.lang.NoClassDefFoundError: org/springframework/ui/jasperreport ...

  6. Hardware Emulation Platform (硬件仿真平台) 在IC验证中的运用情况如何?

    三个EDA公司都有自己的hardware emulation verification platform: Cadence Palladium, Synopsys ZeBu, Mentor Veloc ...

  7. Spring---AOP与DI的初步理解

    依赖注入 依赖注入并没有我们听上去那么复杂,在项目中应用依赖注入,会使代码变的异常简单,更易于理解和测试. 任何一个有实际意义的应用,都是多个类组成,这些类之间相互协作,来实现特定的业务逻辑,通常,每 ...

  8. 九: 操作提示(js版本)

    一.toast 消息提示框 他用到了一个wx.showToast(object) 这样一个方法.作用是显示提示框. /* ---page/test/test.wxml----*/   <butt ...

  9. SQL Serever学习8——数据表3

    创建索引 索引就像是字典的目录一样,可以快速的指定需要的数据. 有没有索引的区别 一个没有索引的集合,如果我们需要查找某一个对象,需要遍历整个集合,直到找到匹配的对象,整个工作费时费力,这只是找一个对 ...

  10. Java常用的排序算法三

    Merge Sort :归并排序:用递归的思想,分解成单个元素的排序,在归并 代码: import java.util.*; public class MergeSort { public stati ...