Skip to content

Commit 1b9698b

Browse files
committed
added option to list latest posts
1 parent fc011b6 commit 1b9698b

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

ui/cli.go

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bufio"
55
"fmt"
66
"os"
7+
"sort"
78
"strconv"
89

910
"github.com/janithl/paataka/entities"
@@ -27,7 +28,8 @@ func (c *CLI) GetInput() {
2728

2829
options := []option{
2930
option{"Add Publication", c.addPublication},
30-
option{"List All Publications", c.listAll},
31+
option{"List All Publications", c.listAllPublications},
32+
option{"List Latest Posts", c.listLatestPosts},
3133
option{"Fetch All Posts", c.fetchAll},
3234
}
3335

@@ -65,7 +67,7 @@ func (c *CLI) addPublication() {
6567
c.PublicationService.Add(entities.Publication{Title: title[:len(title)-1], URL: url[:len(url)-1]})
6668
}
6769

68-
func (c *CLI) listAll() {
70+
func (c *CLI) listAllPublications() {
6971
pubs := c.PublicationService.ListAll()
7072
if len(pubs) == 0 {
7173
fmt.Println("No Publications Yet")
@@ -74,7 +76,36 @@ func (c *CLI) listAll() {
7476
}
7577

7678
for _, pub := range pubs {
77-
fmt.Printf("%-20s %-60s %4d posts\n", pub.Title, pub.URL, len(pub.Posts))
79+
fmt.Printf("%-20s %-48s %4d posts\n", pub.Title, fmt.Sprintf("%.46s", pub.URL), len(pub.Posts))
80+
}
81+
}
82+
83+
func (c *CLI) listLatestPosts() {
84+
pubs := c.PublicationService.ListAll()
85+
posts := []entities.Post{}
86+
87+
for _, pub := range pubs {
88+
for _, post := range pub.Posts {
89+
posts = append(posts, post)
90+
}
91+
}
92+
93+
if len(posts) == 0 {
94+
fmt.Println("No Posts Yet")
95+
} else {
96+
fmt.Println("\nLatest Posts:")
97+
}
98+
99+
sort.Slice(posts, func(i, j int) bool {
100+
return posts[i].AddedAt.After(posts[j].AddedAt)
101+
})
102+
103+
if len(posts) > 20 {
104+
posts = posts[:20]
105+
}
106+
107+
for _, post := range posts {
108+
fmt.Printf("%-60s %19s\n", fmt.Sprintf("%.58s", post.Title), post.AddedAt.Format("2006-01-02 03:04PM"))
78109
}
79110
}
80111

0 commit comments

Comments
 (0)