Exemplo n.º 1
0
func getGenericArchive(w http.ResponseWriter, r *http.Request, ctx routes.Context) error {
	vars := mux.Vars(r)

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

	if page <= 0 {
		return routes.MakeHttpError(fmt.Errorf("Page must be larger than 0"), http.StatusBadRequest, r)
	}

	tpl, err := routes.LoadTemplates("base.tpl", "archive.tpl")
	if err != nil {
		return routes.MakeHttpError(err, http.StatusNotFound, r)
	}

	posts, err := models.GetRecentBlogPosts(ctx.DB, 100, int32((page-1)*100))
	if err != nil {
		return tracederror.New(err)
	}

	data := archivePage{
		Page:     int32(page),
		Posts:    posts,
		Category: "All",
	}

	return routes.RenderTemplateWithData(w, r, tpl, data)
}
Exemplo n.º 2
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
}
Exemplo n.º 3
0
func getAbout(w http.ResponseWriter, r *http.Request, ctx routes.Context) error {
	tpl, err := routes.LoadTemplates("base.tpl", "about.tpl")
	if err != nil {
		return routes.MakeHttpError(err, http.StatusNotFound, r)
	}

	return routes.RenderTemplateWithData(w, r, tpl, nil)
}
Exemplo n.º 4
0
func getHello(w http.ResponseWriter, r *http.Request, ctx routes.Context) error {
	tpl, err := routes.LoadTemplates("base.tpl", "echo.tpl")
	if err != nil {
		return routes.MakeHttpError(err, http.StatusInternalServerError, r)
	}

	return routes.RenderTemplateWithData(w, r, tpl, nil)
}
Exemplo n.º 5
0
func getCategoryArchive(w http.ResponseWriter, r *http.Request, ctx routes.Context) error {
	vars := mux.Vars(r)

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

	if page <= 0 {
		return routes.MakeHttpError(fmt.Errorf("Page must be larger than 0"), http.StatusBadRequest, r)
	}

	category := vars["category"]
	valid, err := models.IsValidCategory(ctx.DB, category)

	if err != nil {
		return tracederror.New(err)
	}

	if !valid {
		return routes.MakeHttpError(fmt.Errorf("Invalid category %s", category), http.StatusBadRequest, r)
	}

	tpl, err := routes.LoadTemplates("base.tpl", "archive.tpl")
	if err != nil {
		return routes.MakeHttpError(err, http.StatusNotFound, r)
	}

	posts, err := models.GetRecentPostsInCategory(ctx.DB, category, 100, int32((page-1)*100))
	if err != nil {
		return tracederror.New(err)
	}

	data := archivePage{
		Page:     int32(page),
		Posts:    posts,
		Category: strings.Title(category),
	}

	return routes.RenderTemplateWithData(w, r, tpl, data)
}
Exemplo n.º 6
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)
}
Exemplo n.º 7
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)
}