MCPcopy
hub / github.com/writefreely/writefreely / GetTopPosts

Method GetTopPosts

database.go:2064–2122  ·  view source on GitHub ↗
(u *User, alias string, hostName string)

Source from the content-addressed store, hash-verified

2062}
2063
2064func (db *datastore) GetTopPosts(u *User, alias string, hostName string) (*[]PublicPost, error) {
2065 params := []interface{}{u.ID}
2066 where := ""
2067 if alias != "" {
2068 where = " AND alias = ?"
2069 params = append(params, alias)
2070 }
2071 rows, err := db.Query("SELECT p.id, p.slug, p.view_count, p.title, p.content, c.alias, c.title, c.description, c.view_count FROM posts p LEFT JOIN collections c ON p.collection_id = c.id WHERE p.owner_id = ?"+where+" ORDER BY p.view_count DESC, created DESC LIMIT 25", params...)
2072 if err != nil {
2073 log.Error("Failed selecting from posts: %v", err)
2074 return nil, impart.HTTPError{http.StatusInternalServerError, "Couldn't retrieve user top posts."}
2075 }
2076 defer rows.Close()
2077
2078 posts := []PublicPost{}
2079 var gotErr bool
2080 for rows.Next() {
2081 p := Post{}
2082 c := Collection{}
2083 var alias, title, description sql.NullString
2084 var views sql.NullInt64
2085 err = rows.Scan(&p.ID, &p.Slug, &p.ViewCount, &p.Title, &p.Content, &alias, &title, &description, &views)
2086 if err != nil {
2087 log.Error("Failed scanning User.getPosts() row: %v", err)
2088 gotErr = true
2089 break
2090 }
2091 p.extractData()
2092 pubPost := p.processPost()
2093
2094 if alias.Valid && alias.String != "" {
2095 c.Alias = alias.String
2096 c.Title = title.String
2097 c.Description = description.String
2098 c.Views = views.Int64
2099 c.hostName = hostName
2100 pubPost.Collection = &CollectionObj{Collection: c}
2101 }
2102 p.LikeCount, err = db.GetPostLikeCounts(p.ID)
2103 if err != nil {
2104 log.Error("Failed GetPostLikeCounts(%s): %v", p.ID, err)
2105 gotErr = true
2106 break
2107 }
2108
2109 posts = append(posts, pubPost)
2110 }
2111 err = rows.Err()
2112 if err != nil {
2113 log.Error("Error after Next() on rows: %v", err)
2114 }
2115
2116 if gotErr && len(posts) == 0 {
2117 // There were a lot of errors
2118 return nil, impart.HTTPError{http.StatusInternalServerError, "Unable to get data."}
2119 }
2120
2121 return &posts, nil

Callers

nothing calls this directly

Calls 4

extractDataMethod · 0.95
processPostMethod · 0.95
GetPostLikeCountsMethod · 0.95
CloseMethod · 0.80

Tested by

no test coverage detected