加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

从golang-gin-realworld-example-app项目学写httpapi (三)

发布时间:2020-12-16 09:32:19 所属栏目:大数据 来源:网络整理
导读:https://github.com/gothinkster/golang-gin-realworld-example-app/tree/master/users 路由定义 package usersimport ( "errors" "github.com/wangzitian0/golang-gin-starter-kit/common" "gopkg.in/gin-gonic/gin.v1" "net/http")// 路由函数 用于用户注

https://github.com/gothinkster/golang-gin-realworld-example-app/tree/master/users

路由定义

package users

import (
    "errors"
    "github.com/wangzitian0/golang-gin-starter-kit/common"
    "gopkg.in/gin-gonic/gin.v1"
    "net/http"
)

// 路由函数 用于用户注册、登录请求,注意 参数 *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,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,gin.H{"profile": serializer.Response()})
}

func ProfileUnfollow(c *gin.Context) {
    username := c.Param("username")
    userModel,errors.New("Invalid username")))
        return
    }
    myUserModel := c.MustGet("my_user_model").(UserModel)

    err = myUserModel.unFollowing(userModel)
    if err != nil {
        c.JSON(http.StatusUnprocessableEntity,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,err))
        return
    }
    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,errors.New("Not Registered email or invalid password")))
        return
    }
    UpdateContextUserModel(c,userModel.ID)
    serializer := UserSerializer{c}
    c.JSON(http.StatusOK,gin.H{"user": serializer.Response()})
}

func UserRetrieve(c *gin.Context) {
    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,err))
        return
    }
    UpdateContextUserModel(c,myUserModel.ID)
    serializer := UserSerializer{c}
    c.JSON(http.StatusOK,gin.H{"user": serializer.Response()})
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读