matlab awag,从golang-gin-realworld-example-app项目学写httpapi (四)

####路由定义

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()})

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值