2018-11-09 12:05:24 +00:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package satellitedb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/skyrings/skyring-common/tools/uuid"
|
2018-11-30 13:40:13 +00:00
|
|
|
"github.com/zeebo/errs"
|
2018-11-29 18:39:27 +00:00
|
|
|
|
2018-11-09 12:05:24 +00:00
|
|
|
"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 user id
|
|
|
|
func (companies *companies) GetByUserID(ctx context.Context, userID uuid.UUID) (*satellite.Company, error) {
|
2018-11-12 09:14:16 +00:00
|
|
|
company, err := companies.db.Get_Company_By_UserId(ctx, dbx.Company_UserId(userID[:]))
|
2018-11-09 12:05:24 +00:00
|
|
|
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) {
|
|
|
|
createdCompany, err := companies.db.Create_Company(
|
|
|
|
ctx,
|
2018-11-12 09:14:16 +00:00
|
|
|
dbx.Company_UserId(company.UserID[:]),
|
2018-11-09 12:05:24 +00:00
|
|
|
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.
|
2018-11-26 14:57:55 +00:00
|
|
|
func (companies *companies) Delete(ctx context.Context, userID uuid.UUID) error {
|
|
|
|
_, err := companies.db.Delete_Company_By_UserId(ctx, dbx.Company_UserId(userID[:]))
|
2018-11-09 12:05:24 +00:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update is a method for updating company entity
|
|
|
|
func (companies *companies) Update(ctx context.Context, company *satellite.Company) error {
|
2018-11-26 14:57:55 +00:00
|
|
|
_, err := companies.db.Update_Company_By_UserId(
|
2018-11-09 12:05:24 +00:00
|
|
|
ctx,
|
2018-11-26 14:57:55 +00:00
|
|
|
dbx.Company_UserId(company.UserID[:]),
|
2018-11-28 12:30:38 +00:00
|
|
|
getCompanyUpdateFields(company))
|
2018-11-09 12:05:24 +00:00
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2018-11-12 09:14:16 +00:00
|
|
|
userID, err := bytesToUUID(company.UserId)
|
2018-11-09 12:05:24 +00:00
|
|
|
if err != nil {
|
2018-11-12 09:14:16 +00:00
|
|
|
return nil, err
|
2018-11-09 12:05:24 +00:00
|
|
|
}
|
|
|
|
|
2018-11-20 14:50:47 +00:00
|
|
|
return &satellite.Company{
|
2018-11-12 09:14:16 +00:00
|
|
|
UserID: userID,
|
2018-11-09 12:05:24 +00:00
|
|
|
Name: company.Name,
|
|
|
|
Address: company.Address,
|
|
|
|
Country: company.Country,
|
|
|
|
City: company.City,
|
|
|
|
State: company.State,
|
|
|
|
PostalCode: company.PostalCode,
|
|
|
|
CreatedAt: company.CreatedAt,
|
2018-11-20 14:50:47 +00:00
|
|
|
}, nil
|
2018-11-09 12:05:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// getCompanyUpdateFields is used to generate company update fields
|
2018-11-28 12:30:38 +00:00
|
|
|
func getCompanyUpdateFields(company *satellite.Company) dbx.Company_Update_Fields {
|
|
|
|
return dbx.Company_Update_Fields{
|
2018-11-09 12:05:24 +00:00
|
|
|
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),
|
|
|
|
}
|
|
|
|
}
|