Example #1
0
func HandleNewNote(w http.ResponseWriter, r *http.Request) {

	// Get the payload
	payload, err := ioutil.ReadAll(r.Body)
	if err != nil {
		log.Print(err)
		fRS := rsrq.NewFailureRS("[1]: Bad Payload", "")
		handler_util.SendResponse(w, fRS)
		return
	}

	// Use payload to build a RQ
	newNoteRQ := rsrq.NewNoteRQ{}
	jsonErr := json.Unmarshal(payload, &newNoteRQ)
	if jsonErr != nil {
		log.Print(jsonErr)
		fRS := rsrq.NewFailureRS("[2]: Unreadable JSON payload", "")
		handler_util.SendResponse(w, fRS)
		return
	}

	// Validate the RQ
	err, _ = validate.Required(newNoteRQ) //TODO get list of infractions to return in failure RS
	if err != nil {
		log.Print(err)
		fRS := rsrq.NewFailureRS("[3]: "+err.Error(), newNoteRQ.UserToken) //TODO return api level error message
		handler_util.SendResponse(w, fRS)
		return
	}

	//TODO VALIDATE USER

	// Create new note key, and insert note into db
	newNoteKey := newNoteToken("notehack") //TODO use a key from config file
	err = insertNewNote(newNoteRQ.UserToken, newNoteKey, newNoteRQ.Parent)
	if err != nil {
		log.Print(err)
		fRS := rsrq.NewFailureRS("[4]: "+err.Error(), newNoteRQ.UserToken) //TODO return api level error message
		handler_util.SendResponse(w, fRS)
		return
	}

	// return new note key to user
	rs := rsrq.NewNoteRS{newNoteKey, "New Note"} // TODO make constant for new note name
	rsByte, err := json.Marshal(rs)
	if err != nil {
		log.Print(err)
		return
	}

	w.Write(rsByte)
}
Example #2
0
func HandleListNotes(w http.ResponseWriter, r *http.Request) {
	// Get the payload
	payload, err := ioutil.ReadAll(r.Body)
	if err != nil {
		log.Print(err)
		fRS := rsrq.NewFailureRS("Bad Payload", "")
		handler_util.SendResponse(w, fRS)
		return
	}

	// Use payload to build a RQ
	listNotesRQ := rsrq.ListNotesRQ{}
	jsonErr := json.Unmarshal(payload, &listNotesRQ)
	if jsonErr != nil {
		log.Print(jsonErr)
		fRS := rsrq.NewFailureRS("Unreadable JSON payload", "")
		handler_util.SendResponse(w, fRS)
		return
	}

	// Validate the RQ
	err, _ = validate.Required(listNotesRQ) //TODO get list of infractions to return in failure RS
	if err != nil {
		log.Print(err)
		fRS := rsrq.NewFailureRS(err.Error(), listNotesRQ.UserToken) //TODO return api level error message
		handler_util.SendResponse(w, fRS)
		return
	}

	//TODO VALIDATE USER

	//Get a list of notes
	notes, err := getNotes(listNotesRQ)
	if err != nil {
		log.Print(err)
		fRS := rsrq.NewFailureRS(err.Error(), listNotesRQ.UserToken) //TODO return api level error message
		handler_util.SendResponse(w, fRS)
		return
	}

	listNotesRS := rsrq.ListNotesRS{notes}
	handler_util.SendResponse(w, listNotesRS)
}
func HandleArchiveNote(w http.ResponseWriter, r *http.Request) {
	// Get the payload
	payload, err := ioutil.ReadAll(r.Body)
	if err != nil {
		log.Print(err)
		fRS := rsrq.NewFailureRS("[1]: Bad Payload", "")
		handler_util.SendResponse(w, fRS)
		return
	}

	// Use payload to build a RQ
	archiveNoteRQ := rsrq.ArchiveNoteRQ{}
	jsonErr := json.Unmarshal(payload, &archiveNoteRQ)
	if jsonErr != nil {
		log.Print(jsonErr)
		fRS := rsrq.NewFailureRS("[2]: Unreadable JSON payload", "")
		handler_util.SendResponse(w, fRS)
		return
	}

	// Validate the RQ
	err, _ = validate.Required(archiveNoteRQ) //TODO get list of infractions to return in failure RS
	if err != nil {
		log.Print(err)
		fRS := rsrq.NewFailureRS("[3]: "+err.Error(), archiveNoteRQ.UserToken) //TODO return api level error message
		handler_util.SendResponse(w, fRS)
		return
	}

	//TODO VALIDATE USER

	err = archiveNote(archiveNoteRQ)
	if err != nil {
		fRS := rsrq.NewFailureRS("[4]: "+err.Error(), archiveNoteRQ.UserToken) //TODO return api level error message
		handler_util.SendResponse(w, fRS)
		return
	}

	w.Write([]byte("ok"))

}