147 lines
2.5 KiB
Go
147 lines
2.5 KiB
Go
package database
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
"log"
|
|
|
|
"nutfactory.org/Matrix/entities/user"
|
|
)
|
|
|
|
func initUserTable(db *sql.DB) (err error) {
|
|
log.Printf("Init User Table")
|
|
statement, err := db.Prepare(`CREATE TABLE IF NOT EXISTS user (
|
|
id TEXT PRIMARY KEY,
|
|
name TEXT,
|
|
password TEXT
|
|
)`)
|
|
if err != nil {
|
|
return
|
|
}
|
|
statement.Exec()
|
|
|
|
/*
|
|
newUser := &user.User{Id: "test", Name: "test", Password: "test"}
|
|
err = CreateUser(db, newUser)
|
|
if err != nil {
|
|
log.Printf("Error Create: %s", err)
|
|
return
|
|
}
|
|
newUser.Name = "TEST2"
|
|
err = UpdateUser(db, newUser)
|
|
if err != nil {
|
|
log.Printf("Error Update: %s", err)
|
|
return
|
|
}
|
|
err = DeleteUser(db, newUser.Id)
|
|
if err != nil {
|
|
log.Printf("Error Delete: %s", err)
|
|
return
|
|
}
|
|
readUser, err := ReadUser(db, newUser.Id)
|
|
if err != nil {
|
|
log.Printf("Error Read: %s", err)
|
|
return
|
|
}
|
|
if readUser != nil {
|
|
log.Printf("User ID: %s Name: %s Password: %s Devices: %s", readUser.Id, readUser.Name, readUser.Password, readUser.Devices)
|
|
} else {
|
|
log.Printf("No User found")
|
|
}
|
|
*/
|
|
|
|
return
|
|
}
|
|
|
|
func CreateUser(db *sql.DB, user *user.User) (err error) {
|
|
sqlStmt := fmt.Sprintf(`INSERT INTO user
|
|
(id, name, password)
|
|
VALUES
|
|
(?, ?, ?)`)
|
|
|
|
tx, err := db.Begin()
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
stmt, err := tx.Prepare(sqlStmt)
|
|
if err != nil {
|
|
return
|
|
}
|
|
defer stmt.Close()
|
|
|
|
_, err = stmt.Exec(user.Id, user.Name, user.Password)
|
|
if err != nil {
|
|
return
|
|
}
|
|
tx.Commit()
|
|
return
|
|
}
|
|
|
|
func ReadUser(db *sql.DB, id string) (foundUser *user.User, err error) {
|
|
queryStmt := fmt.Sprintf(`SELECT id, name, password
|
|
FROM user
|
|
WHERE id = '%s'`, id)
|
|
|
|
rows, err := db.Query(queryStmt)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
if rows.Next() {
|
|
foundUser = &user.User{}
|
|
err = rows.Scan(&foundUser.Id, &foundUser.Name, &foundUser.Password)
|
|
if err != nil {
|
|
return
|
|
}
|
|
foundUser.Devices, err = ReadDevicesForUser(db, foundUser.Id)
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
func UpdateUser(db *sql.DB, user *user.User) (err error) {
|
|
sqlStmt := fmt.Sprintf(`UPDATE user SET
|
|
name = ?,
|
|
password = ?
|
|
WHERE id = ?`)
|
|
|
|
tx, err := db.Begin()
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
stmt, err := tx.Prepare(sqlStmt)
|
|
if err != nil {
|
|
return
|
|
}
|
|
defer stmt.Close()
|
|
|
|
_, err = stmt.Exec(user.Name, user.Password, user.Id)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
tx.Commit()
|
|
return
|
|
}
|
|
|
|
func DeleteUser(db *sql.DB, id string) (err error) {
|
|
queryStmt := fmt.Sprintf(`DELETE FROM user
|
|
WHERE id = '%s'`, id)
|
|
|
|
tx, err := db.Begin()
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
_, err = db.Exec(queryStmt)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
tx.Commit()
|
|
return
|
|
}
|