Exemple #1
0
// GetRecentSwatches is the handler for retrieving paginated arrays of swatches
// ordered in reverse insert order
func GetRecentSwatches(w http.ResponseWriter, r *http.Request) {
	page, err := parsePageNumber(mux.Vars(r)["page"])
	if err != nil {
		errorResponse(w, http.StatusBadRequest, err.Error())
		return
	}

	if s, err := db.GetSwatchesPage("id", descending, page); err != nil {
		if err == sql.ErrNoRows {
			w.WriteHeader(404)
		} else {
			errorResponse(w, http.StatusInternalServerError, err.Error())
		}
	} else {
		jsonResponse(w, http.StatusOK, s)
	}
}
Exemple #2
0
// GetSwatch is the handler for retrieving a swatch by id
func GetSwatch(w http.ResponseWriter, r *http.Request) {
	id, err := strconv.ParseInt(mux.Vars(r)["id"], 10, 64)
	if err != nil {
		errorResponse(w, http.StatusBadRequest, err.Error())
		return
	}

	if s, err := db.GetSwatch(id); err != nil {
		if err == sql.ErrNoRows {
			w.WriteHeader(404)
		} else {
			errorResponse(w, http.StatusInternalServerError, err.Error())
		}
	} else {
		jsonResponse(w, http.StatusOK, s)
	}
}