Home > Article > Backend Development > Go language development of door-to-door cooking system: How to implement order status tracking function?
Go language development of door-to-door cooking system: How to implement order status tracking function?
With the improvement of living standards, more and more people are choosing to order takeout or home cooking at home. Home cooking services provide busy people with a convenient and comfortable dining option. When developing a door-to-door cooking system, an important function is the tracking of order status. This article will discuss how to use the Go language to implement the order status tracking function and provide specific code examples.
Order status tracking means that after the user places an order, the system can record and update the different statuses of the order, such as order received, in delivery, completed, etc. In order to implement this function, we need to design the database table first, and then write the corresponding code logic in the Go language.
First, we create a table named orders to store order information and status. The table structure can contain the following fields: order ID, user ID, dish name, order status, delivery person ID, etc.
CREATE TABLE orders ( id INT PRIMARY KEY AUTO_INCREMENT, user_id INT, dish_name VARCHAR(255), status VARCHAR(20), courier_id INT, created_at DATETIME DEFAULT CURRENT_TIMESTAMP );
Next, we write Go language code to handle the order status tracking function. We use Go's web framework gin to handle HTTP requests and GORM as the ORM tool for database operations.
First, we need to create an API interface that handles order status tracking. Create a file named main.go and write the following code in it:
package main import ( "github.com/gin-gonic/gin" "gorm.io/driver/mysql" "gorm.io/gorm" ) type Order struct { ID int UserID int DishName string Status string CourierID int CreatedAt time.Time } func main() { // 连接数据库 dsn := "<username>:<password>@tcp(<host>:<port>)/<database>?charset=utf8mb4&parseTime=True&loc=Local" db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{}) if err != nil { panic("数据库连接失败") } // 创建订单表 db.AutoMigrate(&Order{}) // 创建gin实例 r := gin.Default() // 创建订单接口 r.POST("/orders", func(c *gin.Context) { var order Order c.BindJSON(&order) // 将订单信息保存到数据库 result := db.Create(&order) if result.Error != nil { c.JSON(500, gin.H{"error": "保存订单失败"}) } else { c.JSON(200, order) } }) // 更新订单状态接口 r.PUT("/orders/:id/status", func(c *gin.Context) { id := c.Param("id") status := c.Query("status") // 更新订单状态 result := db.Model(&Order{}).Where("id = ?", id).Update("status", status) if result.RowsAffected == 0 { c.JSON(404, gin.H{"error": "订单不存在"}) } else { c.JSON(200, gin.H{"status": "订单状态更新成功"}) } }) // 启动服务 r.Run(":8080") }
In the above code, we first connect to the database and create the order table. Then, we created two API interfaces, one for creating orders and the other for updating the status of orders. In the create order interface, we get the order data from the request and save it to the database. In the update order status interface, we update the corresponding order status based on the order ID and status parameters.
Finally, we use gin's Run method to start the service and listen to port 8080.
Using the above code, we can start an order status tracking service. By accessing the corresponding API interface, we can create orders and update the status of orders. The following is an example of using the curl command to access the API interface:
Create an order:
curl -X POST -H "Content-Type: application/json" -d '{"user_id": 1, "dish_name": "宫保鸡丁"}' http://localhost:8080/orders
Update the order status:
curl -X PUT "http://localhost:8080/orders/1/status?status=已接单"
Through the above operations, we can track the order status Function. Users can create orders and update the status of the order through the update order status interface, thereby viewing the status changes of the order in real time.
It should be noted that the above is just a simple example and does not include complete error handling and security measures. In actual development, we need to further improve the code, handle abnormal situations and ensure the security of the system.
To sum up, this article introduces how to use Go language to develop the order status tracking function in the door-to-door cooking system. We implemented the API interface for creating orders and updating order status by designing database tables and writing corresponding Go language code. By accessing these interfaces, users can easily create orders and track status changes of orders in real time. I hope this article will be helpful to your Go language development.
The above is the detailed content of Go language development of door-to-door cooking system: How to implement order status tracking function?. For more information, please follow other related articles on the PHP Chinese website!