示例#1
0
文件: post.go 项目: Raysmond/RaysGo
// 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"
}
示例#2
0
文件: post.go 项目: Raysmond/RaysGo
// Post implemented creating new post
func (this *PostController) NewPost() {
	var (
		valid validation.Validation
		form  models.PostForm
		post  models.Node
		err   error
	)

	if err = this.ParseForm(&form); err == nil {
		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.Status = models.GetStatus(form.Status)
			post.IUser = models.User{Id: this.User().Id}
			post.INodeType = models.NodeType{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.Insert(&post); err == nil {
				models.AddTags(this.User().Id, post.Id, tags)
				this.FlashNotice("Post created successfully.")
				this.SaveFlash()
				this.Redirect("/post/view/"+fmt.Sprintf("%d", post.Id), 302)
				return
			}
		} else {
			for _, m := range valid.Errors {
				this.FlashError(m.Key + " : " + m.Message)
			}
		}
	}

	this.SaveFlash()
	this.Data["Form"] = form
	this.Data["TYPE_"+form.ContentType] = true
	this.Data["STATUS_"+form.Status] = true
	this.Data["Title"] = "New post"
	this.TplNames = "post/new.html"
}