This model I am trying to make with neoism is working now but I can't figure out how to make the return type shorter and or cleaner looking.
There must be a way to make this:
[]struct { UserId string "json:\"account.UserId\""; FirstName string "json:\"account.firstName\""; LastName string "json:\"account.lastName\"" }
Into:
[]Account
or something
package models
import (
"errors"
"strconv"
"time"
// "fmt"
"github.com/jmcvetta/neoism"
)
var (
Accounts map[string]*Account
)
var (
db *neoism.Database
)
func panicErr(err error) {
if err != nil {
panic(err)
}
}
type Account struct {
UserId string `json:"account.UserId"`
FirstName string `json:"account.firstName"`
LastName string `json:"account.lastName"`
}
func init() {
var err error
db, err = neoism.Connect("http://neo4j:password@localhost:7474/db/data")
if err != nil {
panic(err)
}
}
// map[lastName:PATRUS UserId:7f7014f9-bd59-4739-8e1b-5aebfa00f4c5 firstName:LARISSA]
func GetAccounts() []struct { UserId string "json:\"account.UserId\""; FirstName string "json:\"account.firstName\""; LastName string "json:\"account.lastName\"" } {
stmt := `
MATCH (account:Account) RETURN account.UserId, account.firstName, account.lastName
`
res := []struct {
UserId string `json:"account.UserId"`
FirstName string `json:"account.firstName"`
LastName string `json:"account.lastName"`
}{}
// construct query
cq := neoism.CypherQuery{
Statement: stmt,
Result: &res,
}
// execute query
err := db.Cypher(&cq)
panicErr(err)
return res
}
Here is another copy: http://play.golang.org/p/14gzc0a_89
I am new to go and I am going off of these examples for neoism: https://gist.github.com/verdverm/b43444063995a7f5b913
There are many examples, but verdverm is always just outputting and not returning.
I have a controller as well where I would like to output the json.
package controllers
import (
"github.com/astaxie/beego"
"social/models"
)
type AccountsController struct {
beego.Controller
}
func (c *AccountsController) Get() {
c.Data["json"] = models.GetAccounts()
// c.Data["json"] = "hello world"
c.ServeJson()
}
Thanks for any help/tips for go newbie!