github.com/vnforks/kid/v5@v5.22.1-0.20200408055009-b89d99c65676/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 "net/http" 9 10 "github.com/vnforks/kid/v5/einterfaces" 11 "github.com/vnforks/kid/v5/model" 12 "github.com/vnforks/kid/v5/store" 13 ) 14 15 type SqlTermsOfServiceStore struct { 16 SqlStore 17 metrics einterfaces.MetricsInterface 18 } 19 20 func newSqlTermsOfServiceStore(sqlStore SqlStore, metrics einterfaces.MetricsInterface) store.TermsOfServiceStore { 21 s := SqlTermsOfServiceStore{sqlStore, metrics} 22 23 for _, db := range sqlStore.GetAllConns() { 24 table := db.AddTableWithName(model.TermsOfService{}, "TermsOfService").SetKeys(false, "Id") 25 table.ColMap("Id").SetMaxSize(26) 26 table.ColMap("UserId").SetMaxSize(26) 27 table.ColMap("Text").SetMaxSize(65535) 28 } 29 30 return s 31 } 32 33 func (s SqlTermsOfServiceStore) createIndexesIfNotExists() { 34 } 35 36 func (s SqlTermsOfServiceStore) Save(termsOfService *model.TermsOfService) (*model.TermsOfService, *model.AppError) { 37 if len(termsOfService.Id) > 0 { 38 return nil, model.NewAppError("SqlTermsOfServiceStore.Save", "store.sql_terms_of_service_store.save.existing.app_error", nil, "id="+termsOfService.Id, http.StatusBadRequest) 39 } 40 41 termsOfService.PreSave() 42 43 if err := termsOfService.IsValid(); err != nil { 44 return nil, err 45 } 46 47 if err := s.GetMaster().Insert(termsOfService); err != nil { 48 return nil, model.NewAppError("SqlTermsOfServiceStore.Save", "store.sql_terms_of_service.save.app_error", nil, "terms_of_service_id="+termsOfService.Id+",err="+err.Error(), http.StatusInternalServerError) 49 } 50 51 return termsOfService, nil 52 } 53 54 func (s SqlTermsOfServiceStore) GetLatest(allowFromCache bool) (*model.TermsOfService, *model.AppError) { 55 var termsOfService *model.TermsOfService 56 57 err := s.GetReplica().SelectOne(&termsOfService, "SELECT * FROM TermsOfService ORDER BY CreateAt DESC LIMIT 1") 58 if err != nil { 59 if err == sql.ErrNoRows { 60 return nil, model.NewAppError("SqlTermsOfServiceStore.GetLatest", "store.sql_terms_of_service_store.get.no_rows.app_error", nil, "err="+err.Error(), http.StatusNotFound) 61 } 62 return nil, model.NewAppError("SqlTermsOfServiceStore.GetLatest", "store.sql_terms_of_service_store.get.app_error", nil, "err="+err.Error(), http.StatusInternalServerError) 63 } 64 65 return termsOfService, nil 66 } 67 68 func (s SqlTermsOfServiceStore) Get(id string, allowFromCache bool) (*model.TermsOfService, *model.AppError) { 69 obj, err := s.GetReplica().Get(model.TermsOfService{}, id) 70 if err != nil { 71 return nil, model.NewAppError("SqlTermsOfServiceStore.Get", "store.sql_terms_of_service_store.get.app_error", nil, "err="+err.Error(), http.StatusInternalServerError) 72 } 73 if obj == nil { 74 return nil, model.NewAppError("SqlTermsOfServiceStore.GetLatest", "store.sql_terms_of_service_store.get.no_rows.app_error", nil, "", http.StatusNotFound) 75 } 76 return obj.(*model.TermsOfService), nil 77 }