2020-10-03 12:22:29 +00:00
|
|
|
package device
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2020-10-12 14:16:28 +00:00
|
|
|
"git.nutfactory.org/hoernschen/Matrix/utils/database"
|
2020-10-03 12:22:29 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func CreateDevice(device *Device, userId string) (err error) {
|
|
|
|
sqlStmt := fmt.Sprintf(`INSERT INTO device
|
2020-10-11 21:11:30 +00:00
|
|
|
(id, name, accessToken, userId)
|
2020-10-03 12:22:29 +00:00
|
|
|
VALUES
|
2020-10-11 21:11:30 +00:00
|
|
|
(?, ?, ?, ?)`)
|
2020-10-03 12:22:29 +00:00
|
|
|
|
|
|
|
tx, err := database.DB.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
stmt, err := tx.Prepare(sqlStmt)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
|
|
|
|
2020-10-11 21:11:30 +00:00
|
|
|
_, err = stmt.Exec(device.Id, device.Name, device.AccessToken, userId)
|
2020-10-03 12:22:29 +00:00
|
|
|
if err != nil {
|
2020-10-17 10:07:39 +00:00
|
|
|
tx.Rollback()
|
2020-10-03 12:22:29 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
tx.Commit()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReadDevice(id string) (foundDevice *Device, err error) {
|
2020-10-11 21:11:30 +00:00
|
|
|
queryStmt := fmt.Sprintf(`SELECT id, name, accessToken
|
2020-10-03 12:22:29 +00:00
|
|
|
FROM device
|
|
|
|
WHERE id = '%s'`, id)
|
|
|
|
|
|
|
|
rows, err := database.DB.Query(queryStmt)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
if rows.Next() {
|
|
|
|
foundDevice = &Device{}
|
2020-10-11 21:11:30 +00:00
|
|
|
err = rows.Scan(&foundDevice.Id, &foundDevice.Name, &foundDevice.AccessToken)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
foundDevice.Keys, err = ReadKeysForDevice(foundDevice.Id)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReadDeviceFromAccessToken(accessToken string) (foundDevice *Device, err error) {
|
|
|
|
queryStmt := fmt.Sprintf(`SELECT id, name, accessToken
|
|
|
|
FROM device
|
|
|
|
WHERE accessToken = '%s'`, accessToken)
|
2020-10-17 10:07:39 +00:00
|
|
|
|
2020-10-11 21:11:30 +00:00
|
|
|
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, &foundDevice.AccessToken)
|
2020-10-03 12:22:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
foundDevice.Keys, err = ReadKeysForDevice(foundDevice.Id)
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func ReadDevicesForUser(userId string) (devices map[string]*Device, err error) {
|
2020-10-11 21:11:30 +00:00
|
|
|
queryStmt := fmt.Sprintf(`SELECT id, name, accessToken
|
2020-10-03 12:22:29 +00:00
|
|
|
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{}
|
2020-10-11 21:11:30 +00:00
|
|
|
err = rows.Scan(&foundDevice.Id, &foundDevice.Name, &foundDevice.AccessToken)
|
2020-10-03 12:22:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
foundDevice.Keys, err = ReadKeysForDevice(foundDevice.Id)
|
|
|
|
devices[foundDevice.Id] = foundDevice
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-11 21:11:30 +00:00
|
|
|
func UpdateDevice(device *Device) (err error) {
|
2020-10-03 12:22:29 +00:00
|
|
|
sqlStmt := fmt.Sprintf(`UPDATE device SET
|
|
|
|
name = ?,
|
2020-10-11 21:11:30 +00:00
|
|
|
accessToken = ?
|
2020-10-03 12:22:29 +00:00
|
|
|
WHERE id = ?`)
|
|
|
|
|
|
|
|
tx, err := database.DB.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
stmt, err := tx.Prepare(sqlStmt)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer stmt.Close()
|
|
|
|
|
2020-10-11 21:11:30 +00:00
|
|
|
_, err = stmt.Exec(device.Name, device.AccessToken, device.Id)
|
2020-10-03 12:22:29 +00:00
|
|
|
if err != nil {
|
2020-10-17 10:07:39 +00:00
|
|
|
tx.Rollback()
|
2020-10-03 12:22:29 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-10-17 10:07:39 +00:00
|
|
|
_, err = tx.Exec(queryStmt)
|
2020-10-03 12:22:29 +00:00
|
|
|
if err != nil {
|
2020-10-17 10:07:39 +00:00
|
|
|
tx.Rollback()
|
2020-10-03 12:22:29 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
tx.Commit()
|
|
|
|
return
|
|
|
|
}
|