Test DB-Connection, Add CRUD Methods

This commit is contained in:
hoernschen 2020-09-29 09:44:35 +02:00
parent faee833481
commit a39f23411b
7 changed files with 139 additions and 13 deletions

View file

@ -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) {
}

View file

@ -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)
func createEvent(db *sql.DB) {
}
if db == nil {
panic("db nil")
func readEvent(db *sql.DB) {
}
return db
func updateEvent(db *sql.DB) {
}
func deleteEvent(db *sql.DB) {
}

View file

@ -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) {
}

View file

@ -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) {
}

View file

@ -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) {
}

15
main.go
View file

@ -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)))

View file

@ -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()
*/