// CreateUser checks if user already exists, and if not, creates it
CreateUser checks if user already exists, and if not, creates it
// CreateUser checks if user already exists and if not creates it
func (db *Mngo) CreateUser(user *models.User) error {
	c := db.database.Collection(models.UsersCollection)
	err := user.BeforeCreate()
	if err != nil {
		return err
	}
	cursor, err := c.Find(db.context, bson.M{"email": user.Email})
	var results []bson.M
	if err = cursor.All(context.TODO(), &results); err != nil {
		log.Fatal(err)
	}
	if len(results) > 0 {
		return helpers.NewError(http.StatusConflict, "user_already_exists", "User already exists", err)
	}
	_, err = c.InsertOne(db.context, user)
	if err != nil {
		return helpers.NewError(http.StatusInternalServerError, "user_creation_failed", "Failed to insert the user in the database", err)
	}
	return nil
}
func ( db * Mngo ) CreateUser ( user * models . User ) error { c := db . database . Collection ( models . UsersCollection ) err := user . BeforeCreate ( ) if err != nil { return err } cursor , err := c . Find ( db . context , bson . M { " " : user . Email } ) var results [ ] bson . M if err = cursor . All ( context . TODO ( ) , & results ) ; err != nil { log . Fatal ( err ) } if len ( results ) > 0 { return helpers . NewError ( http . StatusConflict , " " , " " , err ) } _ , err = c . InsertOne ( db . context , user ) if err != nil { return helpers . NewError ( http . StatusInternalServerError , " " , " " , err ) } return nil }
CreateUser checks if user already exists, and if not, creates it
CreateUser checks if user already exists and if not creates it
//res.InsertedID