add database interactions
This commit is contained in:
parent
ee812a6c69
commit
86063b5a64
|
@ -6,7 +6,10 @@ import (
|
|||
"log"
|
||||
"net/http"
|
||||
|
||||
"git.xeserv.us/xena/gorqlite"
|
||||
"git.xeserv.us/xena/vyvanse/bot"
|
||||
"git.xeserv.us/xena/vyvanse/internal/dao"
|
||||
|
||||
"github.com/Xe/ln"
|
||||
"github.com/bwmarrin/discordgo"
|
||||
_ "github.com/joho/godotenv/autoload"
|
||||
|
@ -24,6 +27,7 @@ var (
|
|||
pesterChannel = flag.String("upload-channel", "", "Discord channel ID to upload and announce new XKCD items to")
|
||||
token = flag.String("token", "", "discord bot token")
|
||||
zipkinURL = flag.String("zipkin-url", "", "URL for Zipkin traces")
|
||||
databaseURL = flag.String("database-url", "http://", "URL for database (rqlite)")
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
@ -35,6 +39,28 @@ func main() {
|
|||
log.Fatal(err)
|
||||
}
|
||||
|
||||
db, err := gorqlite.Open(*databaseURL)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
us := dao.NewUsers(db)
|
||||
|
||||
type migrator interface {
|
||||
Migrate(ctx context.Context) error
|
||||
}
|
||||
mgs := []migrator{us}
|
||||
|
||||
sp, ctx := opentracing.StartSpan("migrations")
|
||||
for _, mg := range mgs {
|
||||
err := mg.Migrate(ctx)
|
||||
if err != nil {
|
||||
sp.Finish()
|
||||
ln.FatalErr(ctx, err)
|
||||
}
|
||||
}
|
||||
sp.Finish()
|
||||
|
||||
c := cron.New()
|
||||
|
||||
comic, err := xk.Latest()
|
||||
|
@ -111,6 +137,11 @@ func main() {
|
|||
if err != nil {
|
||||
ln.Error(context.Background(), err, ln.F{"action": "run commandSet on message"})
|
||||
}
|
||||
|
||||
_, err = us.Insert(context.Background(), m.Author.ID)
|
||||
if err != nil {
|
||||
ln.Error(context.Background(), err, ln.F{"action": "insert user into database"})
|
||||
}
|
||||
})
|
||||
|
||||
// Open the websocket and begin listening.
|
||||
|
|
|
@ -0,0 +1,62 @@
|
|||
package dao
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.xeserv.us/xena/gorqlite"
|
||||
"github.com/opentracing/opentracing-go"
|
||||
splog "github.com/opentracing/opentracing-go/log"
|
||||
)
|
||||
|
||||
type Users struct {
|
||||
conn gorqlite.Connection
|
||||
}
|
||||
|
||||
func NewUsers(conn gorqlite.Connection) *Users {
|
||||
return &Users{conn: conn}
|
||||
}
|
||||
|
||||
func (u *Users) Migrate(ctx context.Context) error {
|
||||
sp, ctx := opentracing.StartSpanFromContext(ctx, "users.migrate")
|
||||
defer sp.Finish()
|
||||
|
||||
migrationDDL := []string{
|
||||
`CREATE TABLE IF NOT EXISTS users(id INTEGER PRIMARY KEY, discord_id UNIQUE TEXT, score INTEGER)`,
|
||||
}
|
||||
|
||||
res, err := u.conn.Write(migrationDDL)
|
||||
if err != nil {
|
||||
sp.LogFields(splog.Error(err))
|
||||
return err
|
||||
}
|
||||
|
||||
for i, re := range res {
|
||||
if re.Err != nil {
|
||||
sp.LogFields(splog.Error(err))
|
||||
return err
|
||||
}
|
||||
|
||||
sp.LogFields(splog.Int("migration.step", i), splog.Float64("timing", re.Timing), splog.Int64("rows.affected", re.RowsAffected))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *Users) Insert(ctx context.Context, discordID string) (int64, error) {
|
||||
sp, ctx := opentracing.StartSpanFromContext(ctx, "users.insert")
|
||||
defer sp.Finish()
|
||||
sp.LogFields(splog.String("discord.id", discordID))
|
||||
|
||||
re, err := u.conn.WriteOne(gorqlite.NewPreparedStatement("INSERT INTO users (discord_id) VALUES (%s)").Bind(discordID))
|
||||
if err != nil {
|
||||
sp.LogFields(splog.Error(err))
|
||||
return -1, err
|
||||
}
|
||||
|
||||
if re.Err != nil {
|
||||
sp.LogFields(splog.Error(re.Err))
|
||||
return -1, err
|
||||
}
|
||||
|
||||
return re.LastInsertID, nil
|
||||
}
|
Loading…
Reference in New Issue