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
This commit is contained in:
Wilfred Asomani 2023-06-06 11:50:41 +00:00
parent 3e3ec2358d
commit 7422fe393b
2 changed files with 75 additions and 2 deletions

View File

@ -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.

View File

@ -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)
})
}