Documentation ¶
Index ¶
- Constants
- Variables
- func EnsureQuestionData(session Session)
- func EnsureScoreData(session Session)
- func PrepareDB(session Session)
- type Collection
- type DataLayer
- type FakeCollection
- func (fc FakeCollection) Count() (n int, err error)
- func (fc FakeCollection) EnsureIndex(index mgo.Index) error
- func (fc FakeCollection) Find(query interface{}) *mgo.Query
- func (fc FakeCollection) FindId(id interface{}) *mgo.Query
- func (fc FakeCollection) Insert(docs ...interface{}) error
- func (fc FakeCollection) Remove(selector interface{}) error
- func (fc FakeCollection) RemoveAll(selector interface{}) (info *mgo.ChangeInfo, err error)
- func (fc FakeCollection) Update(selector interface{}, update interface{}) error
- func (fc FakeCollection) Upsert(selector interface{}, update interface{}) (info *mgo.ChangeInfo, err error)
- type FakeDatabase
- type FakeSession
- type MongoCollection
- type MongoDatabase
- type MongoSession
- type Question
- type Score
- type Session
Constants ¶
const MongoKey mongokey = "mongo"
MongoKey contains the Mongo session for the Request.
Variables ¶
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)
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) EnsureIndex ¶
func (fc FakeCollection) EnsureIndex(index mgo.Index) error
EnsureIndex 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) 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) 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 ¶
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 ¶
MongoSession is currently a Mongo session.
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 ¶
FormatHTMLSentence return a sentence with matches wrapped with HTML tags.
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 ¶
InsertScore store a new score.
func (*Score) SubmitScore ¶
SubmitScore replace token by username and set submitted to true.
func (*Score) UpsertScore ¶
UpsertScore store/replace a score.