storj/pkg/satellite/satellitedb/companies.go

130 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/zeebo/errs"
"github.com/skyrings/skyring-common/tools/uuid"
"storj.io/storj/pkg/satellite"
"storj.io/storj/pkg/satellite/satellitedb/dbx"
)
// implementation of Companies interface repository using spacemonkeygo/dbx orm
type companies struct {
db *dbx.DB
}
// Get is a method for querying company from the database by id
func (companies *companies) Get(ctx context.Context, id uuid.UUID) (*satellite.Company, error) {
company, err := companies.db.Get_Company_By_Id(ctx, dbx.Company_Id([]byte(id.String())))
if err != nil {
return nil, err
}
return companyFromDBX(company)
}
// Get is a method for querying company from the database by user id
func (companies *companies) GetByUserID(ctx context.Context, userID uuid.UUID) (*satellite.Company, error) {
company, err := companies.db.Get_Company_By_UserId(ctx, dbx.Company_UserId([]byte(userID.String())))
if err != nil {
return nil, err
}
return companyFromDBX(company)
}
// Insert is a method for inserting company into the database
func (companies *companies) Insert(ctx context.Context, company *satellite.Company) (*satellite.Company, error) {
companyID, err := uuid.New()
if err != nil {
return nil, err
}
createdCompany, err := companies.db.Create_Company(
ctx,
dbx.Company_Id([]byte(companyID.String())),
dbx.Company_UserId([]byte(company.UserID.String())),
dbx.Company_Name(company.Name),
dbx.Company_Address(company.Address),
dbx.Company_Country(company.Country),
dbx.Company_City(company.City),
dbx.Company_State(company.State),
dbx.Company_PostalCode(company.PostalCode))
if err != nil {
return nil, err
}
return companyFromDBX(createdCompany)
}
// Delete is a method for deleting company by Id from the database.
func (companies *companies) Delete(ctx context.Context, id uuid.UUID) error {
_, err := companies.db.Delete_Company_By_Id(ctx, dbx.Company_Id([]byte(id.String())))
return err
}
// Update is a method for updating company entity
func (companies *companies) Update(ctx context.Context, company *satellite.Company) error {
_, err := companies.db.Update_Company_By_Id(
ctx,
dbx.Company_Id([]byte(company.ID.String())),
*getCompanyUpdateFields(company))
return err
}
// companyFromDBX is used for creating Company entity from autogenerated dbx.Company struct
func companyFromDBX(company *dbx.Company) (*satellite.Company, error) {
if company == nil {
return nil, errs.New("company parameter is nil")
}
id, err := uuid.Parse(string(company.Id))
if err != nil {
return nil, errs.New("Id in not valid UUID string")
}
userID, err := uuid.Parse(string(company.UserId))
if err != nil {
return nil, errs.New("UserID in not valid UUID string")
}
comp := &satellite.Company{
ID: *id,
UserID: *userID,
Name: company.Name,
Address: company.Address,
Country: company.Country,
City: company.City,
State: company.State,
PostalCode: company.PostalCode,
CreatedAt: company.CreatedAt,
}
return comp, nil
}
// getCompanyUpdateFields is used to generate company update fields
func getCompanyUpdateFields(company *satellite.Company) *dbx.Company_Update_Fields {
return &dbx.Company_Update_Fields{
Name: dbx.Company_Name(company.Name),
Address: dbx.Company_Address(company.Address),
Country: dbx.Company_Country(company.Country),
City: dbx.Company_City(company.City),
State: dbx.Company_State(company.State),
PostalCode: dbx.Company_PostalCode(company.PostalCode),
}
}