Refactor Database Connector, Implement Register
This commit is contained in:
parent
0cc882cff0
commit
c79d1f86e4
18 changed files with 1495 additions and 1234 deletions
127
entities/device/deviceDatabaseConnector.go
Normal file
127
entities/device/deviceDatabaseConnector.go
Normal file
|
@ -0,0 +1,127 @@
|
|||
package device
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"nutfactory.org/Matrix/utils/database"
|
||||
)
|
||||
|
||||
func CreateDevice(device *Device, userId string) (err error) {
|
||||
sqlStmt := fmt.Sprintf(`INSERT INTO device
|
||||
(id, name, userId)
|
||||
VALUES
|
||||
(?, ?, ?)`)
|
||||
|
||||
tx, err := database.DB.Begin()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
stmt, err := tx.Prepare(sqlStmt)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer stmt.Close()
|
||||
|
||||
_, err = stmt.Exec(device.Id, device.Name, userId)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
tx.Commit()
|
||||
return
|
||||
}
|
||||
|
||||
func ReadDevice(id string) (foundDevice *Device, err error) {
|
||||
queryStmt := fmt.Sprintf(`SELECT id, name
|
||||
FROM device
|
||||
WHERE id = '%s'`, id)
|
||||
|
||||
rows, err := database.DB.Query(queryStmt)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
|
||||
if rows.Next() {
|
||||
foundDevice = &Device{}
|
||||
err = rows.Scan(&foundDevice.Id, &foundDevice.Name)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
foundDevice.Keys, err = ReadKeysForDevice(foundDevice.Id)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func ReadDevicesForUser(userId string) (devices map[string]*Device, err error) {
|
||||
queryStmt := fmt.Sprintf(`SELECT id, name
|
||||
FROM device
|
||||
WHERE userId = '%s'`, userId)
|
||||
|
||||
rows, err := database.DB.Query(queryStmt)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
|
||||
devices = make(map[string]*Device)
|
||||
|
||||
for rows.Next() {
|
||||
foundDevice := &Device{}
|
||||
err = rows.Scan(&foundDevice.Id, &foundDevice.Name)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
foundDevice.Keys, err = ReadKeysForDevice(foundDevice.Id)
|
||||
devices[foundDevice.Id] = foundDevice
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func UpdateDevice(device *Device, userId string) (err error) {
|
||||
sqlStmt := fmt.Sprintf(`UPDATE device SET
|
||||
name = ?,
|
||||
userId = ?
|
||||
WHERE id = ?`)
|
||||
|
||||
tx, err := database.DB.Begin()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
stmt, err := tx.Prepare(sqlStmt)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer stmt.Close()
|
||||
|
||||
_, err = stmt.Exec(device.Name, userId, device.Id)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
return
|
||||
}
|
||||
|
||||
func DeleteDevice(id string) (err error) {
|
||||
queryStmt := fmt.Sprintf(`DELETE FROM device
|
||||
WHERE id = '%s'`, id)
|
||||
|
||||
tx, err := database.DB.Begin()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
_, err = database.DB.Exec(queryStmt)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
return
|
||||
}
|
125
entities/device/keyDatabaseConnector.go
Normal file
125
entities/device/keyDatabaseConnector.go
Normal file
|
@ -0,0 +1,125 @@
|
|||
package device
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"nutfactory.org/Matrix/utils/database"
|
||||
)
|
||||
|
||||
func CreateKey(key *Key, deviceId string) (err error) {
|
||||
sqlStmt := fmt.Sprintf(`INSERT INTO key
|
||||
(id, type, key, deviceId)
|
||||
VALUES
|
||||
(?, ?, ?, ?)`)
|
||||
|
||||
tx, err := database.DB.Begin()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
stmt, err := tx.Prepare(sqlStmt)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer stmt.Close()
|
||||
|
||||
_, err = stmt.Exec(key.Id, key.Type, key.Key, deviceId)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
tx.Commit()
|
||||
return
|
||||
}
|
||||
|
||||
func ReadKey(id string) (foundKey *Key, err error) {
|
||||
queryStmt := fmt.Sprintf(`SELECT id, type, key
|
||||
FROM key
|
||||
WHERE id = '%s'`, id)
|
||||
|
||||
rows, err := database.DB.Query(queryStmt)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
|
||||
if rows.Next() {
|
||||
foundKey = &Key{}
|
||||
err = rows.Scan(&foundKey.Id, &foundKey.Type, &foundKey.Key)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func ReadKeysForDevice(deviceId string) (keys map[string]*Key, err error) {
|
||||
queryStmt := fmt.Sprintf(`SELECT id, type, key
|
||||
FROM key
|
||||
WHERE deviceId = '%s'`, deviceId)
|
||||
|
||||
rows, err := database.DB.Query(queryStmt)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
defer rows.Close()
|
||||
|
||||
keys = make(map[string]*Key)
|
||||
|
||||
for rows.Next() {
|
||||
foundKey := &Key{}
|
||||
err = rows.Scan(&foundKey.Id, &foundKey.Type, &foundKey.Key)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
keys[foundKey.Id] = foundKey
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func UpdateKey(key *Key) (err error) {
|
||||
sqlStmt := fmt.Sprintf(`UPDATE key SET
|
||||
type = ?,
|
||||
key = ?
|
||||
WHERE id = ?`)
|
||||
|
||||
tx, err := database.DB.Begin()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
stmt, err := tx.Prepare(sqlStmt)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer stmt.Close()
|
||||
|
||||
_, err = stmt.Exec(key.Type, key.Key, key.Id)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
return
|
||||
}
|
||||
|
||||
func DeleteKey(id string) (err error) {
|
||||
queryStmt := fmt.Sprintf(`DELETE FROM key
|
||||
WHERE id = '%s'`, id)
|
||||
|
||||
tx, err := database.DB.Begin()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
_, err = database.DB.Exec(queryStmt)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
return
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue