github.com/teloshs/mattermost-server@v5.11.1+incompatible/store/sqlstore/user_terms_of_service.go (about)

     1  // Copyright (c) 2016-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/mattermost/mattermost-server/model"
    11  	"github.com/mattermost/mattermost-server/store"
    12  )
    13  
    14  type SqlUserTermsOfServiceStore struct {
    15  	SqlStore
    16  }
    17  
    18  func NewSqlUserTermsOfServiceStore(sqlStore SqlStore) store.UserTermsOfServiceStore {
    19  	s := SqlUserTermsOfServiceStore{sqlStore}
    20  
    21  	for _, db := range sqlStore.GetAllConns() {
    22  		table := db.AddTableWithName(model.UserTermsOfService{}, "UserTermsOfService").SetKeys(false, "UserId")
    23  		table.ColMap("UserId").SetMaxSize(26)
    24  		table.ColMap("TermsOfServiceId").SetMaxSize(26)
    25  	}
    26  
    27  	return s
    28  }
    29  
    30  func (s SqlUserTermsOfServiceStore) CreateIndexesIfNotExists() {
    31  	s.CreateIndexIfNotExists("idx_user_terms_of_service_user_id", "UserTermsOfService", "UserId")
    32  }
    33  
    34  func (s SqlUserTermsOfServiceStore) GetByUser(userId string) store.StoreChannel {
    35  	return store.Do(func(result *store.StoreResult) {
    36  		var userTermsOfService *model.UserTermsOfService
    37  
    38  		err := s.GetReplica().SelectOne(&userTermsOfService, "SELECT * FROM UserTermsOfService WHERE UserId = :userId", map[string]interface{}{"userId": userId})
    39  		if err != nil {
    40  			if err == sql.ErrNoRows {
    41  				result.Err = model.NewAppError("NewSqlUserTermsOfServiceStore.GetByUser", "store.sql_user_terms_of_service.get_by_user.no_rows.app_error", nil, "", http.StatusNotFound)
    42  			} else {
    43  				result.Err = model.NewAppError("NewSqlUserTermsOfServiceStore.GetByUser", "store.sql_user_terms_of_service.get_by_user.app_error", nil, "", http.StatusInternalServerError)
    44  			}
    45  		} else {
    46  			result.Data = userTermsOfService
    47  		}
    48  	})
    49  }
    50  
    51  func (s SqlUserTermsOfServiceStore) Save(userTermsOfService *model.UserTermsOfService) store.StoreChannel {
    52  	return store.Do(func(result *store.StoreResult) {
    53  		userTermsOfService.PreSave()
    54  
    55  		if result.Err = userTermsOfService.IsValid(); result.Err != nil {
    56  			return
    57  		}
    58  
    59  		if c, err := s.GetMaster().Update(userTermsOfService); err != nil {
    60  			result.Err = model.NewAppError(
    61  				"SqlUserTermsOfServiceStore.Save",
    62  				"store.sql_user_terms_of_service.save.app_error",
    63  				nil,
    64  				"user_terms_of_service_user_id="+userTermsOfService.UserId+",user_terms_of_service_terms_of_service_id="+userTermsOfService.TermsOfServiceId+",err="+err.Error(),
    65  				http.StatusInternalServerError,
    66  			)
    67  		} else if c == 0 {
    68  			if err := s.GetMaster().Insert(userTermsOfService); err != nil {
    69  				result.Err = model.NewAppError(
    70  					"SqlUserTermsOfServiceStore.Save",
    71  					"store.sql_user_terms_of_service.save.app_error",
    72  					nil,
    73  					"user_terms_of_service_user_id="+userTermsOfService.UserId+",user_terms_of_service_terms_of_service_id="+userTermsOfService.TermsOfServiceId+",err="+err.Error(),
    74  					http.StatusInternalServerError,
    75  				)
    76  			}
    77  		}
    78  
    79  		result.Data = userTermsOfService
    80  	})
    81  }
    82  
    83  func (s SqlUserTermsOfServiceStore) Delete(userId, termsOfServiceId string) store.StoreChannel {
    84  	return store.Do(func(result *store.StoreResult) {
    85  		if _, err := s.GetMaster().Exec("DELETE FROM UserTermsOfService WHERE UserId = :UserId AND TermsOfServiceId = :TermsOfServiceId", map[string]interface{}{"UserId": userId, "TermsOfServiceId": termsOfServiceId}); err != nil {
    86  			result.Err = model.NewAppError("SqlUserTermsOfServiceStore.Delete", "store.sql_user_terms_of_service.delete.app_error", nil, "userId="+userId+", termsOfServiceId="+termsOfServiceId, http.StatusInternalServerError)
    87  			return
    88  		}
    89  	})
    90  }