示例#1
0
文件: user.go 项目: jvkops/vantaa
// FindUsers contructs and executes a cypher query with the provided parametrs
// and return all User nodes that satisfy the props conditions.
func FindUsers(props map[string]interface{}) ([]*User, error) {
	db := vantaadb.Connect()
	res := []User{}

	// generate condition string to be used in the cypher statement
	condstr := vantaadb.PropString("u", props)
	cq := neoism.CypherQuery{
		Statement: `MATCH (u:User)
                WHERE ` + condstr + `
                RETURN id(u), u.name, u.email, u.password_digest`,
		Parameters: props,
		Result:     &res,
	}

	err := db.Cypher(&cq)

	if err != nil {
		return []*User{}, nil
	}

	users := []*User{}

	for _, u := range res {
		users = append(users, &u)
	}

	if len(users) < 1 {
		return nil, errors.New("not found")
	}

	return users, nil
}
示例#2
0
文件: user.go 项目: jvkops/vantaa
// CreateUser validates the User's information. Upon successful validations,
// it creates a new User node in the database,
func CreateUser(u *User) (*User, error) {
	// user sanitization
	u.sanitize()

	if _, err := ValidateUser(u); err != nil {
		return nil, err
	}

	passwordDigest, err := bcrypt.GenerateFromPassword([]byte(u.Password), settings.Get().HashCost)
	if err != nil {
		return nil, err
	}
	// remove passsword string right after
	u.Password = ""
	u.PasswordDigest = passwordDigest

	res := []User{}
	db := vantaadb.Connect()
	cq := neoism.CypherQuery{
		Statement: `CREATE (u:User {name:{name}, email:{email}, password_digest:{password_digest}})
                RETURN id(u), u.name, u.email`,
		Parameters: neoism.Props{"name": u.Name, "email": u.Email, "password_digest": u.PasswordDigest},
		Result:     &res,
	}
	if err := db.Cypher(&cq); err != nil {
		return nil, err
	}

	newu := &res[0]

	return newu, nil
}
示例#3
0
文件: post.go 项目: malloc-fi/vantaa
// CreatePost creates a new Post
func CreatePost(*Post) (*Post, error) {
	res := []PostAdapter{}
	db := vantaadb.Connect()
	cq := neoism.CypherQuery{
		Statement: `MATCH (u:User)
                WHERE id(u) = {uid}
                CREATE (p:Post {
                  title:{title},
                  content:{content},
                  created:{created},
                  updated:{updated},
                  publish_time:{publish_time},
                  status:{status},
                })
                CREATE
                  (p)-[:written_by]->(u)
                  (u)-[:writes]->(p)
                RETURN
                  id(u),
                  p.title, p.content,
                  p.created,
                  p.updated,
                  p.publish_time,
                  p.status`,
		Parameters: neoism.Props{},
		Result:     &res,
	}
	if err := db.Cypher(&cq); err != nil {
		return nil, err
	}
	pa := &res[0]
	return pa.Transform(), nil
}
示例#4
0
文件: user.go 项目: jvkops/vantaa
// DeleteUser removes a User node from the database.
// error is return even when user is not found
func DeleteUser(props neoism.Props) error {

	// return error if user is not found in the database
	if u, _ := FindUser(props); u == nil {
		return errors.New("user not found")
	}

	db := vantaadb.Connect()
	cq := neoism.CypherQuery{
		Statement: `MATCH (u:User)
                OPTIONAL MATCH (s:Session)-[r]->(u)
                WHERE ` + vantaadb.PropString("u", props) + `DELETE u, s, r`,
		Parameters: props,
	}
	if err := db.Cypher(&cq); err != nil {
		return err
	}
	return nil
}