models

package
v0.0.0-...-9f9c045 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2017 License: GPL-3.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateAdmin

func CreateAdmin(adminPwd string)

func CreateCollections

func CreateCollections(collections []string, keys []string)

CreateCollections ensures that indices exist for a collection @collections: The names of the collections @keys: The names of the keys to create indices for

func DeleteUserSession

func DeleteUserSession(displayName string) error

func GenerateSessionKey

func GenerateSessionKey(length int) string

func GetAllUsers

func GetAllUsers() (*[]ResponseUser, error)

func GetMgoSession

func GetMgoSession(collectionName string) (*mgo.Session, *mgo.Collection)

GetMgoSession returns a session and collection @collection: The name of the collection @return:

session:        Call `defer session.Close()` on this
collectionName: Perform any db operations on this

func GetResponseComments

func GetResponseComments(questionID bson.ObjectId) (*[]ResponseComment, error)

GetResponseComments returns a populated ResponseComment

func IncVotes

func IncVotes(
	colType int,
	id bson.ObjectId,
	inc int,
) error

IncVotes is a generic incrementor for the .Votes properties of the

other models like Answer, Comment and Question

@colType: The type of the collection, see types.go @id: The _id of the object @inc: 1 or -1

func InitMongoDB

func InitMongoDB(host string)

InitMongoDB must be called before any database operations @host: The hostname of the MongoDB server

func IsAuthenticated

func IsAuthenticated(r *http.Request) *string

IsAuthenticated checks the cookies in an HTTP request for valid authentication credentials.

DO NOT pass the error back to the user, it could contain the token

func QuestionIncViews

func QuestionIncViews(questionID bson.ObjectId) error

QuestionIncViews increments the page views for a question

func SetAuthCookies

func SetAuthCookies(
	displayName string,
	isAdmin bool,
	w http.ResponseWriter) error

SetAuthCookies sets the authentication cookies

func UpdateEmail

func UpdateEmail(displayName string, newEmail string) error

func UpdatePassword

func UpdatePassword(displayName string, password string) error

func UserExists

func UserExists(displayName string) bool

Types

type Answer

type Answer struct {
	ID         bson.ObjectId `bson:"_id,omitempty"`
	QuestionID bson.ObjectId
	UserName   string
	Text       string
	CreatedAt  time.Time
	Votes      int
}

Answer abstracts the database schema for an answer to a Question

type Comment

type Comment struct {
	ID         bson.ObjectId `bson:"_id,omitempty"`
	QuestionID bson.ObjectId
	ParentID   bson.ObjectId
	ParentType int
	UserName   string
	Text       string
	CreatedAt  time.Time
	Votes      int
}

Comment abstracts a comment on a Question or Answer

type FormError

type FormError struct {
	Messages []string
}

type IDKeyPair

type IDKeyPair struct {
	Type int
	ID   bson.ObjectId
}

type NewAnswer

type NewAnswer struct {
	QuestionID bson.ObjectId
	UserName   string
	Text       string
}

NewAnswer abstracts the body of the JSON REST call to create an answer

func (*NewAnswer) Create

func (q *NewAnswer) Create() (*Answer, error)

Create converts a NewAnswer instance into a Answer instance that can be inserted into the database, then performs the insertion

func (*NewAnswer) Validate

func (q *NewAnswer) Validate() error

Validate performs validation on NewAnswer that has been unmarshalled from the body of an HTTP request

type NewComment

type NewComment struct {
	QuestionID bson.ObjectId
	ParentID   bson.ObjectId
	ParentType int
	UserName   string
	Text       string
}

NewComment abstracts the body of the JSON REST call to create an comment

func (*NewComment) Create

func (q *NewComment) Create() (*Comment, error)

Create converts a NewComment instance into a Comment instance that can be inserted into the database, then performs the insertion

func (*NewComment) Validate

func (q *NewComment) Validate() error

Validate performs validation on NewComment that has been unmarshalled from the body of an HTTP request

type NewEdit

type NewEdit struct {
	QuestionID bson.ObjectId
	ParentID   bson.ObjectId
	Text       string
	Type       int
	UserName   string
}

func (*NewEdit) Create

func (e *NewEdit) Create() error

type NewQuestion

type NewQuestion struct {
	UserName string
	Text     string
	Title    string
	Tags     []string
}

NewQuestion abstracts the body of the JSON REST call to create a question

func (*NewQuestion) Create

func (q *NewQuestion) Create() (*Question, error)

Create converts a NewQuestion instance into a Question instance that can be inserted into the database, then performs the insertion

func (*NewQuestion) Update

func (q *NewQuestion) Update(questionID bson.ObjectId) error

Update performs a question edit

func (*NewQuestion) Validate

func (q *NewQuestion) Validate() error

Validate performs validation on NewQuestion that has been unmarshalled from the body of an HTTP request

type NewUser

type NewUser struct {
	Email       string
	DisplayName string
	IsAdmin     bool
	Password    string
}

NewUser abstracts a user login

func (*NewUser) Create

func (u *NewUser) Create() error

type NewUserLogin

type NewUserLogin struct {
	DisplayName string
	Password    string
}

NewUserLogin abstracts a user login

func (*NewUserLogin) Validate

func (u *NewUserLogin) Validate() (bool, error)

NewUserLogin.Validate tests a set of login credentials

@return: IsAdmin, error

type NewVote

