(cfg *config.Config, c *Collection, lang string, page int, includeFuture bool)
| 1497 | } |
| 1498 | |
| 1499 | func (db *datastore) GetLangPosts(cfg *config.Config, c *Collection, lang string, page int, includeFuture bool) (*[]PublicPost, error) { |
| 1500 | collID := c.ID |
| 1501 | |
| 1502 | cf := c.NewFormat() |
| 1503 | order := "DESC" |
| 1504 | if cf.Ascending() { |
| 1505 | order = "ASC" |
| 1506 | } |
| 1507 | |
| 1508 | pagePosts := cf.PostsPerPage() |
| 1509 | start := page*pagePosts - pagePosts |
| 1510 | if page == 0 { |
| 1511 | start = 0 |
| 1512 | pagePosts = 1000 |
| 1513 | } |
| 1514 | |
| 1515 | limitStr := "" |
| 1516 | if page > 0 { |
| 1517 | limitStr = fmt.Sprintf(" LIMIT %d, %d", start, pagePosts) |
| 1518 | } |
| 1519 | timeCondition := "" |
| 1520 | if !includeFuture { |
| 1521 | timeCondition = "AND created <= " + db.now() |
| 1522 | } |
| 1523 | |
| 1524 | rows, err := db.Query(`SELECT `+postCols+` |
| 1525 | FROM posts |
| 1526 | WHERE collection_id = ? AND language = ? `+timeCondition+` |
| 1527 | ORDER BY created `+order+limitStr, collID, lang) |
| 1528 | if err != nil { |
| 1529 | log.Error("Failed selecting from posts: %v", err) |
| 1530 | return nil, impart.HTTPError{http.StatusInternalServerError, "Couldn't retrieve collection posts."} |
| 1531 | } |
| 1532 | defer rows.Close() |
| 1533 | |
| 1534 | // TODO: extract this common row scanning logic for queries using `postCols` |
| 1535 | posts := []PublicPost{} |
| 1536 | for rows.Next() { |
| 1537 | p := &Post{} |
| 1538 | err = rows.Scan(&p.ID, &p.Slug, &p.Font, &p.Language, &p.RTL, &p.Privacy, &p.OwnerID, &p.CollectionID, &p.PinnedPosition, &p.Created, &p.Updated, &p.ViewCount, &p.Title, &p.Content) |
| 1539 | if err != nil { |
| 1540 | log.Error("Failed scanning row: %v", err) |
| 1541 | break |
| 1542 | } |
| 1543 | p.extractData() |
| 1544 | p.augmentContent(c) |
| 1545 | p.formatContent(cfg, c, includeFuture, false) |
| 1546 | |
| 1547 | posts = append(posts, p.processPost()) |
| 1548 | } |
| 1549 | err = rows.Err() |
| 1550 | if err != nil { |
| 1551 | log.Error("Error after Next() on rows: %v", err) |
| 1552 | } |
| 1553 | |
| 1554 | return &posts, nil |
| 1555 | } |
| 1556 |
no test coverage detected