示例#1
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)
}
示例#2
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)
}
示例#3
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)
}
示例#4
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)
}