本文整理汇总了Golang中github.com/ardanlabs/kit/db.DB.ExecuteMGO方法的典型用法代码示例。如果您正苦于以下问题:Golang DB.ExecuteMGO方法的具体用法?Golang DB.ExecuteMGO怎么用?Golang DB.ExecuteMGO使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/ardanlabs/kit/db.DB
的用法示例。
在下文中一共展示了DB.ExecuteMGO方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: UpdateUserPassword
// UpdateUserPassword updates an existing user's password and token in the database.
func UpdateUserPassword(context interface{}, db *db.DB, u *User, password string) error {
log.Dev(context, "UpdateUserPassword", "Started : PublicID[%s]", u.PublicID)
if err := u.Validate(); err != nil {
log.Error(context, "UpdateUserPassword", err, "Completed")
return err
}
if len(password) < 8 {
err := errors.New("Invalid password length")
log.Error(context, "UpdateUserPassword", err, "Completed")
return err
}
newPassHash, err := crypto.BcryptPassword(u.PrivateID + password)
if err != nil {
log.Error(context, "UpdateUserPassword", err, "Completed")
return err
}
f := func(c *mgo.Collection) error {
q := bson.M{"public_id": u.PublicID}
upd := bson.M{"$set": bson.M{"password": newPassHash, "modified_at": time.Now().UTC()}}
log.Dev(context, "UpdateUserPassword", "MGO : db.%s.Update(%s, CAN'T SHOW)", c.Name, mongo.Query(q))
return c.Update(q, upd)
}
if err := db.ExecuteMGO(context, Collection, f); err != nil {
log.Error(context, "UpdateUserPassword", err, "Completed")
return err
}
log.Dev(context, "UpdateUserPassword", "Completed")
return nil
}
示例2: removeSessions
// removeSessions is used to clear out all the test sessions that are
// created from tests.
func removeSessions(db *db.DB) error {
f := func(c *mgo.Collection) error {
q := bson.M{"public_id": publicID}
_, err := c.RemoveAll(q)
return err
}
if err := db.ExecuteMGO(tests.Context, session.Collection, f); err != nil {
return err
}
return nil
}
示例3: removeUser
// removeUser is used to clear out all the test user from the collection.
func removeUser(db *db.DB, publicID string) error {
f := func(c *mgo.Collection) error {
q := bson.M{"public_id": publicID}
return c.Remove(q)
}
if err := db.ExecuteMGO(tests.Context, auth.Collection, f); err != nil {
return err
}
f = func(c *mgo.Collection) error {
q := bson.M{"public_id": publicID}
_, err := c.RemoveAll(q)
return err
}
if err := db.ExecuteMGO(tests.Context, session.Collection, f); err != nil {
return err
}
return nil
}
示例4: GetAllMedia
// GetAllMedia deletes a Media from the Media database.
// Returns a non-nil error if it fails.
func GetAllMedia(context interface{}, db *db.DB) ([]*Media, error) {
log.Dev(context, "GetAllMedias", "Started")
var medias []*Media
f := func(c *mgo.Collection) error {
log.Dev(context, "GetAllMedias", "MGO : db.%s.findAll()", c.Name)
return c.Find(nil).All(&medias)
}
if err := db.ExecuteMGO(context, Collection, f); err != nil {
log.Error(context, "GetAllMedias", err, "Completed")
return nil, err
}
log.Dev(context, "GetAllMedias", "Completed")
return medias, nil
}
示例5: GetAllCourse
// GetAllCourse deletes a Course from the Course database.
// Returns a non-nil error if it fails.
func GetAllCourse(context interface{}, db *db.DB) (Courses, error) {
log.Dev(context, "GetAllCourses", "Started")
var Courses Courses
f := func(c *mgo.Collection) error {
log.Dev(context, "GetAllCourses", "MGO : db.%s.findAll()", c.Name)
return c.Find(nil).All(&Courses)
}
if err := db.ExecuteMGO(context, Collection, f); err != nil {
log.Error(context, "GetAllCourses", err, "Completed")
return nil, err
}
log.Dev(context, "GetAllCourses", "Completed")
return Courses, nil
}
示例6: GetMediaByTitle
// GetMediaByTitle retrieves a Media from the Media database using the title.
// Returns a non-nil error if it fails.
func GetMediaByTitle(context interface{}, db *db.DB, title string) (*Media, error) {
log.Dev(context, "GetMediaByTitle", "Started : Media[%s]", title)
var media Media
f := func(c *mgo.Collection) error {
q := bson.M{"title": title}
log.Dev(context, "GetMediaByTitle", "MGO : db.%s.findOne(%q)", c.Name, mongo.Query(q))
return c.Find(q).One(&media)
}
if err := db.ExecuteMGO(context, Collection, f); err != nil {
log.Error(context, "GetMediaByTitle", err, "Completed : Media[%s]", title)
return nil, err
}
log.Dev(context, "GetMediaByTitle", "Completed : Media[%s]", title)
return &media, nil
}
示例7: GetBySessionID
// GetBySessionID retrieves a session from the session store.
func GetBySessionID(context interface{}, db *db.DB, sessionID string) (*Session, error) {
log.Dev(context, "GetBySessionID", "Started : SessionID[%s]", sessionID)
var s Session
f := func(c *mgo.Collection) error {
q := bson.M{"session_id": sessionID}
log.Dev(context, "GetBySessionID", "MGO : db.%s.findOne(%s)", c.Name, mongo.Query(q))
return c.Find(q).One(&s)
}
if err := db.ExecuteMGO(context, Collection, f); err != nil {
log.Error(context, "GetBySessionID", err, "Completed")
return nil, err
}
log.Dev(context, "GetBySessionID", "Completed")
return &s, nil
}
示例8: DeleteMedia
// DeleteMedia deletes a Media from the Media database.
// Returns a non-nil error if it fails.
func DeleteMedia(context interface{}, db *db.DB, code string) error {
log.Dev(context, "DeleteMedia", "Started : Media[%s]", code)
f := func(c *mgo.Collection) error {
q := bson.M{"name": code}
log.Dev(context, "DeleteMedia", "MGO : db.%s.remove(%s)", c.Name, mongo.Query(q))
return c.Remove(q)
}
err := db.ExecuteMGO(context, Collection, f)
if err != nil {
log.Error(context, "DeleteMedia", err, "Completed : Media[%s]", code)
return err
}
log.Dev(context, "DeleteMedia", "Completed : Media[%s]", code)
return nil
}
示例9: GetCoursesByAuthor
// GetCoursesByAuthor retrieves a Course from the Course database using the author.
// Returns a non-nil error if it fails.
func GetCoursesByAuthor(context interface{}, db *db.DB, author string) (Courses, error) {
log.Dev(context, "GetCoursesByAuthor", "Started : Author[%s]", author)
var Courses Courses
f := func(c *mgo.Collection) error {
q := bson.M{"author": author}
log.Dev(context, "GetCoursesByAuthor", "MGO : db.%s.find(%q)", c.Name, mongo.Query(q))
return c.Find(q).All(&Courses)
}
if err := db.ExecuteMGO(context, Collection, f); err != nil {
log.Error(context, "GetCoursesByAuthor", err, "Completed : Author[%s]", author)
return nil, err
}
log.Dev(context, "GetCoursesByAuthor", "Completed : Author[%s]", author)
return Courses, nil
}
示例10: GetLibraryByCode
// GetLibraryByCode retrieves a Library from the Library database using the Code.
// Returns a non-nil error if it fails.
func GetLibraryByCode(context interface{}, db *db.DB, code string) (*Library, error) {
log.Dev(context, "GetLibraryByCode", "Started : Library[%s]", code)
var Library Library
f := func(c *mgo.Collection) error {
q := bson.M{"code": code}
log.Dev(context, "GetLibraryByCode", "MGO : db.%s.findOne(%q)", c.Name, mongo.Query(q))
return c.Find(q).One(&Library)
}
if err := db.ExecuteMGO(context, Collection, f); err != nil {
log.Error(context, "GetLibraryByCode", err, "Completed : Library[%s]", code)
return nil, err
}
log.Dev(context, "GetLibraryByCode", "Completed : Library[%s]", code)
return &Library, nil
}
示例11: GetByLatest
// GetByLatest retrieves the latest session for the specified user.
func GetByLatest(context interface{}, db *db.DB, publicID string) (*Session, error) {
log.Dev(context, "GetByLatest", "Started : PublicID[%s]", publicID)
var s Session
f := func(c *mgo.Collection) error {
q := bson.M{"public_id": publicID}
log.Dev(context, "GetByLatest", "MGO : db.%s.find(%s).sort({\"date_created\": -1}).limit(1)", c.Name, mongo.Query(q))
return c.Find(q).Sort("-date_created").One(&s)
}
if err := db.ExecuteMGO(context, Collection, f); err != nil {
log.Error(context, "GetByLatest", err, "Completed")
return nil, err
}
log.Dev(context, "GetByLatest", "Completed")
return &s, nil
}
示例12: UpsertMedia
// UpsertMedia add/updates a Media into the Media database.
// Returns a non-nil error if it fails.
func UpsertMedia(context interface{}, db *db.DB, b *Media) error {
log.Dev(context, "UpsertMedia", "Started : Add Media : %s", mongo.Query(b))
f := func(c *mgo.Collection) error {
q := bson.M{"code": b.Code}
log.Dev(context, "UpsertMedia", "MGO : db.%s.upsert(%s,%s)", c.Name, mongo.Query(q), mongo.Query(b))
_, err := c.Upsert(q, b)
return err
}
if err := db.ExecuteMGO(context, Collection, f); err != nil {
log.Error(context, "UpsertMedia", err, "Completed : Add Media")
return err
}
log.Dev(context, "UpsertMedia", "Completed : Add Media")
return nil
}
示例13: CreateUser
// CreateUser adds a new user to the database.
func CreateUser(context interface{}, db *db.DB, u *User) error {
log.Dev(context, "CreateUser", "Started : PublicID[%s]", u.PublicID)
if err := u.Validate(); err != nil {
log.Error(context, "CreateUser", err, "Completed")
return err
}
f := func(c *mgo.Collection) error {
log.Dev(context, "CreateUser", "MGO : db.%s.insert(%s)", c.Name, mongo.Query(&u))
return c.Insert(u)
}
if err := db.ExecuteMGO(context, Collection, f); err != nil {
log.Error(context, "CreateUser", err, "Completed")
return err
}
log.Dev(context, "CreateUser", "Completed")
return nil
}
示例14: UpdateUser
// UpdateUser updates an existing user to the database.
func UpdateUser(context interface{}, db *db.DB, uu UpdUser) error {
log.Dev(context, "UpdateUser", "Started : PublicID[%s]", uu.PublicID)
if err := uu.Validate(); err != nil {
log.Error(context, "UpdateUser", err, "Completed")
return err
}
f := func(c *mgo.Collection) error {
q := bson.M{"public_id": uu.PublicID}
upd := bson.M{"$set": bson.M{"full_name": uu.FullName, "email": uu.Email, "status": uu.Status, "modified_at": time.Now().UTC()}}
log.Dev(context, "UpdateUser", "MGO : db.%s.Update(%s, %s)", c.Name, mongo.Query(q), mongo.Query(upd))
return c.Update(q, upd)
}
if err := db.ExecuteMGO(context, Collection, f); err != nil {
log.Error(context, "UpdateUser", err, "Completed")
return err
}
log.Dev(context, "UpdateUser", "Completed")
return nil
}
示例15: GetUserByEmail
// GetUserByEmail retrieves a user record by using the provided email.
func GetUserByEmail(context interface{}, db *db.DB, email string, activeOnly bool) (*User, error) {
log.Dev(context, "GetUserByEmail", "Started : Email[%s]", email)
var user User
f := func(c *mgo.Collection) error {
var q bson.M
if activeOnly {
q = bson.M{"email": strings.ToLower(email), "status": StatusActive}
} else {
q = bson.M{"email": strings.ToLower(email)}
}
log.Dev(context, "GetUserByEmail", "MGO : db.%s.findOne(%s)", c.Name, mongo.Query(q))
return c.Find(q).One(&user)
}
if err := db.ExecuteMGO(context, Collection, f); err != nil {
log.Error(context, "GetUserByEmail", err, "Completed")
return nil, err
}
log.Dev(context, "GetUserByEmail", "Completed")
return &user, nil
}