From 7422fe393b436d8795fb86036b18ee6590942c12 Mon Sep 17 00:00:00 2001 From: Wilfred Asomani Date: Tue, 6 Jun 2023 11:50:41 +0000 Subject: [PATCH] satellite/db: add memberCount to project by userID This change modifies the projects.GetByUserID function to also fetch each project's member count. Change-Id: I5ef4aa3726566920ee9360f5a4f09a8cb490c67e --- satellite/satellitedb/projects.go | 32 ++++++++++++++++-- satellite/satellitedb/projects_test.go | 45 ++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 2 deletions(-) diff --git a/satellite/satellitedb/projects.go b/satellite/satellitedb/projects.go index da2b33910..f5d2c452a 100644 --- a/satellite/satellitedb/projects.go +++ b/satellite/satellitedb/projects.go @@ -66,12 +66,40 @@ func (projects *projects) GetCreatedBefore(ctx context.Context, before time.Time // GetByUserID is a method for querying all projects from the database by userID. func (projects *projects) GetByUserID(ctx context.Context, userID uuid.UUID) (_ []console.Project, err error) { defer mon.Task()(&ctx)(&err) - projectsDbx, err := projects.db.All_Project_By_ProjectMember_MemberId_OrderBy_Asc_Project_Name(ctx, dbx.ProjectMember_MemberId(userID[:])) + + rows, err := projects.sdb.Query(ctx, projects.sdb.Rebind(` + SELECT projects.id, projects.public_id, projects.name, projects.description, projects.owner_id, projects.rate_limit, projects.max_buckets, projects.created_at, + (SELECT COUNT(*) FROM project_members WHERE project_id = projects.id) AS member_count + FROM projects + JOIN project_members ON projects.id = project_members.project_id + WHERE project_members.member_id = ? + ORDER BY name ASC + `), userID) if err != nil { return nil, err } + defer func() { err = errs.Combine(err, rows.Close()) }() - return projectsFromDbxSlice(ctx, projectsDbx) + nextProject := &console.Project{} + var rateLimit, maxBuckets sql.NullInt32 + projectsToSend := make([]console.Project, 0) + for rows.Next() { + err = rows.Scan(&nextProject.ID, &nextProject.PublicID, &nextProject.Name, &nextProject.Description, &nextProject.OwnerID, &rateLimit, &maxBuckets, &nextProject.CreatedAt, &nextProject.MemberCount) + if err != nil { + return nil, err + } + if rateLimit.Valid { + nextProject.RateLimit = new(int) + *nextProject.RateLimit = int(rateLimit.Int32) + } + if maxBuckets.Valid { + nextProject.MaxBuckets = new(int) + *nextProject.MaxBuckets = int(maxBuckets.Int32) + } + projectsToSend = append(projectsToSend, *nextProject) + } + + return projectsToSend, rows.Err() } // Get is a method for querying project from the database by id. diff --git a/satellite/satellitedb/projects_test.go b/satellite/satellitedb/projects_test.go index 4737caed8..8235c3aea 100644 --- a/satellite/satellitedb/projects_test.go +++ b/satellite/satellitedb/projects_test.go @@ -10,6 +10,7 @@ import ( "github.com/stretchr/testify/require" "storj.io/common/testcontext" + "storj.io/common/testrand" "storj.io/common/uuid" "storj.io/storj/satellite" "storj.io/storj/satellite/console" @@ -75,3 +76,47 @@ func TestUpdateProjectUsageLimits(t *testing.T) { require.Equal(t, limits.Segment, *proj.SegmentLimit) }) } + +func TestGetProjectsByUserID(t *testing.T) { + satellitedbtest.Run(t, func(ctx *testcontext.Context, t *testing.T, db satellite.DB) { + projectsRepo := db.Console().Projects() + users := db.Console().Users() + projectMembers := db.Console().ProjectMembers() + + user1, err := users.Insert(ctx, &console.User{ + ID: testrand.UUID(), + Email: "user1@mail.test", + PasswordHash: []byte("password"), + }) + require.NoError(t, err) + + user2, err := users.Insert(ctx, &console.User{ + ID: testrand.UUID(), + Email: "user2@mail.test", + PasswordHash: []byte("password"), + }) + require.NoError(t, err) + + proj, err := projectsRepo.Insert(ctx, &console.Project{ + Name: "Project", + OwnerID: user1.ID, + }) + require.NoError(t, err) + + _, err = projectMembers.Insert(ctx, user1.ID, proj.ID) + require.NoError(t, err) + + projects, err := projectsRepo.GetByUserID(ctx, user1.ID) + require.NoError(t, err) + require.Len(t, projects, 1) + require.Equal(t, 1, projects[0].MemberCount) + + _, err = projectMembers.Insert(ctx, user2.ID, proj.ID) + require.NoError(t, err) + + projects, err = projectsRepo.GetByUserID(ctx, user1.ID) + require.NoError(t, err) + require.Len(t, projects, 1) + require.Equal(t, 2, projects[0].MemberCount) + }) +}