database

package
v0.0.0-...-ca94e73 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 25, 2025 License: BSD-2-Clause Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const ElectionPollTypeId = 1
View Source
const ReferendumPollTypeId = 2

Variables

View Source
var ErrNotFound = errors.New("not found")
View Source
var Migrations = migrate.NewMigrations()

Functions

func BulkInsertElectionOutcomeResult

func BulkInsertElectionOutcomeResult(entities []*ElectionOutcomeResult, x ...bun.IDB) error

func CountUsers

func CountUsers(x ...bun.IDB) (int, error)

func CountVotesForElection

func CountVotesForElection(pollID int, x ...bun.IDB) (int, error)

func DeleteAllCandidatesForUser

func DeleteAllCandidatesForUser(userID string, x ...bun.IDB) error

func DeleteAllUsers

func DeleteAllUsers(x ...bun.IDB) error

func DeleteAllVotesForPoll

func DeleteAllVotesForPoll(pollID int, x ...bun.IDB) error

func DeleteAllVotesForUser

func DeleteAllVotesForUser(userID string, x ...bun.IDB) error

func DeleteBallotForElection

func DeleteBallotForElection(electionID int, x ...bun.IDB) error

func DeleteCandidatesForElection

func DeleteCandidatesForElection(electionID int, x ...bun.IDB) error

func DeletePollByID

func DeletePollByID(pollID int, x ...bun.IDB) error

func DeleteUser

func DeleteUser(id string, x ...bun.IDB) error

func Get

func Get() *bun.DB

func GetTx

func GetTx() (bun.Tx, error)

func HasUserVotedInPoll

func HasUserVotedInPoll(userID string, pollID int, x ...bun.IDB) (bool, error)

func Migrate

func Migrate(db *bun.DB) error

func PublishPollOutcome

func PublishPollOutcome(id int, x ...bun.IDB) error

Types

type BallotEntry

type BallotEntry struct {
	bun.BaseModel `bun:"ballot_entry" json:"-"`

	ID         int    `bun:",pk,autoincrement" json:"id"`
	ElectionID int    `json:"electionID"`
	Name       string `json:"name"`
	IsRON      bool   `json:"isRON"`
}

func CreateBallot

func CreateBallot(electionID int, names []string, x ...bun.IDB) ([]*BallotEntry, error)

func GetAllBallotEntriesForElection

func GetAllBallotEntriesForElection(electionID int, x ...bun.IDB) ([]*BallotEntry, error)

func (*BallotEntry) Delete

func (b *BallotEntry) Delete(x ...bun.IDB) error

func (*BallotEntry) Insert

func (b *BallotEntry) Insert(x ...bun.IDB) error

type Candidate

type Candidate struct {
	bun.BaseModel `json:"-"`

	UserID     string `json:"userID"`
	ElectionID int    `json:"electionID"`
}

func (*Candidate) Delete

func (c *Candidate) Delete(x ...bun.IDB) error

func (*Candidate) Insert

func (c *Candidate) Insert(x ...bun.IDB) error

type DB

type DB struct {
	DB bun.IDB
}

type Election

type Election struct {
	bun.BaseModel `json:"-"`

	ID          int    `bun:",pk" json:"id"`
	RoleName    string `bun:",notnull" json:"roleName"`
	Description string `bun:",notnull" json:"description"`

	Poll *Poll `bun:"rel:belongs-to,join:id=id" json:"-"`
}

func GetElection

func GetElection(id int, x ...bun.IDB) (*Election, error)

func (*Election) Delete

func (e *Election) Delete(x ...bun.IDB) error

func (*Election) GetElection

func (e *Election) GetElection() *Election

func (*Election) GetFriendlyTitle

func (e *Election) GetFriendlyTitle() string

func (*Election) GetPoll

func (e *Election) GetPoll() *Poll

func (*Election) GetReferendum

func (e *Election) GetReferendum() *Referendum

func (*Election) Insert

func (e *Election) Insert(x ...bun.IDB) error

func (*Election) Update

func (e *Election) Update(x ...bun.IDB) error

func (*Election) WithCandidates

