@@ -44,23 +44,9 @@ type (
|
|||||||
PrivacyPolicyURL *string `db:"privacy_policy_url"`
|
PrivacyPolicyURL *string `db:"privacy_policy_url"`
|
||||||
CreatedAt time.Time `db:"created_at"`
|
CreatedAt time.Time `db:"created_at"`
|
||||||
UpdatedAt time.Time `db:"updated_at"`
|
UpdatedAt time.Time `db:"updated_at"`
|
||||||
Version int `db:"version"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Vendors []*Vendor
|
Vendors []*Vendor
|
||||||
|
|
||||||
UpdateVendorParams struct {
|
|
||||||
ExpectedVersion int
|
|
||||||
Name *string
|
|
||||||
Description *string
|
|
||||||
ServiceStartAt *time.Time
|
|
||||||
ServiceTerminationAt *time.Time
|
|
||||||
ServiceCriticality *ServiceCriticality
|
|
||||||
RiskTier *RiskTier
|
|
||||||
StatusPageURL *string
|
|
||||||
TermsOfServiceURL *string
|
|
||||||
PrivacyPolicyURL *string
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func (v Vendor) CursorKey(orderBy VendorOrderField) page.CursorKey {
|
func (v Vendor) CursorKey(orderBy VendorOrderField) page.CursorKey {
|
||||||
@@ -94,8 +80,7 @@ SELECT
|
|||||||
terms_of_service_url,
|
terms_of_service_url,
|
||||||
privacy_policy_url,
|
privacy_policy_url,
|
||||||
created_at,
|
created_at,
|
||||||
updated_at,
|
updated_at
|
||||||
version
|
|
||||||
FROM
|
FROM
|
||||||
vendors
|
vendors
|
||||||
WHERE
|
WHERE
|
||||||
@@ -146,8 +131,7 @@ INSERT INTO
|
|||||||
terms_of_service_url,
|
terms_of_service_url,
|
||||||
privacy_policy_url,
|
privacy_policy_url,
|
||||||
created_at,
|
created_at,
|
||||||
updated_at,
|
updated_at
|
||||||
version
|
|
||||||
)
|
)
|
||||||
VALUES (
|
VALUES (
|
||||||
@tenant_id,
|
@tenant_id,
|
||||||
@@ -163,8 +147,7 @@ VALUES (
|
|||||||
@terms_of_service_url,
|
@terms_of_service_url,
|
||||||
@privacy_policy_url,
|
@privacy_policy_url,
|
||||||
@created_at,
|
@created_at,
|
||||||
@updated_at,
|
@updated_at
|
||||||
1
|
|
||||||
)
|
)
|
||||||
`
|
`
|
||||||
|
|
||||||
@@ -227,8 +210,7 @@ SELECT
|
|||||||
terms_of_service_url,
|
terms_of_service_url,
|
||||||
privacy_policy_url,
|
privacy_policy_url,
|
||||||
created_at,
|
created_at,
|
||||||
updated_at,
|
updated_at
|
||||||
version
|
|
||||||
FROM
|
FROM
|
||||||
vendors
|
vendors
|
||||||
WHERE
|
WHERE
|
||||||
@@ -261,70 +243,41 @@ func (v *Vendor) Update(
|
|||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
conn pg.Conn,
|
conn pg.Conn,
|
||||||
scope Scoper,
|
scope Scoper,
|
||||||
params UpdateVendorParams,
|
|
||||||
) error {
|
) error {
|
||||||
q := `
|
q := `
|
||||||
UPDATE vendors SET
|
UPDATE vendors
|
||||||
name = COALESCE(@name, name),
|
SET
|
||||||
description = COALESCE(@description, description),
|
name = @name,
|
||||||
service_start_at = COALESCE(@service_start_at, service_start_at),
|
description = @description,
|
||||||
service_termination_at = COALESCE(@service_termination_at, service_termination_at),
|
service_start_at = @service_start_at,
|
||||||
service_criticality = COALESCE(@service_criticality, service_criticality),
|
service_termination_at = @service_termination_at,
|
||||||
risk_tier = COALESCE(@risk_tier, risk_tier),
|
service_criticality = @service_criticality,
|
||||||
status_page_url = COALESCE(@status_page_url, status_page_url),
|
risk_tier = @risk_tier,
|
||||||
terms_of_service_url = COALESCE(@terms_of_service_url, terms_of_service_url),
|
status_page_url = @status_page_url,
|
||||||
privacy_policy_url = COALESCE(@privacy_policy_url, privacy_policy_url),
|
terms_of_service_url = @terms_of_service_url,
|
||||||
updated_at = @updated_at,
|
privacy_policy_url = @privacy_policy_url,
|
||||||
version = version + 1
|
updated_at = @updated_at
|
||||||
WHERE %s
|
WHERE %s
|
||||||
AND id = @vendor_id
|
AND id = @vendor_id
|
||||||
AND version = @expected_version
|
|
||||||
RETURNING
|
|
||||||
id,
|
|
||||||
organization_id,
|
|
||||||
name,
|
|
||||||
description,
|
|
||||||
service_start_at,
|
|
||||||
service_termination_at,
|
|
||||||
service_criticality,
|
|
||||||
risk_tier,
|
|
||||||
status_page_url,
|
|
||||||
terms_of_service_url,
|
|
||||||
privacy_policy_url,
|
|
||||||
created_at,
|
|
||||||
updated_at,
|
|
||||||
version
|
|
||||||
`
|
`
|
||||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||||
|
|
||||||
args := pgx.StrictNamedArgs{
|
args := pgx.StrictNamedArgs{
|
||||||
"vendor_id": v.ID,
|
"vendor_id": v.ID,
|
||||||
"expected_version": params.ExpectedVersion,
|
|
||||||
"updated_at": time.Now(),
|
"updated_at": time.Now(),
|
||||||
"name": params.Name,
|
"name": v.Name,
|
||||||
"description": params.Description,
|
"description": v.Description,
|
||||||
"service_start_at": params.ServiceStartAt,
|
"service_start_at": v.ServiceStartAt,
|
||||||
"service_termination_at": params.ServiceTerminationAt,
|
"service_termination_at": v.ServiceTerminationAt,
|
||||||
"service_criticality": params.ServiceCriticality,
|
"service_criticality": v.ServiceCriticality,
|
||||||
"risk_tier": params.RiskTier,
|
"risk_tier": v.RiskTier,
|
||||||
"status_page_url": params.StatusPageURL,
|
"status_page_url": v.StatusPageURL,
|
||||||
"terms_of_service_url": params.TermsOfServiceURL,
|
"terms_of_service_url": v.TermsOfServiceURL,
|
||||||
"privacy_policy_url": params.PrivacyPolicyURL,
|
"privacy_policy_url": v.PrivacyPolicyURL,
|
||||||
}
|
}
|
||||||
|
|
||||||
maps.Copy(args, scope.SQLArguments())
|
maps.Copy(args, scope.SQLArguments())
|
||||||
|
|
||||||
rows, err := conn.Query(ctx, q, args)
|
_, err := conn.Exec(ctx, q, args)
|
||||||
if err != nil {
|
return err
|
||||||
return fmt.Errorf("cannot query vendor: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
vendor, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[Vendor])
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("cannot collect vendor: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
*v = vendor
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -89,26 +89,59 @@ func (s VendorService) Update(
|
|||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
req UpdateVendorRequest,
|
req UpdateVendorRequest,
|
||||||
) (*coredata.Vendor, error) {
|
) (*coredata.Vendor, error) {
|
||||||
params := coredata.UpdateVendorParams{
|
vendor := &coredata.Vendor{}
|
||||||
ExpectedVersion: req.ExpectedVersion,
|
|
||||||
Name: req.Name,
|
|
||||||
Description: req.Description,
|
|
||||||
ServiceStartAt: req.ServiceStartAt,
|
|
||||||
ServiceTerminationAt: req.ServiceTerminationAt,
|
|
||||||
ServiceCriticality: req.ServiceCriticality,
|
|
||||||
RiskTier: req.RiskTier,
|
|
||||||
StatusPageURL: req.StatusPageURL,
|
|
||||||
TermsOfServiceURL: req.TermsOfServiceURL,
|
|
||||||
PrivacyPolicyURL: req.PrivacyPolicyURL,
|
|
||||||
}
|
|
||||||
|
|
||||||
vendor := &coredata.Vendor{ID: req.ID}
|
|
||||||
|
|
||||||
err := s.svc.pg.WithTx(
|
err := s.svc.pg.WithTx(
|
||||||
ctx,
|
ctx,
|
||||||
func(conn pg.Conn) error {
|
func(conn pg.Conn) error {
|
||||||
return vendor.Update(ctx, conn, s.svc.scope, params)
|
if err := vendor.LoadByID(ctx, conn, s.svc.scope, req.ID); err != nil {
|
||||||
})
|
return fmt.Errorf("cannot load vendor %q: %w", req.ID, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if req.Name != nil {
|
||||||
|
vendor.Name = *req.Name
|
||||||
|
}
|
||||||
|
|
||||||
|
if req.Description != nil {
|
||||||
|
vendor.Description = *req.Description
|
||||||
|
}
|
||||||
|
|
||||||
|
if req.ServiceStartAt != nil {
|
||||||
|
vendor.ServiceStartAt = *req.ServiceStartAt
|
||||||
|
}
|
||||||
|
|
||||||
|
if req.ServiceTerminationAt != nil {
|
||||||
|
vendor.ServiceTerminationAt = req.ServiceTerminationAt
|
||||||
|
}
|
||||||
|
|
||||||
|
if req.ServiceCriticality != nil {
|
||||||
|
vendor.ServiceCriticality = *req.ServiceCriticality
|
||||||
|
}
|
||||||
|
|
||||||
|
if req.RiskTier != nil {
|
||||||
|
vendor.RiskTier = *req.RiskTier
|
||||||
|
}
|
||||||
|
|
||||||
|
if req.StatusPageURL != nil {
|
||||||
|
vendor.StatusPageURL = req.StatusPageURL
|
||||||
|
}
|
||||||
|
|
||||||
|
if req.TermsOfServiceURL != nil {
|
||||||
|
vendor.TermsOfServiceURL = req.TermsOfServiceURL
|
||||||
|
}
|
||||||
|
|
||||||
|
if req.PrivacyPolicyURL != nil {
|
||||||
|
vendor.PrivacyPolicyURL = req.PrivacyPolicyURL
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := vendor.Update(ctx, conn, s.svc.scope); err != nil {
|
||||||
|
return fmt.Errorf("cannot update vendor: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user