Esempio n. 1
0
// All posts
func (this *PostController) Posts() {
	this.TplNames = "post/posts.html"
	this.Data["Title"] = "Posts"

	page, _ := helpers.Str2Int64(this.GetString("page"))
	total, _ := models.Engine.Count(new(models.Node))

	posts := make([]*models.Node, 0)
	err := models.Engine.Desc("id").Limit(10, (int)((page-1)*10)).Find(&posts)
	if err == nil {
		this.Data["Posts"] = posts
		this.Data["Total"] = total
		pages := total / 10
		if total%10 > 0 {
			pages++
		}
		this.Data["Pages"] = pages
		this.Data["CurrentPage"] = page
		if page < pages {
			this.Data["NextPage"] = page + 1
		}
		if page > 1 {
			this.Data["PrevPage"] = page - 1
		}
	} else {
		this.Abort("404")
	}
}
Esempio n. 2
0
// View a post
func (this *PostController) View() {
	id, _ := helpers.Str2Int64(this.GetParam(":id"))

	var node *models.Node
	if node = models.GetNode(id); node == nil {
		this.Abort("404")
	}

	node.UpdateCounter()

	if tags, err := models.GetNodeTags(node.IUser.Id, node.Id); err == nil {
		this.Data["Tags"] = *tags
	}

	if comments, err := models.GetNodeComments(id); err == nil {
		this.Data["Comments"] = comments
	}

	var form models.PostForm
	if err := this.ParseForm(&form); err == nil {
		this.Data["CommentForm"] = form
	}

	this.Data["Title"] = node.Title
	this.Data["Post"] = node
	this.Data["User"] = models.GetUser(node.IUser.Id)
	this.Data["CanEdit"] = this.canEditPost(node)
	this.TplNames = "post/view.html"
}
Esempio n. 3
0
// Edit a post
func (this *PostController) Edit() {
	id, _ := helpers.Str2Int64(this.GetParam(":id"))
	var post *models.Node
	if post = models.GetNode(id); post == nil {
		this.Abort("404")
	}
	if !this.canEditPost(post) {
		this.FlashError("Sorry, you don't have the permission to edit the post!")
		this.SaveFlash()
		this.RedirectPost(id)
	}

	form := models.PostForm{}
	form.SetData(post)

	tagStr := ""
	if tags, err := models.GetNodeTags(post.IUser.Id, post.Id); err == nil {
		comma := ""
		for _, tag := range *tags {
			tagStr = tagStr + comma + tag.Name
			comma = ", "
		}
		form.Tags = tagStr
	}

	this.Data["Title"] = "Edit - " + post.Title
	this.Data["Post"] = post
	this.Data["Form"] = form
	this.Data["TYPE_"+form.ContentType] = true
	this.Data["STATUS_"+form.Status] = true
	this.TplNames = "post/edit.html"

}
Esempio n. 4
0
func (this *PostController) Comment() {
	id, _ := helpers.Str2Int64(this.GetParam(":id"))
	var post *models.Node
	if post = models.GetNode(id); post == nil {
		this.Abort("404")
	}
	var (
		valid validation.Validation
		form  models.CommentForm
		err   error
	)

	if err = this.ParseForm(&form); err == nil {
		if ok, e := valid.Valid(form); ok && e == nil {
			var pid int64 = 0
			if form.ReplyTo != "" {
				pid, _ = helpers.Str2Int64(form.ReplyTo)
			}
			comment := models.Comment{
				Pid:         pid, // TODO: dealing with comment replies
				INode:       models.Node{Id: id},
				IUser:       models.User{Id: this.User().Id},
				Title:       form.Title,
				Content:     form.Content,
				ContentType: models.CONTENT_TYPE_MARKDOWN,
				CreateTime:  time.Now(),
				Status:      models.ACTIVE,
				UserHost:    this.Ctx.Input.IP(),
			}
			if _, err = models.Engine.Insert(&comment); err == nil { // comment saved
				this.FlashNotice("Your comment was saved successfully.")
			} else {
				this.FlashNotice("Sorry, internal error!")
			}
		} else {
			for _, m := range valid.Errors {
				this.FlashError(m.Key + " : " + m.Message)
			}
			this.Data["CommentForm"] = form
			this.Ctx.Redirect(302, "/post/view/"+fmt.Sprintf("%d", id))
		}
	}

	this.SaveFlash()
	this.RedirectPost(id)
}
Esempio n. 5
0
// Post implemented editing a post
func (this *PostController) EditPost() {
	var (
		post  *models.Node
		form  models.PostForm
		valid validation.Validation
		err   error
	)

	id, _ := helpers.Str2Int64(this.GetParam(":id"))
	if post = models.GetNode(id); post == nil || !this.canEditPost(post) {
		this.Abort("404")
	}

	if err = this.ParseForm(&form); err != nil {
		this.Abort("404")
	}

	result := false
	if ok, e := valid.Valid(form); ok && e == nil {
		post.Title = form.Title
		post.Content = form.Content
		post.ContentType = form.ContentType
		post.CreateTime = time.Now()
		post.IUser.Id = this.User().Id
		post.Status = models.GetStatus(form.Status)
		post.INodeType.Id = models.GetNodeType("post")

		tag := strings.TrimSpace(form.Tags)
		tags := make([]string, 0)
		tagarr := strings.Split(tag, ",")
		for _, v := range tagarr {
			if t := strings.TrimSpace(v); t != "" {
				tags = append(tags, t)
			}
		}

		if _, err = models.Engine.Id(id).Update(post); err == nil {
			models.AddTags(this.User().Id, post.Id, tags)
			result = true
			this.FlashNotice("Post updated successfully.")
		} else {
			this.FlashError("Post updated failed.")
		}

	} else {
		for _, m := range valid.Errors {
			this.FlashError(m.Key + " : " + m.Message)
		}
	}
	this.SaveFlash()
	if result {
		this.Redirect("/post/view/"+fmt.Sprintf("%d", post.Id), 302)
	}
	this.Data["Title"] = "Edit - " + post.Title
	this.Data["Post"] = post
	this.Data["Form"] = form
	this.TplNames = "post/edit.html"
}
Esempio n. 6
0
func (this *UserController) Block() {
	id, _ := helpers.Str2Int64(this.GetParam(":id"))
	user := models.User{}
	if _, err := models.Engine.Id(id).Get(&user); err == nil {
		user.Status = models.BLOCKED
		if _, e := models.Engine.Id(id).Update(&user); e == nil {
			// do something
		} else {
			// do something
		}
	}
}
Esempio n. 7
0
func (this *UserController) View() {
	id, _ := helpers.Str2Int64(this.GetParam(":id"))

	var user = models.User{Id: int64(id)}
	has, err := models.Engine.Get(&user)

	if !has || err != nil {
		this.Abort("404")
	}

	this.Data["user"] = user
	this.setMeta("Title", user.Name)
	this.GoView("user/view")
}
Esempio n. 8
0
func (this *PostController) Delete() {
	id, _ := helpers.Str2Int64(this.GetParam(":id"))
	post := models.GetNode(id)
	if post != nil && this.canEditPost(post) {
		if _, err := models.Engine.Id(id).Delete(&models.Comment{}); err == nil {
			this.FlashNotice("Post was deleted successfully.")
			this.SaveFlash()
			this.Redirect("/myposts", 302)
			return
		}
	}

	this.FlashNotice("Deleting post failed.")
	this.SaveFlash()
	// TODO : return to the former uri
	this.Abort("404")
}