models

package
v0.0.0-...-4998d35 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2018 License: GPL-3.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const MongoKey mongokey = "mongo"

MongoKey contains the Mongo session for the Request.

Variables

View Source
var ErrNotFound = mgo.ErrNotFound

ErrNotFound returned when an object is not found.

Functions

func EnsureQuestionData

func EnsureQuestionData(session Session)

EnsureQuestionData is used to make sure the question collection ready. The RemoveAll -> Insert is rough but will work at this point (TODO: Find a beautiful way to write this + Improve to do a smart insert)

func EnsureScoreData

func EnsureScoreData(session Session)

EnsureScoreData makes sure the score collection is ready. The RemoveAll -> Insert is rough but will work at this point (TODO: Find a beautiful way to write this + Improve to do a smart insert)

func PrepareDB

func PrepareDB(session Session)

PrepareDB ensure presence of persistent and immutable data in the DB.

Types

type Collection

type Collection interface {
	Find(query interface{}) *mgo.Query
	Count() (n int, err error)
	FindId(id interface{}) *mgo.Query
	Insert(docs ...interface{}) error
	Remove(selector interface{}) error
	Update(selector interface{}, update interface{}) error
	Upsert(selector interface{}, update interface{}) (info *mgo.ChangeInfo, err error)
	EnsureIndex(index mgo.Index) error
	RemoveAll(selector interface{}) (info *mgo.ChangeInfo, err error)
}

Collection is an interface to access to the collection struct.

type DataLayer

type DataLayer interface {
	C(name string) Collection
	GetQuestions() ([]Question, error)
	GetQuestion(qid int) (Question, error)
	GetScores() ([]Score, error)
	FindTopScores() ([]Score, error)
}

DataLayer is an interface to access to the database struct (currently MongoDatabase).

type FakeCollection

type FakeCollection struct{}

FakeCollection satisfies Collection and act as a mock.

func (FakeCollection) Count

func (fc FakeCollection) Count() (n int, err error)

Count mock.

func (FakeCollection) EnsureIndex

func (fc FakeCollection) EnsureIndex(index mgo.Index) error

EnsureIndex mock.

func (FakeCollection) Find

func (fc FakeCollection) Find(query interface{}) *mgo.Query

Find mock.

func (FakeCollection) FindId

func (fc FakeCollection) FindId(id interface{}) *mgo.Query

FindId mock.

func (FakeCollection) Insert

func (fc FakeCollection) Insert(docs ...interface{}) error

Insert mock.

func (FakeCollection) Remove

func (fc FakeCollection) Remove(selector interface{}) error

Remove mock.

func (FakeCollection) RemoveAll

func (fc FakeCollection) RemoveAll(selector interface{}) (info *mgo.ChangeInfo, err error)

RemoveAll mock.

func (FakeCollection) Update

func (fc FakeCollection) Update(selector interface{}, update interface{}) error

Update mock.

func (FakeCollection) Upsert

func (fc FakeCollection) Upsert(selector interface{}, update interface{}) (info *mgo.ChangeInfo, err error)

Upsert mock.

type FakeDatabase

type FakeDatabase struct{}

FakeDatabase satisfies DataLayer and act as a mock.

func (FakeDatabase) C

func (db FakeDatabase) C(name string) Collection

C mocks mgo.Database(name).Collection(name).

func (FakeDatabase) FindTopScores

func (db FakeDatabase) FindTopScores() ([]Score, error)

FindTopScores mocks models.FindTopScores().

func (FakeDatabase) GetQuestion

func (db FakeDatabase) GetQuestion(qid int) (Question, error)

GetQuestion mocks models.GetQuestion().

func (FakeDatabase) GetQuestions

func (db FakeDatabase) GetQuestions() ([]Question, error)

GetQuestions mocks models.GetQuestions().

func (FakeDatabase) GetScores

func (db FakeDatabase) GetScores() ([]Score, error)

GetScores mocks models.GetScores().

type FakeSession

type FakeSession struct{}

FakeSession satisfies Session and act as a mock of *mgo.session.

func (FakeSession) Close

func (fs FakeSession) Close()

