Esempio n. 1
0
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))
	}
}