github.com/status-im/status-go@v1.1.0/services/local-notifications/database.go (about)

     1  package localnotifications
     2  
     3  import (
     4  	"database/sql"
     5  )
     6  
     7  type Database struct {
     8  	db      *sql.DB
     9  	network uint64
    10  }
    11  
    12  type NotificationPreference struct {
    13  	Enabled    bool   `json:"enabled"`
    14  	Service    string `json:"service"`
    15  	Event      string `json:"event,omitempty"`
    16  	Identifier string `json:"identifier,omitempty"`
    17  }
    18  
    19  func NewDB(db *sql.DB, network uint64) *Database {
    20  	return &Database{db: db, network: network}
    21  }
    22  
    23  func (db *Database) GetPreferences() (rst []NotificationPreference, err error) {
    24  	rows, err := db.db.Query("SELECT service, event, identifier, enabled FROM local_notifications_preferences")
    25  	if err != nil {
    26  		return nil, err
    27  	}
    28  	defer rows.Close()
    29  	for rows.Next() {
    30  		pref := NotificationPreference{}
    31  		err = rows.Scan(&pref.Service, &pref.Event, &pref.Identifier, &pref.Enabled)
    32  		if err != nil {
    33  			return nil, err
    34  		}
    35  		rst = append(rst, pref)
    36  	}
    37  	return rst, nil
    38  }
    39  
    40  func (db *Database) GetWalletPreference() (rst NotificationPreference, err error) {
    41  	pref := db.db.QueryRow("SELECT service, event, identifier, enabled FROM local_notifications_preferences WHERE service = 'wallet' AND event = 'transaction' AND identifier = 'all'")
    42  
    43  	err = pref.Scan(&rst.Service, &rst.Event, &rst.Identifier, &rst.Enabled)
    44  	if err == sql.ErrNoRows {
    45  		return rst, nil
    46  	}
    47  	return
    48  }
    49  
    50  func (db *Database) ChangeWalletPreference(preference bool) error {
    51  	_, err := db.db.Exec("INSERT OR REPLACE INTO local_notifications_preferences (service, event, identifier, enabled) VALUES ('wallet', 'transaction', 'all', ?)", preference)
    52  	return err
    53  }