2020-10-01 15:45:57 +00:00
|
|
|
package database
|
2020-09-30 08:28:49 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
2020-10-01 15:45:57 +00:00
|
|
|
"fmt"
|
|
|
|
"log"
|
2020-09-30 08:28:49 +00:00
|
|
|
|
|
|
|
"nutfactory.org/Matrix/entities/user"
|
|
|
|
)
|
|
|
|
|
2020-10-01 15:45:57 +00:00
|
|
|
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
|
|
|
|
}
|
2020-09-30 08:28:49 +00:00
|
|
|
statement.Exec()
|
2020-10-01 15:45:57 +00:00
|
|
|
|
|
|
|
//TODO: Test Queries
|
|
|
|
|
|
|
|
return
|
2020-09-30 08:28:49 +00:00
|
|
|
}
|
|
|
|
|
2020-10-01 15:45:57 +00:00
|
|
|
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
|
|
|
|
}
|
2020-09-30 08:28:49 +00:00
|
|
|
|
2020-10-01 15:45:57 +00:00
|
|
|
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
|
2020-09-30 08:28:49 +00:00
|
|
|
}
|
|
|
|
|
2020-10-01 15:45:57 +00:00
|
|
|
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()
|
2020-09-30 08:28:49 +00:00
|
|
|
|
2020-10-01 15:45:57 +00:00
|
|
|
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
|
2020-09-30 08:28:49 +00:00
|
|
|
}
|
|
|
|
|
2020-10-01 15:45:57 +00:00
|
|
|
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()
|
2020-09-30 08:28:49 +00:00
|
|
|
|
2020-10-01 15:45:57 +00:00
|
|
|
_, err = stmt.Exec(user.Name, user.Password, user.Id)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
tx.Commit()
|
|
|
|
return
|
2020-09-30 08:28:49 +00:00
|
|
|
}
|
|
|
|
|
2020-10-01 15:45:57 +00:00
|
|
|
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
|
|
|
|
}
|
2020-09-30 08:28:49 +00:00
|
|
|
|
2020-10-01 15:45:57 +00:00
|
|
|
tx.Commit()
|
|
|
|
return
|
2020-09-30 08:28:49 +00:00
|
|
|
}
|