github.com/dhax/go-base@v0.0.0-20231004214136-8be7e5c1972b/models/profile.go (about)

     1  // Package models contains application specific entities.
     2  package models
     3  
     4  import (
     5  	"time"
     6  
     7  	"github.com/go-ozzo/ozzo-validation"
     8  
     9  	"github.com/go-pg/pg/orm"
    10  )
    11  
    12  // Profile holds specific application settings linked to an Account.
    13  type Profile struct {
    14  	ID        int       `json:"-"`
    15  	AccountID int       `json:"-"`
    16  	UpdatedAt time.Time `json:"updated_at,omitempty"`
    17  
    18  	Theme string `json:"theme,omitempty"`
    19  }
    20  
    21  // BeforeInsert hook executed before database insert operation.
    22  func (p *Profile) BeforeInsert(db orm.DB) error {
    23  	p.UpdatedAt = time.Now()
    24  	return nil
    25  }
    26  
    27  // BeforeUpdate hook executed before database update operation.
    28  func (p *Profile) BeforeUpdate(db orm.DB) error {
    29  	p.UpdatedAt = time.Now()
    30  	return p.Validate()
    31  }
    32  
    33  // Validate validates Profile struct and returns validation errors.
    34  func (p *Profile) Validate() error {
    35  
    36  	return validation.ValidateStruct(p,
    37  		validation.Field(&p.Theme, validation.Required, validation.In("default", "dark")),
    38  	)
    39  }