New users

  1. To obtain parameters
	err := c.Bind(&r)
	iferr! =nil{
		handler.SendResponse(c,errno.ErrBind,nil)
		return
	}
        u:=model.UserModel{Username: r.Username,Password: r.Password,}
Copy the code
  1. Calibration parameters
	// Validate the data.
	iferr := u.Validate(); err ! =nil {
		handler.SendResponse(c, errno.ErrValidation, nil)
		return
	}
Copy the code
  1. Encrypted password
	// Encrypt the user password.
	iferr := u.Encrypt(); err ! =nil {
		handler.SendResponse(c, errno.ErrEncrypt, nil)
		return
	}
Copy the code
  1. Insert data
	// Insert the user to the database.
	iferr := u.Create(); err ! =nil {
		handler.SendResponse(c, errno.ErrDatabase, nil)
		return
	}

Copy the code
  1. Returns a response
		rsp := CreateResponse{
		Username: r.Username,
	}

	handler.SendResponse(c,nil,rsp)
Copy the code

Query the user

  1. Get parameters and query the database
	usename := c.Param("username")
	user, err := model.GetUser(usename)
Copy the code
  1. Returns a response
	iferr! =nil{
		handler.SendResponse(c,errno.ErrUserNotFound,nil)
		return
	}
	handler.SendResponse(c,nil,user)
Copy the code

Update user

The logic is basically the same as that of a new user, except that in addition to the structure that receives the user’s information, it also receives the user’s ID, combines these and queries

Delete user

  1. Read the parameter
useId, _ := strconv.Atoi(c.Param("id"))

Copy the code
  1. Deletes a record from the database and returns the result of the deletion
	if err := model.Delete(uint64(useId)); err ! =nil {
		handler.SendResponse(c,errno.ErrDatabase,nil)
		return
	}
        	handler.SendResponse(c,nil.nil)
Copy the code

Querying a User List

Querying a REST resource list usually requires paging. If too many lists are returned without paging, API response is slow and front-end experience is poor. Therefore, the query function is paginated, and the offset and limit parameters passed in the received request correspond to the MySQL offset and limit parameters, respectively

  • The sync package is used in the ListUser() function to do parallel queries
  • IdMap is used because the list of the query usually needs to be sorted in chronological order. Generally, the list after the database query has been sorted, but in order to reduce the delay, the program uses concurrency, which will disrupt the sorting. Therefore, IdMap is used to record the order before the concurrent processing, and then reset after processing.
	for _, userModel := range user {

		waitGroup.Add(1)
		go func(userModel *model.UserModel) {

			defer waitGroup.Done()
			id, err2 := util.GenShortId()
			iferr2! =nil{
				errors<-err2
				return
			}
			list.Lock.Lock()
			defer list.Lock.Unlock()
			list.IdMap[userModel.Id] = &model.UserInfo{
				Id:        userModel.Id,
				Username:  userModel.Username,
				SayHello:  fmt.Sprintf("Hello %s", id),
				Password:  userModel.Password,
				CreatedAt:userModel.CreatedAt.Format("The 2006-01-02 15:04:05"),
				UpdatedAt: userModel.UpdatedAt.Format("The 2006-01-02 15:04:05"),
			}

		}(userModel)
	}
Copy the code
  1. User list structure
	list := model.UserList{Lock: new(sync.Mutex),
		IdMap: make(map[uint64]*model.UserInfo, len(user))}
Copy the code
  1. Error message channel and completion message channel
	errors:= make(chan error, 1)
	finish := make(chan bool.1)
Copy the code

test

  • Add user

  • The results of

  • Query the user

  • Modify the user

  • Viewing a User List

  • Delete user