1
0
mirror of synced 2024-11-22 12:26:02 +03:00
mg-transport-telegram/repository.go

83 lines
1.7 KiB
Go
Raw Normal View History

package main
import "github.com/jinzhu/gorm"
2018-05-24 18:08:48 +03:00
func getConnection(uid string) *Connection {
var connection Connection
orm.DB.First(&connection, "client_id = ?", uid)
2018-05-24 18:08:48 +03:00
return &connection
}
2018-05-24 18:08:48 +03:00
func getConnectionByURL(urlCrm string) *Connection {
var connection Connection
orm.DB.First(&connection, "api_url = ?", urlCrm)
2018-05-24 18:08:48 +03:00
return &connection
}
func (c *Connection) setConnectionActivity() error {
return orm.DB.Model(c).Where("client_id = ?", c.ClientID).Update("Active", c.Active).Error
}
func (c *Connection) createConnection() error {
return orm.DB.Create(c).Error
}
func (c *Connection) saveConnection() error {
return orm.DB.Model(c).Where("client_id = ?", c.ClientID).Update(c).Error
}
2018-05-28 18:16:13 +03:00
func (c *Connection) createBot(b Bot) error {
return orm.DB.Model(c).Association("Bots").Append(&b).Error
}
func getConnectionByBotToken(token string) (*Connection, error) {
var c Connection
err := orm.DB.Where("active = ?", true).
Preload("Bots", "token = ?", token).
First(&c).Error
if gorm.IsRecordNotFoundError(err) {
return &c, nil
} else {
return &c, err
}
return &c, nil
}
2018-05-28 18:08:15 +03:00
func getBotByChannel(ch uint64) *Bot {
var bot Bot
orm.DB.First(&bot, "channel = ?", ch)
return &bot
}
func (b *Bot) setBotActivity() error {
return orm.DB.Model(b).Where("token = ?", b.Token).Update("Active", !b.Active).Error
}
func getBotChannelByToken(token string) uint64 {
var b Bot
orm.DB.First(&b, "token = ?", token)
return b.Channel
}
func (c Connection) getBotsByClientID() Bots {
var b Bots
err := orm.DB.Model(c).Association("Bots").Find(&b).Error
if err != nil {
logger.Error(err)
}
return b
2018-05-28 18:16:13 +03:00
}
func getConnectionById(id int) *Connection {
var connection Connection
orm.DB.First(&connection, "id = ?", id)
return &connection
}