Matrix/entities/device/deviceDatabaseConnector.go

155 lines
2.8 KiB
Go
Raw Permalink Normal View History

package device
import (
"fmt"
2020-10-12 14:16:28 +00:00
"git.nutfactory.org/hoernschen/Matrix/utils/database"
)
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)
VALUES
2020-10-11 21:11:30 +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)
if err != nil {
2020-10-17 10:07:39 +00:00
tx.Rollback()
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
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)
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
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)
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) {
sqlStmt := fmt.Sprintf(`UPDATE device SET
name = ?,
2020-10-11 21:11:30 +00:00
accessToken = ?
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)
if err != nil {
2020-10-17 10:07:39 +00:00
tx.Rollback()
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)
if err != nil {
2020-10-17 10:07:39 +00:00
tx.Rollback()
return
}
tx.Commit()
return
}