From 2b6f7c63d0fff07946a5207add72fa85b4b5f310 Mon Sep 17 00:00:00 2001 From: "Daniel J. Summers" Date: Sat, 31 Mar 2018 22:13:26 -0500 Subject: [PATCH] route handler translation --- src/api/routes/handlers.go | 79 +++++++++++++++++++++++++++++++++++++- src/api/routes/routes.go | 56 +++++++++++++++++++++++++++ 2 files changed, 134 insertions(+), 1 deletion(-) diff --git a/src/api/routes/handlers.go b/src/api/routes/handlers.go index 9cb7bd2..1946df6 100644 --- a/src/api/routes/handlers.go +++ b/src/api/routes/handlers.go @@ -2,12 +2,14 @@ package routes import ( "encoding/json" + "errors" "log" "net/http" "strings" "github.com/danieljsummers/myPrayerJournal/src/api/data" jwt "github.com/dgrijalva/jwt-go" + "github.com/husobee/vestigo" ) /* Support */ @@ -39,7 +41,7 @@ func userID(r *http.Request) string { /* Handlers */ -// GET: /api/journal +// GET: /api/journal/ func journal(w http.ResponseWriter, r *http.Request) { reqs := data.Journal(userID(r)) if reqs == nil { @@ -48,6 +50,81 @@ func journal(w http.ResponseWriter, r *http.Request) { sendJSON(w, r, reqs) } +// POST: /api/request/ +func requestAdd(w http.ResponseWriter, r *http.Request) { + if err := r.ParseForm(); err != nil { + sendError(w, r, err) + } + result, ok := data.AddNew(userID(r), r.FormValue("requestText")) + if !ok { + sendError(w, r, errors.New("error adding request")) + } + sendJSON(w, r, result) +} + +// GET: /api/request/:id +func requestGet(w http.ResponseWriter, r *http.Request) { + request, ok := data.ByID(userID(r), vestigo.Param(r, "id")) + if !ok { + sendError(w, r, errors.New("error retrieving request")) + } + sendJSON(w, r, request) +} + +// GET: /api/request/:id/complete +func requestGetComplete(w http.ResponseWriter, r *http.Request) { + request, ok := data.FullByID(userID(r), vestigo.Param(r, "id")) + if !ok { + sendError(w, r, errors.New("error retrieving request")) + } + request.Notes = data.NotesByID(userID(r), vestigo.Param(r, "id")) + sendJSON(w, r, request) +} + +// GET: /api/request/:id/full +func requestGetFull(w http.ResponseWriter, r *http.Request) { + request, ok := data.FullByID(userID(r), vestigo.Param(r, "id")) + if !ok { + sendError(w, r, errors.New("error retrieving request")) + } + sendJSON(w, r, request) +} + +// POST: /api/request/:id/history +func requestAddHistory(w http.ResponseWriter, r *http.Request) { + if err := r.ParseForm(); err != nil { + sendError(w, r, err) + } + w.WriteHeader(data.AddHistory(userID(r), vestigo.Param(r, "id"), r.FormValue("status"), r.FormValue("updateText"))) +} + +// POST: /api/request/:id/note +func requestAddNote(w http.ResponseWriter, r *http.Request) { + if err := r.ParseForm(); err != nil { + sendError(w, r, err) + } + w.WriteHeader(data.AddNote(userID(r), vestigo.Param(r, "id"), r.FormValue("notes"))) +} + +// GET: /api/request/:id/notes +func requestGetNotes(w http.ResponseWriter, r *http.Request) { + notes := data.NotesByID(userID(r), vestigo.Param(r, "id")) + if notes == nil { + w.WriteHeader(http.StatusNotFound) + return + } + sendJSON(w, r, notes) +} + +// GET: /api/request/answered +func requestsAnswered(w http.ResponseWriter, r *http.Request) { + reqs := data.Answered(userID(r)) + if reqs == nil { + reqs = []data.JournalRequest{} + } + sendJSON(w, r, reqs) +} + // GET: /* func staticFiles(w http.ResponseWriter, r *http.Request) { // serve index for known routes handled client-side by the app diff --git a/src/api/routes/routes.go b/src/api/routes/routes.go index 69d05e7..ac8a5d6 100644 --- a/src/api/routes/routes.go +++ b/src/api/routes/routes.go @@ -26,6 +26,62 @@ var routes = Routes{ journal, false, }, + Route{ + "AddNewRequest", + http.MethodPost, + "/api/request/", + requestAdd, + false, + }, + Route{ + "GetRequestByID", + http.MethodGet, + "/api/request/:id", + requestGet, + false, + }, + Route{ + "GetCompleteRequestByID", + http.MethodGet, + "/api/request/:id/complete", + requestGetComplete, + false, + }, + Route{ + "GetFullRequestByID", + http.MethodGet, + "/api/request/:id/full", + requestGetFull, + false, + }, + Route{ + "AddNewHistoryEntry", + http.MethodPost, + "/api/request/:id/history", + requestAddHistory, + false, + }, + Route{ + "AddNewNote", + http.MethodPost, + "/api/request/:id/note", + requestAddNote, + false, + }, + Route{ + "GetNotesForRequest", + http.MethodGet, + "/api/request/:id/notes", + requestGetNotes, + false, + }, + Route{ + "GetAnsweredRequests", + http.MethodGet, + "/api/request/answered", + requestsAnswered, + false, + }, // keep this route last Route{ "StaticFiles",