Close mocks mgo.Session.Close().

func (FakeSession) Copy

func (fs FakeSession) Copy() Session

Copy mocks mgo.Session.Copy(). Regarding the context of use, no need to actually Copy the mock.

func (FakeSession) DB

func (fs FakeSession) DB(name string) DataLayer

DB mocks mgo.Session.DB().

func (FakeSession) SetSafe

func (fs FakeSession) SetSafe(safe *mgo.Safe)

SetSafe mocks mgo.Session.SetSafe().

func (FakeSession) SetSocketTimeout

func (fs FakeSession) SetSocketTimeout(d time.Duration)

SetSocketTimeout mocks mgo.Session.SetSocketTimeout().

func (FakeSession) SetSyncTimeout

func (fs FakeSession) SetSyncTimeout(d time.Duration)

SetSyncTimeout mocks mgo.Session.SetSyncTimeout().

type MongoCollection

type MongoCollection struct {
	*mgo.Collection
}

MongoCollection wraps a mgo.Collection to embed methods in models.

type MongoDatabase

type MongoDatabase struct {
	*mgo.Database
}

MongoDatabase wraps a mgo.Database to embed methods in models.

func (MongoDatabase) C

func (d MongoDatabase) C(name string) Collection

C shadows *mgo.DB to returns a DataLayer interface instead of *mgo.Database.

func (*MongoDatabase) FindTopScores

func (db *MongoDatabase) FindTopScores() ([]Score, error)

FindTopScores returns all scores.

func (*MongoDatabase) GetQuestion

func (db *MongoDatabase) GetQuestion(qid int) (Question, error)

GetQuestion returns a Question from an ID otherwise nil.

func (*MongoDatabase) GetQuestions

func (db *MongoDatabase) GetQuestions() ([]Question, error)

GetQuestions returns all Questions.

func (*MongoDatabase) GetScores

func (db *MongoDatabase) GetScores() ([]Score, error)

GetScores returns all scores.

type MongoSession

type MongoSession struct {
	*mgo.Session
}

MongoSession is currently a Mongo session.

func (MongoSession) Copy

func (s MongoSession) Copy() Session

Copy mocks mgo.Session.Copy()

func (MongoSession) DB

func (s MongoSession) DB(name string) DataLayer

DB shadows *mgo.DB to returns a DataLayer interface instead of *mgo.Database.

type Question

type Question struct {
	QID            int     `bson:"qid" json:"qid"`
	Sentence       string  `bson:"sentence" json:"sentence"`
	MatchPositions [][]int `bson:"match_positions" json:"match_positions"`
}

Question represent a regex to find and the related context (sentence, match).

func (*Question) FormatHTMLSentence

func (q *Question) FormatHTMLSentence() string

FormatHTMLSentence return a sentence with matches wrapped with HTML tags.

func (*Question) GetNextJSONQuestion

func (q *Question) GetNextJSONQuestion(db DataLayer, qid int) map[string]interface{}

GetNextJSONQuestion returns the next JSON question with HTML Sentence.

type Score

type Score struct {
	Username  string `bson:"username" json:"username"`
	BestScore int    `bson:"best_score" json:"best_score"`
	Submitted bool   `bson:"submitted" json:"submitted"`
}

Score represent a unique score.

func (*Score) InsertScore

func (score *Score) InsertScore(db DataLayer) error

InsertScore store a new score.

func (*Score) SubmitScore

func (score *Score) SubmitScore(db DataLayer, token string) error

SubmitScore replace token by username and set submitted to true.

func (*Score) UpsertScore

func (score *Score) UpsertScore(db DataLayer) error

UpsertScore store/replace a score.

type Session

type Session interface {
	DB(name string) DataLayer
	SetSafe(safe *mgo.Safe)
	SetSyncTimeout(d time.Duration)
	SetSocketTimeout(d time.Duration)
	Close()
	Copy() Session
}

Session is an interface to access to the Session struct.

func NewFakeSession

func NewFakeSession() Session

NewFakeSession mosck NewSession.

func NewSession

func NewSession() Session

NewSession returns a new Mongo Session.

Jump to

Keyboard shortcuts

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