一卷旌收千骑虏,万全身出百重围。这篇文章主要讲述浠巊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
【浠巊olang-gin-realworld-example-app椤圭洰瀛﹀啓httpapi (涓?】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"
)// 璺敱鍑芥暟 鐢ㄤ簬鐢ㄦ埛娉ㄥ唽銆佺櫥褰曡姹傦紝娉ㄦ剰 鍙傛暟 *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()})
}
推荐阅读
- Android 技能图谱学习路线
- VMware App Volumes简介
- 微信直接跳转到手机默认浏览器下载APP的实现方式
- 微信自动跳转手机默认浏览器打开APP下载链接的实现方式
- springBoot中application.properties的常用配置
- Windows如何安装Android SDK
- appium键盘输入
- 以CSDN app 为例 软件工程 案例分析
- Android版App的控件元素定位