2018-11-13 08:27:42 +00:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package satellitedb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/skyrings/skyring-common/tools/uuid"
|
|
|
|
"github.com/zeebo/errs"
|
2018-11-29 18:39:27 +00:00
|
|
|
|
2018-11-13 08:27:42 +00:00
|
|
|
"storj.io/storj/pkg/satellite"
|
|
|
|
"storj.io/storj/pkg/satellite/satellitedb/dbx"
|
|
|
|
"storj.io/storj/pkg/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
// implementation of Projects interface repository using spacemonkeygo/dbx orm
|
|
|
|
type projects struct {
|
|
|
|
db *dbx.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAll is a method for querying all projects from the database.
|
|
|
|
func (projects *projects) GetAll(ctx context.Context) ([]satellite.Project, error) {
|
2018-11-14 12:45:49 +00:00
|
|
|
projectsDbx, err := projects.db.All_Project(ctx)
|
2018-11-13 08:27:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-11-14 12:45:49 +00:00
|
|
|
return projectsFromDbxSlice(projectsDbx)
|
2018-11-13 08:27:42 +00:00
|
|
|
}
|
|
|
|
|
2018-11-14 12:45:49 +00:00
|
|
|
// GetByOwnerID is a method for querying projects from the database by ownerID.
|
|
|
|
func (projects *projects) GetByOwnerID(ctx context.Context, ownerID uuid.UUID) ([]satellite.Project, error) {
|
|
|
|
projectsDbx, err := projects.db.All_Project_By_OwnerId(ctx, dbx.Project_OwnerId(ownerID[:]))
|
2018-11-13 08:27:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-11-14 12:45:49 +00:00
|
|
|
return projectsFromDbxSlice(projectsDbx)
|
2018-11-13 08:27:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get is a method for querying project from the database by id.
|
|
|
|
func (projects *projects) Get(ctx context.Context, id uuid.UUID) (*satellite.Project, error) {
|
|
|
|
project, err := projects.db.Get_Project_By_Id(ctx, dbx.Project_Id(id[:]))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return projectFromDBX(project)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert is a method for inserting project into the database.
|
|
|
|
func (projects *projects) Insert(ctx context.Context, project *satellite.Project) (*satellite.Project, error) {
|
|
|
|
projectID, err := uuid.New()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-11-14 12:45:49 +00:00
|
|
|
var ownerID dbx.Project_OwnerId_Field
|
|
|
|
|
|
|
|
if project.OwnerID != nil {
|
|
|
|
ownerID = dbx.Project_OwnerId(project.OwnerID[:])
|
|
|
|
} else {
|
|
|
|
ownerID = dbx.Project_OwnerId(nil)
|
|
|
|
}
|
|
|
|
|
2018-11-13 08:27:42 +00:00
|
|
|
createdProject, err := projects.db.Create_Project(ctx,
|
|
|
|
dbx.Project_Id(projectID[:]),
|
|
|
|
dbx.Project_Name(project.Name),
|
|
|
|
dbx.Project_Description(project.Description),
|
2018-11-20 14:50:47 +00:00
|
|
|
dbx.Project_TermsAccepted(project.TermsAccepted),
|
2018-11-14 12:45:49 +00:00
|
|
|
dbx.Project_Create_Fields{
|
|
|
|
OwnerId: ownerID,
|
|
|
|
})
|
2018-11-13 08:27:42 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return projectFromDBX(createdProject)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete is a method for deleting project by Id from the database.
|
|
|
|
func (projects *projects) Delete(ctx context.Context, id uuid.UUID) error {
|
|
|
|
_, err := projects.db.Delete_Project_By_Id(ctx, dbx.Project_Id(id[:]))
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update is a method for updating user entity
|
|
|
|
func (projects *projects) Update(ctx context.Context, project *satellite.Project) error {
|
|
|
|
_, err := projects.db.Update_Project_By_Id(ctx,
|
|
|
|
dbx.Project_Id(project.ID[:]),
|
|
|
|
dbx.Project_Update_Fields{
|
2018-11-20 14:50:47 +00:00
|
|
|
Name: dbx.Project_Name(project.Name),
|
|
|
|
Description: dbx.Project_Description(project.Description),
|
|
|
|
TermsAccepted: dbx.Project_TermsAccepted(project.TermsAccepted),
|
2018-11-13 08:27:42 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// projectFromDBX is used for creating Project entity from autogenerated dbx.Project struct
|
|
|
|
func projectFromDBX(project *dbx.Project) (*satellite.Project, error) {
|
|
|
|
if project == nil {
|
|
|
|
return nil, errs.New("project parameter is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := bytesToUUID(project.Id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
u := &satellite.Project{
|
2018-11-20 14:50:47 +00:00
|
|
|
ID: id,
|
|
|
|
Name: project.Name,
|
|
|
|
Description: project.Description,
|
|
|
|
TermsAccepted: project.TermsAccepted,
|
|
|
|
CreatedAt: project.CreatedAt,
|
2018-11-13 08:27:42 +00:00
|
|
|
}
|
|
|
|
|
2018-11-14 12:45:49 +00:00
|
|
|
if project.OwnerId == nil {
|
|
|
|
u.OwnerID = nil
|
|
|
|
} else {
|
|
|
|
ownerID, err := bytesToUUID(project.OwnerId)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
u.OwnerID = &ownerID
|
|
|
|
}
|
|
|
|
|
2018-11-13 08:27:42 +00:00
|
|
|
return u, nil
|
|
|
|
}
|
2018-11-14 12:45:49 +00:00
|
|
|
|
|
|
|
// projectsFromDbxSlice is used for creating []Project entities from autogenerated []*dbx.Project struct
|
|
|
|
func projectsFromDbxSlice(projectsDbx []*dbx.Project) ([]satellite.Project, error) {
|
|
|
|
var projects []satellite.Project
|
|
|
|
var errors []error
|
|
|
|
|
|
|
|
// Generating []dbo from []dbx and collecting all errors
|
|
|
|
for _, projectDbx := range projectsDbx {
|
|
|
|
project, err := projectFromDBX(projectDbx)
|
|
|
|
if err != nil {
|
|
|
|
errors = append(errors, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
projects = append(projects, *project)
|
|
|
|
}
|
|
|
|
|
|
|
|
return projects, utils.CombineErrors(errors...)
|
|
|
|
}
|