2019-01-24 16:26:36 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-11-13 08:27:42 +00:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package satellitedb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-06-06 17:07:14 +01:00
|
|
|
"time"
|
2018-11-13 08:27:42 +00:00
|
|
|
|
|
|
|
"github.com/skyrings/skyring-common/tools/uuid"
|
|
|
|
"github.com/zeebo/errs"
|
2018-11-29 18:39:27 +00:00
|
|
|
|
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-13 08:27:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// implementation of Projects interface repository using spacemonkeygo/dbx orm
|
|
|
|
type projects struct {
|
2018-12-10 12:29:01 +00:00
|
|
|
db dbx.Methods
|
2018-11-13 08:27:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetAll is a method for querying all projects from the database.
|
2019-06-04 12:55:38 +01:00
|
|
|
func (projects *projects) GetAll(ctx context.Context) (_ []console.Project, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-06-06 17:07:14 +01:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-06-04 12:55:38 +01:00
|
|
|
return projectsFromDbxSlice(ctx, projectsDbx)
|
2018-11-13 08:27:42 +00:00
|
|
|
}
|
|
|
|
|
2019-06-06 17:07:14 +01:00
|
|
|
// GetCreatedBefore retrieves all projects created before provided date
|
|
|
|
func (projects *projects) GetCreatedBefore(ctx context.Context, before time.Time) (_ []console.Project, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
projectsDbx, err := projects.db.All_Project_By_CreatedAt_Less_OrderBy_Asc_CreatedAt(ctx, dbx.Project_CreatedAt(before))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return projectsFromDbxSlice(ctx, projectsDbx)
|
|
|
|
}
|
|
|
|
|
2018-12-06 15:19:47 +00:00
|
|
|
// GetByUserID is a method for querying all projects from the database by userID.
|
2019-06-04 12:55:38 +01:00
|
|
|
func (projects *projects) GetByUserID(ctx context.Context, userID uuid.UUID) (_ []console.Project, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-01-04 11:32:21 +00:00
|
|
|
projectsDbx, err := projects.db.All_Project_By_ProjectMember_MemberId_OrderBy_Asc_Project_Name(ctx, dbx.ProjectMember_MemberId(userID[:]))
|
2018-12-06 15:19:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-06-04 12:55:38 +01:00
|
|
|
return projectsFromDbxSlice(ctx, projectsDbx)
|
2018-12-06 15:19:47 +00:00
|
|
|
}
|
|
|
|
|
2018-11-13 08:27:42 +00:00
|
|
|
// Get is a method for querying project from the database by id.
|
2019-06-04 12:55:38 +01:00
|
|
|
func (projects *projects) Get(ctx context.Context, id uuid.UUID) (_ *console.Project, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-06-06 17:07:14 +01:00
|
|
|
|
2018-11-13 08:27:42 +00:00
|
|
|
project, err := projects.db.Get_Project_By_Id(ctx, dbx.Project_Id(id[:]))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-06-04 12:55:38 +01:00
|
|
|
return projectFromDBX(ctx, project)
|
2018-11-13 08:27:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Insert is a method for inserting project into the database.
|
2019-06-04 12:55:38 +01:00
|
|
|
func (projects *projects) Insert(ctx context.Context, project *console.Project) (_ *console.Project, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-06-06 17:07:14 +01:00
|
|
|
|
2018-11-13 08:27:42 +00:00
|
|
|
projectID, err := uuid.New()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
createdProject, err := projects.db.Create_Project(ctx,
|
|
|
|
dbx.Project_Id(projectID[:]),
|
|
|
|
dbx.Project_Name(project.Name),
|
2019-05-28 16:36:52 +01:00
|
|
|
dbx.Project_Description(project.Description),
|
|
|
|
dbx.Project_UsageLimit(0),
|
|
|
|
)
|
2018-11-13 08:27:42 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-06-04 12:55:38 +01:00
|
|
|
return projectFromDBX(ctx, createdProject)
|
2018-11-13 08:27:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Delete is a method for deleting project by Id from the database.
|
2019-06-04 12:55:38 +01:00
|
|
|
func (projects *projects) Delete(ctx context.Context, id uuid.UUID) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-06-06 17:07:14 +01:00
|
|
|
|
2019-06-04 12:55:38 +01:00
|
|
|
_, err = projects.db.Delete_Project_By_Id(ctx, dbx.Project_Id(id[:]))
|
2018-11-13 08:27:42 +00:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-12-06 15:19:47 +00:00
|
|
|
// Update is a method for updating project entity
|
2019-06-04 12:55:38 +01:00
|
|
|
func (projects *projects) Update(ctx context.Context, project *console.Project) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-06-06 17:07:14 +01:00
|
|
|
|
2018-12-06 15:19:47 +00:00
|
|
|
updateFields := dbx.Project_Update_Fields{
|
2019-01-28 18:20:33 +00:00
|
|
|
Description: dbx.Project_Description(project.Description),
|
2019-05-28 16:36:52 +01:00
|
|
|
UsageLimit: dbx.Project_UsageLimit(project.UsageLimit),
|
2018-12-06 15:19:47 +00:00
|
|
|
}
|
|
|
|
|
2019-06-04 12:55:38 +01:00
|
|
|
_, err = projects.db.Update_Project_By_Id(ctx,
|
2018-11-13 08:27:42 +00:00
|
|
|
dbx.Project_Id(project.ID[:]),
|
2018-12-06 15:19:47 +00:00
|
|
|
updateFields)
|
2018-11-13 08:27:42 +00:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// projectFromDBX is used for creating Project entity from autogenerated dbx.Project struct
|
2019-06-04 12:55:38 +01:00
|
|
|
func projectFromDBX(ctx context.Context, project *dbx.Project) (_ *console.Project, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-06-06 17:07:14 +01:00
|
|
|
|
2018-11-13 08:27:42 +00:00
|
|
|
if project == nil {
|
|
|
|
return nil, errs.New("project parameter is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := bytesToUUID(project.Id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-01-15 13:03:24 +00:00
|
|
|
u := &console.Project{
|
2019-01-28 18:20:33 +00:00
|
|
|
ID: id,
|
|
|
|
Name: project.Name,
|
|
|
|
Description: project.Description,
|
|
|
|
CreatedAt: project.CreatedAt,
|
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
|
2019-06-04 12:55:38 +01:00
|
|
|
func projectsFromDbxSlice(ctx context.Context, projectsDbx []*dbx.Project) (_ []console.Project, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-06-06 17:07:14 +01:00
|
|
|
|
2019-01-15 13:03:24 +00:00
|
|
|
var projects []console.Project
|
2018-11-14 12:45:49 +00:00
|
|
|
var errors []error
|
|
|
|
|
|
|
|
// Generating []dbo from []dbx and collecting all errors
|
|
|
|
for _, projectDbx := range projectsDbx {
|
2019-06-04 12:55:38 +01:00
|
|
|
project, err := projectFromDBX(ctx, projectDbx)
|
2018-11-14 12:45:49 +00:00
|
|
|
if err != nil {
|
|
|
|
errors = append(errors, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
projects = append(projects, *project)
|
|
|
|
}
|
|
|
|
|
2019-03-29 12:30:23 +00:00
|
|
|
return projects, errs.Combine(errors...)
|
2018-11-14 12:45:49 +00:00
|
|
|
}
|