github.com/xzl8028/xenia-server@v0.0.0-20190809101854-18450a97da63/store/sqlstore/license_store.go (about)

     1  // Copyright (c) 2016-present Xenia, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package sqlstore
     5  
     6  import (
     7  	"net/http"
     8  
     9  	"github.com/xzl8028/xenia-server/model"
    10  	"github.com/xzl8028/xenia-server/store"
    11  )
    12  
    13  type SqlLicenseStore struct {
    14  	SqlStore
    15  }
    16  
    17  func NewSqlLicenseStore(sqlStore SqlStore) store.LicenseStore {
    18  	ls := &SqlLicenseStore{sqlStore}
    19  
    20  	for _, db := range sqlStore.GetAllConns() {
    21  		table := db.AddTableWithName(model.LicenseRecord{}, "Licenses").SetKeys(false, "Id")
    22  		table.ColMap("Id").SetMaxSize(26)
    23  		table.ColMap("Bytes").SetMaxSize(10000)
    24  	}
    25  
    26  	return ls
    27  }
    28  
    29  func (ls SqlLicenseStore) CreateIndexesIfNotExists() {
    30  }
    31  
    32  func (ls SqlLicenseStore) Save(license *model.LicenseRecord) (*model.LicenseRecord, *model.AppError) {
    33  	license.PreSave()
    34  	if err := license.IsValid(); err != nil {
    35  		return nil, err
    36  	}
    37  
    38  	var storedLicense model.LicenseRecord
    39  	if err := ls.GetReplica().SelectOne(&storedLicense, "SELECT * FROM Licenses WHERE Id = :Id", map[string]interface{}{"Id": license.Id}); err != nil {
    40  		// Only insert if not exists
    41  		if err := ls.GetMaster().Insert(license); err != nil {
    42  			return nil, model.NewAppError("SqlLicenseStore.Save", "store.sql_license.save.app_error", nil, "license_id="+license.Id+", "+err.Error(), http.StatusInternalServerError)
    43  		}
    44  		return license, nil
    45  	}
    46  	return &storedLicense, nil
    47  }
    48  
    49  func (ls SqlLicenseStore) Get(id string) (*model.LicenseRecord, *model.AppError) {
    50  	obj, err := ls.GetReplica().Get(model.LicenseRecord{}, id)
    51  	if err != nil {
    52  		return nil, model.NewAppError("SqlLicenseStore.Get", "store.sql_license.get.app_error", nil, "license_id="+id+", "+err.Error(), http.StatusInternalServerError)
    53  	}
    54  	if obj == nil {
    55  		return nil, model.NewAppError("SqlLicenseStore.Get", "store.sql_license.get.missing.app_error", nil, "license_id="+id, http.StatusNotFound)
    56  	}
    57  	return obj.(*model.LicenseRecord), nil
    58  }