示例#1
0
// storyHasUserFlag returns true if we already have a flag for this story from this user
func storyHasUserFlag(story *stories.Story, user *users.User) bool {
	// Query flags table for rows with userId and storyId
	// if we don't get error, return true
	results, err := query.New("flags", "story_id").Where("story_id=?", story.Id).Where("user_id=?", user.Id).Results()
	if err == nil && len(results) == 0 {
		return false
	}

	return true
}
示例#2
0
// commentHasUserFlag returns true if we already have a flag for this comment from this user
func commentHasUserFlag(comment *comments.Comment, user *users.User) bool {
	// Query flags table for rows with userId and commentId
	// if we don't get error, return true
	results, err := query.New("flags", "comment_id").Where("comment_id=?", comment.Id).Where("user_id=?", user.Id).Results()
	if err == nil && len(results) == 0 {
		return false
	}

	return true
}
示例#3
0
// HandleUpvoted displays a list of stories the user has upvoted in the past
func HandleUpvoted(context router.Context) error {

	// Build a query
	q := stories.Query().Limit(listLimit)

	// Select only above 0 points,  Order by rank, then points, then name
	q.Where("points > 0").Order("rank desc, points desc, id desc")

	// Select only stories which the user has upvoted
	user := authorise.CurrentUser(context)
	if !user.Anon() {
		// Can we use a join instead?
		v := query.New("votes", "story_id").Select("select story_id as id from votes").Where("user_id=? AND story_id IS NOT NULL AND points > 0", user.Id)

		storyIDs := v.ResultIDs()
		if len(storyIDs) > 0 {
			q.WhereIn("id", storyIDs)
		}
	}

	// Set the offset in pages if we have one
	page := int(context.ParamInt("page"))
	if page > 0 {
		q.Offset(listLimit * page)
	}

	// Fetch the stories
	results, err := stories.FindAll(q)
	if err != nil {
		return router.InternalError(err)
	}

	// Render the template
	view := view.New(context)
	setStoriesMetadata(view, context.Request())
	view.AddKey("page", page)
	view.AddKey("stories", results)
	view.Template("stories/views/index.html.got")

	if context.Param("format") == ".xml" {
		view.Layout("")
		view.Template("stories/views/index.xml.got")
	}

	return view.Render()

}
示例#4
0
// Query returns a new query for stories
func Query() *query.Query {
	p := New()
	return query.New(p.TableName, p.KeyName)
}
示例#5
0
// Query a new query relation referencing this model, optionally setting a default order
func Query() *query.Query {
	return query.New("users", "id")
}
示例#6
0
// Query creates a new query relation referencing this model
func Query() *query.Query {
	return query.New("images", "id")
}
示例#7
0
// Create a new query relation referencing this model
func Query() *query.Query {
	return query.New("tags", "id")
}