Example #1
0
func Post(ctx banana.Context) error {
	err := Auth(ctx, PrivilegePostRead)
	switch err {
	case ErrNoPermit:
		return err
	case ErrNotLogin:
		http.Redirect(ctx.Res(), ctx.Req(), "/login?error", http.StatusFound)
		return nil
	case nil:
	default:
		return err
	}

	var (
		idStr string
		ok    bool
	)
	if idStr, ok = ctx.Params()["id"]; !ok {
		panic("no id")
	}
	id, err := strconv.ParseInt(idStr, 10, 32)
	if err != nil {
		panic(err)
	}
	p := post.ReadRaw(int(id))
	categories, err := category.Query()
	if err != nil {
		return err
	}

	layout := ThemeLayout{}
	layout.Content = ThemeBlock{theme.CP("post"), struct{ Post, Categories interface{} }{p, categories}}
	return ctx.Tpl(theme.CP("layout"), layout)
}
Example #2
0
func SavePost(ctx banana.Context) error {
	err := Auth(ctx, PrivilegePostWrite)
	switch err {
	case ErrNoPermit:
		return err
	case ErrNotLogin:
		return err
	case nil:
	default:
		return err
	}

	var (
		idStr string
		ok    bool
	)
	if idStr, ok = ctx.Params()["id"]; !ok {
		panic("no id")
	}
	id, err := strconv.ParseInt(idStr, 10, 32)
	if err != nil {
		return err
	}
	r := ctx.Req()
	cid, err := strconv.ParseInt(r.FormValue("category"), 10, 32)
	if err != nil {
		return err
	}

	p := post.New()
	p.Id = int(id)
	p.Title = r.FormValue("title")
	p.Content = r.FormValue("content")
	p.Category.Id = int(cid)
	p.Description = r.FormValue("description")
	p.Keywords.Parse(r.FormValue("keywords"))
	err = p.Save()
	if err != nil {
		return err
	}

	return ctx.Json(p)
}
Example #3
0
func Category(ctx banana.Context) error {
	c, ok := ctx.Params()["category"]
	if !ok {
		return errors.New("no category")

	}
	ps, err := post.QueryByCategory(c, 0, 10)
	if err != nil {
		return err
	}
	var ca = category.Category{}
	if len(ps) > 0 {
		ca = ps[0].Category
	}

	layout := ThemeLayout{}
	layout.Content = ThemeBlock{theme.UI("category"), struct {
		List  []post.Post
		Bread category.Category
	}{ps, ca}}
	return ctx.Tpl(theme.UI("layout"), layout)
}
Example #4
0
func Read(ctx banana.Context) error {
	var (
		idStr string
		ok    bool
	)
	if idStr, ok = ctx.Params()["id"]; !ok {
		return errors.New("no id")
	}
	id, err := strconv.ParseInt(idStr, 10, 32)
	if err != nil {
		return err
	}
	x := post.Read(int(id))
	if yearStr, ok := ctx.Params()["year"]; ok {
		t, err := strconv.ParseInt(yearStr, 10, 32)
		if err != nil {
			return err
		}
		if int(t) != x.PubTime.Year() {
			return errors.New("404")
		}
	}
	if monthStr, ok := ctx.Params()["month"]; ok {
		t, err := strconv.ParseInt(monthStr, 10, 32)
		if err != nil {
			return err
		}
		if int(t) != int(x.PubTime.Month()) {
			return errors.New("404")
		}
	}
	if dayStr, ok := ctx.Params()["day"]; ok {
		t, err := strconv.ParseInt(dayStr, 10, 32)
		if err != nil {
			return err
		}
		if int(t) != x.PubTime.Day() {
			return errors.New("404")
		}
	}

	layout := ThemeLayout{}
	layout.Content = ThemeBlock{theme.UI("post"), x}
	return ctx.Tpl(theme.UI("layout"), layout)
}