Skip to content

Commit

Permalink
update: create sql handler
Browse files Browse the repository at this point in the history
  • Loading branch information
claustra01 committed Apr 7, 2024
1 parent 1456330 commit 111ecbc
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 25 deletions.
24 changes: 0 additions & 24 deletions bot/db/connect.go

This file was deleted.

22 changes: 22 additions & 0 deletions bot/db/postgres.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package db

import (
"database/sql"
"log"
"os"

_ "github.com/lib/pq"
)

func (db SqlHandler) Connect() {
conn, err := sql.Open("postgres", os.Getenv("POSTGRES_DATABASE_URL"))
if err != nil {
panic(err)
}
log.Println("Connected to database!")
db.Conn = conn
}

func (db SqlHandler) Close() {
db.Conn.Close()
}
20 changes: 20 additions & 0 deletions bot/db/sql.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package db

import "database/sql"

var DB SqlHandler

type SqlHandlerInterface interface {
Connect()
Close()
Query(string)
}

type SqlHandler struct {
SqlHandlerInterface
Conn *sql.DB
}

func (db SqlHandler) Query(query string) {
db.Conn.Query(query)
}
2 changes: 1 addition & 1 deletion bot/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func main() {
}

// Connect to database
db.Psql = db.Connect()
db.DB.Connect()

// Get channel secret and channel token from environment variables
channelSecret := os.Getenv("LINE_CHANNEL_SECRET")
Expand Down

0 comments on commit 111ecbc

Please sign in to comment.