storj/pkg/satellite/satellitedb/projects.go

139 lines
3.6 KiB
Go
Raw Normal View History

// 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"
"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) {
projectsDbxSlice, err := projects.db.All_Project(ctx)
if err != nil {
return nil, err
}
projectsCount := len(projectsDbxSlice)
var projectDboSlice []satellite.Project
var errors []error
// Generating []dbo from []dbx and collecting all errors
for i := 0; i < projectsCount; i++ {
projectDbo, err := projectFromDBX(projectsDbxSlice[i])
if err != nil {
errors = append(errors, err)
}
projectDboSlice = append(projectDboSlice, *projectDbo)
}
return projectDboSlice, utils.CombineErrors(errors...)
}
// GetByUserID is a method for querying project from the database by user id
func (projects *projects) GetByUserID(ctx context.Context, userID uuid.UUID) (*satellite.Project, error) {
project, err := projects.db.Get_Project_By_UserId(ctx, dbx.Project_UserId(userID[:]))
if err != nil {
return nil, err
}
return projectFromDBX(project)
}
// 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
}
createdProject, err := projects.db.Create_Project(ctx,
dbx.Project_Id(projectID[:]),
dbx.Project_UserId(project.UserID[:]),
dbx.Project_Name(project.Name),
dbx.Project_Description(project.Description),
dbx.Project_IsAgreedWithTerms(project.IsAgreedWithTerms))
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{
Name: dbx.Project_Name(project.Name),
Description: dbx.Project_Description(project.Description),
IsAgreedWithTerms: dbx.Project_IsAgreedWithTerms(project.IsAgreedWithTerms),
})
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
}
userID, err := bytesToUUID(project.UserId)
if err != nil {
return nil, err
}
u := &satellite.Project{
ID: id,
UserID: userID,
Name: project.Name,
Description: project.Description,
IsAgreedWithTerms: project.IsAgreedWithTerms,
CreatedAt: project.CreatedAt,
}
return u, nil
}