Home  >  Article  >  Backend Development  >  Go language development of door-to-door cooking system: How to implement user delivery address management function?

Go language development of door-to-door cooking system: How to implement user delivery address management function?

WBOY
WBOYOriginal
2023-11-01 14:07:43833browse

Go language development of door-to-door cooking system: How to implement user delivery address management function?

With the improvement of people's quality of life, more and more families are beginning to choose to enjoy high-quality catering services at home. The door-to-door cooking system emerged as the times require and has become a convenient, safe and healthy way to choose food. Under such a service, users can place an order online, and professional chefs will come to prepare the ingredients, cook the food, and deliver it to the user's home for enjoyment. The Go language has the characteristics of high efficiency, stability, and security, so it can achieve very good results when developed with a door-to-door cooking system. This article will introduce how to implement the user delivery address management function in the door-to-door cooking system.

1. Requirements for user delivery address management function

In the door-to-door cooking system, users need to fill in their own delivery address to ensure that food can be delivered to their homes in time. In order to allow users to better manage their own delivery addresses, the following functions need to be implemented:

  1. Users can add, delete, and edit their own address information;
  2. Users can set the default address , use the default address when placing orders;
  3. Users can view all their address information.

2. Design database

In order to realize the above functions, we need to design a database of user shipping addresses. Assuming we use a MySQL database, we can design the following table structure:

CREATE TABLE `address` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'id',
  `user_id` int(11) NOT NULL COMMENT '用户id',
  `name` varchar(50) NOT NULL COMMENT '收货人姓名',
  `mobile` varchar(20) NOT NULL COMMENT '收货人手机号',
  `province` varchar(50) NOT NULL COMMENT '省份',
  `city` varchar(50) NOT NULL COMMENT '城市',
  `district` varchar(50) NOT NULL COMMENT '区/县',
  `address` varchar(200) NOT NULL COMMENT '详细地址',
  `is_default` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否为默认地址,0表示不是,1表示是',
  `create_time` datetime NOT NULL COMMENT '创建时间',
  `update_time` datetime NOT NULL COMMENT '修改时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户地址表'

3. Implement the delivery address management function

  1. User adds address

Add The address function needs to obtain the consignee's name, mobile phone number, province, city, district/county, detailed address and other information from the front end, and perform data verification in the background to avoid errors or malicious data.

// 添加用户地址
func AddAddress(userId int, addForm *forms.AddressAddForm) error {
    // 数据校验
    err := validateAddressForm(addForm)
    if err != nil {
        return err
    }

    // 插入数据库
    address := models.Address{
        UserId:     userId,
        Name:       addForm.Name,
        Mobile:     addForm.Mobile,
        Province:   addForm.Province,
        City:       addForm.City,
        District:   addForm.District,
        Address:    addForm.Address,
        IsDefault:  addForm.IsDefault,
        CreateTime: time.Now(),
        UpdateTime: time.Now(),
    }
    err = address.Insert()
    if err != nil {
        return err
    }

    return nil
}

// 数据校验
func validateAddressForm(addForm *forms.AddressAddForm) error {
    // 校验收货人姓名
    if addForm.Name == "" {
        return errors.New("收货人姓名不能为空")
    }
    if utf8.RuneCountInString(addForm.Name) > 50 {
        return errors.New("收货人姓名长度不能超过50个字符")
    }
    // 校验手机号码
    if addForm.Mobile == "" {
        return errors.New("手机号码不能为空")
    }
    if !regexp.MustCompile(`^[1][3,4,5,7,8][0-9]{9}$`).MatchString(addForm.Mobile) {
        return errors.New("手机号码格式不正确")
    }
    // 校验省份、城市、区/县、详细地址
    if addForm.Province == "" {
        return errors.New("省份不能为空")
    }
    if utf8.RuneCountInString(addForm.Province) > 50 {
        return errors.New("省份长度不能超过50个字符")
    }
    if addForm.City == "" {
        return errors.New("城市不能为空")
    }
    if utf8.RuneCountInString(addForm.City) > 50 {
        return errors.New("城市长度不能超过50个字符")
    }
    if addForm.District == "" {
        return errors.New("区/县不能为空")
    }
    if utf8.RuneCountInString(addForm.District) > 50 {
        return errors.New("区/县长度不能超过50个字符")
    }
    if addForm.Address == "" {
        return errors.New("详细地址不能为空")
    }
    if utf8.RuneCountInString(addForm.Address) > 200 {
        return errors.New("详细地址长度不能超过200个字符")
    }

    return nil
}
  1. User edit address

The edit address function requires first querying the address to be edited, and then verifying the data before saving.

// 编辑用户地址
func EditAddress(userId, addressId int, editForm *forms.AddressEditForm) error {
    // 根据地址id查询地址信息
    address, err := models.GetAddressById(userId, addressId)
    if err != nil {
        return err
    }
    if address.Id == 0 {
        return errors.New("地址不存在")
    }

    // 数据校验
    err = validateAddressForm(&forms.AddressAddForm{
        Name:     editForm.Name,
        Mobile:   editForm.Mobile,
        Province: editForm.Province,
        City:     editForm.City,
        District: editForm.District,
        Address:  editForm.Address,
    })
    if err != nil {
        return err
    }

    // 修改数据库
    address.Name = editForm.Name
    address.Mobile = editForm.Mobile
    address.Province = editForm.Province
    address.City = editForm.City
    address.District = editForm.District
    address.Address = editForm.Address
    address.IsDefault = editForm.IsDefault
    address.UpdateTime = time.Now()
    err = address.Update()
    if err != nil {
        return err
    }

    return nil
}
  1. User deletion address

