Remove unnecessary connection chain step

This commit is contained in:
lash 2025-01-04 21:02:08 +00:00
parent 3a9f3fa373
commit cc9760125a
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
10 changed files with 11 additions and 35 deletions

View File

@ -8,6 +8,7 @@ AT_ENDPOINT=/ussd/africastalking
#PostgreSQL #PostgreSQL
DB_CONN=postgres://postgres:strongpass@localhost:5432/urdt_ussd DB_CONN=postgres://postgres:strongpass@localhost:5432/urdt_ussd
#DB_TIMEZONE=Africa/Nairobi #DB_TIMEZONE=Africa/Nairobi
#DB_SCHEMA=vise
#External API Calls #External API Calls
CUSTODIAL_URL_BASE=http://localhost:5003 CUSTODIAL_URL_BASE=http://localhost:5003

View File

@ -80,8 +80,7 @@ func main() {
cfg.EngineDebug = true cfg.EngineDebug = true
} }
menuStorageService := storage.NewMenuStorageService(resourceDir) menuStorageService := storage.NewMenuStorageService(connData, resourceDir)
menuStorageService = menuStorageService.WithConn(connData)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, err.Error()) fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1) os.Exit(1)

View File

@ -91,8 +91,7 @@ func main() {
cfg.EngineDebug = true cfg.EngineDebug = true
} }
menuStorageService := storage.NewMenuStorageService(resourceDir) menuStorageService := storage.NewMenuStorageService(connData, resourceDir)
menuStorageService = menuStorageService.WithConn(connData)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, err.Error()) fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1) os.Exit(1)

View File

@ -79,8 +79,7 @@ func main() {
cfg.EngineDebug = true cfg.EngineDebug = true
} }
menuStorageService := storage.NewMenuStorageService(resourceDir) menuStorageService := storage.NewMenuStorageService(connData, resourceDir)
menuStorageService = menuStorageService.WithConn(connData)
rs, err := menuStorageService.GetResource(ctx) rs, err := menuStorageService.GetResource(ctx)
if err != nil { if err != nil {

View File

@ -68,8 +68,7 @@ func main() {
} }
resourceDir := scriptDir resourceDir := scriptDir
menuStorageService := storage.NewMenuStorageService(resourceDir) menuStorageService := storage.NewMenuStorageService(connData, resourceDir)
menuStorageService = menuStorageService.WithConn(connData)
rs, err := menuStorageService.GetResource(ctx) rs, err := menuStorageService.GetResource(ctx)
if err != nil { if err != nil {

View File

@ -32,9 +32,8 @@ type StorageService struct {
func NewStorageService(conn storage.ConnData) (*StorageService, error) { func NewStorageService(conn storage.ConnData) (*StorageService, error) {
svc := &StorageService{ svc := &StorageService{
svc: storage.NewMenuStorageService(""), svc: storage.NewMenuStorageService(conn, ""),
} }
svc.SetConn(conn)
return svc, nil return svc, nil
} }
@ -49,7 +48,3 @@ func(ss *StorageService) GetUserdataDb(ctx context.Context) (db.Db, error) {
func(ss *StorageService) GetResource(ctx context.Context) (resource.Resource, error) { func(ss *StorageService) GetResource(ctx context.Context) (resource.Resource, error) {
return nil, errors.New("not implemented") return nil, errors.New("not implemented")
} }
func(ss *StorageService) SetConn(conn storage.ConnData) {
ss.svc = ss.svc.WithConn(conn)
}

View File

@ -55,8 +55,7 @@ func main() {
ctx = context.WithValue(ctx, "Database", database) ctx = context.WithValue(ctx, "Database", database)
resourceDir := scriptDir resourceDir := scriptDir
menuStorageService := storage.NewMenuStorageService(resourceDir) menuStorageService := storage.NewMenuStorageService(connData, resourceDir)
menuStorageService = menuStorageService.WithConn(connData)
store, err := menuStorageService.GetUserdataDb(ctx) store, err := menuStorageService.GetUserdataDb(ctx)
if err != nil { if err != nil {

View File

@ -53,12 +53,7 @@ func main() {
ctx = context.WithValue(ctx, "Database", database) ctx = context.WithValue(ctx, "Database", database)
resourceDir := scriptDir resourceDir := scriptDir
menuStorageService := storage.NewMenuStorageService(resourceDir) menuStorageService := storage.NewMenuStorageService(connData, resourceDir)
menuStorageService = menuStorageService.WithConn(connData)
if err != nil {
fmt.Fprintf(os.Stderr, "connection string error: %v", err)
os.Exit(1)
}
store, err := menuStorageService.GetUserdataDb(ctx) store, err := menuStorageService.GetUserdataDb(ctx)
if err != nil { if err != nil {

View File

@ -34,17 +34,13 @@ type MenuStorageService struct {
userDataStore db.Db userDataStore db.Db
} }
func NewMenuStorageService(resourceDir string) *MenuStorageService { func NewMenuStorageService(conn ConnData, resourceDir string) *MenuStorageService {
return &MenuStorageService{ return &MenuStorageService{
conn: conn,
resourceDir: resourceDir, resourceDir: resourceDir,
} }
} }
func (ms *MenuStorageService) WithConn(conn ConnData) *MenuStorageService {
ms.conn = conn
return ms
}
func (ms *MenuStorageService) getOrCreateDb(ctx context.Context, existingDb db.Db, section string) (db.Db, error) { func (ms *MenuStorageService) getOrCreateDb(ctx context.Context, existingDb db.Db, section string) (db.Db, error) {
var newDb db.Db var newDb db.Db
var err error var err error

View File

@ -50,13 +50,7 @@ func TestEngine(sessionId string) (engine.Engine, func(), chan bool) {
os.Exit(1) os.Exit(1)
} }
resourceDir := scriptDir resourceDir := scriptDir
menuStorageService := storage.NewMenuStorageService(resourceDir) menuStorageService := storage.NewMenuStorageService(conn, resourceDir)
menuStorageService.WithConn(conn)
if err != nil {
fmt.Fprintf(os.Stderr, "conn error: %v", err)
os.Exit(1)
}
rs, err := menuStorageService.GetResource(ctx) rs, err := menuStorageService.GetResource(ctx)
if err != nil { if err != nil {