2024-08-15 16:25:15 +00:00
|
|
|
package db
|
|
|
|
|
2024-08-16 09:02:44 +00:00
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
2024-08-15 16:25:15 +00:00
|
|
|
|
2024-08-16 09:02:44 +00:00
|
|
|
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/config"
|
2024-10-15 17:35:14 +00:00
|
|
|
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/internal/db/authcode"
|
2024-09-22 08:26:27 +00:00
|
|
|
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/internal/db/authrequest"
|
2024-08-16 09:02:44 +00:00
|
|
|
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/internal/db/backend"
|
|
|
|
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/internal/db/client"
|
2024-10-18 20:06:05 +00:00
|
|
|
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/internal/db/user"
|
2024-08-16 09:02:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Storage interface {
|
|
|
|
DB() *sql.DB
|
|
|
|
ClientStorage() client.ClientDB
|
|
|
|
BackendStorage() backend.BackendDB
|
2024-09-22 08:26:27 +00:00
|
|
|
AuthRequestStorage() authrequest.AuthRequestDB
|
2024-10-15 17:35:14 +00:00
|
|
|
AuthCodeStorage() authcode.AuthCodeDB
|
2024-10-18 20:06:05 +00:00
|
|
|
UserStorage() user.UserDB
|
2024-08-16 09:02:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type sqlStorage struct {
|
|
|
|
db *sql.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sqlStorage) DB() *sql.DB {
|
|
|
|
return s.db
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sqlStorage) ClientStorage() client.ClientDB {
|
|
|
|
return client.New(s.db)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sqlStorage) BackendStorage() backend.BackendDB {
|
|
|
|
return backend.New(s.db)
|
|
|
|
}
|
|
|
|
|
2024-09-22 08:26:27 +00:00
|
|
|
func (s *sqlStorage) AuthRequestStorage() authrequest.AuthRequestDB {
|
|
|
|
return authrequest.New(s.db)
|
|
|
|
}
|
|
|
|
|
2024-10-15 17:35:14 +00:00
|
|
|
func (s *sqlStorage) AuthCodeStorage() authcode.AuthCodeDB {
|
|
|
|
return authcode.New(s.db)
|
|
|
|
}
|
|
|
|
|
2024-10-18 20:06:05 +00:00
|
|
|
func (s *sqlStorage) UserStorage() user.UserDB {
|
|
|
|
return user.New(s.db)
|
|
|
|
}
|
|
|
|
|
2024-08-16 09:02:44 +00:00
|
|
|
func New(conf config.AppConfig) (Storage, error) {
|
|
|
|
db, err := sql.Open("sqlite3", conf.StorageConfig.File)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to open DB: %w", err)
|
|
|
|
}
|
|
|
|
return &sqlStorage{db: db}, nil
|
|
|
|
}
|