// 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 }
// 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 }