govote/voting/vote/vote.go
2024-05-14 10:27:13 +02:00

45 lines
1.1 KiB
Go

package vote
import (
"fmt"
"time"
)
// Vote represents an individual vote in a voting.
type Vote struct {
// Id contains the unique ID of the vote.
Id string
// Elector contains the name of whoever placed the vote.
Elector string
// Choice contains the choice of the vote.
Choice Choice
// timestamp contains the UTC timestamp of when the vote was placed.
timestamp time.Time
}
// NewVote returns an initialized Vote.
func NewVote(id, elector string, choice Choice) Vote {
return Vote{
Id: id,
Elector: elector,
Choice: choice,
timestamp: time.Now().UTC(),
}
}
// NewVoteWithTimestamp returns an initialized Vote with a predefined
// timestamp. This can be useful when loading a vote from the store.
func NewVoteWithTimestamp(id, elector string, choice Choice, timestamp time.Time) Vote {
return Vote{
Id: id,
Elector: elector,
Choice: choice,
timestamp: timestamp,
}
}
// String implements fmt.Stringer
func (vote Vote) String() string {
return fmt.Sprintf("%s %s %s %s", vote.Id, vote.timestamp.Format(time.DateTime), vote.Choice, vote.Elector)
}