route handler translation

This commit is contained in:
Daniel J. Summers 2018-03-31 22:13:26 -05:00
parent 419c181eff
commit 2b6f7c63d0
2 changed files with 134 additions and 1 deletions

View File

@ -2,12 +2,14 @@ package routes
import ( import (
"encoding/json" "encoding/json"
"errors"
"log" "log"
"net/http" "net/http"
"strings" "strings"
"github.com/danieljsummers/myPrayerJournal/src/api/data" "github.com/danieljsummers/myPrayerJournal/src/api/data"
jwt "github.com/dgrijalva/jwt-go" jwt "github.com/dgrijalva/jwt-go"
"github.com/husobee/vestigo"
) )
/* Support */ /* Support */
@ -39,7 +41,7 @@ func userID(r *http.Request) string {
/* Handlers */ /* Handlers */
// GET: /api/journal // GET: /api/journal/
func journal(w http.ResponseWriter, r *http.Request) { func journal(w http.ResponseWriter, r *http.Request) {
reqs := data.Journal(userID(r)) reqs := data.Journal(userID(r))
if reqs == nil { if reqs == nil {
@ -48,6 +50,81 @@ func journal(w http.ResponseWriter, r *http.Request) {
sendJSON(w, r, reqs) 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: /* // GET: /*
func staticFiles(w http.ResponseWriter, r *http.Request) { func staticFiles(w http.ResponseWriter, r *http.Request) {
// serve index for known routes handled client-side by the app // serve index for known routes handled client-side by the app

View File

@ -26,6 +26,62 @@ var routes = Routes{
journal, journal,
false, 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 // keep this route last
Route{ Route{
"StaticFiles", "StaticFiles",