github.com/retailcrm/mg-bot-helper@v0.0.0-20201229112329-a17255681a84/src/repository.go (about)

     1  package main
     2  
     3  import (
     4  	"regexp"
     5  )
     6  
     7  var rx = regexp.MustCompile(`/+$`)
     8  
     9  func getConnection(uid string) *Connection {
    10  	var connection Connection
    11  	orm.DB.First(&connection, "client_id = ?", uid)
    12  
    13  	return &connection
    14  }
    15  
    16  func getConnectionByURL(urlCrm string) *Connection {
    17  	var connection Connection
    18  	orm.DB.First(&connection, "api_url = ?", urlCrm)
    19  
    20  	return &connection
    21  }
    22  
    23  func getActiveConnection() []*Connection {
    24  	var connection []*Connection
    25  	orm.DB.Find(&connection, "active = ?", true)
    26  
    27  	return connection
    28  }
    29  
    30  func (c *Connection) setConnectionActivity() error {
    31  	return orm.DB.Model(c).Where("client_id = ?", c.ClientID).Updates(map[string]interface{}{"active": c.Active, "api_url": c.APIURL}).Error
    32  }
    33  
    34  func (c *Connection) createConnection() error {
    35  	return orm.DB.Create(c).Error
    36  }
    37  
    38  func (c *Connection) saveConnection() error {
    39  	return orm.DB.Model(c).Where("client_id = ?", c.ClientID).Update(c).Error
    40  }
    41  
    42  func (c *Connection) NormalizeApiUrl() {
    43  	c.APIURL = rx.ReplaceAllString(c.APIURL, ``)
    44  }