38 lines
1.3 KiB
Go
38 lines
1.3 KiB
Go
|
|
package entities
|
||
|
|
|
||
|
|
import (
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"github.com/google/uuid"
|
||
|
|
"gorm.io/gorm"
|
||
|
|
)
|
||
|
|
|
||
|
|
type VoteEvent struct {
|
||
|
|
ID uuid.UUID `gorm:"type:uuid;primary_key;default:gen_random_uuid()" json:"id"`
|
||
|
|
Title string `gorm:"not null;size:255" json:"title" validate:"required,min=1,max=255"`
|
||
|
|
Description string `gorm:"type:text" json:"description"`
|
||
|
|
StartDate time.Time `gorm:"not null" json:"start_date" validate:"required"`
|
||
|
|
EndDate time.Time `gorm:"not null" json:"end_date" validate:"required"`
|
||
|
|
IsActive bool `gorm:"default:true" json:"is_active"`
|
||
|
|
ResultsOpen bool `gorm:"default:false" json:"results_open"`
|
||
|
|
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
|
||
|
|
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"`
|
||
|
|
Candidates []Candidate `gorm:"foreignKey:VoteEventID;references:ID" json:"candidates,omitempty"`
|
||
|
|
Votes []Vote `gorm:"foreignKey:VoteEventID;references:ID" json:"votes,omitempty"`
|
||
|
|
}
|
||
|
|
|
||
|
|
func (ve *VoteEvent) BeforeCreate(tx *gorm.DB) error {
|
||
|
|
if ve.ID == uuid.Nil {
|
||
|
|
ve.ID = uuid.New()
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func (VoteEvent) TableName() string {
|
||
|
|
return "vote_events"
|
||
|
|
}
|
||
|
|
|
||
|
|
func (ve *VoteEvent) IsVotingOpen() bool {
|
||
|
|
now := time.Now()
|
||
|
|
return ve.IsActive && now.After(ve.StartDate) && now.Before(ve.EndDate)
|
||
|
|
}
|