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

	b := banner.Banner{}

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

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

	segs := strings.Split(b.Image, "/")
	blobstore.Delete(ctx, appengine.BlobKey(segs[len(segs)-1]))

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

	r.Status(200)
	return
}
Beispiel #2
0
func Edit(rw http.ResponseWriter, req *http.Request, r render.Render, params martini.Params) {
	c := appengine.NewContext(req)
	var b banner.Banner

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

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

	uploadURL, err := blobstore.UploadURL(c, "/admin/banners/"+strconv.Itoa(intID), nil)
	if 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["Banner"] = b
	bag["URL"] = uploadURL
	bag["ActiveNav"] = "banners"
	r.HTML(200, "admin/banners/edit", bag)

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

	blobs, vals, err := blobstore.ParseUpload(req)
	if err != nil {
		http.Redirect(rw, req, "/admin/banners/"+params["id"]+"?error="+err.Error(), http.StatusFound)
		return
	}

	var title string
	var caption string
	if len(vals["title"]) > 0 {
		title = vals["title"][0]
	}
	if len(vals["caption"]) > 0 {
		caption = vals["caption"][0]
	}

	b := banner.Banner{}

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

	b.Get(ctx)
	b.Title = title
	b.Caption = caption

	file := blobs["image"]
	if len(file) != 0 {
		b.Image = fmt.Sprintf("/blob/%s", string(file[0].BlobKey))
	} else if intID == 0 {
		http.Redirect(rw, req, "/admin/banners/"+params["id"]+"?error=You must upload an image", http.StatusFound)
		return
	}

	err = b.Save(ctx)
	if err != nil {
		http.Redirect(rw, req, "/admin/banners/"+params["id"]+"?error="+err.Error(), http.StatusFound)
		return
	}

	http.Redirect(rw, req, "/admin/banners/"+strconv.Itoa(int(b.ID))+"?success=Banner saved", http.StatusFound)
	return
}
Beispiel #4
0
func Get(rw http.ResponseWriter, req *http.Request, r render.Render, params martini.Params) {
	var b banner.Banner
	ctx := appengine.NewContext(req)

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

	err = b.Get(ctx)
	if err != nil {
		http.Error(rw, err.Error(), http.StatusInternalServerError)
		return
	}

	r.JSON(200, b)
}