Skip to content

Commit

Permalink
Initial DB Domain Types
Browse files Browse the repository at this point in the history
  • Loading branch information
gbdubs committed Aug 31, 2023
1 parent e9198d9 commit 3573cb4
Show file tree
Hide file tree
Showing 24 changed files with 3,548 additions and 197 deletions.
2 changes: 1 addition & 1 deletion cmd/server/pactasrv/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ go_library(
importpath = "github.com/RMI/pacta/cmd/server/pactasrv",
visibility = ["//visibility:public"],
deps = [
"//:pacta",
"//db",
"//openapi:pacta_generated",
"//pacta",
],
)

Expand Down
46 changes: 44 additions & 2 deletions cmd/server/pactasrv/pactasrv.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
"fmt"
"net/http"

"github.com/RMI/pacta"
"github.com/RMI/pacta/db"

pactaapi "github.com/RMI/pacta/openapi/pacta"
"github.com/RMI/pacta/pacta"
)

type DB interface {
Expand All @@ -20,7 +20,49 @@ type DB interface {
Transactional(context.Context, func(tx db.Tx) error) error
RunOrContinueTransaction(db.Tx, func(tx db.Tx) error) error

User(db.Tx, pacta.UserID) (*pacta.User, error)
Blob(tx db.Tx, id pacta.BlobID) (*pacta.Blob, error)
Blobs(tx db.Tx, ids []pacta.BlobID) (map[pacta.BlobID]*pacta.Blob, error)
CreateBlob(tx db.Tx, b *pacta.Blob) (pacta.BlobID, error)
UpdateBlob(tx db.Tx, id pacta.BlobID, mutations ...db.UpdateBlobFn) error
DeleteBlob(tx db.Tx, id pacta.BlobID) (pacta.BlobURI, error)

InitiativeInvitation(tx db.Tx, id pacta.InitiativeInvitationID) (*pacta.InitiativeInvitation, error)
InitiativeInvitationsByInitiative(tx db.Tx, iid pacta.InitiativeID) ([]*pacta.InitiativeInvitation, error)
CreateInitiativeInvitation(tx db.Tx, ii *pacta.InitiativeInvitation) (pacta.InitiativeInvitationID, error)
UpdateInitiativeInvitation(tx db.Tx, id pacta.InitiativeInvitationID, mutations ...db.UpdateInitiativeInvitationFn) error
DeleteInitiativeInvitation(tx db.Tx, id pacta.InitiativeInvitationID) error

InitiativeUserRelationship(tx db.Tx, iid pacta.InitiativeID, uid pacta.UserID) (*pacta.InitiativeUserRelationship, error)
InitiativeUserRelationshipsByUser(tx db.Tx, uid pacta.UserID) ([]*pacta.InitiativeUserRelationship, error)
InitiativeUserRelationshipsByInitiatives(tx db.Tx, iid pacta.InitiativeID) ([]*pacta.InitiativeUserRelationship, error)
PutInitiativeUserRelationship(tx db.Tx, iur *pacta.InitiativeUserRelationship) error

Initiative(tx db.Tx, id pacta.InitiativeID) (*pacta.Initiative, error)
Initiatives(tx db.Tx, ids []pacta.InitiativeID) (map[pacta.InitiativeID]*pacta.Initiative, error)
AllInitiatives(tx db.Tx) ([]*pacta.Initiative, error)
CreateInitiative(tx db.Tx, i *pacta.Initiative) error
UpdateInitiative(tx db.Tx, id pacta.InitiativeID, mutations ...db.UpdateInitiativeFn) error
DeleteInitiative(tx db.Tx, id pacta.InitiativeID) error

PACTAVersion(tx db.Tx, id pacta.PACTAVersionID) (*pacta.PACTAVersion, error)
DefaultPACTAVersion(tx db.Tx) (*pacta.PACTAVersion, error)
PACTAVersions(tx db.Tx) ([]*pacta.PACTAVersion, error)
CreatePACTAVersion(tx db.Tx, pv *pacta.PACTAVersion) (pacta.PACTAVersionID, error)
SetDefaultPACTAVersion(tx db.Tx, id pacta.PACTAVersionID) error
UpdatePACTAVersion(tx db.Tx, id pacta.PACTAVersionID, mutations ...db.UpdatePACTAVersionFn) error
DeletePACTAVersion(tx db.Tx, id pacta.PACTAVersionID) error

PortfolioInitiativeMembershipsByPortfolio(tx db.Tx, pid pacta.PortfolioID) ([]*pacta.PortfolioInitiativeMembership, error)
PortfolioInitiativeMembershipsByInitiative(tx db.Tx, iid pacta.InitiativeID) ([]*pacta.PortfolioInitiativeMembership, error)
CreatePortfolioInitiativeMembership(tx db.Tx, pim *pacta.PortfolioInitiativeMembership) error
DeletePortfolioInitiativeMembership(tx db.Tx, pid pacta.PortfolioID, iid pacta.InitiativeID) error

User(tx db.Tx, id pacta.UserID) (*pacta.User, error)
UserByAuthn(tx db.Tx, authnMechanism pacta.AuthnMechanism, authnID string) (*pacta.User, error)
Users(tx db.Tx, ids []pacta.UserID) (map[pacta.UserID]*pacta.User, error)
CreateUser(tx db.Tx, u *pacta.User) (pacta.UserID, error)
UpdateUser(tx db.Tx, id pacta.UserID, mutations ...db.UpdateUserFn) error
DeleteUser(tx db.Tx, id pacta.UserID) error
}

type Server struct {
Expand Down
2 changes: 1 addition & 1 deletion db/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ go_library(
srcs = ["db.go"],
importpath = "github.com/RMI/pacta/db",
visibility = ["//visibility:public"],
deps = ["//:pacta"],
deps = ["//pacta"],
)
138 changes: 135 additions & 3 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ package db
import (
"errors"
"fmt"
"time"

"github.com/RMI/pacta"
"github.com/RMI/pacta/pacta"
)

type errNotFound struct {
Expand Down Expand Up @@ -41,16 +42,147 @@ type Tx interface {

type UpdateUserFn func(*pacta.User) error

func SetUserAdmin(value bool) UpdateUserFn {
return func(u *pacta.User) error {
u.Admin = value
return nil
}
}

func SetUserSuperAdmin(value bool) UpdateUserFn {
return func(u *pacta.User) error {
u.SuperAdmin = value
return nil
}
}

func SetUserName(value string) UpdateUserFn {
return func(u *pacta.User) error {
u.Name = value
return nil
}
}

func SetUserEmail(value string) UpdateUserFn {
func SetUserPreferredLanguage(value pacta.Language) UpdateUserFn {
return func(u *pacta.User) error {
u.Email = value
u.PreferredLanguage = value
return nil
}
}

type UpdatePACTAVersionFn func(*pacta.PACTAVersion) error

func SetPACTAVersionName(value string) UpdatePACTAVersionFn {
return func(v *pacta.PACTAVersion) error {
v.Name = value
return nil
}
}

func SetPACTAVersionDescription(value string) UpdatePACTAVersionFn {
return func(v *pacta.PACTAVersion) error {
v.Description = value
return nil
}
}

func SetPACTAVersionDigest(value string) UpdatePACTAVersionFn {
return func(v *pacta.PACTAVersion) error {
v.Digest = value
return nil
}
}

type UpdateInitiativeFn func(*pacta.Initiative) error

func SetInitiativeName(value string) UpdateInitiativeFn {
return func(v *pacta.Initiative) error {
v.Name = value
return nil
}
}

func SetInitiativeAffiliation(value string) UpdateInitiativeFn {
return func(v *pacta.Initiative) error {
v.Affiliation = value
return nil
}
}

func SetInitiativePublicDescription(value string) UpdateInitiativeFn {
return func(v *pacta.Initiative) error {
v.PublicDescription = value
return nil
}
}

func SetInitiativeInternalDescription(value string) UpdateInitiativeFn {
return func(v *pacta.Initiative) error {
v.InternalDescription = value
return nil
}
}

func SetInitiativeRequiresInvitationToJoin(value bool) UpdateInitiativeFn {
return func(v *pacta.Initiative) error {
v.RequiresInvitationToJoin = value
return nil
}
}

func SetInitiativeIsAcceptingNewMembers(value bool) UpdateInitiativeFn {
return func(v *pacta.Initiative) error {
v.IsAcceptingNewMembers = value
return nil
}
}

func SetInitiativeIsAcceptingNewPortfolios(value bool) UpdateInitiativeFn {
return func(v *pacta.Initiative) error {
v.IsAcceptingNewPortfolios = value
return nil
}
}

func SetInitiativePACTAVersion(pvid pacta.PACTAVersionID) UpdateInitiativeFn {
return func(v *pacta.Initiative) error {
v.PACTAVersion = &pacta.PACTAVersion{ID: pvid}
return nil
}
}

func SetInitiativeLanguage(l pacta.Language) UpdateInitiativeFn {
return func(v *pacta.Initiative) error {
v.Language = l
return nil
}
}

type UpdateInitiativeInvitationFn func(*pacta.InitiativeInvitation) error

func SetInitiativeInvitationUsedAt(t time.Time) UpdateInitiativeInvitationFn {
return func(ii *pacta.InitiativeInvitation) error {
ii.UsedAt = t
return nil
}
}

func SetInitiativeInvitationUsedBy(u pacta.UserID) UpdateInitiativeInvitationFn {
return func(ii *pacta.InitiativeInvitation) error {
if u == "" {
ii.UsedBy = nil
} else {
ii.UsedBy = &pacta.User{ID: u}
}
return nil
}
}

type UpdateBlobFn func(*pacta.Blob) error

func SetBlobFileName(v string) UpdateBlobFn {
return func(b *pacta.Blob) error {
b.FileName = v
return nil
}
}
16 changes: 14 additions & 2 deletions db/sqldb/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,23 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
go_library(
name = "sqldb",
srcs = [
"blob.go",
"initiative.go",
"initiative_invitation.go",
"initiative_user.go",
"pacta_version.go",
"portfolio_initiative.go",
"sqldb.go",
"user.go",
],
importpath = "github.com/RMI/pacta/db/sqldb",
visibility = ["//visibility:public"],
deps = [
"//:pacta",
"//db",
"//pacta",
"@com_github_hashicorp_go_multierror//:go-multierror",
"@com_github_jackc_pgconn//:pgconn",
"@com_github_jackc_pgtype//:pgtype",
"@com_github_jackc_pgx_v4//:pgx",
"@com_github_silicon_ally_cryptorand//:cryptorand",
"@com_github_silicon_ally_idgen//:idgen",
Expand All @@ -23,6 +30,11 @@ go_test(
name = "sqldb_test",
size = "large",
srcs = [
"blob_test.go",
"initiative_invitation_test.go",
"initiative_test.go",
"initiative_user_test.go",
"pacta_version_test.go",
"sqldb_test.go",
"user_test.go",
],
Expand All @@ -32,8 +44,8 @@ go_test(
],
embed = [":sqldb"],
deps = [
"//:pacta",
"//db",
"//pacta",
"@com_github_google_go_cmp//cmp",
"@com_github_google_go_cmp//cmp/cmpopts",
"@com_github_silicon_ally_idgen//:idgen",
Expand Down
Loading

0 comments on commit 3573cb4

Please sign in to comment.