Ingen beskrivning

b 07fc55c700 docs: update README with startup instructions 1 månad sedan
handler 31c4b86c50 feat: add Gin handlers with integration tests 1 månad sedan
model f79fb48740 fix: add GetOrder, fix ErrConflict code to match spec 1 månad sedan
repository 42d599cb1c feat: add SQLite repository with CAS update, wire storage selection in main.go 1 månad sedan
service f79fb48740 fix: add GetOrder, fix ErrConflict code to match spec 1 månad sedan
README.md 07fc55c700 docs: update README with startup instructions 1 månad sedan
go.mod 42d599cb1c feat: add SQLite repository with CAS update, wire storage selection in main.go 1 månad sedan
go.sum 42d599cb1c feat: add SQLite repository with CAS update, wire storage selection in main.go 1 månad sedan
main.go 42d599cb1c feat: add SQLite repository with CAS update, wire storage selection in main.go 1 månad sedan

README.md

订单服务

简化的订单 HTTP 服务,支持创建订单、服务者接单、取消订单三个操作。

快速启动

# 启动服务(内存存储 — 默认)
cd project/cheng-xing && go run main.go

# 启动服务(SQLite 存储)
cd project/cheng-xing && STORAGE=sqlite go run main.go

服务启动在 http://localhost:8080

API

创建订单

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 订单可接单
  • 同一订单只能被一名服务者接单(并发安全,CAS 乐观锁保证)
  • pendingaccepted 订单可取消
  • 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          # 单元测试 + 集成测试

技术栈

  • 语言: Go
  • 框架: Gin
  • 存储: 内存 / SQLite(mattn/go-sqlite3)
  • 测试: testing + httptest