


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:
- Users can add, delete, and edit their own address information;
- Users can set the default address , use the default address when placing orders;
- 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
- 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 }
- 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 }
- 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 }
- 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 }
- 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!

如何利用Go语言开发点餐系统的会员管理功能一、引言随着移动互联网的普及,点餐系统成为了餐饮行业不可或缺的一部分。而会员管理功能作为点餐系统的重要组成部分,对于提升用户体验、增强用户黏性具有重要作用。本文将介绍如何利用Go语言开发点餐系统的会员管理功能,并提供具体的代码示例。二、会员管理功能的需求分析会员注册:用户可以通过手机号、邮箱等方式注册成为会员。会员登

如何进行Go语言开发中的单元测试和集成测试摘要:在软件开发中,单元测试和集成测试是确保代码质量和功能稳定性的重要手段。而在Go语言中,也有一套完善的工具支持,使得单元测试和集成测试变得更加简单和高效。本文将介绍如何进行Go语言开发中的单元测试和集成测试,并通过一些示例代码进行演示。引言Go语言是一种开源的编程语言,因其简洁而强大的特性而受到越来越多开发者的喜

随着互联网的发展,计算机科学领域也迎来了许多全新的编程语言。其中,Go语言以其并发性和简洁的语法,逐渐成为许多开发者的首选。作为一名从事软件开发的工程师,我有幸参与了一个基于Go语言的工作项目,并在这个过程中积累了一些宝贵的经验和教训。首先,选择适合的框架和库是至关重要的。在开始项目之前,我们进行了详细的调研,尝试了不同的框架和库,最终选择了Gin框架作为我

Go语言开发小技巧:阿里云接口对接实践分享前言:现如今,云计算已经成为了企业信息化建设的核心技术之一,而阿里云作为国内知名的云计算服务提供商,拥有丰富的云产品和服务。本文将分享笔者在使用Go语言对接阿里云接口时的一些实践经验,并以代码示例的形式进行阐述。一、引入阿里云GoSDK在使用Go语言对接阿里云接口之前,首先我们需要引入相应的阿里云GoSDK,以便

随着人们生活质量的提高,越来越多的家庭开始选择在家享用优质的餐饮服务。而上门做菜系统应运而生,成为了一种便捷、安全、健康的饮食选择方式。在这样的服务下,用户可以在网上下单,由专业厨师上门准备食材、烹饪美食,并送到用户家中享用。Go语言有着高效、稳定、安全等特点,因此配合上门做菜系统进行开发可以得到非常好的效果。本文将介绍如何在上门做菜系统中实现用户收货地址

区块链技术的出现,使得数字货币的应用成为可能,也在许多领域得到了广泛应用。随着区块链技术领域的扩大,开发人员对于更好的应用程序编写方式的需求也高涨起来。于是,一个叫做Go语言(简称Golang)的编程语言悄悄兴起,成为了区块链开发人员的最爱。Go语言是谷歌公司开发的一种系统级编程语言,自诞生以来,一直着重强调程序设计的简捷和高效。Go语言的优点包括:静态类型

如何在Go语言开发中实现高性能的分布式数据库缓存系统引言:随着互联网的高速发展,数据量和访问量不断增加,对数据库性能的要求也越来越高。分布式数据库缓存系统是一种提高数据库访问性能的解决方案,它将数据库中的数据缓存在内存中,以提供更快速的读写操作。本文将介绍如何利用Go语言开发高性能的分布式数据库缓存系统。选择合适的缓存存储引擎在开发分布式数据库缓存系统时,选

使用Go语言开发跨平台应用程序的优点和挑战随着移动互联网的迅速发展,跨平台应用程序成为了开发者们的必备技能。Go语言作为一门简洁高效、并发性能出色的语言,因其独特的特性而逐渐受到开发者的青睐。本文将探讨使用Go语言开发跨平台应用程序的优点和挑战,并提供相应的代码示例。一、优点1.语言特性齐备:Go语言提供了丰富的标准库,涵盖了各种常用功能,如文件操作、网络通


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Dreamweaver CS6
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
