Exemple #1
0
func postEdit(w http.ResponseWriter, r *http.Request, ctx routes.Context) error {
	err := routes.CheckAuth(r, ctx)
	if err != nil {
		return err
	}

	body, err := ioutil.ReadAll(r.Body)
	if err != nil {
		return routes.MakeHttpError(err, http.StatusBadRequest, r)
	}

	res := editBody{}
	err = json.Unmarshal(body, &res)
	if err != nil {
		return routes.MakeHttpError(err, http.StatusBadRequest, r)
	}

	post := &models.BlogPost{
		ID:         res.ID,
		CategoryID: 1,
	}

	if res.ID != 0 {
		post, err = models.GetBlogPostByID(ctx.DB, res.ID)
		if err != nil {
			return tracederror.New(err)
		}
	}

	post.Content = res.Content
	post.Title = res.Title
	post.CategoryID = res.Category
	post.Hero = res.Hero

	t := time.Unix(int64(res.Publish), 0)
	post.Publish = &t

	if res.Publish == 0 {
		post.Publish = nil
	}

	err = post.Save(ctx.DB)
	if err != nil {
		return tracederror.New(err)
	}

	result := editResponse{
		ID: post.ID,
	}

	bytes, err := json.Marshal(result)
	if err != nil {
		return tracederror.New(err)
	}

	w.Write(bytes)
	return nil
}
Exemple #2
0
func getEdit(w http.ResponseWriter, r *http.Request, ctx routes.Context) error {
	err := routes.CheckAuth(r, ctx)
	if err != nil {
		return err
	}

	vars := mux.Vars(r)
	id := int64(0)
	if vars["id"] != "new" {
		id, err = strconv.ParseInt(vars["id"], 10, 64)
		if err != nil {
			return tracederror.New(err)
		}
	}

	post := &models.BlogPost{}

	if id != 0 {
		post, err = models.GetBlogPostByID(ctx.DB, int32(id))
		if err != nil {
			return routes.MakeHttpError(err, http.StatusNotFound, r)
		}
	}

	categories, err := models.GetCategories(ctx.DB)
	if err != nil {
		return tracederror.New(err)
	}

	tpl, err := routes.LoadTemplates("base.tpl", "edit.tpl")
	if err != nil {
		return tracederror.New(err)
	}

	data := editPage{
		Post:       post,
		Categories: categories,
	}

	return routes.RenderTemplateWithData(w, r, tpl, &data)
}
Exemple #3
0
func getPost(w http.ResponseWriter, r *http.Request, ctx routes.Context) error {
	vars := mux.Vars(r)

	tpl, err := routes.LoadTemplates("base.tpl", "post.tpl")
	if err != nil {
		return tracederror.New(err)
	}

	recent, err := models.GetRecentBlogPosts(ctx.DB, 4, 0)
	if err != nil {
		return tracederror.New(err)
	}

	id, err := strconv.ParseInt(vars["id"], 10, 32)
	if err != nil {
		return tracederror.New(err)
	}

	self, err := models.GetBlogPostByID(ctx.DB, int32(id))
	if err != nil {
		return tracederror.New(err)
	}

	authErr := routes.CheckAuth(r, ctx)
	if self.Publish == nil && authErr != nil {
		return routes.MakeHttpError(nil, http.StatusNotFound, r)
	}

	if self.Publish != nil && self.Publish.After(time.Now().UTC()) && authErr != nil {
		return routes.MakeHttpError(nil, http.StatusNotFound, r)
	}

	data := postPage{
		Post:   self,
		Recent: recent,
	}

	return routes.RenderTemplateWithData(w, r, tpl, &data)
}