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

路由定义

package users

import (
"errors"
"net/http" "github.com/gin-gonic/gin"
"github.com/wangzitian0/golang-gin-starter-kit/common"
) // 路由函数 用于用户注册、登录请求,注意 参数 *gin.RouterGroup, 用gin的groups router调用
func UsersRegister(router *gin.RouterGroup) {
router.POST("/", UsersRegistration)
router.POST("/login", UsersLogin)
} // 路由函数 用于用户信息获取、更新请求
func UserRegister(router *gin.RouterGroup) {
router.GET("/", UserRetrieve)
router.PUT("/", UserUpdate)
} // 路由函数 用于用户简介获取、增加关注、删除关注
// 请求参数变量,使用 :变量名
func ProfileRegister(router *gin.RouterGroup) {
router.GET("/:username", ProfileRetrieve)
router.POST("/:username/follow", ProfileFollow)
router.DELETE("/:username/follow", ProfileUnfollow)
} // 请求处理函数,用户简介获取,注意 参数 *gin.Context
func ProfileRetrieve(c *gin.Context) {
// 获取请求参数 username
username := c.Param("username") // 调用模型定义的FindOneUser函数
userModel, err := FindOneUser(&UserModel{Username: username}) if err != nil {
c.JSON(http.StatusNotFound, common.NewError("profile", errors.New("Invalid username")))
return
} // 序列化查询的结果
profileSerializer := ProfileSerializer{c, userModel}
c.JSON(http.StatusOK, gin.H{"profile": profileSerializer.Response()})
} // 请求处理函数,用户简介选择关注
func ProfileFollow(c *gin.Context) {
username := c.Param("username")
userModel, err := FindOneUser(&UserModel{Username: username})
if err != nil {
c.JSON(http.StatusNotFound, common.NewError("profile", errors.New("Invalid username")))
return
} // 中间件
myUserModel := c.MustGet("my_user_model").(UserModel)
err = myUserModel.following(userModel)
if err != nil {
c.JSON(http.StatusUnprocessableEntity, common.NewError("database", err))
return
}
serializer := ProfileSerializer{c, userModel}
c.JSON(http.StatusOK, gin.H{"profile": serializer.Response()})
} // 请求处理函数,用户简介取消关注
func ProfileUnfollow(c *gin.Context) {
username := c.Param("username")
userModel, err := FindOneUser(&UserModel{Username: username})
if err != nil {
c.JSON(http.StatusNotFound, common.NewError("profile", errors.New("Invalid username")))
return
} // 中间件
myUserModel := c.MustGet("my_user_model").(UserModel)
err = myUserModel.unFollowing(userModel)
if err != nil {
c.JSON(http.StatusUnprocessableEntity, common.NewError("database", err))
return
}
serializer := ProfileSerializer{c, userModel}
c.JSON(http.StatusOK, gin.H{"profile": serializer.Response()})
} // 请求处理函数,用户注册
func UsersRegistration(c *gin.Context) {
// 初始化注册验证
userModelValidator := NewUserModelValidator()
if err := userModelValidator.Bind(c); err != nil {
c.JSON(http.StatusUnprocessableEntity, common.NewValidatorError(err))
return
} if err := SaveOne(&userModelValidator.userModel); err != nil {
c.JSON(http.StatusUnprocessableEntity, common.NewError("database", err))
return
} // 设置全局中间件 my_user_model
c.Set("my_user_model", userModelValidator.userModel)
serializer := UserSerializer{c}
c.JSON(http.StatusCreated, gin.H{"user": serializer.Response()})
} // 请求处理函数,用户登录
func UsersLogin(c *gin.Context) {
// 初始化登录验证
loginValidator := NewLoginValidator()
if err := loginValidator.Bind(c); err != nil {
c.JSON(http.StatusUnprocessableEntity, common.NewValidatorError(err))
return
} // 验证用户是否存在
userModel, err := FindOneUser(&UserModel{Email: loginValidator.userModel.Email})
if err != nil {
c.JSON(http.StatusForbidden, common.NewError("login", errors.New("Not Registered email or invalid password")))
return
} // 验证密码是否有效
if userModel.checkPassword(loginValidator.User.Password) != nil {
c.JSON(http.StatusForbidden, common.NewError("login", errors.New("Not Registered email or invalid password")))
return
} // 更新上下文中的用户id
UpdateContextUserModel(c, userModel.ID)
serializer := UserSerializer{c}
c.JSON(http.StatusOK, gin.H{"user": serializer.Response()})
} // 请求处理函数,用户信息获取
func UserRetrieve(c *gin.Context) {
// UserSerializer执行中间件
serializer := UserSerializer{c}
c.JSON(http.StatusOK, gin.H{"user": serializer.Response()})
} // 请求处理函数,用户信息更新
func UserUpdate(c *gin.Context) {
myUserModel := c.MustGet("my_user_model").(UserModel)
userModelValidator := NewUserModelValidatorFillWith(myUserModel)
if err := userModelValidator.Bind(c); err != nil {
c.JSON(http.StatusUnprocessableEntity, common.NewValidatorError(err))
return
} userModelValidator.userModel.ID = myUserModel.ID
if err := myUserModel.Update(userModelValidator.userModel); err != nil {
c.JSON(http.StatusUnprocessableEntity, common.NewError("database", err))
return
} // 更新上下文中的用户id
UpdateContextUserModel(c, myUserModel.ID)
serializer := UserSerializer{c}
c.JSON(http.StatusOK, gin.H{"user": serializer.Response()})
}

