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