This is the 26th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

Writing in the front

Since user login involves authentication of identity, we can authenticate users through JWT to confirm their identity.

1. Register routes

  • Write a request route for user updates
authed.PUT("user", api.UserUpdate) 
Copy the code

2. Function writing

2.1 the service layer

  • Define a structure to be modified by the user

Only the user name or nickname can be changed. Change the password later.

// The user modifies the information service
type UserUpdateService struct {
	NickName string `form:"nickname" json:"nickname" binding:"required,min=5,max=10"`
	UserName string `form:"user_name" json:"user_name" binding:"required,min=5,max=15"`
}
Copy the code
  • Define user information modification functions
func (service UserUpdateService) Update(id uint) serializer.Response{... }Copy the code

2.2 the API layer

  • Define a user update service
var userUpdateService service.UserUpdateService
Copy the code
  • Read the request headerAuthorizationThe value of the
claims,_ := util.ParseToken(c.GetHeader("Authorization"))
Copy the code
  • Bind the service
c.ShouldBind(&userUpdateService)
Copy the code
  • That calls the serviceupdatemethods
res := userUpdateService.Update(claims.ID)
Copy the code
  • Context return
c.JSON(200, res)
Copy the code
  • API layer complete code
func UserUpdate(c *gin.Context) {
	var userUpdateService service.UserUpdateService
	claims,_ := util.ParseToken(c.GetHeader("Authorization"))
	if err := c.ShouldBind(&userUpdateService); err == nil {
		res := userUpdateService.Update(claims.ID)
		c.JSON(200, res)
	} else {
		c.JSON(200, ErrorResponse(err))
		logging.Info(err)
	}
}
Copy the code

3. Service writing

  • According to theAuthorizationThe parsed ID finds the user
	var user model.User
	code := e.SUCCESS
	// Find the user
	err := model.DB.First(&user, id).Error
	iferr ! =nil {
		logging.Info(err)
		code = e.ErrorDatabase
		return serializer.Response{
			Status: code,
			Msg:    e.GetMsg(code),
			Error:  err.Error(),
		}
	}
Copy the code
  • Modify the user’s original information based on the incoming information
	user.Nickname = service.NickName
	user.UserName = service.UserName
Copy the code
  • Save to the database
err = model.DB.Save(&user).Error
Copy the code
  • Returns information about the user
	return serializer.Response{
		Status: code,
		Data:   serializer.BuildUser(user),
		Msg:    e.GetMsg(code),
	}
Copy the code
  • Complete service layer code
func (service UserUpdateService) Update(id uint) serializer.Response {
	var user model.User
	code := e.SUCCESS
	// Find the user
	err := model.DB.First(&user, id).Error
	iferr ! =nil {
		logging.Info(err)
		code = e.ErrorDatabase
		return serializer.Response{
			Status: code,
			Msg:    e.GetMsg(code),
			Error:  err.Error(),
		}
	}
	user.Nickname = service.NickName
	user.UserName = service.UserName
	err = model.DB.Save(&user).Error
	iferr ! =nil {
		logging.Info(err)
		code = e.ErrorDatabase
		return serializer.Response{
			Status: code,
			Msg:    e.GetMsg(code),
			Error:  err.Error(),
		}
	}
	return serializer.Response{
		Status: code,
		Data:   serializer.BuildUser(user),
		Msg:    e.GetMsg(code),
	}
}
Copy the code

4. Verify