mock sync group editing: made tables columns more consistent; initialize database rows upon editing
This commit is contained in:
+103
-16
@@ -39,6 +39,7 @@ type (
|
||||
|
||||
Listing struct {
|
||||
AccountShopListingIDs
|
||||
ShopName string
|
||||
SKU string
|
||||
Name string
|
||||
Description string
|
||||
@@ -362,17 +363,23 @@ func (db *Store) GetMockListingsForShop(ctx context.Context, acctID int64, platf
|
||||
fmt.Sprintf(
|
||||
`
|
||||
SELECT
|
||||
listing_id,
|
||||
sku,
|
||||
name,
|
||||
description,
|
||||
"count"
|
||||
sh.name as shop_name,
|
||||
li.listing_id,
|
||||
li.sku,
|
||||
li.name,
|
||||
li.description,
|
||||
li."count"
|
||||
FROM
|
||||
%s
|
||||
%s as sh
|
||||
JOIN
|
||||
%s as li
|
||||
USING
|
||||
(account_id, shop_id)
|
||||
WHERE
|
||||
account_id = @account_id
|
||||
AND shop_id = @shop_id
|
||||
`,
|
||||
in.shopTable,
|
||||
in.listingsTable,
|
||||
),
|
||||
pgx.NamedArgs{
|
||||
@@ -385,6 +392,7 @@ func (db *Store) GetMockListingsForShop(ctx context.Context, acctID int64, platf
|
||||
}
|
||||
|
||||
vs, err := pgx.CollectRows(rows, pgx.RowToStructByNameLax[struct {
|
||||
Shop_name string
|
||||
Listing_id string
|
||||
SKU string
|
||||
Name string
|
||||
@@ -401,6 +409,7 @@ func (db *Store) GetMockListingsForShop(ctx context.Context, acctID int64, platf
|
||||
AccountShopListingIDs: NewAccountIDs(acctID).
|
||||
ShopID(in.platform, shopID).
|
||||
ListingID(v.Listing_id),
|
||||
ShopName: v.Shop_name,
|
||||
SKU: v.SKU,
|
||||
Name: v.Name,
|
||||
Description: v.Description,
|
||||
@@ -422,17 +431,23 @@ func (db *Store) GetMockListing(ctx context.Context, acctID int64, platform Plat
|
||||
fmt.Sprintf(
|
||||
`
|
||||
SELECT
|
||||
name,
|
||||
count,
|
||||
sku,
|
||||
description
|
||||
sh.name as shop_name,
|
||||
li.name,
|
||||
li.count,
|
||||
li.sku,
|
||||
li.description
|
||||
FROM
|
||||
%s
|
||||
%s AS sh
|
||||
JOIN
|
||||
%s AS li
|
||||
USING
|
||||
(account_id, shop_id)
|
||||
WHERE
|
||||
account_id = @account_id
|
||||
AND shop_id = @shop_id
|
||||
AND listing_id = @listing_id
|
||||
`,
|
||||
in.shopTable,
|
||||
in.listingsTable,
|
||||
),
|
||||
pgx.NamedArgs{
|
||||
@@ -446,6 +461,7 @@ func (db *Store) GetMockListing(ctx context.Context, acctID int64, platform Plat
|
||||
}
|
||||
|
||||
v, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByNameLax[struct {
|
||||
Shop_name string
|
||||
Name string
|
||||
Count int64
|
||||
Sku string
|
||||
@@ -469,6 +485,7 @@ func (db *Store) GetMockListing(ctx context.Context, acctID int64, platform Plat
|
||||
},
|
||||
ListingID: listingID,
|
||||
},
|
||||
ShopName: v.Shop_name,
|
||||
SKU: v.Sku,
|
||||
Name: v.Name,
|
||||
Description: v.Name,
|
||||
@@ -502,7 +519,13 @@ func (db *Store) DeleteMockSyncGroup(ctx context.Context, acctID, syncGroupID in
|
||||
}
|
||||
|
||||
func (db *Store) StartEditingMockSyncGroup(ctx context.Context, acctID, syncGroupID int64) (prevSyncGroupID int64, prevSyncGroupExists bool, err error) {
|
||||
rows, err := db.db.Query(
|
||||
tx, err := db.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return 0, false, fmt.Errorf("failed to begin transaction: %w", err)
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
|
||||
rows, err := tx.Query(
|
||||
ctx,
|
||||
`
|
||||
WITH deleted_row AS (
|
||||
@@ -551,13 +574,77 @@ func (db *Store) StartEditingMockSyncGroup(ctx context.Context, acctID, syncGrou
|
||||
}
|
||||
|
||||
v, err := pgx.CollectExactlyOneRow(rows, pgx.RowTo[pgtype.Int8])
|
||||
if err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return 0, false, nil
|
||||
}
|
||||
if err != nil && !errors.Is(err, pgx.ErrNoRows) {
|
||||
return 0, false, fmt.Errorf("failed to scan rows: %w", err)
|
||||
}
|
||||
|
||||
// insert the mock sync group listings into the draft table
|
||||
|
||||
for _, in := range getAllMockShopSchemaInfos() {
|
||||
_, err := tx.Exec(
|
||||
ctx,
|
||||
fmt.Sprintf(
|
||||
`
|
||||
WITH existing_listings AS (
|
||||
SELECT
|
||||
shop_id,
|
||||
listing_id,
|
||||
order_index
|
||||
FROM
|
||||
%s
|
||||
WHERE
|
||||
account_id = @account_id
|
||||
AND sync_group_id = @sync_group_id
|
||||
), inserted_order_indexes AS (
|
||||
INSERT INTO
|
||||
mock.sync_group_editing_listing_order_indexes (
|
||||
account_id,
|
||||
order_index
|
||||
)
|
||||
SELECT
|
||||
@account_id,
|
||||
order_index
|
||||
FROM
|
||||
existing_listings
|
||||
RETURNING
|
||||
order_index
|
||||
)
|
||||
INSERT INTO
|
||||
%s (
|
||||
account_id,
|
||||
shop_id,
|
||||
listing_id,
|
||||
order_index
|
||||
)
|
||||
SELECT
|
||||
@account_id,
|
||||
shop_id,
|
||||
listing_id,
|
||||
order_index
|
||||
FROM
|
||||
existing_listings
|
||||
JOIN
|
||||
inserted_order_indexes
|
||||
USING
|
||||
(order_index)
|
||||
`,
|
||||
in.syncGroupListingsTable,
|
||||
in.syncGroupEditingListingTable,
|
||||
),
|
||||
pgx.NamedArgs{
|
||||
"account_id": acctID,
|
||||
"sync_group_id": syncGroupID,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return 0, false, fmt.Errorf("failed to insert into editing listings table: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
return 0, false, fmt.Errorf("failed to commit transaction: %w", err)
|
||||
}
|
||||
|
||||
return v.Int64, v.Valid, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user