Refacto vendor associations queries
Signed-off-by: Sacha Al Himdani <sacha@getprobo.com>
This commit is contained in:
committed by
Bryan Frimin
parent
e547a4e96e
commit
c7f6dba576
@@ -365,96 +365,3 @@ WHERE
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type AssetVendor struct {
|
||||
AssetID int64 `db:"asset_id"`
|
||||
VendorID int64 `db:"vendor_id"`
|
||||
CreatedAt time.Time `db:"created_at"`
|
||||
}
|
||||
|
||||
func (a *Asset) CreateWithVendors(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
organization *Organization,
|
||||
vendorIDs []gid.GID,
|
||||
now time.Time,
|
||||
) error {
|
||||
if err := organization.LoadByID(ctx, conn, scope, a.OrganizationID); err != nil {
|
||||
return fmt.Errorf("cannot load organization %q: %w", a.OrganizationID, err)
|
||||
}
|
||||
|
||||
if err := a.Insert(ctx, conn, scope); err != nil {
|
||||
return fmt.Errorf("cannot insert asset: %w", err)
|
||||
}
|
||||
|
||||
if len(vendorIDs) > 0 {
|
||||
for _, vendorID := range vendorIDs {
|
||||
_, err := conn.Exec(ctx, `
|
||||
INSERT INTO asset_vendors (tenant_id, asset_id, vendor_id, created_at)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
`, scope.GetTenantID(), a.ID, vendorID, now)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot insert asset vendor: %w", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *Asset) UpdateWithVendors(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
vendorIDs []gid.GID,
|
||||
now time.Time,
|
||||
) error {
|
||||
existing := &Asset{}
|
||||
if err := existing.LoadByID(ctx, conn, scope, a.ID); err != nil {
|
||||
return fmt.Errorf("cannot load asset: %w", err)
|
||||
}
|
||||
|
||||
a.OrganizationID = existing.OrganizationID
|
||||
a.CreatedAt = existing.CreatedAt
|
||||
a.UpdatedAt = now
|
||||
|
||||
if err := a.Update(ctx, conn, scope); err != nil {
|
||||
return fmt.Errorf("cannot update asset: %w", err)
|
||||
}
|
||||
|
||||
_, err := conn.Exec(ctx, `
|
||||
DELETE FROM asset_vendors
|
||||
WHERE tenant_id = $1 AND asset_id = $2
|
||||
`, scope.GetTenantID(), a.ID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot delete asset vendors: %w", err)
|
||||
}
|
||||
|
||||
if len(vendorIDs) > 0 {
|
||||
for _, vendorID := range vendorIDs {
|
||||
_, err := conn.Exec(ctx, `
|
||||
INSERT INTO asset_vendors (tenant_id, asset_id, vendor_id, created_at)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
`, scope.GetTenantID(), a.ID, vendorID, now)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot insert asset vendor: %w", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateWithVendorsTx updates an asset and its vendor relationships in a single transaction
|
||||
func (a *Asset) UpdateWithVendorsTx(
|
||||
ctx context.Context,
|
||||
db *pg.Client,
|
||||
scope Scoper,
|
||||
vendorIDs []gid.GID,
|
||||
now time.Time,
|
||||
) error {
|
||||
return db.WithTx(ctx, func(conn pg.Conn) error {
|
||||
return a.UpdateWithVendors(ctx, conn, scope, vendorIDs, now)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user