ActivityPub/entities/actor/actorDatabaseConnector.go

77 lines
1.4 KiB
Go
Raw Normal View History

2020-10-17 10:13:15 +00:00
package actor
import (
"fmt"
"git.nutfactory.org/hoernschen/ActivityPub/utils/database"
)
func CreateActor(actor *Actor) (err error) {
sqlStmt := fmt.Sprintf(`INSERT INTO actor
(id, type, name, preferredUsername, summary, inbox, outbox, followers, following, liked)
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(
actor.Id,
actor.Type,
actor.Name,
actor.PreferredUsername,
actor.Summary,
actor.Inbox,
actor.Outbox,
actor.Followers,
actor.Following,
actor.Liked,
)
if err != nil {
tx.Rollback()
return
}
tx.Commit()
return
}
func ReadActor(id string) (foundActor *Actor, err error) {
queryStmt := fmt.Sprintf(`SELECT id, type, name, preferredUsername, summary, inbox, outbox, followers, following, liked
FROM actor
WHERE id = '%s'`, id)
rows, err := database.DB.Query(queryStmt)
if err != nil {
return
}
defer rows.Close()
if rows.Next() {
foundActor = &Actor{}
err = rows.Scan(
&foundActor.Id,
&foundActor.Type,
&foundActor.Name,
&foundActor.PreferredUsername,
&foundActor.Summary,
&foundActor.Inbox,
&foundActor.Outbox,
&foundActor.Followers,
&foundActor.Following,
&foundActor.Liked,
)
if err != nil {
return
}
}
return
}