add db
This commit is contained in:
@@ -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