add some works
This commit is contained in:
parent
cd9ec0c774
commit
8807e5aaab
22 changed files with 1074 additions and 3 deletions
32
data/data.go
Normal file
32
data/data.go
Normal file
|
@ -0,0 +1,32 @@
|
|||
package data
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
var (
|
||||
pool *sql.DB
|
||||
)
|
||||
|
||||
func GetDB() *sql.DB {
|
||||
if pool == nil {
|
||||
// TODO write text
|
||||
panic("")
|
||||
}
|
||||
return pool
|
||||
}
|
||||
|
||||
func NewDB() (*sql.DB, error) {
|
||||
const (
|
||||
host = "localhost"
|
||||
user = "admin"
|
||||
pass = "Bambukalo2201"
|
||||
dbname = "subhub"
|
||||
)
|
||||
|
||||
dbConnString := fmt.Sprintf("%s:%s@%s/%s?charset=utf8mb4&parseTime=true",
|
||||
user, pass, host, dbname)
|
||||
|
||||
return sql.Open("mysql", dbConnString)
|
||||
}
|
52
data/models/group.go
Normal file
52
data/models/group.go
Normal file
|
@ -0,0 +1,52 @@
|
|||
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
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue