bing work with gorm

This commit is contained in:
inhosin 2019-09-07 19:10:28 +03:00
parent 8807e5aaab
commit 61b976259a
7 changed files with 203 additions and 13 deletions

53
data/models/activity.go Normal file
View file

@ -0,0 +1,53 @@
package models
import (
"database/sql"
"time"
)
type Activity struct {
Id string `json:"id"`
Payload string `json:"note"`
Remote bool `json:"remote"`
Group `json:"group"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
// GetActivity gets a Activity at any slug
func GetActivity(db *sql.DB, slug string) (*Activity, error) {
row := db.QueryRow(`
SELECT id, Payload, remote, goupid, created_at, updated_at
FROM activity WHERE id = $1
`, slug)
var note Activity
err := row.Scan(&note.Slug,
&note.Name, &note.Note, &note.CreatedAt, &note.UpdatedAt)
// This is not an error from the user's perspective
if err == sql.ErrNoRows {
return nil, nil
}
if err != nil {
return nil, err
}
return &note, nil
}
/*
// PutNote creates a note with this name and note
func PutActivity(db *sql.DB, name string, note string) (string, error) {
// TODO make guid but not slug
id := slugify.MakeLang(name, "en")
query := `
INSERT INTO activity (id, name, note)
VALUES ($1, $2, $3)
`
_, err := db.Exec(query, slug, name, note)
return slug, err
}
*/

View file

@ -2,21 +2,20 @@ package models
import (
"database/sql"
"time"
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 {
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"`
gorm.Model
Id string `json:"id"`
Name string `json:"name"`
Note string `json:"note"`
Type string
UserID uint
}
// GetGroup returns a single Group object or nil
@ -27,7 +26,7 @@ func GetGroup(db *sql.DB, slug string) (*Group, error) {
`, slug)
var group Group
err := row.Scan(&group.Slug, &group.Name, &group.Note, &group.CreatedAt, &group.UpdatedAt)
err := row.Scan(&group.id, &group.Name, &group.Note, &group.CreatedAt, &group.UpdatedAt)
if err == sql.ErrNoRows {
return nil, nil

19
data/models/user.go Normal file
View file

@ -0,0 +1,19 @@
package models
import (
"github.com/jinzhu/gorm"
)
type User struct {
gorm.Model
Name string
Role string `gorm:"size:255"`
Groups []Group `gorm:"foreignkey:UserID"`
}
type Profile struct {
gorm.Model
UserID int
User User
Email string `gorm:"type:varchar(100);unique_index"`
}