117 lines
2.7 KiB
Go
117 lines
2.7 KiB
Go
package database
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"math"
|
|
"system-trace/core/parsers"
|
|
"system-trace/core/types"
|
|
"time"
|
|
|
|
"github.com/uptrace/bun"
|
|
)
|
|
|
|
type Group struct {
|
|
ID int32 `bun:",pk,autoincrement"`
|
|
IssuerID int32 `bun:",notnull" json:"issuerID"`
|
|
Issuer *User `bun:"rel:belongs-to,join:issuer_id=id" json:"issuer"`
|
|
Name string `bun:",notnull,unique" json:"name"`
|
|
Users []*User `bun:"rel:has-many,join:id=group_id,array" json:"users"`
|
|
Permissions []*GroupPermission `bun:"rel:has-many,join:id=group_id,array" json:"permissions"`
|
|
CreatedAt time.Time `bun:",notnull,default:current_timestamp" json:"createdAt"`
|
|
UpdatedAt time.Time `json:"updatedAt"`
|
|
DeletedAt time.Time `bun:",soft_delete,nullzero" json:"deletedAt"`
|
|
}
|
|
|
|
var _ bun.BeforeAppendModelHook = (*Group)(nil)
|
|
|
|
func (g *Group) BeforeAppendModel(ctx context.Context, query bun.Query) error {
|
|
switch query.(type) {
|
|
case *bun.UpdateQuery:
|
|
g.UpdatedAt = time.Now()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func FindGroups(s *types.Search, p *types.Pagination, ob *types.OrderBy) (groups *[]Group, cursor *types.Cursor, err error) {
|
|
groups = new([]Group)
|
|
q := PG.NewSelect().
|
|
Model(groups).
|
|
Relation("Issuer").
|
|
Relation("Permissions").
|
|
WhereAllWithDeleted().
|
|
Offset(p.Page*p.Count - p.Count).
|
|
Limit(p.Count).
|
|
Order(fmt.Sprintf("%s %s", ob.Key, ob.Order))
|
|
|
|
if len(s.Input) > 0 {
|
|
q.WhereGroup(" AND ", func(q *bun.SelectQuery) *bun.SelectQuery {
|
|
if parsers.IsInt(s.Input) {
|
|
return q.Where(`"group"."id" = ?`, s.Input)
|
|
} else {
|
|
return q.
|
|
Where(`"group"."name" LIKE ?`, "%"+s.Input+"%").
|
|
WhereOr(`"issuer"."email" LIKE ?`, "%"+s.Input+"%").
|
|
WhereOr(`"issuer"."real_name" LIKE ?`, "%"+s.Input+"%")
|
|
}
|
|
})
|
|
}
|
|
|
|
count, err := q.ScanAndCount(ctx)
|
|
|
|
return groups, &types.Cursor{
|
|
Count: len(*groups),
|
|
CurrentPage: p.Page,
|
|
TotalPages: math.Ceil(float64(count) / float64(p.Count)),
|
|
TotalRows: float64(count),
|
|
}, err
|
|
}
|
|
|
|
func FindGroupByID(id int) (group *Group, err error) {
|
|
group = new(Group)
|
|
err = PG.NewSelect().
|
|
Model(group).
|
|
Where("id = ?", id).
|
|
Scan(ctx)
|
|
|
|
return group, err
|
|
}
|
|
|
|
func InsertGroup(g *Group) error {
|
|
_, err := PG.NewInsert().
|
|
Model(g).
|
|
Returning("id").
|
|
Exec(ctx)
|
|
|
|
return err
|
|
}
|
|
|
|
func UpdateGroup(g *Group, cols []string) error {
|
|
cols = append(cols, "updated_at")
|
|
_, err := PG.NewUpdate().
|
|
Model(g).
|
|
Column(cols...).
|
|
WherePK().
|
|
Exec(ctx)
|
|
|
|
return err
|
|
}
|
|
|
|
func DeleteGroup(id int) error {
|
|
_, err := PG.NewDelete().
|
|
Model(new(Group)).
|
|
Where("id = ?", id).
|
|
Exec(ctx)
|
|
|
|
return err
|
|
}
|
|
|
|
func DeleteManyGroups(ids []int) error {
|
|
_, err := PG.NewDelete().
|
|
Model(new(Group)).
|
|
Where("id IN (?)", bun.In(ids)).
|
|
Exec(ctx)
|
|
|
|
return err
|
|
}
|