electricity_bill_calc_service/model/user.go

49 lines
1.4 KiB
Go

package model
import (
"context"
"time"
"github.com/uptrace/bun"
)
const (
USER_TYPE_ENT int8 = iota
USER_TYPE_SUP
USER_TYPE_OPS
)
type User struct {
bun.BaseModel `bun:"table:user,alias:u"`
Created `bun:"extend"`
Id string `bun:",pk,notnull" json:"id"`
Username string `bun:",notnull" json:"username"`
Password string `bun:",notnull" json:"-"`
ResetNeeded bool `bun:",notnull" json:"resetNeeded"`
Type int8 `bun:"type:smallint,notnull" json:"type"`
Enabled bool `bun:",notnull" json:"enabled"`
Detail *UserDetail `bun:"rel:has-one,join:id=id" json:"-"`
Charges []*UserCharge `bun:"rel:has-many,join:id=user_id" json:"-"`
}
type UserWithCredentials struct {
bun.BaseModel `bun:"table:user,alias:u"`
Created `bun:"extend"`
Id string `bun:",pk,notnull" json:"id"`
Username string `bun:",notnull" json:"username"`
Password string `bun:",notnull" json:"credential"`
ResetNeeded bool `bun:",notnull" json:"resetNeeded"`
Type int8 `bun:"type:smallint,notnull" json:"type"`
Enabled bool `bun:",notnull" json:"enabled"`
}
var _ bun.BeforeAppendModelHook = (*User)(nil)
func (u *User) BeforeAppendModel(ctx context.Context, query bun.Query) error {
switch query.(type) {
case *bun.InsertQuery:
u.CreatedAt = time.Now()
}
return nil
}