subhub/data/models/group.go
2019-09-07 17:54:53 +03:00

53 lines
1.3 KiB
Go

package models
import (
"database/sql"
"time"
slugify "github.com/gosimple/slug"
)
// Group is a collection of Hubs (equivalent to ActivityPub Organizations)
// Refers to the https://www.w3.org/TR/activitystreams-vocabulary/#dfn-group
// Also refers to the Groups table in the database
type Group struct {
Slug string `json:"slug"`
Name string `json:"name"`
Note string `json:"note"`
Type string
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// GetGroup returns a single Group object or nil
func GetGroup(db *sql.DB, slug string) (*Group, error) {
row := db.QueryRow(`
select slug, name, note, created_at, updated_at
from groups where slug = $1
`, slug)
var group Group
err := row.Scan(&group.Slug, &group.Name, &group.Note, &group.CreatedAt, &group.UpdatedAt)
if err == sql.ErrNoRows {
return nil, nil
}
if err != nil {
return nil, err
}
return &group, nil
}
// PutGroup put new group into db and return slur or error
func PutGroup(db *sql.DB, name string, note string) (string, error) {
// TODO: make slugify "github.com/gosimple/slug"
slug := slugify.MakeLang(name, "en")
query := `
insert into groups(slug, name, note)
values ($1, $2, $3)
`
_, err := db.Exec(query, slug, name, note)
return slug, err
}