Example #1
0
func View(rw http.ResponseWriter, req *http.Request, r render.Render, params martini.Params) {
	c := appengine.NewContext(req)
	var q quote.Quote

	intID, err := strconv.Atoi(params["id"])
	if err == nil {
		q.ID = int64(intID)
	}

	if q.ID > 0 {
		if err := q.Get(c); err != nil {
			http.Redirect(rw, req, "/admin?error="+err.Error(), http.StatusFound)
			return
		}
	}

	bag := make(map[string]interface{}, 0)
	bag["Host"] = req.URL.Host
	bag["Admin"] = true
	bag["Quote"] = q
	bag["ActiveNav"] = "quotes"
	r.HTML(200, "admin/quote/view", bag)

	return
}
Example #2
0
func Submit(rw http.ResponseWriter, req *http.Request) {

	var q quote.Quote
	defer req.Body.Close()

	data, err := ioutil.ReadAll(req.Body)
	if err != nil {
		http.Error(rw, err.Error(), http.StatusInternalServerError)
		return
	}

	err = json.Unmarshal(data, &q)
	if err != nil {
		http.Error(rw, err.Error(), http.StatusInternalServerError)
		return
	}

	ctx := appengine.NewContext(req)
	if err := q.Save(ctx); err != nil {
		http.Error(rw, err.Error(), http.StatusInternalServerError)
		return
	}

	js, err := json.Marshal(q)
	if err != nil {
		http.Error(rw, err.Error(), http.StatusInternalServerError)
		return
	}

	rw.Header().Set("Content-Type", "application/json")
	rw.Write(js)

	return
}
Example #3
0
func Delete(rw http.ResponseWriter, req *http.Request, r render.Render, params martini.Params) {
	ctx := appengine.NewContext(req)

	q := quote.Quote{}

	intID, err := strconv.Atoi(params["id"])
	if err == nil {
		q.ID = int64(intID)
	}

	if err := q.Get(ctx); err != nil {
		http.Error(rw, "failed to delete quote", http.StatusInternalServerError)
		return
	}

	if err := q.Delete(ctx); err != nil {
		http.Error(rw, "failed to delete quote", http.StatusInternalServerError)
		return
	}

	r.Status(200)
	return
}