model

package
v0.0.0-...-26b15f6 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2017 License: MIT Imports: 11 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrUserDoesNotExist = errors.New("This user does not exist")

ErrUserDoesNotExist error message if user does not exist in database

Functions

func AddNewPoll

func AddNewPoll(pollTitle string, pollOptions []string, user User) (string, error)

AddNewPoll adds new poll to the database with pollTitle, pollOptions, Author returns: pollID, error

func CreateUserSession

func CreateUserSession(w http.ResponseWriter, userID, username string) error

CreateUserSession creates session for current user, used for loggin user in. Currently Logged In user is defined with Cookie that contains jwt string with the relevant user data (id: user.id, username: user.username, loggedIn: bool)

func DeletePoll

func DeletePoll(pollID string) error

DeletePoll deletes poll with id = pollID and returns error if error occur

func DestroyUserSession

func DestroyUserSession(w http.ResponseWriter, r *http.Request) error

DestroyUserSession destroys session by changing/emptying fields in currently active user cookie

func GetPollVotes

func GetPollVotes(pollID string) ([][]string, error)

GetPollVotes returns vote count for poll with id = pollID returns [[VoteOption1, count1], [VoteOption2, count2]]

func GetVoteOptions

func GetVoteOptions(pollID string) ([]string, error)

GetVoteOptions returns array of vote options that exist in db for chosen poll with id = pollID.

func HashPassword

func HashPassword(password string) ([]byte, error)

HashPassword hashes inserted users password

func PollAddUserVote

func PollAddUserVote(pollID, optionID, userID string) error

PollAddUserVote checks if user already voted on the chosen poll

func PollUpdate

func PollUpdate(poll Poll, newPollTitle string, newPollOptions [][]string) error

PollUpdate handles poll updates with new data posted from poll_edit template

func RegisterNewUser

func RegisterNewUser(username string, passwordHash []byte, email string) (string, error)

RegisterNewUser creates new row in database for new user

func UserEmailCheck

func UserEmailCheck(email string) (bool, error)

UserEmailCheck checks if email already exists in database false => email does not exist true => email does exist in db

func UserExistCheck

func UserExistCheck(username string) (bool, error)

UserExistCheck checks if user with username exists false => user does not exist true => user already in database error => an error happened

Types

type LoginErrors

type LoginErrors struct {
	Username      string
	ErrorUsername string
	ErrorPassword string
	LoggedInUser  User
}

LoginErrors struct is used for displaying errors on login screen

type LoginUser

type LoginUser struct {
	ID           string
	Username     string
	PasswordHash []byte
}

LoginUser is used to gather data from database query

func GetUserLoginData

func GetUserLoginData(username string, password string) (LoginUser, error)

GetUserLoginData get's the login data for chosen username

type Poll

type Poll struct {
	ID         string
	Author     string // author username
	Title      string // title of the poll
	Time       string
	Options    [][]string // contains [[option title, option_id]]
	Votes      [][]string // contains [[vote Option, vote count]]
	NumOfVotes string     // for displaying number of all votes for the Poll
	//ErrorPostVote string     // display error when user submits his vote
	LoggedInUser User       // User struct for rendering different templates based on user login status
	Errors       PollErrors // displaying error messages in new/edit poll templates
}

Poll structure used to parse values from database

func GetFrontPageData

func GetFrontPageData(maxID int, limit int) ([]Poll, error)

GetFrontPageData returns array of polls based on chosen maxID(max poll id) and limit of results

func GetPollDetails

func GetPollDetails(pollID string) (Poll, error)

GetPollDetails get's data about poll with id = pollID returned: {pollID, Title, Author, [pollOption, polloptionID]}

func GetUserPolls

func GetUserPolls(username string, maxID int, limit int) ([]Poll, error)

GetUserPolls fetches the database and returns user polls based on the the function arguments userName: user useranem maxID: maximum poll id limit: number of polls

type PollErrors

type PollErrors struct {
	Title            string // upon error fill input with .Title
	TitleError       string // display error when title is not suitable
	VoteOptions      []string
	VoteOptionsError string // display error when user submitted < 2 vote options
	PostVoteError    string // display error when user submits his vote
	EditPollError    string // display error upon submitting edit poll form
}

PollErrors are used to display /new/edit poll tempaltes

type RegisterErrors

type RegisterErrors struct {
	Username      string
	ErrorUsername string

	Email      string
	ErrorEmail string

	Password      string
	ErrorPassword string
	LoggedInUser  User
}

RegisterErrors stores error messages that will be displayed on register template if they occur

type User

type User struct {
	ID       string // user id
	Username string // users username
	LoggedIn bool   // true => logged in, false => not logged in
}

User struct used for creating tokens and parsing data from cookies

func GetUserData

func GetUserData(tokenString string) (User, bool)

GetUserData gets userData from JWT token string and returns UserData (users.id, users.username) loggedIn (bool): true if token is formed correctly

false if token is forged or an error occured

func LoggedInUser

func LoggedInUser(r *http.Request) User

LoggedInUser checks if GoVote(auth) cookie is present in client request and returns userStruct with {ID: user.id, Username: user.username, LoggedIn: true/false}

Jump to

Keyboard shortcuts

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