type NewVote struct {
	ParentID   bson.ObjectId
	QuestionID bson.ObjectId
	IsUp       bool // true if up-vote, false if down-vote
	UserName   string
	Type       int
}

NewVote abstracts the JSON body of an upvote or downvote to a Question, Answer or Comment

func (*NewVote) Create

func (v *NewVote) Create() (*Vote, error)

Create converts a NewVote instance into a Vote instance that can be inserted into the database, then performs the insertion

func (*NewVote) Validate

func (v *NewVote) Validate() error

Validate performs validation on NewVote that has been unmarshalled from the body of an HTTP request

type Question

type Question struct {
	ID             bson.ObjectId `bson:"_id,omitempty"`
	AcceptedAnswer *bson.ObjectId
	UserName       string
	LastEditedBy   string
	Text           string
	Title          string
	CreatedAt      time.Time
	Tags           []string
	Votes          int
	Views          int64
}

Question abstracts the database schema for a question that has been asked

type QuestionEdit

type QuestionEdit struct {
	LastEditedBy string
	Text         string
	Title        string
	Tags         []string
}

NewQuestion abstracts the body of the JSON REST call to create a question

type ResponseAnswer

type ResponseAnswer struct {
	ID           string
	QuestionID   string
	ParentID     string
	UserName     string
	Text         string
	CreatedAt    time.Time
	Comments     []ResponseComment
	Type         int
	Votes        int
	LastEditedBy string
}

ResponseAnswer abstracts the JSON response for a question page load

type ResponseAnswers

type ResponseAnswers []ResponseAnswer

implements sort.Interface

func GetResponseAnswers

func GetResponseAnswers(questionID bson.ObjectId) (ResponseAnswers, error)

GetResponseAnswers returns a populated ResponseAnswer

func (ResponseAnswers) Len

func (slice ResponseAnswers) Len() int

func (ResponseAnswers) Less

func (slice ResponseAnswers) Less(i, j int) bool

func (ResponseAnswers) Swap

func (slice ResponseAnswers) Swap(i, j int)

type ResponseComment

type ResponseComment struct {
	ID           string
	QuestionID   string
	ParentID     string
	ParentType   int
	UserName     string
	Text         string
	CreatedAt    time.Time
	Votes        int
	Type         int
	LastEditedBy string
}

ResponseComment abstracts the JSON response for a question page load

type ResponseQuestion

type ResponseQuestion struct {
	ID           string
	QuestionID   string
	ParentID     string
	UserName     string
	LastEditedBy string
	CreatedAt    time.Time
	Text         string
	Title        string
	Tags         []string
	Type         int
	Comments     []ResponseComment
	Answers      ResponseAnswers
	Votes        int
	Views        int64
}

ResponseQuestion abstracts the JSON response for a question page load

func GetResponseQuestion

func GetResponseQuestion(questionID bson.ObjectId) (*ResponseQuestion, error)

GetResponseQuestion returns a populated ResponseQuestion

type ResponseText

type ResponseText struct {
	Text string
}

func Text

func Text(objType int, id bson.ObjectId) (*ResponseText, error)

type ResponseUser

type ResponseUser struct {
	Email       string
	DisplayName string
	IsAdmin     bool
	Questions   []SearchResult
}

ResponseUser abstracts a user login

func GetResponseUser

func GetResponseUser(displayName string) *ResponseUser

type ResponseVote

type ResponseVote struct {
	ID      bson.ObjectId `bson:"_id,omitempty"`
	Votes   int           // TODO: index
	VoteMap map[bson.ObjectId]int
}

ResponseVote tallies the votes for each item

func GetResponseVotes

func GetResponseVotes(questionID bson.ObjectId) (*ResponseVote, error)

GetResponseVotes returns a populated ResponseVote

func (*ResponseVote) GetVotes

func (r *ResponseVote) GetVotes(parentID bson.ObjectId) int

GetVotes counts the votes for a question and all child answers and comments

type SearchPage

type SearchPage struct {
	Message string
	Results []SearchResult
}
func Search(query string, limit int) (*SearchPage, error)

type SearchResult

type SearchResult struct {
	Title      string
	QuestionID string
	UserName   string
	CreatedAt  time.Time
	Tags       []string
	Votes      int
	Views      int64
}

type User

type User struct {
	ID           bson.ObjectId `bson:"_id,omitempty"`
	Email        string
	DisplayName  string
	IsAdmin      bool
	PasswordHash []byte
	CreatedAt    time.Time
	Deleted      bool
}

User abstracts a user login

func GetUser

func GetUser(displayName string) *User

type UserSession

type UserSession struct {
	ID          bson.ObjectId `bson:"_id,omitempty"`
	DisplayName string
	SessionKey  string
	CreatedAt   time.Time
	Expires     time.Time
}

UserSession abstracts an authenticated session cookie pair

func CreateUserSession

func CreateUserSession(displayName string) (*UserSession, error)

func GetUserSession

func GetUserSession(displayName string) *UserSession

type Vote

type Vote struct {
	ID         bson.ObjectId `bson:"_id"`
	QuestionID bson.ObjectId
	ParentID   bson.ObjectId
	IsUp       bool // true if up-vote, false if down-vote
	UserName   string
	CreatedAt  time.Time
	Type       int
}

Vote abstracts an upvote or downvote to a Question, Answer or Comment

Jump to

Keyboard shortcuts

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