コード例 #1
0
ファイル: handlers.go プロジェクト: nivaha/notify
// Index returns a JSON array of all feeds
func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
	feeds, err := list()
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	jsonUtils.Output(w, 200, feeds)
}
コード例 #2
0
ファイル: handlers.go プロジェクト: nivaha/notify
// Show returns the data for a specific feed as JSON
func Show(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	f, err := lookup(ps.ByName("id"))
	if err != nil {
		jsonUtils.Error(w, http.StatusInternalServerError, err.Error())
		return
	}

	jsonUtils.Output(w, 200, f)
}
コード例 #3
0
ファイル: handlers.go プロジェクト: nivaha/notify
// Index returns a JSON array of all events
func (h *Handler) Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
	events, err := h.db.list()
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	jsonUtils.Output(w, 200, events)
}
コード例 #4
0
ファイル: handlers.go プロジェクト: nivaha/notify
// Destroy is a REST API for destroying an subscription, based on the subscription id
func (h *Handler) Destroy(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
	subscription, err := h.db.destroy(p.ByName("id"))
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	jsonUtils.Output(w, 200, subscription)
}
コード例 #5
0
ファイル: handlers.go プロジェクト: nivaha/notify
// Create constructs a new event from the data in the POST body
func (h *Handler) Create(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
	e := Event{}

	if err := jsonUtils.Decode(r.Body, &e); err != nil {
		jsonUtils.Error(w, http.StatusBadRequest, err.Error())
		return
	}

	if err := e.insert(); err != nil {
		jsonUtils.Error(w, http.StatusInternalServerError, err.Error())
		return
	}

	jsonUtils.Output(w, 201, e)
}
コード例 #6
0
ファイル: handlers.go プロジェクト: nivaha/notify
// Create is a REST API for creating a new subscription, based on the JSON payload
func (h *Handler) Create(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
	subscription := Subscription{}

	if err := jsonUtils.Decode(r.Body, &subscription); err != nil {
		jsonUtils.Error(w, http.StatusBadRequest, err.Error())
		return
	}

	if err := subscription.insert(); err != nil {
		jsonUtils.Error(w, http.StatusBadRequest, err.Error())
		return
	}

	jsonUtils.Output(w, 201, subscription)
}
コード例 #7
0
ファイル: handlers.go プロジェクト: nivaha/notify
// Create constructs a new feed from the data in the POST body
func Create(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
	f := Feed{}

	decoder := json.NewDecoder(r.Body)
	err := decoder.Decode(&f)
	if err != nil {
		jsonUtils.Error(w, http.StatusBadRequest, err.Error())
		return
	}

	err = f.insert()
	if err != nil {
		jsonUtils.Error(w, http.StatusInternalServerError, err.Error())
		return
	}

	jsonUtils.Output(w, 201, f)
}