156 lines
3.7 KiB
Go
156 lines
3.7 KiB
Go
package database
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"math"
|
|
"system-trace/core/parsers"
|
|
"system-trace/core/types"
|
|
"system-trace/core/utils"
|
|
"time"
|
|
|
|
"github.com/uptrace/bun"
|
|
)
|
|
|
|
type User struct {
|
|
ID int32 `bun:",pk,autoincrement"`
|
|
Email string `bun:",notnull,unique" json:"email"`
|
|
PasswordHash string `bun:",notnull,type:varchar(64)" json:"-"`
|
|
PasswordLength int8 `bun:",notnull" json:"passwordLength"`
|
|
RealName string `bun:",notnull" json:"realName"`
|
|
GroupID int32 `bun:",notnull" json:"groupID"`
|
|
Group *Group `bun:"rel:belongs-to,join:group_id=id" json:"group"`
|
|
IsRequiredToSetPassword bool `bun:",notnull,default:true" json:"isRequiredToSetPassword"`
|
|
IsActive bool `bun:",notnull,default:true" json:"isActive"`
|
|
LastLogin time.Time `bun:",nullzero" json:"lastLogin"`
|
|
CreatedAt time.Time `bun:",notnull,default:current_timestamp" json:"createdAt"`
|
|
UpdatedAt time.Time `bun:",nullzero" json:"updatedAt"`
|
|
DeletedAt time.Time `bun:",soft_delete,nullzero" json:"deletedAt"`
|
|
}
|
|
|
|
var _ bun.BeforeAppendModelHook = (*User)(nil)
|
|
|
|
func (u *User) BeforeAppendModel(ctx context.Context, query bun.Query) error {
|
|
switch query.(type) {
|
|
case *bun.UpdateQuery:
|
|
u.UpdatedAt = time.Now()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func FindByEmailAndPassword(email, password string) (*User, error) {
|
|
passwordHash := utils.SHA256(password)
|
|
u := new(User)
|
|
err := PG.NewSelect().
|
|
Model(u).
|
|
Where("email = ?", email).
|
|
Where("password_hash = ?", passwordHash).
|
|
Scan(ctx)
|
|
|
|
return u, err
|
|
}
|
|
|
|
func FindUsers(s *types.Search, p *types.Pagination, ob *types.OrderBy) (users *[]User, cursor *types.Cursor, err error) {
|
|
users = new([]User)
|
|
q := PG.NewSelect().
|
|
Model(users).
|
|
Relation("Group").
|
|
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(`"user"."id" = ?`, s.Input)
|
|
} else {
|
|
return q.
|
|
Where(`"user"."email" LIKE ?`, "%"+s.Input+"%").
|
|
WhereOr(`"user"."real_name" LIKE ?`, "%"+s.Input+"%")
|
|
}
|
|
})
|
|
}
|
|
|
|
if len(s.GroupID) > 0 && parsers.IsInt(s.GroupID) {
|
|
q.Where(`"user"."group_id" = ?`, s.GroupID)
|
|
}
|
|
|
|
count, err := q.ScanAndCount(ctx)
|
|
|
|
return users, &types.Cursor{
|
|
Count: len(*users),
|
|
CurrentPage: p.Page,
|
|
TotalPages: math.Ceil(float64(count) / float64(p.Count)),
|
|
TotalRows: float64(count),
|
|
}, err
|
|
}
|
|
|
|
func FindUserByID(id int) (user *User, err error) {
|
|
user = new(User)
|
|
err = PG.NewSelect().
|
|
Model(user).
|
|
Where("id = ?", id).
|
|
Scan(ctx)
|
|
|
|
return user, err
|
|
}
|
|
|
|
func InsertUser(u *User) error {
|
|
_, err := PG.NewInsert().
|
|
Model(u).
|
|
Returning("NULL").
|
|
Exec(ctx)
|
|
|
|
return err
|
|
}
|
|
|
|
func UpdateUser(u *User, cols []string) error {
|
|
cols = append(cols, "updated_at")
|
|
_, err := PG.NewUpdate().
|
|
Model(u).
|
|
Column(cols...).
|
|
WherePK().
|
|
Exec(ctx)
|
|
|
|
return err
|
|
}
|
|
|
|
func DeleteUser(id int) error {
|
|
_, err := PG.NewDelete().
|
|
Model(new(User)).
|
|
Where("id = ?", id).
|
|
Exec(ctx)
|
|
|
|
return err
|
|
}
|
|
|
|
func DeleteManyUsers(ids []int) error {
|
|
_, err := PG.NewDelete().
|
|
Model(new(User)).
|
|
Where("id IN (?)", bun.In(ids)).
|
|
Exec(ctx)
|
|
|
|
return err
|
|
}
|
|
|
|
func BlockManyUsers(ids []int) error {
|
|
_, err := PG.NewUpdate().
|
|
Model(new(User)).
|
|
Set("is_active = ?", false).
|
|
Where("id IN (?)", bun.In(ids)).
|
|
Exec(ctx)
|
|
|
|
return err
|
|
}
|
|
|
|
func UnblockManyUsers(ids []int) error {
|
|
_, err := PG.NewUpdate().
|
|
Model(new(User)).
|
|
Set("is_active = ?", true).
|
|
Where("id IN (?)", bun.In(ids)).
|
|
Exec(ctx)
|
|
|
|
return err
|
|
}
|