add db
This commit is contained in:
@@ -5,12 +5,12 @@ import "context"
|
||||
//go:generate mockgen -source=$GOFILE -destination=mocks/$GOFILE -package=mocks
|
||||
|
||||
type User struct {
|
||||
ChatID string
|
||||
UserID string
|
||||
ChatID string `sql:"chat_id"`
|
||||
UserID string `sql:"user_id"`
|
||||
}
|
||||
|
||||
type IStorage interface {
|
||||
UpsertUser(ctx context.Context, user User) error
|
||||
GetAllUsersByChatID(ctx context.Context, chatID string) ([]User, error)
|
||||
UpsertUser(ctx context.Context, user *User) error
|
||||
GetAllUsersByChatID(ctx context.Context, chatID string) ([]*User, error)
|
||||
Close()
|
||||
}
|
||||
|
||||
@@ -48,10 +48,10 @@ func (mr *MockIStorageMockRecorder) Close() *gomock.Call {
|
||||
}
|
||||
|
||||
// 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()
|
||||
ret := m.ctrl.Call(m, "GetAllUsersByChatID", ctx, chatID)
|
||||
ret0, _ := ret[0].([]storage.User)
|
||||
ret0, _ := ret[0].([]*storage.User)
|
||||
ret1, _ := ret[1].(error)
|
||||
return ret0, ret1
|
||||
}
|
||||
@@ -63,7 +63,7 @@ func (mr *MockIStorageMockRecorder) GetAllUsersByChatID(ctx, chatID interface{})
|
||||
}
|
||||
|
||||
// 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()
|
||||
ret := m.ctrl.Call(m, "UpsertUser", ctx, user)
|
||||
ret0, _ := ret[0].(error)
|
||||
|
||||
@@ -21,13 +21,36 @@ func NewStoragePostgres(urlConnect string) (storage.IStorage, error) {
|
||||
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) GetAllUsersByChatID(ctx context.Context, chatID string) ([]*storage.User, error) {
|
||||
rows, err := s.db.Query(`select chat_id, user_id from users where chat_id = $1`, chatID)
|
||||
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 {
|
||||
// TODO: imptement
|
||||
func (s *storagePostgres) UpsertUser(ctx context.Context, user *storage.User) error {
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user