feat(order): 添加订单管理功能
- 在配置文件中新增订单相关SQL查询路径 - 创建获取单个订单及其商品详情的SQL查询 - 创建获取订单列表及其商品详情的SQL查询 - 创建保存订单及订单商品详情的SQL查询- 在数据库初始化脚本中添加订单和订单详情表结构 - 在数据库种子数据中添加初始订单数据 - 定义订单、配送信息和商品选择的模型结构- 在仓储接口中添加订单相关的操作方法- 注释掉内存仓储的注册函数以准备移除- 扩展SQL仓储结构体以支持订单相关查询和保存语句
This commit is contained in:
@@ -25,7 +25,13 @@
|
||||
"GetPage": "sql/get_product_page.sql",
|
||||
"GetPageCount": "sql/get_page_count.sql",
|
||||
"GetCategoryPage": "sql/get_category_product_page.sql",
|
||||
"GetCategoryPageCount": "sql/get_category_product_page_count.sql"
|
||||
"GetCategoryPageCount": "sql/get_category_product_page_count.sql",
|
||||
"GetOrder": "sql/get_order.sql",
|
||||
"GetOrderLines": "sql/get_order_lines.sql",
|
||||
"GetOrders": "sql/get_orders.sql",
|
||||
"GetOrdersLines": "sql/get_orders_lines.sql",
|
||||
"SaveOrder": "sql/save_order.sql",
|
||||
"SaveOrderLine": "sql/save_order_line.sql"
|
||||
}
|
||||
}
|
||||
}
|
||||
22
32-platform/sportsstore/models/order.go
Normal file
22
32-platform/sportsstore/models/order.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package models
|
||||
|
||||
type Order struct {
|
||||
Id int
|
||||
ShippingDetails
|
||||
Products []ProductSelection
|
||||
Shipped bool
|
||||
}
|
||||
|
||||
type ShippingDetails struct {
|
||||
Name string `validation:"required"`
|
||||
StreetAddr string `validation:"required"`
|
||||
City string `validation:"required"`
|
||||
State string `validation:"required"`
|
||||
Zip string `validation:"required"`
|
||||
Country string `validation:"required"`
|
||||
}
|
||||
|
||||
type ProductSelection struct {
|
||||
Product
|
||||
Quantity int
|
||||
}
|
||||
@@ -2,10 +2,10 @@ package repo
|
||||
|
||||
import (
|
||||
"math"
|
||||
"platform/services"
|
||||
"sportstore/models"
|
||||
)
|
||||
|
||||
/*
|
||||
func RegisterMemoryRepoService() {
|
||||
err := services.AddSingleton(func() models.Repository {
|
||||
repo := &MemoryRepo{}
|
||||
@@ -17,6 +17,7 @@ func RegisterMemoryRepoService() {
|
||||
return
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
type MemoryRepo struct {
|
||||
products []models.Product
|
||||
|
||||
@@ -24,5 +24,11 @@ type SqlCommands struct {
|
||||
GetPage,
|
||||
GetPageCount,
|
||||
GetCategoryPage,
|
||||
GetCategoryPageCount *LoggedStmt
|
||||
GetCategoryPageCount,
|
||||
GetOrder,
|
||||
GetOrderLines,
|
||||
GetOrders,
|
||||
GetOrdersLines,
|
||||
SaveOrder,
|
||||
SaveOrderLine *LoggedStmt
|
||||
}
|
||||
|
||||
@@ -11,5 +11,11 @@ type Repository interface {
|
||||
|
||||
GetCategories() []Category
|
||||
|
||||
GetOrder(id int) Order
|
||||
|
||||
GetOrders() []Order
|
||||
|
||||
SaveOrder(*Order)
|
||||
|
||||
Seed()
|
||||
}
|
||||
|
||||
9
32-platform/sportsstore/sql/get_order.sql
Normal file
9
32-platform/sportsstore/sql/get_order.sql
Normal file
@@ -0,0 +1,9 @@
|
||||
SELECT Orders.Id,
|
||||
Orders.Name,
|
||||
Orders.StreetAddr,
|
||||
Orders.City,
|
||||
Orders.Zip,
|
||||
Orders.Country,
|
||||
Orders.Shipped
|
||||
FROM Orders
|
||||
WHERE Orders.Id = ?
|
||||
16
32-platform/sportsstore/sql/get_order_lines.sql
Normal file
16
32-platform/sportsstore/sql/get_order_lines.sql
Normal file
@@ -0,0 +1,16 @@
|
||||
SELECT OrderLines.Quantity,
|
||||
Products.Id,
|
||||
Products.Name,
|
||||
Products.Description,
|
||||
Products.Price,
|
||||
Categories.Id AS CategoryId,
|
||||
Categories.Name AS CategoryName
|
||||
FROM Orders,
|
||||
OrderLines,
|
||||
Products,
|
||||
Categories
|
||||
WHERE Orders.Id = OrderLines.OrderId
|
||||
AND OrderLines.ProductId = Products.Id
|
||||
AND Products.Category = Categories.Id
|
||||
AND Orders.Id = ?
|
||||
GROUP BY Products.Id
|
||||
9
32-platform/sportsstore/sql/get_orders.sql
Normal file
9
32-platform/sportsstore/sql/get_orders.sql
Normal file
@@ -0,0 +1,9 @@
|
||||
SELECT Orders.Id,
|
||||
Orders.Name,
|
||||
Orders.StreetAddr,
|
||||
Orders.City,
|
||||
Orders.Zip,
|
||||
Orders.Country,
|
||||
Orders.Shipped
|
||||
FROM Orders
|
||||
ORDER BY Orders.Shipped, Orders.Id
|
||||
15
32-platform/sportsstore/sql/get_orders_lines.sql
Normal file
15
32-platform/sportsstore/sql/get_orders_lines.sql
Normal file
@@ -0,0 +1,15 @@
|
||||
SELECT OrderLines.Quantity,
|
||||
Products.Id,
|
||||
Products.Name,
|
||||
Products.Description,
|
||||
Products.Price,
|
||||
Categories.Id AS CategoryId,
|
||||
Categories.Name AS CategoryName
|
||||
FROM Orders,
|
||||
OrderLines,
|
||||
Products,
|
||||
Categories
|
||||
WHERE Orders.Id = OrderLines.OrderId
|
||||
AND OrderLines.ProductId = Products.Id
|
||||
AND Products.Category = Categories.Id
|
||||
GROUP BY Products.Id
|
||||
@@ -1,5 +1,7 @@
|
||||
DROP TABLE if exists Products;
|
||||
DROP TABLE if exists Categories;
|
||||
DROP TABLE IF exists OrderLines;
|
||||
DROP TABLE IF exists Orders;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS Categories (
|
||||
Id INTEGER NOT NULL PRIMARY KEY,
|
||||
@@ -13,4 +15,23 @@ CREATE TABLE IF NOT EXISTS Products (
|
||||
Category INTEGER NOT NULL,
|
||||
Price decimal(8,2) NOT NULL,
|
||||
CONSTRAINT CatRef FOREIGN KEY(Category) REFERENCES Categories(Id)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS OrderLines (
|
||||
Id INTEGER NOT NULL PRIMARY KEY,
|
||||
OrderId INTEGER NOT NULL,
|
||||
ProductId INTEGER NOT NULL,
|
||||
Quantity INTEGER NOT NULL,
|
||||
CONSTRAINT OrderRef FOREIGN KEY(OrderId) REFERENCES Orders(Id),
|
||||
CONSTRAINT OrderRef FOREIGN KEY(ProductId) REFERENCES Products(Id)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS Orders (
|
||||
Id INTEGER NOT NULL PRIMARY KEY,
|
||||
Name TEXT NOT NULL,
|
||||
StreetAddr Text NOT NULL,
|
||||
City Text NOT NULL,
|
||||
Zip Text NOT NULL,
|
||||
Country Text NOT NULL,
|
||||
Shipped BOOLEAN NOT NULL
|
||||
);
|
||||
7
32-platform/sportsstore/sql/save_order.sql
Normal file
7
32-platform/sportsstore/sql/save_order.sql
Normal file
@@ -0,0 +1,7 @@
|
||||
INSERT INTO Orders (Name,
|
||||
StreetAddr,
|
||||
City,
|
||||
Zip,
|
||||
Country,
|
||||
Shipped)
|
||||
VALUES (?, ?, ?, ?, ?, ?)
|
||||
2
32-platform/sportsstore/sql/save_order_line.sql
Normal file
2
32-platform/sportsstore/sql/save_order_line.sql
Normal file
@@ -0,0 +1,2 @@
|
||||
INSERT INTO OrderLines (OrderId, ProductId, Quantity)
|
||||
VALUES (?, ?, ?)
|
||||
@@ -12,4 +12,14 @@ VALUES (1, 'Kayak', 'A boat for one person', 1, 275),
|
||||
(6, 'Thinking Cap', 'Improve brain efficiency by 75%', 3, 16),
|
||||
(7, 'Unsteady Chair', 'Secretly give your opponent a disadvantage', 3, 29.95),
|
||||
(8, 'Human Chess Board', 'A fun game for the family', 3, 75),
|
||||
(9, 'Bling Bling King', 'Gold-plated, diamond-studded King', 3, 1200);
|
||||
(9, 'Bling Bling King', 'Gold-plated, diamond-studded King', 3, 1200);
|
||||
|
||||
INSERT INTO Orders (Id, Name, StreetAddr, City, Zip, Country, Shipped)
|
||||
VALUES (1, 'Alice', '123 Main St', 'New Town', '12345', 'USA', false),
|
||||
(2, 'Bob', 'The Grange', 'Upton', 'Up12 6YT', 'UK', false);
|
||||
|
||||
INSERT INTO OrderLines (Id, OrderId, ProductId, Quantity)
|
||||
VALUES (1, 1, 1, 1),
|
||||
(2, 1, 2, 2),
|
||||
(3, 2, 8, 1),
|
||||
(4, 2, 5, 2);
|
||||
Binary file not shown.
Reference in New Issue
Block a user