コード例 #1
0
ファイル: user.go プロジェクト: imattman/gowebapp
// UserByEmail gets user information from email
func UserByEmail(email string) (User, error) {
	var err error

	result := User{}

	switch database.ReadConfig().Type {
	case database.TypeMySQL:
		err = database.Sql.Get(&result, "SELECT id, password, status_id, first_name FROM user WHERE email = ? LIMIT 1", email)
	case database.TypeMongoDB:
		if database.CheckConnection() {
			session := database.Mongo.Copy()
			defer session.Close()
			c := session.DB(database.ReadConfig().MongoDB.Database).C("user")
			err = c.Find(bson.M{"email": email}).One(&result)
		} else {
			err = ErrUnavailable
		}
	case database.TypeBolt:
		err = database.View("user", email, &result)
		if err != nil {
			err = ErrNoResult
		}
	default:
		err = ErrCode
	}

	return result, standardizeError(err)
}
コード例 #2
0
ファイル: user.go プロジェクト: imattman/gowebapp
// UserCreate creates user
func UserCreate(first_name, last_name, email, password string) error {
	var err error

	now := time.Now()

	switch database.ReadConfig().Type {
	case database.TypeMySQL:
		_, err = database.Sql.Exec("INSERT INTO user (first_name, last_name, email, password) VALUES (?,?,?,?)", first_name,
			last_name, email, password)
	case database.TypeMongoDB:
		if database.CheckConnection() {
			session := database.Mongo.Copy()
			defer session.Close()
			c := session.DB(database.ReadConfig().MongoDB.Database).C("user")

			user := &User{
				ObjectId:   bson.NewObjectId(),
				First_name: first_name,
				Last_name:  last_name,
				Email:      email,
				Password:   password,
				Status_id:  1,
				Created_at: now,
				Updated_at: now,
				Deleted:    0,
			}
			err = c.Insert(user)
		} else {
			err = ErrUnavailable
		}
	case database.TypeBolt:
		user := &User{
			ObjectId:   bson.NewObjectId(),
			First_name: first_name,
			Last_name:  last_name,
			Email:      email,
			Password:   password,
			Status_id:  1,
			Created_at: now,
			Updated_at: now,
			Deleted:    0,
		}

		err = database.Update("user", user.Email, &user)
	default:
		err = ErrCode
	}

	return standardizeError(err)
}
コード例 #3
0
ファイル: user.go プロジェクト: imattman/gowebapp
// UserCreate creates user
func UserCreate(first_name, last_name, email, password string) error {
	var err error

	now := time.Now()

	if database.CheckConnection() {
		session := database.Mongo.Copy()
		defer session.Close()
		c := session.DB(database.ReadConfig().MongoDB.Database).C("user")

		user := &User{
			ObjectId:   bson.NewObjectId(),
			First_name: first_name,
			Last_name:  last_name,
			Email:      email,
			Password:   password,
			Status_id:  1,
			Created_at: now,
			Updated_at: now,
			Deleted:    0,
		}
		err = c.Insert(user)
	} else {
		err = ErrUnavailable
	}

	return standardizeError(err)
}
コード例 #4
0
ファイル: user.go プロジェクト: imattman/gowebapp
// Id returns the user id
func (u *User) ID() string {
	r := ""

	switch database.ReadConfig().Type {
	case database.TypeMySQL:
		r = fmt.Sprintf("%v", u.Id)
	case database.TypeMongoDB:
		r = u.ObjectId.Hex()
	case database.TypeBolt:
		r = u.ObjectId.Hex()
	}

	return r
}
コード例 #5
0
ファイル: user.go プロジェクト: imattman/gowebapp
// UserByEmail gets user information from email
func UserByEmail(email string) (User, error) {
	var err error

	result := User{}

	if database.CheckConnection() {
		session := database.Mongo.Copy()
		defer session.Close()
		c := session.DB(database.ReadConfig().MongoDB.Database).C("user")
		err = c.Find(bson.M{"email": email}).One(&result)
	} else {
		err = ErrUnavailable
	}

	return result, standardizeError(err)
}