add db
This commit is contained in:
parent
6ce7cee5bc
commit
fd88563096
|
@ -5,12 +5,12 @@ import "context"
|
||||||
//go:generate mockgen -source=$GOFILE -destination=mocks/$GOFILE -package=mocks
|
//go:generate mockgen -source=$GOFILE -destination=mocks/$GOFILE -package=mocks
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
ChatID string
|
ChatID string `sql:"chat_id"`
|
||||||
UserID string
|
UserID string `sql:"user_id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type IStorage interface {
|
type IStorage interface {
|
||||||
UpsertUser(ctx context.Context, user User) error
|
UpsertUser(ctx context.Context, user *User) error
|
||||||
GetAllUsersByChatID(ctx context.Context, chatID string) ([]User, error)
|
GetAllUsersByChatID(ctx context.Context, chatID string) ([]*User, error)
|
||||||
Close()
|
Close()
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,10 +48,10 @@ func (mr *MockIStorageMockRecorder) Close() *gomock.Call {
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAllUsersByChatID mocks base method.
|
// GetAllUsersByChatID mocks base method.
|
||||||
func (m *MockIStorage) GetAllUsersByChatID(ctx context.Context, chatID string) ([]storage.User, error) {
|
func (m *MockIStorage) GetAllUsersByChatID(ctx context.Context, chatID string) ([]*storage.User, error) {
|
||||||
m.ctrl.T.Helper()
|
m.ctrl.T.Helper()
|
||||||
ret := m.ctrl.Call(m, "GetAllUsersByChatID", ctx, chatID)
|
ret := m.ctrl.Call(m, "GetAllUsersByChatID", ctx, chatID)
|
||||||
ret0, _ := ret[0].([]storage.User)
|
ret0, _ := ret[0].([]*storage.User)
|
||||||
ret1, _ := ret[1].(error)
|
ret1, _ := ret[1].(error)
|
||||||
return ret0, ret1
|
return ret0, ret1
|
||||||
}
|
}
|
||||||
|
@ -63,7 +63,7 @@ func (mr *MockIStorageMockRecorder) GetAllUsersByChatID(ctx, chatID interface{})
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpsertUser mocks base method.
|
// UpsertUser mocks base method.
|
||||||
func (m *MockIStorage) UpsertUser(ctx context.Context, user storage.User) error {
|
func (m *MockIStorage) UpsertUser(ctx context.Context, user *storage.User) error {
|
||||||
m.ctrl.T.Helper()
|
m.ctrl.T.Helper()
|
||||||
ret := m.ctrl.Call(m, "UpsertUser", ctx, user)
|
ret := m.ctrl.Call(m, "UpsertUser", ctx, user)
|
||||||
ret0, _ := ret[0].(error)
|
ret0, _ := ret[0].(error)
|
||||||
|
|
|
@ -21,13 +21,36 @@ func NewStoragePostgres(urlConnect string) (storage.IStorage, error) {
|
||||||
return &storagePostgres{db: db}, nil
|
return &storagePostgres{db: db}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *storagePostgres) GetAllUsersByChatID(ctx context.Context, chatID string) ([]storage.User, error) {
|
func (s *storagePostgres) GetAllUsersByChatID(ctx context.Context, chatID string) ([]*storage.User, error) {
|
||||||
// TODO: imptement
|
rows, err := s.db.Query(`select chat_id, user_id from users where chat_id = $1`, chatID)
|
||||||
return nil, nil
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
var users []*storage.User
|
||||||
|
for rows.Next() {
|
||||||
|
user := &storage.User{}
|
||||||
|
err := rows.Scan(&user.ChatID, &user.UserID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
users = append(users, user)
|
||||||
|
}
|
||||||
|
err = rows.Err()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return users, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *storagePostgres) UpsertUser(ctx context.Context, user storage.User) error {
|
func (s *storagePostgres) UpsertUser(ctx context.Context, user *storage.User) error {
|
||||||
// TODO: imptement
|
stmt, err := s.db.Prepare(`insert into users (chat_id, user_id) select $1, $2 where not exists (select * FROM users WHERE chat_id = $1 and user_id = $2);`)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if _, err := stmt.Exec(user.ChatID, user.UserID); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,7 @@ package bot_all
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/samber/lo"
|
"github.com/samber/lo"
|
||||||
|
@ -27,21 +28,21 @@ func NewBotAll(
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bot *botAll) Process(ctx context.Context, msg *messenger.Message) error {
|
func (bot *botAll) Process(ctx context.Context, msg *messenger.Message) error {
|
||||||
if err := bot.storage.UpsertUser(ctx, storage.User{ChatID: msg.ChatID, UserID: msg.UserID}); err != nil {
|
if err := bot.storage.UpsertUser(ctx, &storage.User{ChatID: msg.ChatID, UserID: msg.UserID}); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if !strings.Contains(msg.Text, "@all") {
|
if !strings.Contains(msg.Text, "@all") && !strings.Contains(msg.Text, "@все") && strings.ToLower(msg.Text) != "ау" {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
users, err := bot.storage.GetAllUsersByChatID(ctx, msg.ChatID)
|
users, err := bot.storage.GetAllUsersByChatID(ctx, msg.ChatID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
usernames := lo.FilterMap(users, func(item storage.User, _ int) (string, bool) {
|
usernames := lo.FilterMap(users, func(item *storage.User, _ int) (string, bool) {
|
||||||
if item.UserID == msg.UserID {
|
if item.UserID == msg.UserID {
|
||||||
return "", false
|
return "", false
|
||||||
}
|
}
|
||||||
return item.UserID, true
|
return fmt.Sprintf("@%s", item.UserID), true
|
||||||
})
|
})
|
||||||
if len(usernames) > 0 {
|
if len(usernames) > 0 {
|
||||||
bot.messenger.SendMessage(
|
bot.messenger.SendMessage(
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
DROP TABLE IF EXISTS users;
|
||||||
|
CREATE TABLE users (
|
||||||
|
id SERIAL PRIMARY KEY,
|
||||||
|
chat_id TEXT NOT NULL,
|
||||||
|
user_id TEXT NOT NULL,
|
||||||
|
CONSTRAINT unique__chat_id__user_id UNIQUE (chat_id, user_id)
|
||||||
|
);
|
Loading…
Reference in New Issue