func (e *Election) WithCandidates(x ...bun.IDB) (*ElectionWithCandidates, error)

type ElectionCandidate

type ElectionCandidate struct {
	Name string
	ID   string
}

type ElectionOutcome

type ElectionOutcome struct {
	bun.BaseModel `json:"-"`

	ID     int `bun:",pk" json:"id"`
	Rounds int `bun:",notnull" json:"rounds"`

	PollOutcome *PollOutcome             `bun:"rel:belongs-to,join:id=id" json:"-"`
	Results     []*ElectionOutcomeResult `bun:"rel:has-many,join:id=election_outcome_id" json:"results"`
}

func (*ElectionOutcome) Delete

func (e *ElectionOutcome) Delete(x ...bun.IDB) error

func (*ElectionOutcome) Insert

func (e *ElectionOutcome) Insert(x ...bun.IDB) error

func (*ElectionOutcome) Update

func (e *ElectionOutcome) Update(x ...bun.IDB) error

type ElectionOutcomeResult

type ElectionOutcomeResult struct {
	bun.BaseModel `json:"-"`

	ID                int    `bun:",pk,autoincrement" json:"id"`
	Name              string `bun:",notnull" json:"name"`
	Round             int    `bun:",notnull" json:"round"`
	Votes             int    `bun:",notnull" json:"voteCount"`
	IsRejected        bool   `bun:",notnull" json:"isRejected"`
	IsElected         bool   `bun:",notnull" json:"isElected"`
	ElectionOutcomeID int    `bun:",notnull" json:"-"`

	ElectionOutcome *ElectionOutcome `bun:"rel:belongs-to,join:election_outcome_id=id" json:"-"`
}

type ElectionWithCandidates

type ElectionWithCandidates struct {
	Election
	Candidates []*ElectionCandidate `json:"candidates"`
}

type Poll

type Poll struct {
	bun.BaseModel `json:"-"`

	ID          int  `bun:",pk,autoincrement" json:"id"`
	PollTypeID  int  `bun:",notnull" json:"-"`
	IsActive    bool `bun:",notnull" json:"isActive"`
	IsConcluded bool `bun:",notnull" json:"isConcluded"`

	PollType   *PollType    `bun:"rel:has-one,join:poll_type_id=id" json:"pollType"`
	Election   *Election    `bun:"rel:has-one,join:id=id" json:"election,omitempty"`
	Referendum *Referendum  `bun:"rel:has-one,join:id=id" json:"referendum,omitempty"`
	Outcome    *PollOutcome `bun:"rel:has-one,join:id=poll_id" json:"-"`
}

func GetActivePoll

func GetActivePoll(x ...bun.IDB) (*Poll, error)

func GetAllPolls

func GetAllPolls(x ...bun.IDB) ([]*Poll, error)

func GetPoll

func GetPoll(id int, x ...bun.IDB) (*Poll, error)

func (*Poll) Delete

func (p *Poll) Delete(x ...bun.IDB) error

func (*Poll) Insert

func (p *Poll) Insert(x ...bun.IDB) error

func (*Poll) Update

func (p *Poll) Update(x ...bun.IDB) error

type PollOutcome

type PollOutcome struct {
	bun.BaseModel `json:"-"`

	ID          int       `bun:",pk,autoincrement" json:"id"`
	PollID      int       `bun:",notnull,unique" json:"-"`
	Date        time.Time `bun:",notnull,default:current_timestamp" json:"date"`
	Ballots     int       `bun:",notnull" json:"ballots"`
	IsPublished bool      `bun:",notnull" json:"isPublished"`

	Poll              *Poll              `bun:"rel:belongs-to,join:poll_id=id" json:"poll"`
	ElectionOutcome   *ElectionOutcome   `bun:"rel:has-one,join:id=id" json:"electionOutcome,omitempty"`
	ReferendumOutcome *ReferendumOutcome `bun:"rel:has-one,join:id=id" json:"referendumOutcome,omitempty"`
}

func CreatePollOutcome

func CreatePollOutcome(pollID int, ballots int, x ...bun.IDB) (*PollOutcome, error)

