コード例 #1
0
ファイル: handlers.go プロジェクト: juztin/wombat-articles
func (h Handler) PostArticles(ctx wombat.Context) {
	title := ctx.FormValue("title")
	if request.IsApplicationJson(ctx.Request) {
		ctx.Response.Header().Set("Content-Type", "application/json")
		defer ctx.Body.Close()
		if b, err := ioutil.ReadAll(ctx.Body); err != nil {
			m := make(map[string]interface{})
			json.Unmarshal(b, &m)
			title, _ = m["title"].(string)
		}
	}

	if title == "" {
		// Missing title
		ctx.HttpError(http.StatusBadRequest, GetErrorStr(ctx.Request, "Missing title"))
		return
	}

	t, err := CreateArticle(ctx, title)
	if err != nil {
		ctx.HttpError(http.StatusInternalServerError, GetError(ctx.Request, err))
		return
	}

	// When not a JSON request issue redirect to new article
	if !request.IsApplicationJson(ctx.Request) {
		ctx.Redirect(fmt.Sprintf("%s/%s?view=edit", h.BasePath, t))
	}
}
コード例 #2
0
ファイル: handlers.go プロジェクト: juztin/wombat-articles
func ImagesHandler(ctx wombat.Context, a *articles.Article, imagePath string) {
	// Get the name of the image, or random name if missing/empty
	filename := ctx.FormValue("name")
	if filename == "" {
		filename = web.RandName(5)
	}

	// Save the thumbnail, or image
	path := filepath.Join(imagePath, a.TitlePath)
	if t := ctx.FormValue("type"); t == "thumb" {
		ThumbHandler(ctx, a, path, "thumb."+filename)
	} else {
		ImageHandler(ctx, a, path, filename)
	}
}
コード例 #3
0
ファイル: handlers.go プロジェクト: juztin/wombat-articles
/*----------Article-----------*/
func (h Handler) GetArticle(ctx wombat.Context, titlePath string) {
	isAdmin := ctx.User.IsAdmin()
	o, ok := h.Article(titlePath, isAdmin)
	if !ok {
		ctx.HttpError(http.StatusNotFound)
		return
	}

	tmpl := "view"
	if isAdmin && ctx.FormValue("view") == "edit" {
		tmpl = "edit"
	}

	// Handle HTTP/JSON response
	articleResponse(ctx, &h, o, tmpl, titlePath)
}
コード例 #4
0
ファイル: handlers.go プロジェクト: juztin/wombat-articles
/*----------Articles----------*/
func (h Handler) GetArticles(ctx wombat.Context) {
	var tmpl string
	var o interface{}

	switch view := ctx.FormValue("view"); {
	default:
		tmpl = "list"
		page, err := strconv.Atoi(ctx.FormValue("page"))
		if err != nil {
			page = 0
		}
		o, _ = h.articles.Recent(h.PageCount, page, ctx.User.IsAdmin())
	case view == "create" && ctx.User.IsAdmin():
		tmpl = "create"
	}

	// Handle HTTP/JSON response
	articleResponse(ctx, &h, o, tmpl, "")
}