商品已完成优化

This commit is contained in:
yml2213
2026-06-27 23:01:03 +08:00
parent fed7d6af71
commit 21aabd77f1
12 changed files with 148 additions and 10 deletions
+10 -3
View File
@@ -208,7 +208,7 @@ func (r *Repository) Update(ctx context.Context, ownerID uint64, listingID uint6
if err != nil {
return err
}
if listing.Status == "rented" || listing.InTransaction {
if listingLockedForOwnerMutation(listing) {
return ErrListingLocked
}
@@ -256,7 +256,7 @@ func (r *Repository) SubmitReview(ctx context.Context, ownerID uint64, listingID
if err != nil {
return err
}
if listing.Status == "rented" || listing.InTransaction {
if listingLockedForOwnerMutation(listing) {
return ErrListingLocked
}
listingStatus, reviewStatus, publishedAt := initialPublishState(reviewRequired)
@@ -346,7 +346,7 @@ func (r *Repository) Offline(ctx context.Context, ownerID uint64, listingID uint
if err != nil {
return err
}
if listing.Status == "rented" || listing.InTransaction {
if listingLockedForOwnerMutation(listing) {
return ErrListingLocked
}
listing.Status = "offline"
@@ -365,3 +365,10 @@ func (r *Repository) Offline(ctx context.Context, ownerID uint64, listingID uint
})
return dto, err
}
func listingLockedForOwnerMutation(listing *model.RentalListing) bool {
if listing == nil {
return true
}
return listing.Status == "rented" || listing.Status == "completed" || listing.InTransaction
}
@@ -213,6 +213,47 @@ func TestRepositoryUpdateAllowsOfflineListingResubmit(t *testing.T) {
}
}
func TestRepositoryRejectsCompletedListingOwnerMutations(t *testing.T) {
db := database.NewTestDB()
if err := db.AutoMigrate(&model.GameAccount{}, &model.RentalListing{}); err != nil {
t.Fatalf("failed to migrate test db: %v", err)
}
repo := NewRepository(db, nil)
account := model.GameAccount{
OwnerID: 1,
GameName: "delta_force",
ServerRegion: "QQ",
LoginPlatform: "QQ账号密码",
Title: "已完成账号",
Status: "offline",
}
if err := db.Create(&account).Error; err != nil {
t.Fatalf("failed to create account: %v", err)
}
listing := model.RentalListing{
ListingNo: "202606270099",
AccountID: account.ID,
OwnerID: 1,
Status: "completed",
ReviewStatus: "approved",
PriceCent: 10000,
DepositAmountCent: 50000,
}
if err := db.Create(&listing).Error; err != nil {
t.Fatalf("failed to create listing: %v", err)
}
if _, err := repo.Update(t.Context(), 1, listing.ID, validCreateRequest(), false); err != ErrListingLocked {
t.Fatalf("Update expected ErrListingLocked, got %v", err)
}
if _, err := repo.SubmitReview(t.Context(), 1, listing.ID, false); err != ErrListingLocked {
t.Fatalf("SubmitReview expected ErrListingLocked, got %v", err)
}
if _, err := repo.Offline(t.Context(), 1, listing.ID); err != ErrListingLocked {
t.Fatalf("Offline expected ErrListingLocked, got %v", err)
}
}
func validCreateRequest() CreateRequest {
return CreateRequest{
Title: "测试账号",