51 lines
1.1 KiB
Go
51 lines
1.1 KiB
Go
package models
|
|
|
|
import (
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
// Poll table for user surveys posted in the forums.
|
|
type Poll struct {
|
|
ID uint64 `gorm:"primaryKey"`
|
|
|
|
// Poll options
|
|
Choices string // line-separated choices
|
|
MultipleChoice bool // User can vote multiple choices
|
|
CustomAnswers bool // Users can contribute a custom response
|
|
|
|
Expires bool // if it stops accepting new votes
|
|
ExpiresAt time.Time // when it stops accepting new votes
|
|
|
|
CreatedAt time.Time
|
|
UpdatedAt time.Time
|
|
}
|
|
|
|
// CreatePoll initializes a poll.
|
|
//
|
|
// expires is in days (0 = doesn't expire)
|
|
func CreatePoll(choices []string, expires int) *Poll {
|
|
return &Poll{
|
|
Choices: strings.Join(choices, "\n"),
|
|
ExpiresAt: time.Now().Add(time.Duration(expires) * 24 * time.Hour),
|
|
}
|
|
}
|
|
|
|
// GetPoll by ID.
|
|
func GetPoll(id uint64) (*Poll, error) {
|
|
m := &Poll{}
|
|
result := DB.First(&m, id)
|
|
return m, result.Error
|
|
}
|
|
|
|
// Options returns a conveniently formatted listing of the options.
|
|
func (p *Poll) Options() []string {
|
|
return strings.Split(p.Choices, "\n")
|
|
}
|
|
|
|
// Save Poll.
|
|
func (p *Poll) Save() error {
|
|
result := DB.Save(p)
|
|
return result.Error
|
|
}
|