Go-Back-Skeleton/models/profile.go
2024-08-31 19:01:33 +02:00

38 lines
1.1 KiB
Go

// Package models contains application specific entities.
package models
import (
"time"
validation "github.com/go-ozzo/ozzo-validation"
"github.com/uptrace/bun"
)
// Profile holds specific application settings linked to an Account.
type Profile struct {
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 `bun:"theme" json:"theme,omitempty"`
}
// BeforeInsert hook executed before database insert operation.
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 *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")),
)
}