// DeleteCookie deletes a cookie from the cookie storage func (store *DatabaseCookieStore) DeleteCookie(cookie *Session) error { session, collection := service.Connect(store.location) defer session.Close() err := collection.Remove(bson.M{"token": cookie.Token}) return err }
// DeleteTransaction removes a Transaction from the database func DeleteTransaction(transactionID bson.ObjectId) error { session, collection := service.Connect(collectionName) defer session.Close() err := collection.RemoveId(transactionID) return err }
func deleteUser(userID bson.ObjectId) error { session, collection := service.Connect(collectionName) defer session.Close() err := collection.RemoveId(userID) return err }
// ReadCookie fetches a cookie from the cookie store func (store *DatabaseCookieStore) ReadCookie(key string) (*Session, error) { session, collection := service.Connect(store.location) defer session.Close() cookie := Session{} err := collection.Find(bson.M{"token": key}).One(&cookie) return &cookie, err }
// GetAllTransactionsLimited retrieves the first X Transaction entities from the database, where X is the specified limit func GetAllTransactionsLimited(limit int) ([]dbmodels.Transaction, error) { session, collection := service.Connect(collectionName) defer session.Close() var transactions []dbmodels.Transaction err := collection.Find(bson.M{}).Limit(limit).All(&transactions) return transactions, err }
// GetTransaction retrieves an Transaction from the database, based on its ID func GetTransaction(transactionID bson.ObjectId) (*dbmodels.Transaction, error) { session, collection := service.Connect(collectionName) defer session.Close() transaction := dbmodels.Transaction{} err := collection.FindId(transactionID).One(&transaction) return &transaction, err }
// GetUserByEmail retrieves an ApplicationUser from the database, based on its email address func GetUserByEmail(emailAddress string) (*ApplicationUser, error) { session, collection := service.Connect(collectionName) defer session.Close() var user = ApplicationUser{} var err = collection.Find(bson.M{"email": emailAddress}).One(&user) return &user, err }
// GetUserByResetPasswordToken retrieves an ApplicationUser from the database, based on its reset password token func GetUserByResetPasswordToken(token string) (*ApplicationUser, error) { session, collection := service.Connect(collectionName) defer session.Close() var user = ApplicationUser{} var err = collection.Find(bson.M{"resetPasswordToken": token}).One(&user) return &user, err }
// GetUserByActivationToken retrieves an ApplicationUser from the database, based on its account activation token func GetUserByActivationToken(token string) (*ApplicationUser, error) { session, collection := service.Connect(collectionName) defer session.Close() var user = ApplicationUser{} var err = collection.Find(bson.M{"activateAccountToken": token}).One(&user) return &user, err }
// GetUser retrieves an ApplicationUser from the database, based on its ID func GetUser(userID bson.ObjectId) (*ApplicationUser, error) { session, collection := service.Connect(collectionName) defer session.Close() var user = ApplicationUser{} var err = collection.FindId(userID).One(&user) return &user, err }
// GetAllUserCookies returns all the cookies that a certain user has func (store *DatabaseCookieStore) GetAllUserCookies(userID bson.ObjectId) ([]*Session, error) { session, collection := service.Connect(store.location) defer session.Close() var userSessions []*Session err := collection.Find(bson.M{"userID": userID}).All(&userSessions) return userSessions, err }
// WriteCookie writes a cookie in the cookie store. If that cookie already exists, // it is overwritten func (store *DatabaseCookieStore) WriteCookie(cookie *Session) error { session, collection := service.Connect(store.location) defer session.Close() err := collection.UpdateId(cookie.ID, cookie) if err == mgo.ErrNotFound { err = collection.Insert(cookie) } return err }
// UpdateUser updates an existing ApplicationUser in the database func UpdateUser(user *ApplicationUser) error { session, collection := service.Connect(collectionName) defer session.Close() if user.ID == "" { return service.ErrNoIDSpecified } var err = collection.UpdateId(user.ID, user) return err }
// CreateTransaction adds a new Transaction to the database func CreateTransaction(transaction *dbmodels.Transaction) error { session, collection := service.Connect(collectionName) defer session.Close() if transaction.ID == "" { transaction.ID = bson.NewObjectId() } err := collection.Insert(transaction) return err }
// UpdateTransaction updates an existing Transaction in the database func UpdateTransaction(transaction *dbmodels.Transaction) error { session, collection := service.Connect(collectionName) defer session.Close() if transaction.ID == "" { return service.ErrNoIDSpecified } err := collection.UpdateId(transaction.ID, transaction) return err }
// CreateUser adds a new ApplicationUser to the database func CreateUser(user *ApplicationUser) error { session, collection := service.Connect(collectionName) defer session.Close() if user.ID == "" { user.ID = bson.NewObjectId() } var err = collection.Insert(user) return err }
// Init initializes the cookie store func (store *DatabaseCookieStore) Init() { session, collection := service.Connect(store.location) defer session.Close() session.SetMode(mgo.Monotonic, true) if store.hasTokenIndex(collection) { return } index := mgo.Index{ Key: []string{"$text:token"}, } err := collection.EnsureIndex(index) if err != nil { panic(err) } }