|
|
vor 1 Monat | |
|---|---|---|
| handler | vor 1 Monat | |
| model | vor 1 Monat | |
| repository | vor 1 Monat | |
| service | vor 1 Monat | |
| README.md | vor 1 Monat | |
| go.mod | vor 1 Monat | |
| go.sum | vor 1 Monat | |
| main.go | vor 1 Monat | |
| test.sh | vor 1 Monat |
简化的订单 HTTP 服务,支持创建订单、服务者接单、取消订单三个操作。
# 启动服务(内存存储 — 默认)
cd project/cheng-xing && go run main.go
# 启动服务(SQLite 存储)
cd project/cheng-xing && STORAGE=sqlite go run main.go
服务启动在 http://localhost:8080
curl -X POST http://localhost:8080/orders \
-H "Content-Type: application/json" \
-d '{"service_time":"2025-08-01T14:00:00Z","duration":120,"address":"北京朝阳区xxx路xxx号"}'
curl -X PUT http://localhost:8080/orders/{id}/accept \
-H "Content-Type: application/json" \
-d '{"provider_id":"prov_001"}'
curl -X PUT http://localhost:8080/orders/{id}/cancel \
-H "Content-Type: application/json" \
-d '{}'
cd project/cheng-xing && go test ./... -v -race
pending 订单可接单pending 或 accepted 订单可取消canceled 订单不可再操作├── main.go # 入口,依赖注入,存储选择
├── model/order.go # 数据模型、状态常量、错误类型
├── repository/
│ ├── order.go # OrderRepository 接口
│ ├── memory.go # 内存实现(sync.RWMutex + CAS)
│ └── sqlite.go # SQLite 实现(行锁 + WHERE CAS)
├── service/order.go # 业务逻辑(状态机)
├── handler/order.go # HTTP handler(Gin)
└── */*_test.go # 单元测试 + 集成测试