Exemple #1
0
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})
}
Exemple #2
0
func getPeople(rend render.Render, req *http.Request, dbh *db.DBHandle) {
	err := req.ParseForm()
	if err != nil {
		rend.JSON(500, err.Error())
		return
	}

	var people_json []*personWithRelations
	param_ids := req.Form["ids[]"]
	if len(param_ids) > 0 {
		people_ids, err := parseParamIds(param_ids)
		if err != nil {
			rend.JSON(500, err.Error())
			return
		}
		people_json = make([]*personWithRelations, len(people_ids))
		for i, pid := range people_ids {
			person, err := dbh.GetPersonById(pid)
			if err != nil {
				if err == orm.ErrNoRows {
					rend.JSON(404, fmt.Sprintf("Person with ID %d doesn't exist", pid))
					return
				} else {
					rend.JSON(500, err.Error())
					return
				}
			}
			pn, err := newPersonWithRelations(person, dbh)
			if err != nil {
				rend.JSON(500, err)
				return
			}

			people_json[i] = &pn
		}
	} else {
		people, err := dbh.GetPeopleById([]int64{})
		if err != nil {
			rend.JSON(500, err)
			return
		}

		people_json = make([]*personWithRelations, len(people))
		for i, p := range people {
			pn, err := newPersonWithRelations(p, dbh)
			if err != nil {
				rend.JSON(500, err)
				return
			}
			people_json[i] = &pn
		}
	}
	rend.JSON(200, people_json)
}
Exemple #3
0
func getNote(rend render.Render, req *http.Request, params martini.Params, dbh *db.DBHandle) {
	id, err := strconv.ParseInt(params["id"], 10, 64)
	if err != nil {
		rend.JSON(500, "Invalid id: "+err.Error())
		return
	}
	n, err := dbh.GetNoteById(id)
	if err != nil {
		rend.JSON(500, err.Error())
		return
	}

	rend.JSON(200, noteWithPersonIdJSON{n, n.Person.Id})
}
Exemple #4
0
func createTodo(rend render.Render, req *http.Request, params martini.Params, dbh *db.DBHandle) {
	err := req.ParseForm()
	if err != nil {
		rend.JSON(500, err.Error())
		return
	}
	u := unmarshalTodoJSON{}
	glog.Info("Decoded json: %+v", u)
	err = json.NewDecoder(req.Body).Decode(&u)
	if err != nil {
		rend.JSON(500, err.Error())
		return
	}

	queryParams, _ := url.ParseQuery(req.URL.RawQuery)
	dbtodo := db.Todo{
		Text:     u.Text,
		Category: u.Category,
		Date:     u.Date,
	}

	if _, ok := queryParams["addToAll"]; ok {
		glog.Info(u)
		//do something here
		err = dbh.AddTodoToAllPeople(&dbtodo)
		if err != nil {
			rend.JSON(http.StatusInternalServerError, err.Error())
			return
		}
	} else {
		p := db.Person{Id: u.PersonId}
		err = dbh.ORM.Read(&p)
		if err != nil {
			rend.JSON(500, fmt.Sprintf("Unknown Person id: %d", u.PersonId))
			return
		}

		dbtodo.Person = &p
		_, err = dbh.ORM.Insert(&dbtodo)
		if err != nil {
			rend.JSON(500, err.Error())
			return
		}
		rend.JSON(200, todoWithPersonIdJSON{&dbtodo, p.Id})
	}
}
Exemple #5
0
func deleteNote(rend render.Render, params martini.Params, dbh *db.DBHandle) {
	note_id, err := strconv.ParseInt(params["id"], 10, 64)
	if err != nil {
		rend.JSON(http.StatusBadRequest, err.Error())
		return
	}

	note, err := dbh.GetNoteById(note_id)
	if err != nil {
		rend.JSON(http.StatusNotFound, err.Error())
		return
	}

	err = dbh.RemoveNote(note)
	if err != nil {
		rend.JSON(http.StatusInternalServerError, err.Error())
		return
	}

	rend.JSON(http.StatusNoContent, "")
}
Exemple #6
0
func getNotes(rend render.Render, req *http.Request, dbh *db.DBHandle) {
	err := req.ParseForm()
	if err != nil {
		rend.JSON(500, err.Error())
		return
	}

	var notes []noteWithPersonIdJSON
	param_ids := req.Form["ids[]"]
	if len(param_ids) > 0 {
		note_ids, err := parseParamIds(param_ids)
		if err != nil {
			rend.JSON(500, err.Error())
			return
		}

		notes = make([]noteWithPersonIdJSON, len(note_ids))
		for i, nid := range note_ids {
			note, err := dbh.GetNoteById(nid)
			if err != nil {
				rend.JSON(404, err.Error())
				return
			}
			notes[i] = noteWithPersonIdJSON{note, note.Person.Id}
		}
	} else {
		dbnotes, err := dbh.GetNotesById([]int64{})
		notes = make([]noteWithPersonIdJSON, len(dbnotes))
		for i, n := range dbnotes {
			notes[i] = noteWithPersonIdJSON{n, n.Person.Id}
		}
		if err != nil {
			rend.JSON(500, err.Error())
			return
		}
	}
	rend.JSON(http.StatusOK, notes)
}
Exemple #7
0
func getPerson(rend render.Render, params martini.Params, dbh *db.DBHandle) {
	id, err := strconv.ParseInt(params["id"], 10, 64)
	if err != nil {
		rend.JSON(500, "Invalid id: "+err.Error())
		return
	}
	p, err := dbh.GetPersonById(id)
	if err != nil {
		if err == orm.ErrNoRows {
			rend.JSON(404, fmt.Sprintf("No Person with id %d found.", id))
			return
		} else {
			rend.JSON(500, err.Error())
			return
		}
	}

	pn, err := newPersonWithRelations(p, dbh)
	if err != nil {
		rend.JSON(500, err)
		return
	}
	rend.JSON(200, pn)
}
Exemple #8
0
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++
	}
}