func createNote(rend render.Render, req *http.Request, params martini.Params, dbh *db.DBHandle) { err := req.ParseForm() if err != nil { rend.JSON(http.StatusBadRequest, err.Error()) return } u := unmarshalNoteJSON{} err = json.NewDecoder(req.Body).Decode(&u) if err != nil { rend.JSON(http.StatusBadRequest, err.Error()) return } p := db.Person{Id: u.PersonId} err = dbh.ORM.Read(&p) if err != nil { rend.JSON(http.StatusInternalServerError, fmt.Sprintf("Unknown Person ID: %d", u.PersonId)) return } dbnote := db.Note{ Text: u.Text, Category: u.Category, Date: u.Date, Person: &p, } err = dbh.CreateNote(&dbnote) if err != nil { rend.JSON(http.StatusInternalServerError, err.Error()) return } rend.JSON(200, noteWithPersonIdJSON{&dbnote, p.Id}) }
func loadFixtures(dbh *db.DBHandle) { people := map[string]db.Person{ "test1": db.Person{Name: "test1"}, "test2": db.Person{Name: "test2"}, "test3": db.Person{Name: "test3"}, } notes := map[string]db.Note{ "test_feed1": db.Note{Text: "http://testfeed1/feed.atom"}, "test_feed2": db.Note{Text: "http://testfeed2/feed.atom"}, "test_feed3": db.Note{Text: "http://testfeed3/feed.atom"}, } todos := map[string]db.Todo{ "todo1": db.Todo{ Subject: "test todo1", Notes: "test todo1 notes", }, "todo2": db.Todo{ Subject: "test todo2", Notes: "test todo2 notes", }, "todo3": db.Todo{ Subject: "test todo3", Notes: "test todo3 notes", }, } db_people := make([]*db.Person, len(people)) i := 0 for _, p := range people { err := dbh.CreatePerson(&p) if err != nil { glog.Fatal(err.Error()) } db_people[i] = &p i++ } for _, n := range notes { n.Person = db_people[0] err := dbh.CreateNote(&n) if err != nil { glog.Fatal(err.Error()) } } i = 0 for _, t := range todos { t.Person = db_people[i] err := dbh.CreateTodo(&t) if err != nil { glog.Fatal(err.Error()) } i++ } }