zoukankan      html  css  js  c++  java
  • 从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()})
    }
    
  • 相关阅读:
    鹅厂女专家:用“爱折腾”实现跨界之美
    基于腾讯云的视频聊天研究
    iOS微信内存监控
    2017年数据库技术盘点
    如何做好游戏内实时语音体验
    腾讯云微计算实践:从Serverless说起,谈谈边缘计算的未来
    使用腾讯云“自定义监控”监控GPU使用率
    如何在Python中从零开始实现随机森林
    DataGridView 设置某个列为只能为数字
    Ieditor
  • 原文地址:https://www.cnblogs.com/liujitao79/p/9987253.html
Copyright © 2011-2022 走看看