浠巊olang-gin-realworld-example-app椤圭洰瀛﹀啓httpapi (涓?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了浠巊olang-gin-realworld-example-app椤圭洰瀛﹀啓httpapi (涓?相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/get' title='get'>get   package   res   create   orb   ext   use   ati   proc   

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锛?鐢╣in鐨刧roups 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")

    // 璋冪敤妯″瀷瀹氫箟鐨凢indOneUser鍑芥暟
    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
    }
    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
    }
    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, common.NewError("database", err))
        return
    }
    UpdateContextUserModel(c, myUserModel.ID)
    serializer := UserSerializer{c}
    c.JSON(http.StatusOK, gin.H{"user": serializer.Response()})
}

以上是关于浠巊olang-gin-realworld-example-app椤圭洰瀛﹀啓httpapi (涓?的主要内容,如果未能解决你的问题,请参考以下文章