upgrade from go-pg to bun

This commit is contained in:
dhax 2024-08-31 18:57:54 +02:00
parent f59f129354
commit 1886be62bc
23 changed files with 415 additions and 385 deletions

View file

@ -4,35 +4,34 @@ package models
import (
"time"
"github.com/go-ozzo/ozzo-validation"
validation "github.com/go-ozzo/ozzo-validation"
"github.com/go-pg/pg/orm"
"github.com/uptrace/bun"
)
// Profile holds specific application settings linked to an Account.
type Profile struct {
ID int `json:"-"`
AccountID int `json:"-"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
ID int `bun:"id,pk,autoincrement" json:"-"`
AccountID int `bun:"account_id,notnull" json:"-"`
UpdatedAt time.Time `bun:"updated_at,nullzero,notnull,default:current_timestamp" json:"updated_at,omitempty"`
Theme string `json:"theme,omitempty"`
Theme string `bun:"theme" json:"theme,omitempty"`
}
// BeforeInsert hook executed before database insert operation.
func (p *Profile) BeforeInsert(db orm.DB) error {
func (p *Profile) BeforeInsert(db *bun.DB) error {
p.UpdatedAt = time.Now()
return nil
}
// BeforeUpdate hook executed before database update operation.
func (p *Profile) BeforeUpdate(db orm.DB) error {
func (p *Profile) BeforeUpdate(db *bun.DB) error {
p.UpdatedAt = time.Now()
return p.Validate()
}
// Validate validates Profile struct and returns validation errors.
func (p *Profile) Validate() error {
return validation.ValidateStruct(p,
validation.Field(&p.Theme, validation.Required, validation.In("default", "dark")),
)