store messages

This commit is contained in:
Rogee
2024-09-02 18:47:12 +08:00
parent 98cd1a0365
commit 32b53c0b18
3 changed files with 26 additions and 11 deletions

View File

@@ -14,6 +14,7 @@ type Config struct {
BotToken string `mapstructure:"bot_token"` BotToken string `mapstructure:"bot_token"`
SessionFile string `mapstructure:"session_file"` SessionFile string `mapstructure:"session_file"`
LogFile string `mapstructure:"log_file"` LogFile string `mapstructure:"log_file"`
DSN string `mapstructure:"dsn"`
} }
func Load(path string) error { func Load(path string) error {

View File

@@ -2,7 +2,6 @@ package internal
import ( import (
"context" "context"
"database/sql"
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
@@ -12,6 +11,8 @@ import (
"exporter/database/telegram_resource/public/table" "exporter/database/telegram_resource/public/table"
. "github.com/go-jet/jet/v2/postgres" . "github.com/go-jet/jet/v2/postgres"
"github.com/go-jet/jet/v2/qrm"
"github.com/lib/pq"
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/samber/lo" "github.com/samber/lo"
) )
@@ -58,13 +59,13 @@ func (c *DBChannel) Asset(assetID int64, ext string) string {
func (c *DBChannel) Get(ctx context.Context) error { func (c *DBChannel) Get(ctx context.Context) error {
tbl := table.Channels tbl := table.Channels
var m *model.Channels var m model.Channels
err := tbl.SELECT(tbl.AllColumns).QueryContext(ctx, db, &m) err := tbl.SELECT(tbl.AllColumns).WHERE(tbl.UUID.EQ(Int64(c.UUID))).QueryContext(ctx, db, &m)
if err != nil { if err != nil {
if errors.Is(err, sql.ErrNoRows) { if errors.Is(err, qrm.ErrNoRows) {
// create new channel with default value // create new channel with default value
m = &model.Channels{ m = model.Channels{
UUID: c.UUID, UUID: c.UUID,
Username: c.Username, Username: c.Username,
Title: c.Title, Title: c.Title,
@@ -74,7 +75,7 @@ func (c *DBChannel) Get(ctx context.Context) error {
UpdatedAt: lo.ToPtr(time.Now()), UpdatedAt: lo.ToPtr(time.Now()),
} }
if _, err := tbl.INSERT(tbl.AllColumns).MODEL(m).ExecContext(ctx, db); err != nil { if _, err := tbl.INSERT(tbl.AllColumns.Except(tbl.ID)).MODEL(m).ExecContext(ctx, db); err != nil {
return errors.Wrap(err, "insert channel") return errors.Wrap(err, "insert channel")
} }
} else { } else {
@@ -95,10 +96,13 @@ func (c *DBChannel) Update(ctx context.Context, offsetID int) error {
} }
tbl := table.Channels tbl := table.Channels
_, err := tbl.UPDATE().SET( _, err := tbl.UPDATE().
tbl.Offset.SET(Int(int64(c.Offset))), SET(
tbl.MinID.SET(Int(int64(c.MinID))), tbl.Offset.SET(Int64(int64(c.Offset))),
).ExecContext(ctx, db) tbl.MinID.SET(Int64(int64(c.MinID))),
).
WHERE(tbl.UUID.EQ(Int64(c.UUID))).
ExecContext(ctx, db)
if err != nil { if err != nil {
return errors.Wrap(err, "update channel") return errors.Wrap(err, "update channel")
} }
@@ -118,8 +122,13 @@ func (c *DBChannel) SaveMessage(ctx context.Context, msg *ChannelMessage) error
tbl := table.ChannelMessages tbl := table.ChannelMessages
_, err := tbl.INSERT(tbl.AllColumns).MODEL(message).ExecContext(ctx, db) _, err := tbl.INSERT(tbl.AllColumns.Except(tbl.ID)).MODEL(message).ExecContext(ctx, db)
if err != nil { if err != nil {
if e, ok := err.(*pq.Error); ok {
if e.Code == "23505" {
return nil
}
}
return errors.Wrap(err, "insert message") return errors.Wrap(err, "insert message")
} }
return nil return nil

View File

@@ -23,6 +23,11 @@ func main() {
return errors.Wrap(err, "load config") return errors.Wrap(err, "load config")
} }
fmt.Println("init db")
if err := internal.InitDB(config.C.DSN); err != nil {
return errors.Wrap(err, "init db")
}
fmt.Println("init client") fmt.Println("init client")
defer fmt.Println("init client done") defer fmt.Println("init client done")
return internal.InitClient(config.C) return internal.InitClient(config.C)