database

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

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

Go to latest
Published: Oct 8, 2018 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (

	// TableCategory ...
	TableCategory = "category"
	// TableUser ...
	TableUser = "user"
	// TableTopic ...
	TableTopic = "topic"
	// TableCookie ...
	TableCookie = "cookie"
	// TableComment ...
	TableComment = "comment"
	//TableEmailToken = "eToken"
	// TableAdmin ...
	TableAdmin = "admin"
)

DATABASE TABLES

View Source
const (
	// CookieName ...
	CookieName = "HackerBook"
	// CookieExpiration defines lifespan of cookie
	CookieExpiration = time.Hour * 24
)

COOKIE CONST

Variables

This section is empty.

Functions

This section is empty.

Types

type AdminUser

type AdminUser struct {
	ID     bson.ObjectId `bson:"_id,omitempty" valid:"-"`
	UserID bson.ObjectId `json:"userID" valid:"-, required"`
}

AdminUser is json struct for a user that also is an admin

type Category

type Category struct {
	ID     bson.ObjectId   `bson:"_id,omitempty" valid:"-"`
	Name   string          `json:"name" valid:"printableascii, required"`
	Topics []bson.ObjectId `json:"topics" valid:"-"`
}

Category is json struct to contain data relevant for a forum category

type Comment

type Comment struct {
	CommentID bson.ObjectId `bson:"_id,omitempty" valid:"-"`
	Username  string        `json:"username" valid:"alphanum, required"`
	Text      string        `json:"text" valid:"halfwidth"`
	ReplyTo   int           `json:"replyto" valid:"-"`
}

Comment is json struct to contain data relevant for a forum comment

type CookieData

type CookieData struct {
	ID    bson.ObjectId `json:"userid" valid:"-, required"`
	Token string        `json:"token" valid:"alphanum, required"`
}

CookieData is json struct for cookie data to be stored

type Db

type Db interface {
	InitState()
	CreateMainSession() error
	CreateSessionPtr() (*mgo.Session, error)
	ValidateMainSession() error
	InsertToCollection(collectionName string, data interface{}, session *mgo.Session) error
	AuthenticateUser(user LoginUser, session *mgo.Session) bson.ObjectId
	AuthenticateAdmin(userID bson.ObjectId, session *mgo.Session) bson.ObjectId
	IsExistingUser(user SignUpUser, session *mgo.Session) (*string, error)
	GetCookie(cookie CookieData, entry *CookieData, session *mgo.Session)
	DeleteCookie(id bson.ObjectId, session *mgo.Session)
	GetUsername(id bson.ObjectId, session *mgo.Session) string
	GetCategories(categories interface{}, session *mgo.Session) error
	GetCategory(categoryName string, category interface{}, session *mgo.Session) error
	IsExistingCategory(categoryName string, session *mgo.Session) bool
	GetTopic(categoryName string, topicID string, topic interface{}, session *mgo.Session) error
	CreateTopic(categoryName string, topic Topic, session *mgo.Session) error
	PushTopicComment(topicID string, comment Comment, session *mgo.Session) error
}

Db defines our DB API

type DbState

type DbState struct {
	Hosts    string
	DbName   string
	Username string
	Password string
	Session  *mgo.Session
}

DbState contains the connection data for our mongodb

func (*DbState) AuthenticateAdmin

func (db *DbState) AuthenticateAdmin(userID bson.ObjectId, session *mgo.Session) bson.ObjectId

AuthenticateAdmin will verify if sent admin actually is a stored admin Returns id of admin if admin was authentic

func (*DbState) AuthenticateUser

func (db *DbState) AuthenticateUser(user LoginUser, session *mgo.Session) bson.ObjectId

AuthenticateUser will verify if sent user actually is a stored user Returns id of user if user was authentic

func (*DbState) CreateMainSession

func (db *DbState) CreateMainSession() (err error)

CreateMainSession dials the mongodb with DbState data and create main session Returns error if failed

func (*DbState) CreateSessionPtr

func (db *DbState) CreateSessionPtr() (*mgo.Session, error)

