From 59aad78f66cf58be7f88f2c0675f94a858163560 Mon Sep 17 00:00:00 2001 From: r Date: Tue, 17 Dec 2019 20:17:25 +0000 Subject: Use filesystem based kv store instead of sqlite --- repository/appRepository.go | 37 ++++++++-------------------------- repository/sessionRepository.go | 44 +++++++++++++++++++---------------------- 2 files changed, 28 insertions(+), 53 deletions(-) (limited to 'repository') diff --git a/repository/appRepository.go b/repository/appRepository.go index 1a8f204..00ef64d 100644 --- a/repository/appRepository.go +++ b/repository/appRepository.go @@ -1,54 +1,33 @@ package repository import ( - "database/sql" - + "web/kv" "web/model" ) type appRepository struct { - db *sql.DB + db *kv.Database } -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 - } - +func NewAppRepository(db *kv.Database) *appRepository { 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) + err = repo.db.Set(a.InstanceDomain, a.Marshal()) return } -func (repo *appRepository) Get(instanceURL string) (a model.App, err error) { - rows, err := repo.db.Query("SELECT * FROM app WHERE instance_url = ?", instanceURL) +func (repo *appRepository) Get(instanceDomain string) (a model.App, err error) { + data, err := repo.db.Get(instanceDomain) 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 - } + err = a.Unmarshal(instanceDomain, data) return } diff --git a/repository/sessionRepository.go b/repository/sessionRepository.go index 2a88b40..6c26313 100644 --- a/repository/sessionRepository.go +++ b/repository/sessionRepository.go @@ -1,54 +1,50 @@ package repository import ( - "database/sql" - + "web/kv" "web/model" ) type sessionRepository struct { - db *sql.DB + db *kv.Database } -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 - } - +func NewSessionRepository(db *kv.Database) *sessionRepository { 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) + err = repo.db.Set(s.ID, s.Marshal()) 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) +func (repo *sessionRepository) Update(id string, accessToken string) (err error) { + data, err := repo.db.Get(id) if err != nil { return } - defer rows.Close() - if !rows.Next() { - err = model.ErrSessionNotFound + var s model.Session + err = s.Unmarshal(id, data) + if err != nil { return } - err = rows.Scan(&s.ID, &s.InstanceURL, &s.AccessToken) + s.AccessToken = accessToken + + return repo.db.Set(id, s.Marshal()) +} + +func (repo *sessionRepository) Get(id string) (s model.Session, err error) { + data, err := repo.db.Get(id) if err != nil { + err = model.ErrSessionNotFound return } + err = s.Unmarshal(id, data) + return } -- cgit v1.2.3