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


    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()})
    }
    
  • 相关阅读:
    appium之模拟坐标方法介绍
    mysql操作数据库常用命令
    appium使用无线连接手机方法
    mysql数据之增删改操作
    mysql之子查询与分组查询
    selenium之多个窗口之间切换
    selenium之内嵌网页iframe切换
    CF103E
    CF724E
    光伏元件
  • 原文地址:https://www.cnblogs.com/liujitao79/p/9987253.html
Copyright © 2020-2023  润新知