示例#1
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})
}
示例#2
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, "")
}
示例#3
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)
}