从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/serializers.go 序列化 ...

  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. Golang Gin 项目包依赖管理 godep 使用

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

  9. Golang Gin 项目使用 Swagger

    Golang Gin 项目使用 Swagger 标签(空格分隔): Go 首先需要github.com/swaggo/gin-swagger和github.com/swaggo/gin-swagger ...

随机推荐

  1. vertical-aligin

    垂直对齐元素只应用于行内元素(图像.文本)和表单元素 垂直对齐文本会影响行高 默认值为baseline

  2. Java jstl标签使用总结

    1.在jsp文件中引用 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %&g ...

  3. logstash 启动报找不主类或无法加载 java

    logstash 启动报无法找到主类解决方案 Zparkle 关注 2018.03.08 22:04* 字数 2051 阅读 1评论 0喜欢 0 当logstash启动时,首先要注意正确配置java ...

  4. CSS动态控制DIV居中

    1.所谓的动态:就是即使手动去拖拉浏览器,DIV还是会自动居中 2.之前一直以为这个事情是JavaScript做的, 步骤:通过先获取页面的Height和Width, 然后定义DIV的Height和W ...

  5. Spring中线程池的应用

    多线程并发处理起来通常比较麻烦,如果你使用spring容器来管理业务bean,事情就好办了多了.spring封装了Java的多线程的实现,你只需要关注于并发事物的流程以及一些并发负载量等特性,具体来说 ...

  6. AutoDetectChangesEnabled及AddRange解决EF插入的性能问题

    转自:http://www.cnblogs.com/nianming/archive/2013/06/07/3123103.html#2699851 记录下. 园友莱布尼茨写了一篇<Entity ...

  7. 请别再拿“String s = new String("xyz");创建了多少个String实例”来面试了吧---转

    http://www.iteye.com/topic/774673 羞愧呀,不知道多少人干过,我也干过,面壁去! 这帖是用来回复高级语言虚拟机圈子里的一个问题,一道Java笔试题的. 本来因为见得太多 ...

  8. 【TCP协议】MTU和MSS详解

    需要注意的是,区别两种帧封装格式:802标准帧和以太网帧 1,在802标准定义的帧格式中,长度字段是指它后续数据的字节长度,但不包括C R C检验码.RFC 1042(IEEE 802) 2,RFC ...

  9. MySQL建表语句的一些特殊字段

    这里的字段会不断更新 unsigned 这个字段一般在建表的时候写在Id上,用来表示不分正负号 tinyint 这个字段如果设置为UNSIGNED类型,只能存储从0到255的整数,不能用来储存负数. ...

  10. 微信小程序二维码识别

    目前市场上二维码识别的软件或者网站越来越多,可是真正方便,无广告的却少之很少. 于是,自己突发奇想做了一个微信二维码识别的小程序. 包含功能: 1.识别二维码 ①普通二维码 ②条形码 ③只是复制解析出 ...