func GetOutcomeForPoll

func GetOutcomeForPoll(id int, x ...bun.IDB) (*PollOutcome, error)

func (*PollOutcome) Delete

func (p *PollOutcome) Delete(x ...bun.IDB) error

func (*PollOutcome) Insert

func (p *PollOutcome) Insert(x ...bun.IDB) error

func (*PollOutcome) Update

func (p *PollOutcome) Update(x ...bun.IDB) error

type PollType

type PollType struct {
	bun.BaseModel `json:"-"`

	ID   int    `bun:",pk,autoincrement" json:"id"`
	Name string `bun:",notnull" json:"name"`
}

type Pollable

type Pollable interface {
	GetPoll() *Poll
	GetFriendlyTitle() string
	GetElection() *Election
	GetReferendum() *Referendum
}

type Referendum

type Referendum struct {
	bun.BaseModel `json:"-"`

	ID          int    `bun:",pk" json:"id"`
	Title       string `bun:",notnull" json:"title"`
	Question    string `bun:",notnull" json:"question"`
	Description string `bun:",notnull" json:"description"`

	Poll *Poll `bun:"rel:belongs-to,join:id=id" json:"-"`
}

func GetReferendum

func GetReferendum(id int, x ...bun.IDB) (*Referendum, error)

func (*Referendum) Delete

func (r *Referendum) Delete(x ...bun.IDB) error

func (*Referendum) GetElection

func (r *Referendum) GetElection() *Election

func (*Referendum) GetFriendlyTitle

func (r *Referendum) GetFriendlyTitle() string

func (*Referendum) GetPoll

func (r *Referendum) GetPoll() *Poll

func (*Referendum) GetReferendum

func (r *Referendum) GetReferendum() *Referendum

func (*Referendum) Insert

func (r *Referendum) Insert(x ...bun.IDB) error

func (*Referendum) Update

func (r *Referendum) Update(x ...bun.IDB) error

type ReferendumOutcome

type ReferendumOutcome struct {
	bun.BaseModel `json:"-"`

	ID           int `bun:",pk" json:"id"`
	VotesFor     int `bun:",notnull" json:"votesFor"`
	VotesAgainst int `bun:",notnull" json:"votesAgainst"`
	VotesAbstain int `bun:",notnull" json:"votesAbstain"`

	PollOutcome *PollOutcome `bun:"rel:belongs-to,join:id=id" json:"-"`
}

func (*ReferendumOutcome) Delete

func (r *ReferendumOutcome) Delete(x ...bun.IDB) error

func (*ReferendumOutcome) Insert

func (r *ReferendumOutcome) Insert(x ...bun.IDB) error

func (*ReferendumOutcome) Update

func (r *ReferendumOutcome) Update(x ...bun.IDB) error

type User

type User struct {
	bun.BaseModel `json:"-"`

	StudentID    string `bun:"id,pk" json:"studentID"`
	Name         string `json:"name"`
	PasswordHash string `json:"-"`
	IsRestricted bool   `json:"isRestricted"`
	IsAdmin      bool   `json:"isAdmin"`
}

func GetAllUsers

func GetAllUsers(x ...bun.IDB) ([]*User, error)

func GetUser

func GetUser(id string, x ...bun.IDB) (*User, error)

func GetUsersStandingForElection

func GetUsersStandingForElection(electionID int, x ...bun.IDB) ([]*User, error)

func (*User) Insert

func (u *User) Insert(x ...bun.IDB) error

func (*User) Update

func (u *User) Update(x ...bun.IDB) error

type Vote

type Vote struct {
	bun.BaseModel `json:"-"`

	ID      int    `bun:",pk,autoincrement" json:"id"`
	PollID  int    `json:"pollID"`
	UserID  string `json:"userID"`
	Choices []int  `json:"choices"`

	Poll *Poll `bun:"rel:belongs-to,join:poll_id=id" json:"poll"`
}

func GetAllVotesForPoll

func GetAllVotesForPoll(pollID int, x ...bun.IDB) ([]*Vote, error)

func (*Vote) Insert

func (v *Vote) Insert(x ...bun.IDB) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL