github.com/masterhung0112/hk_server/v5@v5.0.0-20220302090640-ec71aef15e1c/store/sqlstore/terms_of_service_store.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package sqlstore
     5  
     6  import (
     7  	"database/sql"
     8  
     9  	"github.com/pkg/errors"
    10  
    11  	"github.com/masterhung0112/hk_server/v5/einterfaces"
    12  	"github.com/masterhung0112/hk_server/v5/model"
    13  	"github.com/masterhung0112/hk_server/v5/store"
    14  )
    15  
    16  type SqlTermsOfServiceStore struct {
    17  	*SqlStore
    18  	metrics einterfaces.MetricsInterface
    19  }
    20  
    21  func newSqlTermsOfServiceStore(sqlStore *SqlStore, metrics einterfaces.MetricsInterface) store.TermsOfServiceStore {
    22  	s := SqlTermsOfServiceStore{sqlStore, metrics}
    23  
    24  	for _, db := range sqlStore.GetAllConns() {
    25  		table := db.AddTableWithName(model.TermsOfService{}, "TermsOfService").SetKeys(false, "Id")
    26  		table.ColMap("Id").SetMaxSize(26)
    27  		table.ColMap("UserId").SetMaxSize(26)
    28  		table.ColMap("Text").SetMaxSize(model.POST_MESSAGE_MAX_BYTES_V2)
    29  	}
    30  
    31  	return s
    32  }
    33  
    34  func (s SqlTermsOfServiceStore) createIndexesIfNotExists() {
    35  }
    36  
    37  func (s SqlTermsOfServiceStore) Save(termsOfService *model.TermsOfService) (*model.TermsOfService, error) {
    38  	if termsOfService.Id != "" {
    39  		return nil, store.NewErrInvalidInput("TermsOfService", "Id", termsOfService.Id)
    40  	}
    41  
    42  	termsOfService.PreSave()
    43  
    44  	if err := termsOfService.IsValid(); err != nil {
    45  		return nil, err
    46  	}
    47  
    48  	if err := s.GetMaster().Insert(termsOfService); err != nil {
    49  		return nil, errors.Wrapf(err, "could not save a new TermsOfService")
    50  	}
    51  
    52  	return termsOfService, nil
    53  }
    54  
    55  func (s SqlTermsOfServiceStore) GetLatest(allowFromCache bool) (*model.TermsOfService, error) {
    56  	var termsOfService *model.TermsOfService
    57  
    58  	query := s.getQueryBuilder().
    59  		Select("*").
    60  		From("TermsOfService").
    61  		OrderBy("CreateAt DESC").
    62  		Limit(uint64(1))
    63  
    64  	queryString, args, err := query.ToSql()
    65  	if err != nil {
    66  		return nil, errors.Wrap(err, "could not build sql query to get latest TOS")
    67  	}
    68  
    69  	if err := s.GetReplica().SelectOne(&termsOfService, queryString, args...); err != nil {
    70  		if err == sql.ErrNoRows {
    71  			return nil, store.NewErrNotFound("TermsOfService", "CreateAt=latest")
    72  		}
    73  		return nil, errors.Wrap(err, "could not find latest TermsOfService")
    74  	}
    75  
    76  	return termsOfService, nil
    77  }
    78  
    79  func (s SqlTermsOfServiceStore) Get(id string, allowFromCache bool) (*model.TermsOfService, error) {
    80  	obj, err := s.GetReplica().Get(model.TermsOfService{}, id)
    81  	if err != nil {
    82  		return nil, errors.Wrapf(err, "could not find TermsOfService with id=%s", id)
    83  	}
    84  	if obj == nil {
    85  		return nil, store.NewErrNotFound("TermsOfService", id)
    86  	}
    87  	return obj.(*model.TermsOfService), nil
    88  }