示例#1
0
文件: post.go 项目: jango2015/baseapp
func (c Post) Save(username string, post models.Post) r.Result {
	profile := c.connected()
	if profile == nil || profile.UserName != username {
		c.Flash.Error("You must log in to access your account")
		return c.Redirect(routes.Account.Logout())
	}

	// Post component validation
	models.ValidatePostTitle(c.Validation, post.Title).Key("post.Title")

	if c.Validation.HasErrors() {
		c.Validation.Keep()
		c.FlashParams()
		c.Flash.Error("Could not create post")
		return c.Redirect(routes.Post.Create(username))
	}

	post.ProfileId = profile.ProfileId
	post.DateObj = time.Now()
	post.Status = "public"

	err := c.Txn.Insert(&post)
	if err != nil {
		panic(err)
	}

	c.Flash.Success("Post created")
	return c.Redirect(routes.Post.Show(username, post.PostId))
}
示例#2
0
文件: post.go 项目: jango2015/baseapp
func (c Post) Update(username string, id int, post models.Post) r.Result {
	existingPost := c.loadPostById(id)
	if existingPost == nil {
		return c.NotFound("Post does not exist")
	}

	profile := c.connected()
	if profile == nil || profile.UserName != username {
		c.Flash.Error("You must log in to access your account")
		return c.Redirect(routes.Account.Logout())
	}

	// Post component validation
	models.ValidatePostTitle(c.Validation, post.Title).Key("post.Title")

	if c.Validation.HasErrors() {
		c.Validation.Keep()
		c.FlashParams()
		c.Flash.Error("Could not update post")
		return c.Redirect(routes.Post.Edit(username, id))
	}

	// Update fields
	existingPost.Title = post.Title
	existingPost.ContentStr = post.ContentStr

	_, err := c.Txn.Update(existingPost)
	if err != nil {
		panic(err)
	}

	c.Flash.Success("Post updated")
	return c.Redirect(routes.Post.Show(username, existingPost.PostId))
}