// CreateUser creates a new user resource func CreateUser(context interface{}, db *db.DB, user *User) error { log.Dev(context, "CreateUser", "Started : User: "******"CreateUser", err, "User exists") return "CreateUser: user with same UserName already exists" } */ // set defaults, may want to move this to a factory method on the User struct if user.UserID == "" { user.UserID = uuid.New() } user.MemberSince = time.Now() f := func(col *mgo.Collection) error { log.Dev(context, "CreateUser", "MGO: db.%s.insert()", userCollection) return col.Insert(user) } // Write the user to mongo if err := db.ExecuteMGO(context, userCollection, f); err != nil { log.Error(context, "CreateUser", err, "Completed") return err } log.Dev(context, "CreateUser", "Completed") return nil }
// removeUser is used to clear out all the test user from the collection. func removeUser(db *db.DB, UserID string) error { f := func(c *mgo.Collection) error { q := bson.M{"user_id": UserID} return c.Remove(q) } if err := db.ExecuteMGO(tests.Context, "users", f); err != nil { return err } return nil }
// removeComment is used to clear out all the test user from the collection. func removeComment(db *db.DB, CommentID string) error { f := func(c *mgo.Collection) error { q := bson.M{"comment_id": CommentID} return c.Remove(q) } if err := db.ExecuteMGO(tests.Context, "comments", f); err != nil { return err } return nil }
// GetUserByID retrieves an individual user by ID func GetUserByID(context interface{}, db *db.DB, id string) (*User, error) { log.Dev(context, "GetUserById", "Started : Id[%s]", id) var user User f := func(c *mgo.Collection) error { q := bson.M{"_id": id} log.Dev(context, "GetUserById", "MGO : db.%s.findOne(%s)", userCollection, mongo.Query(q)) return c.Find(q).One(&user) } if err := db.ExecuteMGO(context, userCollection, f); err != nil { log.Error(context, "GetUserById", err, "Completed") return nil, err } log.Dev(context, "GetUserById", "Completed") return &user, nil }
// GetUserByUserName retrieves an individual user by email func GetUserByUserName(context interface{}, db *db.DB, userName string) (*User, error) { log.Dev(context, "GetUserByUserName", "Started : User[%s]", userName) userName = strings.ToLower(userName) var user User f := func(c *mgo.Collection) error { q := bson.M{"user_name": userName} log.Dev(context, "GetUserByUserName", "MGO : db.%s.findOne(%s)", userCollection, mongo.Query(q)) return c.Find(q).One(&user) } if err := db.ExecuteMGO(context, userCollection, f); err != nil { log.Error(context, "GetUserByUserName", err, "Completed") return nil, err } log.Dev(context, "GetUserByUserName", "Completed") return &user, nil }
// CreateComment adds a new comment in the database. func CreateComment(context interface{}, db *db.DB, com *Comment) error { if com.CommentID == "" { com.CommentID = uuid.New() } com.DateCreated = time.Now() com.Status = "New" f := func(col *mgo.Collection) error { log.Dev(context, "CreateComment", "MGO: db.%s.insert()", commentCollection) return col.Insert(com) } if err := db.ExecuteMGO(context, commentCollection, f); err != nil { log.Error(context, "CreateComment", err, "Completed") return err } log.Dev(context, "CreateComment", "Completed") return nil }