subhub/data/models/group.go

52 lines
1.2 KiB
Go

package models
import (
"database/sql"
slugify "github.com/gosimple/slug"
"github.com/jinzhu/gorm"
)
// 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 {
gorm.Model
Id string `json:"id"`
Name string `json:"name"`
Note string `json:"status"`
Type string
UserID uint
}
// GetGroup returns a single Group object or nil
func GetGroup(db *sql.DB, slug string) (*Group, error) {
row := db.QueryRow(`
select slug, name, status, created_at, updated_at
from groups where slug = $1
`, slug)
var group Group
err := row.Scan(&group.Id, &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, status)
values ($1, $2, $3)
`
_, err := db.Exec(query, slug, name, note)
return slug, err
}