add database models

This commit is contained in:
lyx0 2022-03-10 23:41:13 +01:00
parent 1ebf4df8e7
commit ddca49605c
5 changed files with 66 additions and 1 deletions

View file

@ -1,16 +1,22 @@
package main package main
import ( import (
"database/sql"
"log"
"time" "time"
"github.com/gempir/go-twitch-irc/v3" "github.com/gempir/go-twitch-irc/v3"
"github.com/lyx0/nourybot/internal/config" "github.com/lyx0/nourybot/internal/config"
"github.com/lyx0/nourybot/pkg/models/mysql"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
_ "github.com/go-sql-driver/mysql"
) )
type nourybot struct { type nourybot struct {
twitchClient *twitch.Client twitchClient *twitch.Client
uptime time.Time uptime time.Time
channels *mysql.ChannelModel
} }
func main() { func main() {
@ -20,9 +26,16 @@ func main() {
twitchClient := twitch.NewClient(cfg.Username, cfg.Oauth) twitchClient := twitch.NewClient(cfg.Username, cfg.Oauth)
now := time.Now() now := time.Now()
db, err := openDB(cfg.MySQLURI)
if err != nil {
log.Fatal(err)
}
defer db.Close()
nb := &nourybot{ nb := &nourybot{
twitchClient: twitchClient, twitchClient: twitchClient,
uptime: now, uptime: now,
channels: &mysql.ChannelModel{DB: db},
} }
nb.twitchClient.OnPrivateMessage(func(message twitch.PrivateMessage) { nb.twitchClient.OnPrivateMessage(func(message twitch.PrivateMessage) {
@ -31,9 +44,20 @@ func main() {
nb.twitchClient.OnConnect(nb.onConnect) nb.twitchClient.OnConnect(nb.onConnect)
err := nb.connect() err = nb.connect()
if err != nil { if err != nil {
panic(err) panic(err)
} }
} }
func openDB(dsn string) (*sql.DB, error) {
db, err := sql.Open("mysql", dsn)
if err != nil {
return nil, err
}
if err = db.Ping(); err != nil {
return nil, err
}
return db, nil
}

1
go.mod
View file

@ -4,6 +4,7 @@ go 1.17
require ( require (
github.com/gempir/go-twitch-irc/v3 v3.0.0 github.com/gempir/go-twitch-irc/v3 v3.0.0
github.com/go-sql-driver/mysql v1.6.0
github.com/joho/godotenv v1.4.0 github.com/joho/godotenv v1.4.0
github.com/sirupsen/logrus v1.8.1 github.com/sirupsen/logrus v1.8.1
) )

2
go.sum
View file

@ -3,6 +3,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gempir/go-twitch-irc/v3 v3.0.0 h1:e34R+9BdKy+qrO/wN+FCt+BUtyn38gCnJuKWscIKbl4= github.com/gempir/go-twitch-irc/v3 v3.0.0 h1:e34R+9BdKy+qrO/wN+FCt+BUtyn38gCnJuKWscIKbl4=
github.com/gempir/go-twitch-irc/v3 v3.0.0/go.mod h1:/W9KZIiyizVecp4PEb7kc4AlIyXKiCmvlXrzlpPUytU= github.com/gempir/go-twitch-irc/v3 v3.0.0/go.mod h1:/W9KZIiyizVecp4PEb7kc4AlIyXKiCmvlXrzlpPUytU=
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg= github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg=
github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=

15
pkg/models/models.go Normal file
View file

@ -0,0 +1,15 @@
package models
import (
"errors"
"time"
)
var ErrNoRecord = errors.New("models: no matching record found")
type Channel struct {
ID int
Username string
TwitchID string
Added time.Time
}

View file

@ -0,0 +1,23 @@
package mysql
import (
"database/sql"
"github.com/lyx0/nourybot/pkg/models"
)
type ChannelModel struct {
DB *sql.DB
}
func (m *ChannelModel) Insert(username string, twitchid string) (int, error) {
return 0, nil
}
func (m *ChannelModel) Get(id int) (*models.Channel, error) {
return nil, nil
}
func (m *ChannelModel) Latest() ([]*models.Channel, error) {
return nil, nil
}