github.com/leg100/ots@v0.0.7-0.20210919080622-034055ced4bd/sqlite/organization.go (about)

     1  package sqlite
     2  
     3  import (
     4  	"github.com/leg100/go-tfe"
     5  	"github.com/leg100/ots"
     6  	"gorm.io/gorm"
     7  	"gorm.io/gorm/clause"
     8  )
     9  
    10  var _ ots.OrganizationStore = (*OrganizationDB)(nil)
    11  
    12  type OrganizationDB struct {
    13  	*gorm.DB
    14  }
    15  
    16  func NewOrganizationDB(db *gorm.DB) *OrganizationDB {
    17  	return &OrganizationDB{
    18  		DB: db,
    19  	}
    20  }
    21  
    22  // Create persists a Organization to the DB.
    23  func (db OrganizationDB) Create(domain *ots.Organization) (*ots.Organization, error) {
    24  	model := &Organization{}
    25  	model.FromDomain(domain)
    26  
    27  	if result := db.DB.Create(model); result.Error != nil {
    28  		return nil, result.Error
    29  	}
    30  
    31  	return model.ToDomain(), nil
    32  }
    33  
    34  // Update persists an updated Organization to the DB. The existing org is
    35  // fetched from the DB, the supplied func is invoked on the org, and the updated
    36  // org is persisted back to the DB.
    37  func (db OrganizationDB) Update(name string, fn func(*ots.Organization) error) (*ots.Organization, error) {
    38  	var model *Organization
    39  
    40  	err := db.Transaction(func(tx *gorm.DB) (err error) {
    41  		// Get existing model obj from DB
    42  		model, err = getOrganizationByName(tx, name)
    43  		if err != nil {
    44  			return err
    45  		}
    46  
    47  		// Update domain obj using client-supplied fn
    48  		if err := model.Update(fn); err != nil {
    49  			return err
    50  		}
    51  
    52  		if result := tx.Save(model); result.Error != nil {
    53  			return err
    54  		}
    55  
    56  		return nil
    57  	})
    58  	if err != nil {
    59  		return nil, err
    60  	}
    61  
    62  	return model.ToDomain(), nil
    63  }
    64  
    65  func (db OrganizationDB) List(opts tfe.OrganizationListOptions) (*ots.OrganizationList, error) {
    66  	var count int64
    67  	var models OrganizationList
    68  
    69  	err := db.Transaction(func(tx *gorm.DB) error {
    70  		if result := tx.Model(models).Count(&count); result.Error != nil {
    71  			return result.Error
    72  		}
    73  
    74  		if result := tx.Scopes(paginate(opts.ListOptions)).Find(&models); result.Error != nil {
    75  			return result.Error
    76  		}
    77  
    78  		return nil
    79  	})
    80  	if err != nil {
    81  		return nil, err
    82  	}
    83  
    84  	return &ots.OrganizationList{
    85  		Items:      models.ToDomain(),
    86  		Pagination: ots.NewPagination(opts.ListOptions, int(count)),
    87  	}, nil
    88  }
    89  
    90  func (db OrganizationDB) Get(name string) (*ots.Organization, error) {
    91  	org, err := getOrganizationByName(db.DB, name)
    92  	if err != nil {
    93  		return nil, err
    94  	}
    95  	return org.ToDomain(), nil
    96  }
    97  
    98  func (db OrganizationDB) Delete(name string) error {
    99  	if result := db.Where("name = ?", name).Delete(&Organization{}); result.Error != nil {
   100  		return result.Error
   101  	}
   102  
   103  	return nil
   104  }
   105  
   106  func getOrganizationByName(db *gorm.DB, name string) (*Organization, error) {
   107  	var model Organization
   108  
   109  	if result := db.Preload(clause.Associations).Where("name = ?", name).First(&model); result.Error != nil {
   110  		return nil, result.Error
   111  	}
   112  
   113  	return &model, nil
   114  }