// HandleShow displays a single story func HandleShow(context router.Context) error { // Find the story story, err := stories.Find(context.ParamInt("id")) if err != nil { return router.InternalError(err) } // Redirect requests to the canonical url if context.Path() != story.URLShow() { return router.Redirect(context, story.URLShow()) } // Find the comments for this story // Fetch the comments q := comments.Where("story_id=?", story.Id).Order(comments.RankOrder) rootComments, err := comments.FindAll(q) if err != nil { return router.InternalError(err) } // Render the template view := view.New(context) view.AddKey("story", story) view.AddKey("meta_title", story.Name) view.AddKey("meta_desc", story.Summary) view.AddKey("meta_keywords", story.Name) view.AddKey("comments", rootComments) return view.Render() }
// HandleIndex displays a list of comments func HandleIndex(context router.Context) error { // No auth this is public // Build a query to fetch latest 100 comments q := comments.Query().Limit(100).Order("created_at desc") // Filter on user id - we only show the actual user's comments // so not a nested view as in HN userID := context.ParamInt("u") if userID > 0 { q.Where("user_id=?", userID) } // Filter if necessary - this assumes name and summary cols filter := context.Param("filter") if len(filter) > 0 { filter = strings.Replace(filter, "&", "", -1) filter = strings.Replace(filter, " ", "", -1) filter = strings.Replace(filter, " ", " & ", -1) q.Where("(to_tsvector(text) @@ to_tsquery(?) )", filter) } // Fetch the comments results, err := comments.FindAll(q) if err != nil { return router.InternalError(err) } // Render the template view := view.New(context) view.AddKey("filter", filter) view.AddKey("comments", results) view.AddKey("meta_title", "Comments") return view.Render() }
// HandleShow serve a get request at /users/1 func HandleShow(context router.Context) error { // No auth - this is public // Find the user user, err := users.Find(context.ParamInt("id")) if err != nil { context.Logf("#error parsing user id: %s", err) return router.NotFoundError(err) } // Get the user comments q := comments.Where("user_id=?", user.Id).Limit(10).Order("created_at desc") userComments, err := comments.FindAll(q) if err != nil { return router.InternalError(err) } // Get the user stories q = stories.Where("user_id=?", user.Id).Limit(50).Order("created_at desc") userStories, err := stories.FindAll(q) if err != nil { return router.InternalError(err) } // Render the Template view := view.New(context) view.AddKey("user", user) view.AddKey("comments", userComments) view.AddKey("stories", userStories) view.AddKey("meta_title", user.Name) view.AddKey("meta_desc", user.Name) return view.Render() }