github.com/vnforks/kid/v5@v5.22.1-0.20200408055009-b89d99c65676/store/sqlstore/user_terms_of_service.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/model"
    11  	"github.com/vnforks/kid/v5/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) (*model.UserTermsOfService, *model.AppError) {
    35  	var userTermsOfService *model.UserTermsOfService
    36  
    37  	err := s.GetReplica().SelectOne(&userTermsOfService, "SELECT * FROM UserTermsOfService WHERE UserId = :userId", map[string]interface{}{"userId": userId})
    38  	if err != nil {
    39  		if err == sql.ErrNoRows {
    40  			return nil, model.NewAppError("NewSqlUserTermsOfServiceStore.GetByUser", "store.sql_user_terms_of_service.get_by_user.no_rows.app_error", nil, "", http.StatusNotFound)
    41  		}
    42  		return nil, model.NewAppError("NewSqlUserTermsOfServiceStore.GetByUser", "store.sql_user_terms_of_service.get_by_user.app_error", nil, "", http.StatusInternalServerError)
    43  	}
    44  	return userTermsOfService, nil
    45  }
    46  
    47  func (s SqlUserTermsOfServiceStore) Save(userTermsOfService *model.UserTermsOfService) (*model.UserTermsOfService, *model.AppError) {
    48  	userTermsOfService.PreSave()
    49  
    50  	if err := userTermsOfService.IsValid(); err != nil {
    51  		return nil, err
    52  	}
    53  
    54  	c, err := s.GetMaster().Update(userTermsOfService)
    55  	if err != nil {
    56  		return nil, model.NewAppError("SqlUserTermsOfServiceStore.Save", "store.sql_user_terms_of_service.save.app_error", nil, "user_terms_of_service_user_id="+userTermsOfService.UserId+",user_terms_of_service_terms_of_service_id="+userTermsOfService.TermsOfServiceId+",err="+err.Error(), http.StatusInternalServerError)
    57  	}
    58  
    59  	if c == 0 {
    60  		if err := s.GetMaster().Insert(userTermsOfService); err != nil {
    61  			return nil, model.NewAppError("SqlUserTermsOfServiceStore.Save", "store.sql_user_terms_of_service.save.app_error", nil, "user_terms_of_service_user_id="+userTermsOfService.UserId+",user_terms_of_service_terms_of_service_id="+userTermsOfService.TermsOfServiceId+",err="+err.Error(), http.StatusInternalServerError)
    62  		}
    63  	}
    64  
    65  	return userTermsOfService, nil
    66  }
    67  
    68  func (s SqlUserTermsOfServiceStore) Delete(userId, termsOfServiceId string) *model.AppError {
    69  	if _, err := s.GetMaster().Exec("DELETE FROM UserTermsOfService WHERE UserId = :UserId AND TermsOfServiceId = :TermsOfServiceId", map[string]interface{}{"UserId": userId, "TermsOfServiceId": termsOfServiceId}); err != nil {
    70  		return model.NewAppError("SqlUserTermsOfServiceStore.Delete", "store.sql_user_terms_of_service.delete.app_error", nil, "userId="+userId+", termsOfServiceId="+termsOfServiceId, http.StatusInternalServerError)
    71  	}
    72  	return nil
    73  }