CreateSessionPtr copies the DbState session. Remember to close returned pointer value when done with it Returns the copy of the session and an error if failed

func (*DbState) CreateTopic

func (db *DbState) CreateTopic(categoryName string, topic Topic, session *mgo.Session) error

CreateTopic creates a new topic in the db Returns error if session is nil or if it failed to update collection

func (*DbState) DeleteCookie

func (db *DbState) DeleteCookie(id bson.ObjectId, session *mgo.Session)

DeleteCookie deletes all cookies that share id with parameter id

func (*DbState) EnsureAllIndices

func (db *DbState) EnsureAllIndices() error

EnsureAllIndices defines index values in the DB to ensure certain properties of the db Returns an error if failed

func (*DbState) GetCategories

func (db *DbState) GetCategories(categories interface{}, session *mgo.Session) error

GetCategories retrieves all categories in the collection Returns error if session was nil

func (*DbState) GetCategory

func (db *DbState) GetCategory(categoryName string, category interface{}, session *mgo.Session) error

GetCategory retrieves a category from the collection Returns error if it failed

func (*DbState) GetCookie

func (db *DbState) GetCookie(cookie CookieData, entry *CookieData, session *mgo.Session)

GetCookie tries to insert cookie data into the entry parameter

func (*DbState) GetTopic

func (db *DbState) GetTopic(categoryName string, topicID string, topic interface{}, session *mgo.Session) error

GetTopic retrieves a topic from collection Returns an error if session was nil

func (*DbState) GetUsername

func (db *DbState) GetUsername(id bson.ObjectId, session *mgo.Session) string

GetUsername retrieves username of user that share id with parameter id Returns username or <bad boi> if failed

func (*DbState) InitState

func (db *DbState) InitState()

InitState retrieves environment variables and stores them in a DbState

func (*DbState) InsertToCollection

func (db *DbState) InsertToCollection(collectionName string, data interface{}, session *mgo.Session) error

InsertToCollection will attempt to insert interface into collection with collectionName as name Returns error if failed to insert

func (*DbState) IsExistingCategory

func (db *DbState) IsExistingCategory(categoryName string, session *mgo.Session) bool

IsExistingCategory verifies that said category does not already exist Returns false if it does not exist, true otherwise

func (*DbState) IsExistingUser

func (db *DbState) IsExistingUser(user SignUpUser, session *mgo.Session) (*string, error)

IsExistingUser checks if certain parts of a sign-up form is already used by another user Returns a string with what is in use and error if something went wrong

func (*DbState) PushTopicComment

func (db *DbState) PushTopicComment(topicID string, comment Comment, session *mgo.Session) error

PushTopicComment inserts comment into topic collection Returns error if session is nil or if it failed to update collection

func (*DbState) ValidateMainSession

func (db *DbState) ValidateMainSession() error

ValidateMainSession check if DbState session is still valid Will also attempt to recover a new session if invalid Returns error if failed to validate

type LoginUser

type LoginUser struct {
	Username string `json:"username" valid:"alphanum, required"`
	Password string `json:"password" valid:"alphanum, required"`
}

LoginUser is json struct for users manually login in

type SignUpUser

type SignUpUser struct {
	ID       bson.ObjectId `bson:"_id,omitempty" valid:"-"`
	Email    string        `json:"email" valid:"email, required"`
	Username string        `json:"username" valid:"alphanum, required"`
	Password string        `json:"password" valid:"alphanum, required"`
	Response string        `json:"captcha" valid:"ascii, required"`
}

SignUpUser is a json struct for sign up form post

type Topic

type Topic struct {
	ID       bson.ObjectId `bson:"_id" valid:"-"`
	Category string        `json:"name" valid:"alphanum, required"`
	Username string        `json:"username" valid:"alphanum, required"`
	Title    string        `json:"title" valid:"printableascii, required"`
	Content  string        `json:"content" valid:"halfwidth"`
}

Topic is json struct to contain data relevant for a forum topic

Jump to

Keyboard shortcuts

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