迁移订单列表应用服务
This commit is contained in:
1 parent
e609f7b39a
commit
10371fa74a
5 files changed
+193
-22
No files matched your search
@@ -748,3 +748,4 @@ npm --prefix frontend run build
|
||||
| 2026-08-15 | P1 订单刷新持久化后台任务切片 | 已完成 | 新增 SQLite/MySQL/Postgres `order_refresh_jobs` 迁移及应用层 `RefreshJobRepository` Port;订单刷新 HTTP 端点改为 202 + 用户隔离的状态查询,后台 worker 受 Server 生命周期管理,使用 worker token/lease fencing,并提供过期任务恢复扫描;React 客户端改为可取消的状态轮询;新增任务生命周期、跨用户隔离、旧 worker 拒绝写入、成功/失败/取消和 HTTP/前端契约回归;Go 全量测试、架构/中文注释门禁、前端 317 测试、类型检查和生产构建通过。仍待补充取消端点、进度事件、MySQL/Postgres 实际任务迁移回归与统一运维查询,阶段 4/5/8/9 继续保持“进行中” |
|
||||
| 2026-08-15 | P1 订单运行时 Port 基础设施类型收口切片 | 已完成 | `orderRuntimePort` 的完整自动化发货参数改为应用层 `orders.Order`,Cookie 会话持久化参数改为 `orders.PlatformRuntimeData`;`db.Order`/`db.CookieDetail` 转换下沉到 Server 装配适配器,业务服务不再通过运行时 Port 接触数据库模型;补充订单与平台视图边界转换回归,架构门禁、中文注释门禁和订单定向测试通过。订单应用服务实现仍位于 `internal/server`,Port 仍含平台协议接口,其他应用服务和基础设施适配器继续按后续切片迁移,阶段 4/8/9 保持“进行中” |
|
||||
| 2026-08-15 | P1 调度器与通知 worker 关闭上下文切片 | 已完成 | 自动化调度器、续期调度器和通知 outbox worker 新增 `WaitContext`;`cmd/server` 在同一个 10 秒关闭 Context 内等待 HTTP、后台 worker、调度器、账号和通知,并将 `stopCancel` 延后到全部等待结束,避免使用已取消 Context;补充三个 worker 超时/完成回归测试,相关包测试和中文注释门禁通过。Engine 内部任务等待、平台浏览器关闭和生命周期统一清单仍待继续治理,阶段 5 保持“进行中” |
|
||||
| 2026-08-15 | P1 订单列表应用服务迁移切片 | 已完成 | 新增 `internal/application/orders.ListService`、纯业务 `ListQuery/ListResult` 和窄 `ListRepository`;分页规范化、账号所有权校验和列表查询从 `internal/server/order_service.go` 迁入应用层,Server 仅负责 DTO 转换和兼容错误映射;新增应用层分页/跨用户/存储错误测试,Server 列表与订单服务回归、架构门禁和中文注释门禁通过。订单详情、更新、导入、手动发货、刷新等用例仍在 Server,阶段 4/8/9 保持“进行中” |
|
||||
@@ -0,0 +1,98 @@
|
||||
package orders
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
)
|
||||
|
||||
// ErrForbidden 表示当前用户无权访问订单筛选范围。
|
||||
var ErrForbidden = errors.New("无权访问订单")
|
||||
|
||||
// ListQuery 是订单列表用例的分页和筛选条件。
|
||||
type ListQuery struct {
|
||||
// UserID 是当前用户标识。
|
||||
UserID int64
|
||||
// CookieID 是可选的账号筛选条件。
|
||||
CookieID string
|
||||
// Status 是可选的订单状态筛选条件。
|
||||
Status string
|
||||
// Search 是订单号、商品或买家搜索词。
|
||||
Search string
|
||||
// Page 是从 1 开始的页码。
|
||||
Page int
|
||||
// PageSize 是单页记录数上限。
|
||||
PageSize int
|
||||
}
|
||||
|
||||
// ListResult 是订单列表用例的分页结果。
|
||||
type ListResult struct {
|
||||
// Rows 是当前页订单展示行。
|
||||
Rows []OrderRow
|
||||
// Total 是符合筛选条件的订单总数。
|
||||
Total int
|
||||
// Page 是规范化后的页码。
|
||||
Page int
|
||||
// PageSize 是规范化后的单页记录数。
|
||||
PageSize int
|
||||
// TotalPages 是根据总数计算出的总页数。
|
||||
TotalPages int
|
||||
}
|
||||
|
||||
// ListRepository 定义订单列表用例所需的最小数据访问能力。
|
||||
type ListRepository interface {
|
||||
// ExistsOwned 判断账号是否归属于指定用户。
|
||||
ExistsOwned(ctx context.Context, userID int64, cookieID string) (bool, error)
|
||||
// ListOrdersForUser 查询用户范围内的订单展示行。
|
||||
ListOrdersForUser(ctx context.Context, filter ListFilter) ([]OrderRow, int, error)
|
||||
}
|
||||
|
||||
// ListService 承载订单列表分页和所有权规则,不依赖 HTTP 或数据库实现。
|
||||
type ListService struct {
|
||||
// repository 保存订单列表用例所需的窄数据访问 Port。
|
||||
repository ListRepository
|
||||
}
|
||||
|
||||
// NewListService 创建订单列表应用服务。
|
||||
func NewListService(repository ListRepository) *ListService {
|
||||
return &ListService{repository: repository}
|
||||
}
|
||||
|
||||
// List 查询当前用户可见的订单,并集中处理分页和账号所有权规则。
|
||||
func (s *ListService) List(ctx context.Context, query ListQuery) (ListResult, error) {
|
||||
if s == nil || s.repository == nil {
|
||||
return ListResult{}, errors.New("订单列表 repository 未初始化")
|
||||
}
|
||||
if query.Page < 1 {
|
||||
query.Page = 1
|
||||
}
|
||||
if query.PageSize < 1 {
|
||||
query.PageSize = 20
|
||||
}
|
||||
if query.PageSize > 200 {
|
||||
query.PageSize = 200
|
||||
}
|
||||
if query.CookieID != "" {
|
||||
// owned、err 保存账号归属检查结果及错误。
|
||||
owned, err := s.repository.ExistsOwned(ctx, query.UserID, query.CookieID)
|
||||
if err != nil {
|
||||
return ListResult{}, err
|
||||
}
|
||||
if !owned {
|
||||
return ListResult{}, ErrForbidden
|
||||
}
|
||||
}
|
||||
// offset 是本次列表查询对应的数据库偏移量。
|
||||
offset := (query.Page - 1) * query.PageSize
|
||||
// rows、total、err 保存订单列表查询结果及错误。
|
||||
rows, total, err := s.repository.ListOrdersForUser(ctx, ListFilter{
|
||||
UserID: query.UserID, CookieID: query.CookieID, Status: query.Status,
|
||||
Search: query.Search, Limit: query.PageSize, Offset: offset,
|
||||
})
|
||||
if err != nil {
|
||||
return ListResult{}, err
|
||||
}
|
||||
return ListResult{
|
||||
Rows: rows, Total: total, Page: query.Page, PageSize: query.PageSize,
|
||||
TotalPages: (total + query.PageSize - 1) / query.PageSize,
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
package orders
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// listRepositoryStub 是订单列表服务测试使用的最小数据访问替身。
|
||||
type listRepositoryStub struct {
|
||||
// owned 表示账号是否属于当前用户。
|
||||
owned bool
|
||||
// ownerErr 保存账号归属查询错误。
|
||||
ownerErr error
|
||||
// rows 保存待返回的订单行。
|
||||
rows []OrderRow
|
||||
// total 保存待返回的订单总数。
|
||||
total int
|
||||
// listErr 保存订单列表查询错误。
|
||||
listErr error
|
||||
// filter 保存最近一次收到的列表条件。
|
||||
filter ListFilter
|
||||
}
|
||||
|
||||
// ExistsOwned 返回测试替身预设的账号归属结果。
|
||||
func (s *listRepositoryStub) ExistsOwned(context.Context, int64, string) (bool, error) {
|
||||
return s.owned, s.ownerErr
|
||||
}
|
||||
|
||||
// ListOrdersForUser 返回测试替身预设的订单列表结果。
|
||||
func (s *listRepositoryStub) ListOrdersForUser(_ context.Context, filter ListFilter) ([]OrderRow, int, error) {
|
||||
s.filter = filter
|
||||
return s.rows, s.total, s.listErr
|
||||
}
|
||||
|
||||
// TestListServiceNormalizesPagination 验证订单列表服务规范化分页并传递筛选条件。
|
||||
func TestListServiceNormalizesPagination(t *testing.T) {
|
||||
// repository 保存列表服务使用的数据访问替身。
|
||||
repository := &listRepositoryStub{owned: true, rows: []OrderRow{{OrderID: "order-1"}}, total: 401}
|
||||
// service 保存待测试的订单列表服务。
|
||||
service := NewListService(repository)
|
||||
// result、err 保存列表查询结果及错误。
|
||||
result, err := service.List(context.Background(), ListQuery{UserID: 7, CookieID: "cookie-1", Search: "buyer", Page: 0, PageSize: 999})
|
||||
if err != nil {
|
||||
t.Fatalf("List error=%v", err)
|
||||
}
|
||||
if result.Page != 1 || result.PageSize != 200 || result.TotalPages != 3 || len(result.Rows) != 1 {
|
||||
t.Fatalf("unexpected result=%+v", result)
|
||||
}
|
||||
if repository.filter.Offset != 0 || repository.filter.Limit != 200 || repository.filter.Search != "buyer" {
|
||||
t.Fatalf("unexpected filter=%+v", repository.filter)
|
||||
}
|
||||
}
|
||||
|
||||
// TestListServiceRejectsUnownedCookie 验证订单列表服务阻止跨用户账号筛选。
|
||||
func TestListServiceRejectsUnownedCookie(t *testing.T) {
|
||||
// service 保存不拥有目标账号的列表服务。
|
||||
service := NewListService(&listRepositoryStub{owned: false})
|
||||
// err 保存跨用户账号筛选返回的错误。
|
||||
_, err := service.List(context.Background(), ListQuery{UserID: 7, CookieID: "other"})
|
||||
if !errors.Is(err, ErrForbidden) {
|
||||
t.Fatalf("error=%v, want ErrForbidden", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestListServicePropagatesRepositoryError 验证订单列表服务保留数据访问错误。
|
||||
func TestListServicePropagatesRepositoryError(t *testing.T) {
|
||||
// expected 保存测试预设的数据访问错误。
|
||||
expected := errors.New("数据库不可用")
|
||||
// service 保存返回数据访问错误的列表服务。
|
||||
service := NewListService(&listRepositoryStub{owned: true, listErr: expected})
|
||||
// err 保存列表查询返回的错误。
|
||||
_, err := service.List(context.Background(), ListQuery{UserID: 7})
|
||||
if !errors.Is(err, expected) {
|
||||
t.Fatalf("error=%v, want repository error", err)
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
package server
|
||||
|
||||
import orderapp "xianyu-go/internal/application/orders"
|
||||
|
||||
// applicationServices 聚合 Server 使用的应用服务实例,统一管理共享基础设施依赖。
|
||||
type applicationServices struct {
|
||||
// orders 是订单应用服务。
|
||||
@@ -16,8 +18,10 @@ type applicationServices struct {
|
||||
|
||||
// newApplicationServices 为指定 Server 装配全部应用服务实例。
|
||||
func newApplicationServices(server *Server) *applicationServices {
|
||||
// orderRepository 保存订单应用服务共享的基础设施适配器。
|
||||
orderRepository := newStoreOrderRepository(server.Store)
|
||||
return &applicationServices{
|
||||
orders: &orderApplicationService{server: newServerOrderRuntime(server), repository: newStoreOrderRepository(server.Store), refreshJobs: newStoreOrderRefreshJobRepository(server.Store)},
|
||||
orders: &orderApplicationService{server: newServerOrderRuntime(server), repository: orderRepository, list: orderapp.NewListService(orderRepository), refreshJobs: newStoreOrderRefreshJobRepository(server.Store)},
|
||||
itemPublish: &itemPublishService{server: server, repository: newStoreItemPublishRepository(server.Store)},
|
||||
accountLogin: &accountLoginService{server: server, repository: newStoreAccountLoginRepository(server.Store)},
|
||||
communication: &communicationService{server: server, repository: newStoreCommunicationRepository(server.Store)},
|
||||
|
||||
@@ -146,6 +146,8 @@ type orderApplicationService struct {
|
||||
server orderRuntimePort
|
||||
// repository 提供订单用例所需的应用层持久化与凭证锁 Port。
|
||||
repository orderapp.Repository
|
||||
// list 负责订单列表分页和账号所有权规则。
|
||||
list *orderapp.ListService
|
||||
// refreshJobs 提供订单刷新后台任务的持久化 Port。
|
||||
refreshJobs orderapp.RefreshJobRepository
|
||||
}
|
||||
@@ -322,37 +324,26 @@ type orderDetailResult struct {
|
||||
|
||||
// List 查询当前用户可见的订单,并集中处理分页和账号所有权规则。
|
||||
func (a *orderApplicationService) List(ctx context.Context, query orderListQuery) (orderListResult, error) {
|
||||
if query.Page < 1 {
|
||||
query.Page = 1
|
||||
}
|
||||
if query.PageSize < 1 {
|
||||
query.PageSize = 20
|
||||
}
|
||||
if query.PageSize > 200 {
|
||||
query.PageSize = 200
|
||||
}
|
||||
if query.CookieID != "" && !a.orderOwnedByUser(ctx, query.UserID, query.CookieID) {
|
||||
// result、err 保存应用层订单列表结果及错误。
|
||||
result, err := a.list.List(ctx, orderapp.ListQuery{
|
||||
UserID: query.UserID, CookieID: query.CookieID, Status: query.Status,
|
||||
Search: query.Search, Page: query.Page, PageSize: query.PageSize,
|
||||
})
|
||||
if errors.Is(err, orderapp.ErrForbidden) {
|
||||
return orderListResult{}, db.ErrForbidden
|
||||
}
|
||||
// offset 保存偏移量,供当前处理流程使用
|
||||
offset := (query.Page - 1) * query.PageSize
|
||||
// rows、total、err 保存rows、total、err,供当前处理流程使用
|
||||
rows, total, err := a.repository.ListOrdersForUser(ctx, orderapp.ListFilter{
|
||||
UserID: query.UserID, CookieID: query.CookieID, Status: query.Status,
|
||||
Search: query.Search, Limit: query.PageSize, Offset: offset,
|
||||
})
|
||||
if err != nil {
|
||||
return orderListResult{}, err
|
||||
}
|
||||
// orders 保存订单列表,供当前处理流程使用
|
||||
orders := make([]orderDTO, 0, len(rows))
|
||||
orders := make([]orderDTO, 0, len(result.Rows))
|
||||
// row 表示当前遍历过程中的row
|
||||
for _, row := range rows {
|
||||
for _, row := range result.Rows {
|
||||
orders = append(orders, orderDTOFromRow(row))
|
||||
}
|
||||
return orderListResult{
|
||||
Orders: orders, Total: total, Page: query.Page, PageSize: query.PageSize,
|
||||
TotalPages: (total + query.PageSize - 1) / query.PageSize,
|
||||
Orders: orders, Total: result.Total, Page: result.Page, PageSize: result.PageSize,
|
||||
TotalPages: result.TotalPages,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user