Misc final tweaks
- Handle notes being nil - Distinguish between request not found, error retrieving notes, and no notes for present request - Minor UI tweaks to use smart quotes
This commit was merged in pull request #14.
This commit is contained in:
@@ -232,7 +232,11 @@ func FullByID(userID, reqID string) (*JournalRequest, bool) {
|
||||
log.Print(hRows.Err())
|
||||
return nil, false
|
||||
}
|
||||
req.Notes = NotesByID(userID, reqID)
|
||||
req.Notes, err = NotesByID(userID, reqID)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return nil, false
|
||||
}
|
||||
return req, true
|
||||
}
|
||||
|
||||
@@ -248,9 +252,9 @@ func Journal(userID string) []JournalRequest {
|
||||
}
|
||||
|
||||
// NotesByID retrieves the notes for a given prayer request
|
||||
func NotesByID(userID, reqID string) []Note {
|
||||
func NotesByID(userID, reqID string) ([]Note, error) {
|
||||
if _, ok := retrieveRequest(reqID, userID); !ok {
|
||||
return nil
|
||||
return nil, sql.ErrNoRows
|
||||
}
|
||||
rows, err := db.Query(`
|
||||
SELECT "asOf", "notes"
|
||||
@@ -260,7 +264,7 @@ func NotesByID(userID, reqID string) []Note {
|
||||
reqID)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
return nil
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var notes []Note
|
||||
@@ -275,9 +279,9 @@ func NotesByID(userID, reqID string) []Note {
|
||||
}
|
||||
if rows.Err() != nil {
|
||||
log.Print(rows.Err())
|
||||
return nil
|
||||
return nil, err
|
||||
}
|
||||
return notes
|
||||
return notes, nil
|
||||
}
|
||||
|
||||
/* DDL */
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package routes
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"log"
|
||||
@@ -36,6 +37,12 @@ func sendJSON(c *routing.Context, result interface{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Send an HTTP 404 response.
|
||||
func notFound(c *routing.Context) error {
|
||||
c.Response.WriteHeader(404)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Parse the request body as JSON.
|
||||
func parseJSON(c *routing.Context) (map[string]interface{}, error) {
|
||||
payload := make(map[string]interface{})
|
||||
@@ -84,8 +91,7 @@ func requestGet(c *routing.Context) error {
|
||||
return sendError(c, errors.New("error retrieving request"))
|
||||
}
|
||||
if request == nil {
|
||||
c.Response.WriteHeader(404)
|
||||
return nil
|
||||
return notFound(c)
|
||||
}
|
||||
return sendJSON(c, request)
|
||||
}
|
||||
@@ -96,7 +102,11 @@ func requestGetComplete(c *routing.Context) error {
|
||||
if !ok {
|
||||
return sendError(c, errors.New("error retrieving request"))
|
||||
}
|
||||
request.Notes = data.NotesByID(userID(c), c.Param("id"))
|
||||
var err error
|
||||
request.Notes, err = data.NotesByID(userID(c), c.Param("id"))
|
||||
if err != nil {
|
||||
return sendError(c, err)
|
||||
}
|
||||
return sendJSON(c, request)
|
||||
}
|
||||
|
||||
@@ -132,10 +142,15 @@ func requestAddNote(c *routing.Context) error {
|
||||
|
||||
// GET: /api/request/<id>/notes
|
||||
func requestGetNotes(c *routing.Context) error {
|
||||
notes := data.NotesByID(userID(c), c.Param("id"))
|
||||
notes, err := data.NotesByID(userID(c), c.Param("id"))
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return notFound(c)
|
||||
}
|
||||
return sendError(c, err)
|
||||
}
|
||||
if notes == nil {
|
||||
c.Response.WriteHeader(http.StatusNotFound)
|
||||
return errors.New("Not Found")
|
||||
notes = []data.Note{}
|
||||
}
|
||||
return sendJSON(c, notes)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user