2019-01-24 16:26:36 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-11-20 14:50:47 +00:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package satellitedb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-01-31 14:11:53 +00:00
|
|
|
"strings"
|
2018-11-20 14:50:47 +00:00
|
|
|
|
|
|
|
"github.com/skyrings/skyring-common/tools/uuid"
|
|
|
|
"github.com/zeebo/errs"
|
2018-11-29 18:39:27 +00:00
|
|
|
|
2019-12-16 17:59:01 +00:00
|
|
|
"storj.io/storj/private/dbutil"
|
2019-01-15 13:03:24 +00:00
|
|
|
"storj.io/storj/satellite/console"
|
2019-01-16 20:23:28 +00:00
|
|
|
dbx "storj.io/storj/satellite/satellitedb/dbx"
|
2018-11-20 14:50:47 +00:00
|
|
|
)
|
|
|
|
|
2019-11-04 14:37:39 +00:00
|
|
|
// ensures that projectMembers implements console.ProjectMembers.
|
|
|
|
var _ console.ProjectMembers = (*projectMembers)(nil)
|
|
|
|
|
2018-11-20 14:50:47 +00:00
|
|
|
// ProjectMembers exposes methods to manage ProjectMembers table in database.
|
|
|
|
type projectMembers struct {
|
2018-12-28 12:07:35 +00:00
|
|
|
methods dbx.Methods
|
2019-12-14 02:29:54 +00:00
|
|
|
db *satelliteDB
|
2018-11-20 14:50:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetByMemberID is a method for querying project member from the database by memberID.
|
2019-06-04 12:55:38 +01:00
|
|
|
func (pm *projectMembers) GetByMemberID(ctx context.Context, memberID uuid.UUID) (_ []console.ProjectMember, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-12-28 12:07:35 +00:00
|
|
|
projectMembersDbx, err := pm.methods.All_ProjectMember_By_MemberId(ctx, dbx.ProjectMember_MemberId(memberID[:]))
|
2018-11-20 14:50:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-06-04 12:55:38 +01:00
|
|
|
return projectMembersFromDbxSlice(ctx, projectMembersDbx)
|
2018-11-20 14:50:47 +00:00
|
|
|
}
|
|
|
|
|
2018-12-19 13:03:12 +00:00
|
|
|
// GetByProjectID is a method for querying project members from the database by projectID, offset and limit.
|
2019-08-12 11:22:32 +01:00
|
|
|
func (pm *projectMembers) GetPagedByProjectID(ctx context.Context, projectID uuid.UUID, cursor console.ProjectMembersCursor) (_ *console.ProjectMembersPage, err error) {
|
2019-06-04 12:55:38 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-08-12 11:22:32 +01:00
|
|
|
|
|
|
|
search := "%" + strings.Replace(cursor.Search, " ", "%", -1) + "%"
|
|
|
|
|
|
|
|
if cursor.Limit > 50 {
|
|
|
|
cursor.Limit = 50
|
2018-12-28 12:07:35 +00:00
|
|
|
}
|
|
|
|
|
2019-08-12 11:22:32 +01:00
|
|
|
if cursor.Page == 0 {
|
|
|
|
return nil, errs.New("page cannot be 0")
|
|
|
|
}
|
|
|
|
|
|
|
|
page := &console.ProjectMembersPage{
|
|
|
|
Search: cursor.Search,
|
|
|
|
Limit: cursor.Limit,
|
|
|
|
Offset: uint64((cursor.Page - 1) * cursor.Limit),
|
|
|
|
Order: cursor.Order,
|
|
|
|
OrderDirection: cursor.OrderDirection,
|
|
|
|
}
|
2018-12-28 12:07:35 +00:00
|
|
|
|
2019-08-12 11:22:32 +01:00
|
|
|
countQuery := pm.db.Rebind(`
|
|
|
|
SELECT COUNT(*)
|
|
|
|
FROM project_members pm
|
|
|
|
INNER JOIN users u ON pm.member_id = u.id
|
|
|
|
WHERE pm.project_id = ?
|
|
|
|
AND ( u.email LIKE ? OR
|
|
|
|
u.full_name LIKE ? OR
|
|
|
|
u.short_name LIKE ?
|
|
|
|
)`)
|
|
|
|
|
|
|
|
countRow := pm.db.QueryRowContext(ctx,
|
|
|
|
countQuery,
|
|
|
|
projectID[:],
|
|
|
|
search,
|
|
|
|
search,
|
|
|
|
search)
|
|
|
|
|
|
|
|
err = countRow.Scan(&page.TotalCount)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if page.TotalCount == 0 {
|
|
|
|
return page, nil
|
|
|
|
}
|
|
|
|
if page.Offset > page.TotalCount-1 {
|
|
|
|
return nil, errs.New("page is out of range")
|
|
|
|
}
|
2019-01-31 13:01:13 +00:00
|
|
|
// TODO: LIKE is case-sensitive postgres, however this should be case-insensitive and possibly allow typos
|
|
|
|
reboundQuery := pm.db.Rebind(`
|
2018-12-28 12:07:35 +00:00
|
|
|
SELECT pm.*
|
|
|
|
FROM project_members pm
|
|
|
|
INNER JOIN users u ON pm.member_id = u.id
|
2019-08-12 11:22:32 +01:00
|
|
|
WHERE pm.project_id = ?
|
|
|
|
AND ( u.email LIKE ? OR
|
|
|
|
u.full_name LIKE ? OR
|
|
|
|
u.short_name LIKE ? )
|
|
|
|
ORDER BY ` + sanitizedOrderColumnName(cursor.Order) + `
|
|
|
|
` + sanitizeOrderDirectionName(page.OrderDirection) + `
|
|
|
|
LIMIT ? OFFSET ?`)
|
|
|
|
|
|
|
|
rows, err := pm.db.QueryContext(ctx,
|
|
|
|
reboundQuery,
|
|
|
|
projectID[:],
|
|
|
|
search,
|
|
|
|
search,
|
|
|
|
search,
|
|
|
|
page.Limit,
|
|
|
|
page.Offset)
|
2018-12-28 12:07:35 +00:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
err = errs.Combine(err, rows.Close())
|
|
|
|
}()
|
2018-12-17 14:28:58 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-08-12 11:22:32 +01:00
|
|
|
var projectMembers []console.ProjectMember
|
2018-12-28 12:07:35 +00:00
|
|
|
for rows.Next() {
|
2019-01-15 13:03:24 +00:00
|
|
|
pm := console.ProjectMember{}
|
2018-12-28 12:07:35 +00:00
|
|
|
var memberIDBytes, projectIDBytes []uint8
|
|
|
|
var memberID, projectID uuid.UUID
|
|
|
|
|
2019-03-29 12:55:05 +00:00
|
|
|
err = rows.Scan(&memberIDBytes, &projectIDBytes, &pm.CreatedAt)
|
2018-12-28 12:07:35 +00:00
|
|
|
if err != nil {
|
2019-03-29 12:55:05 +00:00
|
|
|
return nil, err
|
2018-12-28 12:07:35 +00:00
|
|
|
}
|
|
|
|
|
2019-12-16 17:59:01 +00:00
|
|
|
memberID, err := dbutil.BytesToUUID(memberIDBytes)
|
2019-03-29 12:55:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-12-28 12:07:35 +00:00
|
|
|
}
|
|
|
|
|
2019-12-16 17:59:01 +00:00
|
|
|
projectID, err = dbutil.BytesToUUID(projectIDBytes)
|
2019-03-29 12:55:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2018-12-28 12:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pm.ProjectID = projectID
|
|
|
|
pm.MemberID = memberID
|
|
|
|
|
|
|
|
projectMembers = append(projectMembers, pm)
|
|
|
|
}
|
|
|
|
|
2019-08-12 11:22:32 +01:00
|
|
|
page.ProjectMembers = projectMembers
|
|
|
|
page.Order = cursor.Order
|
|
|
|
|
|
|
|
page.PageCount = uint(page.TotalCount / uint64(cursor.Limit))
|
|
|
|
if page.TotalCount%uint64(cursor.Limit) != 0 {
|
|
|
|
page.PageCount++
|
|
|
|
}
|
|
|
|
|
|
|
|
page.CurrentPage = cursor.Page
|
|
|
|
|
|
|
|
return page, err
|
2018-12-17 14:28:58 +00:00
|
|
|
}
|
|
|
|
|
2018-11-20 14:50:47 +00:00
|
|
|
// Insert is a method for inserting project member into the database.
|
2019-06-04 12:55:38 +01:00
|
|
|
func (pm *projectMembers) Insert(ctx context.Context, memberID, projectID uuid.UUID) (_ *console.ProjectMember, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-12-28 12:07:35 +00:00
|
|
|
createdProjectMember, err := pm.methods.Create_ProjectMember(ctx,
|
2018-11-20 14:50:47 +00:00
|
|
|
dbx.ProjectMember_MemberId(memberID[:]),
|
|
|
|
dbx.ProjectMember_ProjectId(projectID[:]))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-06-04 12:55:38 +01:00
|
|
|
return projectMemberFromDBX(ctx, createdProjectMember)
|
2018-11-20 14:50:47 +00:00
|
|
|
}
|
|
|
|
|
2018-12-10 12:29:01 +00:00
|
|
|
// Delete is a method for deleting project member by memberID and projectID from the database.
|
2019-06-04 12:55:38 +01:00
|
|
|
func (pm *projectMembers) Delete(ctx context.Context, memberID, projectID uuid.UUID) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
_, err = pm.methods.Delete_ProjectMember_By_MemberId_And_ProjectId(
|
2018-12-10 12:29:01 +00:00
|
|
|
ctx,
|
|
|
|
dbx.ProjectMember_MemberId(memberID[:]),
|
|
|
|
dbx.ProjectMember_ProjectId(projectID[:]),
|
|
|
|
)
|
2018-11-20 14:50:47 +00:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// projectMemberFromDBX is used for creating ProjectMember entity from autogenerated dbx.ProjectMember struct
|
2019-06-04 12:55:38 +01:00
|
|
|
func projectMemberFromDBX(ctx context.Context, projectMember *dbx.ProjectMember) (_ *console.ProjectMember, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-11-20 14:50:47 +00:00
|
|
|
if projectMember == nil {
|
|
|
|
return nil, errs.New("projectMember parameter is nil")
|
|
|
|
}
|
|
|
|
|
2019-12-16 17:59:01 +00:00
|
|
|
memberID, err := dbutil.BytesToUUID(projectMember.MemberId)
|
2018-11-20 14:50:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-12-16 17:59:01 +00:00
|
|
|
projectID, err := dbutil.BytesToUUID(projectMember.ProjectId)
|
2018-11-20 14:50:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-01-15 13:03:24 +00:00
|
|
|
return &console.ProjectMember{
|
2018-11-20 14:50:47 +00:00
|
|
|
MemberID: memberID,
|
|
|
|
ProjectID: projectID,
|
|
|
|
CreatedAt: projectMember.CreatedAt,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-12-28 12:07:35 +00:00
|
|
|
// sanitizedOrderColumnName return valid order by column
|
2019-01-15 13:03:24 +00:00
|
|
|
func sanitizedOrderColumnName(pmo console.ProjectMemberOrder) string {
|
2018-12-28 12:07:35 +00:00
|
|
|
switch pmo {
|
|
|
|
case 2:
|
|
|
|
return "u.email"
|
|
|
|
case 3:
|
2019-10-15 13:24:53 +01:00
|
|
|
return "pm.created_at"
|
2018-12-28 12:07:35 +00:00
|
|
|
default:
|
2019-03-27 12:33:32 +00:00
|
|
|
return "u.full_name"
|
2018-12-28 12:07:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-12 15:19:30 +01:00
|
|
|
func sanitizeOrderDirectionName(pmo console.OrderDirection) string {
|
2019-08-12 11:22:32 +01:00
|
|
|
if pmo == 2 {
|
|
|
|
return "DESC"
|
|
|
|
}
|
|
|
|
|
|
|
|
return "ASC"
|
|
|
|
}
|
|
|
|
|
2018-11-20 14:50:47 +00:00
|
|
|
// projectMembersFromDbxSlice is used for creating []ProjectMember entities from autogenerated []*dbx.ProjectMember struct
|
2019-06-04 12:55:38 +01:00
|
|
|
func projectMembersFromDbxSlice(ctx context.Context, projectMembersDbx []*dbx.ProjectMember) (_ []console.ProjectMember, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-01-15 13:03:24 +00:00
|
|
|
var projectMembers []console.ProjectMember
|
2018-11-20 14:50:47 +00:00
|
|
|
var errors []error
|
|
|
|
|
|
|
|
// Generating []dbo from []dbx and collecting all errors
|
|
|
|
for _, projectMemberDbx := range projectMembersDbx {
|
2019-06-04 12:55:38 +01:00
|
|
|
projectMember, err := projectMemberFromDBX(ctx, projectMemberDbx)
|
2018-11-20 14:50:47 +00:00
|
|
|
if err != nil {
|
|
|
|
errors = append(errors, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
projectMembers = append(projectMembers, *projectMember)
|
|
|
|
}
|
|
|
|
|
2019-03-29 12:30:23 +00:00
|
|
|
return projectMembers, errs.Combine(errors...)
|
2018-11-20 14:50:47 +00:00
|
|
|
}
|