From bdc89bea14257299b3ec5e0c9907071946ce1648 Mon Sep 17 00:00:00 2001 From: Fedorov Vladimir Date: Sun, 13 Aug 2023 21:06:28 +0700 Subject: [PATCH] add db --- cmd/butler/main.go | 17 ++++++--- go.mod | 1 + go.sum | 2 ++ internal/modules/storage/interface.go | 1 + internal/modules/storage/postgres/postgres.go | 36 +++++++++++++++++++ 5 files changed, 53 insertions(+), 4 deletions(-) create mode 100644 internal/modules/storage/postgres/postgres.go diff --git a/cmd/butler/main.go b/cmd/butler/main.go index cddf030..0dbebd2 100644 --- a/cmd/butler/main.go +++ b/cmd/butler/main.go @@ -5,19 +5,28 @@ import ( "os" "git.3crabs.ru/VLADIMIR/butler/internal/modules/messenger/telegram" - "git.3crabs.ru/VLADIMIR/butler/internal/modules/storage" + "git.3crabs.ru/VLADIMIR/butler/internal/modules/storage/postgres" "git.3crabs.ru/VLADIMIR/butler/internal/services/bot" "git.3crabs.ru/VLADIMIR/butler/internal/services/bot/bot_all" "git.3crabs.ru/VLADIMIR/butler/internal/services/listener" ) func main() { - args := os.Args[1:] // tg token - messengerTelegram, err := telegram.NewMessengerTelegram(args[0]) + args := os.Args[1:] + + tgToken := args[0] + messengerTelegram, err := telegram.NewMessengerTelegram(tgToken) if err != nil { panic(err) } - var storage storage.IStorage // TODO: init + + urlConnect := args[1] + storage, err := postgres.NewStoragePostgres(urlConnect) + if err != nil { + panic(err) + } + defer storage.Close() + listenerService := listener.NewListener( messengerTelegram, []bot.IBot{ diff --git a/go.mod b/go.mod index 0213a0c..1bfe9f3 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( ) require ( + github.com/lib/pq v1.10.9 // indirect github.com/samber/lo v1.38.1 // indirect golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 // indirect ) diff --git a/go.sum b/go.sum index 64463b0..0a82542 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 h1:wG8n/XJQ07TmjbITcGiUaOtXxdrINDz1b0J1w0SzqDc= github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1/go.mod h1:A2S0CWkNylc2phvKXWBBdD3K0iGnDBGbzRpISP2zBl8= +github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw= +github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o= github.com/samber/lo v1.38.1 h1:j2XEAqXKb09Am4ebOg31SpvzUTTs6EN3VfgeLUhPdXM= github.com/samber/lo v1.38.1/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA= go.uber.org/mock v0.2.0 h1:TaP3xedm7JaAgScZO7tlvlKrqT0p7I6OsdGB5YNSMDU= diff --git a/internal/modules/storage/interface.go b/internal/modules/storage/interface.go index d794025..bf32db7 100644 --- a/internal/modules/storage/interface.go +++ b/internal/modules/storage/interface.go @@ -12,4 +12,5 @@ type User struct { type IStorage interface { UpsertUser(ctx context.Context, user User) error GetAllUsersByChatID(ctx context.Context, chatID string) ([]User, error) + Close() } diff --git a/internal/modules/storage/postgres/postgres.go b/internal/modules/storage/postgres/postgres.go new file mode 100644 index 0000000..af8a371 --- /dev/null +++ b/internal/modules/storage/postgres/postgres.go @@ -0,0 +1,36 @@ +package postgres + +import ( + "context" + "database/sql" + + _ "github.com/lib/pq" + + "git.3crabs.ru/VLADIMIR/butler/internal/modules/storage" +) + +type storagePostgres struct { + db *sql.DB +} + +func NewStoragePostgres(urlConnect string) (storage.IStorage, error) { + db, err := sql.Open("postgres", urlConnect) + if err != nil { + return nil, err + } + return &storagePostgres{db: db}, nil +} + +func (s *storagePostgres) GetAllUsersByChatID(ctx context.Context, chatID string) ([]storage.User, error) { + // TODO: imptement + return nil, nil +} + +func (s *storagePostgres) UpsertUser(ctx context.Context, user storage.User) error { + // TODO: imptement + return nil +} + +func (s *storagePostgres) Close() { + s.db.Close() +}