User deletion address needs to specify the address id, and if the default address is deleted, another address needs to be set as the default address. Data needs to be verified before deletion to avoid malicious operations.

// 删除用户地址
func DeleteAddress(userId, addressId int) error {
    // 根据地址id查询地址信息
    address, err := models.GetAddressById(userId, addressId)
    if err != nil {
        return err
    }
    if address.Id == 0 {
        return errors.New("地址不存在")
    }

    // 判断是否默认地址
    defaultAddress, err := models.GetDefaultAddressByUserId(userId)
    if err != nil {
        return err
    }
    if address.IsDefault && defaultAddress.Id == address.Id {
        // 删除的是默认地址,需要将另一个地址设置为默认地址
        otherAddress, err := models.GetOtherAddressByUserIdAndAddressId(userId, address.Id)
        if err != nil {
            return err
        }
        if otherAddress.Id > 0 {
            otherAddress.IsDefault = true
            err = otherAddress.Update()
            if err != nil {
                return err
            }
        }
    }

    // 删除数据库
    err = address.Delete()
    if err != nil {
        return err
    }

    return nil
}
  1. User sets the default address

To set the default address, users need to first check whether the address to be set exists, and then set the original default address to a non-default address. Then set the address that needs to be set as the default address as the default address.

// 设置用户默认地址
func SetDefaultAddress(userId, addressId int) error {
    // 查询需要设置为默认地址的地址是否存在,如果不存在则返回错误
    address, err := models.GetAddressById(userId, addressId)
    if err != nil {
        return err
    }
    if address.Id == 0 {
        return errors.New("地址不存在")
    }

    // 将原来的默认地址设置为非默认地址
    defaultAddress, err := models.GetDefaultAddressByUserId(userId)
    if err != nil {
        return err
    }
    if defaultAddress.Id > 0 {
        defaultAddress.IsDefault = false
        err = defaultAddress.Update()
        if err != nil {
            return err
        }
    }

    // 将需要设置为默认地址的地址设置为默认地址
    address.IsDefault = true
    err = address.Update()
    if err != nil {
        return err
    }

    return nil
}
  1. User query address list

User query address list needs to sort the results, with the default address in front, and other addresses in reverse order according to the creation time.

// 查询用户地址列表
func GetAddressList(userId int) ([]models.Address, error) {
    addressList, err := models.GetAddressListByUserId(userId)
    if err != nil {
        return nil, err
    }

    // 对结果进行排序,将默认地址排在前面,其他地址按照创建时间逆序排列
    sort.SliceStable(addressList, func(i, j int) bool {
        if addressList[i].IsDefault {
            return true
        }
        if addressList[j].IsDefault {
            return false
        }
        return addressList[i].CreateTime.After(addressList[j].CreateTime)
    })

    return addressList, nil
}

4. Interface design

The interface design should realize the five functions of adding, editing, deleting, setting default address and querying address list.

// 添加用户地址接口
func AddAddress(c *gin.Context) {
    var addForm forms.AddressAddForm
    err := c.ShouldBindJSON(&addForm)
    if err != nil {
        ResponseError(c, CodeInvalidParams)
        return
    }

    userId, _ := getCurrentUserId(c)
    err = services.AddAddress(userId, &addForm)
    if err != nil {
        ResponseError(c, CodeServerBusy)
        return
    }

    ResponseSuccess(c, nil)
}

// 编辑用户地址接口
func EditAddress(c *gin.Context) {
    addressId, ok := getQueryInt(c, "address_id")
    if !ok {
        ResponseError(c, CodeInvalidParams)
        return
    }

    var editForm forms.AddressEditForm
    err := c.ShouldBindJSON(&editForm)
    if err != nil {
        ResponseError(c, CodeInvalidParams)
        return
    }

    userId, _ := getCurrentUserId(c)
    err = services.EditAddress(userId, addressId, &editForm)
    if err != nil {
        ResponseError(c, CodeServerBusy)
        return
    }

    ResponseSuccess(c, nil)
}

// 删除用户地址接口
func DeleteAddress(c *gin.Context) {
    addressId, ok := getQueryInt(c, "address_id")
    if !ok {
        ResponseError(c, CodeInvalidParams)
        return
    }

    userId, _ := getCurrentUserId(c)
    err := services.DeleteAddress(userId, addressId)
    if err != nil {
        ResponseError(c, CodeServerBusy)
        return
    }

    ResponseSuccess(c, nil)
}

// 设置用户默认地址接口
func SetDefaultAddress(c *gin.Context) {
    addressId, ok := getQueryInt(c, "address_id")
    if !ok {
        ResponseError(c, CodeInvalidParams)
        return
    }

    userId, _ := getCurrentUserId(c)
    err := services.SetDefaultAddress(userId, addressId)
    if err != nil {
        ResponseError(c, CodeServerBusy)
        return
    }

    ResponseSuccess(c, nil)
}

// 获取用户地址列表接口
func GetAddressList(c *gin.Context) {
    userId, _ := getCurrentUserId(c)
    addressList, err := services.GetAddressList(userId)
    if err != nil {
        ResponseError(c, CodeServerBusy)
        return
    }

    ResponseSuccess(c, gin.H{
        "list": addressList,
    })
}

5. Summary

The user delivery address management function is one of the important functions of the door-to-door cooking system. Implementing this function requires three aspects: designing the database, implementing specific functions and interface design, and also requires data verification. Through the introduction of this article, I believe readers can basically understand how to implement the user delivery address management function in the door-to-door cooking system.

The above is the detailed content of Go language development of door-to-door cooking system: How to implement user delivery address management function?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn