govote/voting/vote/vote.go

45 lines
1.1 KiB
Go
Raw Normal View History

2024-05-12 22:47:24 +00:00
package vote
import (
"fmt"
"time"
)
2024-05-14 08:26:48 +00:00
// Vote represents an individual vote in a voting.
2024-05-12 22:47:24 +00:00
type Vote struct {
2024-05-14 08:26:48 +00:00
// 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.
2024-05-13 08:45:38 +00:00
timestamp time.Time
2024-05-12 22:47:24 +00:00
}
2024-05-14 08:26:48 +00:00
// NewVote returns an initialized Vote.
func NewVote(id, elector string, choice Choice) Vote {
2024-05-13 08:45:38 +00:00
return Vote{
2024-05-13 11:20:26 +00:00
Id: id,
2024-05-13 08:45:38 +00:00
Elector: elector,
Choice: choice,
timestamp: time.Now().UTC(),
}
2024-05-12 22:47:24 +00:00
}
2024-05-14 08:26:48 +00:00
// 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 {
2024-05-13 08:45:38 +00:00
return Vote{
2024-05-13 11:20:26 +00:00
Id: id,
2024-05-13 08:45:38 +00:00
Elector: elector,
Choice: choice,
timestamp: timestamp,
}
2024-05-12 22:47:24 +00:00
}
2024-05-14 08:26:48 +00:00
// String implements fmt.Stringer
2024-05-12 22:47:24 +00:00
func (vote Vote) String() string {
return fmt.Sprintf("%s %s %s %s", vote.Id, vote.timestamp.Format(time.DateTime), vote.Choice, vote.Elector)
2024-05-12 22:47:24 +00:00
}