ActivityPub/entities/object/objectDatabaseConnector.go
2020-10-17 12:13:15 +02:00

70 lines
1.1 KiB
Go

package object
import (
"fmt"
"git.nutfactory.org/hoernschen/ActivityPub/utils/database"
)
func CreateObject(object *Object) (err error) {
sqlStmt := fmt.Sprintf(`INSERT INTO object
(id, type, attributedTo, content, published, toActor)
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(
object.Id,
object.Type,
object.AttributedTo,
object.Content,
object.Published,
object.To,
)
if err != nil {
tx.Rollback()
return
}
tx.Commit()
return
}
func ReadObject(id string) (foundObject *Object, err error) {
queryStmt := fmt.Sprintf(`SELECT id, type, attributedTo, content, published, toActor
FROM object
WHERE id = '%s'`, id)
rows, err := database.DB.Query(queryStmt)
if err != nil {
return
}
defer rows.Close()
if rows.Next() {
foundObject = &Object{}
err = rows.Scan(
&foundObject.Id,
&foundObject.Type,
&foundObject.AttributedTo,
&foundObject.Content,
&foundObject.Published,
&foundObject.To,
)
if err != nil {
return
}
}
return
}