diff --git a/cmd/server/pactasrv/pactasrv.go b/cmd/server/pactasrv/pactasrv.go index ebf66f4..597f58c 100644 --- a/cmd/server/pactasrv/pactasrv.go +++ b/cmd/server/pactasrv/pactasrv.go @@ -102,6 +102,7 @@ type DB interface { PortfolioSnapshots(tx db.Tx, ids []pacta.PortfolioSnapshotID) (map[pacta.PortfolioSnapshotID]*pacta.PortfolioSnapshot, error) GetOwnerForUser(tx db.Tx, uID pacta.UserID) (pacta.OwnerID, error) + GetOwnerForInitiative(tx db.Tx, iID pacta.InitiativeID) (pacta.OwnerID, error) PortfolioGroup(tx db.Tx, id pacta.PortfolioGroupID) (*pacta.PortfolioGroup, error) PortfolioGroupsByOwner(tx db.Tx, owner pacta.OwnerID) ([]*pacta.PortfolioGroup, error) diff --git a/db/sqldb/initiative.go b/db/sqldb/initiative.go index 86bd651..df41c4a 100644 --- a/db/sqldb/initiative.go +++ b/db/sqldb/initiative.go @@ -78,16 +78,22 @@ func (d *DB) CreateInitiative(tx db.Tx, i *pacta.Initiative) error { if err := validateInitiativeForCreation(i); err != nil { return fmt.Errorf("validating initiative for creation: %w", err) } - err := d.exec(tx, ` - INSERT INTO initiative - (id, name, affiliation, public_description, internal_description, requires_invitation_to_join, is_accepting_new_members, is_accepting_new_portfolios, pacta_version_id, language) - VALUES - ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10);`, - i.ID, i.Name, i.Affiliation, i.PublicDescription, i.InternalDescription, i.RequiresInvitationToJoin, i.IsAcceptingNewMembers, i.IsAcceptingNewPortfolios, i.PACTAVersion.ID, i.Language) - if err != nil { - return fmt.Errorf("creating initiative: %w", err) - } - return nil + return d.RunOrContinueTransaction(tx, func(tx db.Tx) error { + err := d.exec(tx, ` + INSERT INTO initiative + (id, name, affiliation, public_description, internal_description, requires_invitation_to_join, is_accepting_new_members, is_accepting_new_portfolios, pacta_version_id, language) + VALUES + ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10);`, + i.ID, i.Name, i.Affiliation, i.PublicDescription, i.InternalDescription, i.RequiresInvitationToJoin, i.IsAcceptingNewMembers, i.IsAcceptingNewPortfolios, i.PACTAVersion.ID, i.Language) + if err != nil { + return fmt.Errorf("creating initiative: %w", err) + } + _, err = d.createOwner(tx, &pacta.Owner{Initiative: &pacta.Initiative{ID: i.ID}}) + if err != nil { + return fmt.Errorf("creating owner: %w", err) + } + return nil + }) } func (d *DB) UpdateInitiative(tx db.Tx, id pacta.InitiativeID, mutations ...db.UpdateInitiativeFn) error { @@ -117,7 +123,7 @@ func (d *DB) UpdateInitiative(tx db.Tx, id pacta.InitiativeID, mutations ...db.U func (d *DB) DeleteInitiative(tx db.Tx, id pacta.InitiativeID) ([]pacta.BlobURI, error) { buris := []pacta.BlobURI{} err := d.RunOrContinueTransaction(tx, func(db.Tx) error { - owner, err := d.GetOrCreateOwnerForInitiative(tx, id) + owner, err := d.GetOwnerForInitiative(tx, id) if err != nil { return fmt.Errorf("getting owner for initiative: %w", err) } diff --git a/db/sqldb/owner.go b/db/sqldb/owner.go index 5fc65da..61c2cbd 100644 --- a/db/sqldb/owner.go +++ b/db/sqldb/owner.go @@ -71,45 +71,25 @@ func (d *DB) ownerByInitiative(tx db.Tx, id pacta.InitiativeID) (*pacta.Owner, e } func (d *DB) GetOwnerForUser(tx db.Tx, uID pacta.UserID) (pacta.OwnerID, error) { - var ownerID pacta.OwnerID - err := d.RunOrContinueTransaction(tx, func(tx db.Tx) error { - owner, err := d.ownerByUser(tx, uID) - if err == nil { - ownerID = owner.ID - return nil - } - if !db.IsNotFound(err) { - return fmt.Errorf("user owner not found: %w", err) - } - return fmt.Errorf("looking up owner: %w", err) - }) + owner, err := d.ownerByUser(tx, uID) if err != nil { - return "", fmt.Errorf("getting or creating owner for initiative: %w", err) + if db.IsNotFound(err) { + return "", db.NotFound(uID, "ownerByUserId") + } + return "", fmt.Errorf("error retrieving user owner: %w", err) } - return ownerID, nil + return owner.ID, nil } -func (d *DB) GetOrCreateOwnerForInitiative(tx db.Tx, iID pacta.InitiativeID) (pacta.OwnerID, error) { - var ownerID pacta.OwnerID - err := d.RunOrContinueTransaction(tx, func(tx db.Tx) error { - owner, err := d.ownerByInitiative(tx, iID) - if err == nil { - ownerID = owner.ID - return nil - } - if !db.IsNotFound(err) { - return fmt.Errorf("querying owner by user: %w", err) - } - ownerID, err = d.createOwner(tx, &pacta.Owner{Initiative: &pacta.Initiative{ID: iID}}) - if err != nil { - return fmt.Errorf("creating owner: %w", err) - } - return nil - }) +func (d *DB) GetOwnerForInitiative(tx db.Tx, iID pacta.InitiativeID) (pacta.OwnerID, error) { + owner, err := d.ownerByInitiative(tx, iID) if err != nil { - return "", fmt.Errorf("getting or creating owner for initiative: %w", err) + if db.IsNotFound(err) { + return "", db.NotFound(iID, "ownerByInitiativeId") + } + return "", fmt.Errorf("error retrieving initiative owner: %w", err) } - return ownerID, nil + return owner.ID, nil } func (d *DB) DeleteOwner(tx db.Tx, oID pacta.OwnerID) ([]pacta.BlobURI, error) { diff --git a/db/sqldb/owner_test.go b/db/sqldb/owner_test.go index 76f385c..0b1ccd7 100644 --- a/db/sqldb/owner_test.go +++ b/db/sqldb/owner_test.go @@ -29,11 +29,11 @@ func TestGetOrCreateOwners(t *testing.T) { t.Fatalf("expected owner id %q, got %q", uo, uo2) } - io, err := tdb.GetOrCreateOwnerForInitiative(tx, i.ID) + io, err := tdb.GetOwnerForInitiative(tx, i.ID) if err != nil { t.Fatalf("creating owner for initiative: %v", err) } - io2, err := tdb.GetOrCreateOwnerForInitiative(tx, i.ID) + io2, err := tdb.GetOwnerForInitiative(tx, i.ID) if err != nil { t.Fatalf("creating owner for initiative: %v", err) } @@ -96,7 +96,7 @@ func ownerUserForTesting(t *testing.T, tdb *DB, u *pacta.User) *pacta.Owner { func ownerInitiativeForTesting(t *testing.T, tdb *DB, i *pacta.Initiative) *pacta.Owner { t.Helper() tx := tdb.NoTxn(context.Background()) - oid, err := tdb.GetOrCreateOwnerForInitiative(tx, i.ID) + oid, err := tdb.GetOwnerForInitiative(tx, i.ID) if err != nil { t.Fatalf("creating owner for user: %v", err) }