From a39f23411b32059efef0228984d8da5841d2ae97 Mon Sep 17 00:00:00 2001 From: hoernschen Date: Tue, 29 Sep 2020 09:44:35 +0200 Subject: [PATCH] Test DB-Connection, Add CRUD Methods --- entities/device/deviceController.go | 19 ++++++++++ entities/event/eventController.go | 25 +++++++------ entities/room/roomController.go | 19 ++++++++++ entities/transaction/transactionController.go | 19 ++++++++++ entities/user/userController.go | 19 ++++++++++ main.go | 15 +++++++- utils/databaseController.go | 36 ++++++++++++++++++- 7 files changed, 139 insertions(+), 13 deletions(-) diff --git a/entities/device/deviceController.go b/entities/device/deviceController.go index e69de29..6d85b83 100644 --- a/entities/device/deviceController.go +++ b/entities/device/deviceController.go @@ -0,0 +1,19 @@ +package device + +import "database/sql" + +func createDevice(db *sql.DB) { + +} + +func readDevice(db *sql.DB) { + +} + +func updateDevice(db *sql.DB) { + +} + +func deleteDevice(db *sql.DB) { + +} diff --git a/entities/event/eventController.go b/entities/event/eventController.go index 3e2fa8c..341d5f6 100644 --- a/entities/event/eventController.go +++ b/entities/event/eventController.go @@ -2,17 +2,20 @@ package event import ( "database/sql" - - _ "github.com/mattn/go-sqlite3" ) -func InitDB(filepath string) *sql.DB { - db, err := sql.Open("sqlite3", filepath) - if err != nil { - panic(err) - } - if db == nil { - panic("db nil") - } - return db +func createEvent(db *sql.DB) { + +} + +func readEvent(db *sql.DB) { + +} + +func updateEvent(db *sql.DB) { + +} + +func deleteEvent(db *sql.DB) { + } diff --git a/entities/room/roomController.go b/entities/room/roomController.go index e69de29..2439df8 100644 --- a/entities/room/roomController.go +++ b/entities/room/roomController.go @@ -0,0 +1,19 @@ +package room + +import "database/sql" + +func createRoom(db *sql.DB) { + +} + +func readRoom(db *sql.DB) { + +} + +func updateRoom(db *sql.DB) { + +} + +func deleteRoom(db *sql.DB) { + +} diff --git a/entities/transaction/transactionController.go b/entities/transaction/transactionController.go index e69de29..6dd2dc6 100644 --- a/entities/transaction/transactionController.go +++ b/entities/transaction/transactionController.go @@ -0,0 +1,19 @@ +package transaction + +import "database/sql" + +func createTransaction(db *sql.DB) { + +} + +func readTransaction(db *sql.DB) { + +} + +func updateTransaction(db *sql.DB) { + +} + +func deleteTransaction(db *sql.DB) { + +} diff --git a/entities/user/userController.go b/entities/user/userController.go index e69de29..8f17be8 100644 --- a/entities/user/userController.go +++ b/entities/user/userController.go @@ -0,0 +1,19 @@ +package user + +import "database/sql" + +func createUser(db *sql.DB) { + +} + +func readUser(db *sql.DB) { + +} + +func updateUser(db *sql.DB) { + +} + +func deleteUser(db *sql.DB) { + +} diff --git a/main.go b/main.go index 0d5586d..41638c8 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,8 @@ import ( "fmt" "log" "net/http" + "os" + "strconv" "nutfactory.org/Matrix/utils" "nutfactory.org/Matrix/utils/router" @@ -62,7 +64,6 @@ var routes = router.Routes{ } func Test(w http.ResponseWriter, r *http.Request) { - fmt.Printf("TEST") w.Header().Set("Content-Type", "application/json; charset=UTF-8") w.WriteHeader(http.StatusOK) if err := json.NewEncoder(w).Encode("Test"); err != nil { @@ -71,9 +72,21 @@ func Test(w http.ResponseWriter, r *http.Request) { } func main() { + // TODO: Remove later + os.Remove("sqlite.db") + db := utils.InitDB("sqlite.db") defer db.Close() + rows, _ := db.Query("SELECT id, firstname, lastname FROM people") + var id int + var firstname string + var lastname string + for rows.Next() { + rows.Scan(&id, &firstname, &lastname) + fmt.Println(strconv.Itoa(id) + ": " + firstname + " " + lastname) + } + router := router.NewRouter(routes) //router.PathPrefix("/").Handler(http.FileServer(http.Dir(htmlPath))) diff --git a/utils/databaseController.go b/utils/databaseController.go index dffd9b1..2a412ff 100644 --- a/utils/databaseController.go +++ b/utils/databaseController.go @@ -14,7 +14,41 @@ func InitDB(filepath string) *sql.DB { panic(err) } if db == nil { - panic("db nil") + panic("DB couldn't be initialized") } + + // TODO: Change to correct DB-Structure + statement, _ := db.Prepare("CREATE TABLE IF NOT EXISTS people (id INTEGER PRIMARY KEY, firstname TEXT, lastname TEXT)") + statement.Exec() + return db } + +/* +sqlStmt := fmt.Sprintf(`INSERT INTO data + (id, content) + VALUES + (?, ?)`) + +tx, err := db.Begin() +if err != nil { + log.Panic(err) +} + +stmt, err := tx.Prepare(sqlStmt) +if err != nil { + return err +} +defer stmt.Close() + +for i := 1; i < 10; i++ { + id := fmt.Sprintf("%d", i) + content := fmt.Sprintf("content #%d %s", i, shortuuid.New()) + log.Printf("Inserting %s: %s", id, content) + _, err := stmt.Exec(id, content) + if err != nil { + log.Panic(err) + } +} +tx.Commit() +*/