diff options
author | r <r@freesoftwareextremist.com> | 2019-12-13 18:08:26 +0000 |
---|---|---|
committer | r <r@freesoftwareextremist.com> | 2019-12-13 18:26:24 +0000 |
commit | 5e4da01c3ae3ae2e870faba9085d9d9213c01c29 (patch) | |
tree | 39d6f1e76b901549f194ddbac3c6cb82e0abd019 /repository | |
download | bloat-5e4da01c3ae3ae2e870faba9085d9d9213c01c29.tar.gz bloat-5e4da01c3ae3ae2e870faba9085d9d9213c01c29.zip |
Initial commit
Diffstat (limited to 'repository')
-rw-r--r-- | repository/appRepository.go | 54 | ||||
-rw-r--r-- | repository/sessionRepository.go | 54 |
2 files changed, 108 insertions, 0 deletions
diff --git a/repository/appRepository.go b/repository/appRepository.go new file mode 100644 index 0000000..1a8f204 --- /dev/null +++ b/repository/appRepository.go @@ -0,0 +1,54 @@ +package repository + +import ( + "database/sql" + + "web/model" +) + +type appRepository struct { + db *sql.DB +} + +func NewAppRepository(db *sql.DB) (*appRepository, error) { + _, err := db.Exec(`CREATE TABLE IF NOT EXISTS app + (instance_url varchar, client_id varchar, client_secret varchar)`, + ) + if err != nil { + return nil, err + } + + return &appRepository{ + db: db, + }, nil +} + +func (repo *appRepository) Add(a model.App) (err error) { + _, err = repo.db.Exec("INSERT INTO app VALUES (?, ?, ?)", a.InstanceURL, a.ClientID, a.ClientSecret) + return +} + +func (repo *appRepository) Update(instanceURL string, clientID string, clientSecret string) (err error) { + _, err = repo.db.Exec("UPDATE app SET client_id = ?, client_secret = ? where instance_url = ?", clientID, clientSecret, instanceURL) + return +} + +func (repo *appRepository) Get(instanceURL string) (a model.App, err error) { + rows, err := repo.db.Query("SELECT * FROM app WHERE instance_url = ?", instanceURL) + if err != nil { + return + } + defer rows.Close() + + if !rows.Next() { + err = model.ErrAppNotFound + return + } + + err = rows.Scan(&a.InstanceURL, &a.ClientID, &a.ClientSecret) + if err != nil { + return + } + + return +} diff --git a/repository/sessionRepository.go b/repository/sessionRepository.go new file mode 100644 index 0000000..2a88b40 --- /dev/null +++ b/repository/sessionRepository.go @@ -0,0 +1,54 @@ +package repository + +import ( + "database/sql" + + "web/model" +) + +type sessionRepository struct { + db *sql.DB +} + +func NewSessionRepository(db *sql.DB) (*sessionRepository, error) { + _, err := db.Exec(`CREATE TABLE IF NOT EXISTS session + (id varchar, instance_url varchar, access_token varchar)`, + ) + if err != nil { + return nil, err + } + + return &sessionRepository{ + db: db, + }, nil +} + +func (repo *sessionRepository) Add(s model.Session) (err error) { + _, err = repo.db.Exec("INSERT INTO session VALUES (?, ?, ?)", s.ID, s.InstanceURL, s.AccessToken) + return +} + +func (repo *sessionRepository) Update(sessionID string, accessToken string) (err error) { + _, err = repo.db.Exec("UPDATE session SET access_token = ? where id = ?", accessToken, sessionID) + return +} + +func (repo *sessionRepository) Get(id string) (s model.Session, err error) { + rows, err := repo.db.Query("SELECT * FROM session WHERE id = ?", id) + if err != nil { + return + } + defer rows.Close() + + if !rows.Next() { + err = model.ErrSessionNotFound + return + } + + err = rows.Scan(&s.ID, &s.InstanceURL, &s.AccessToken) + if err != nil { + return + } + + return +} |