Skip to content

Commit fc011b6

Browse files
committed
fixed duplicate posts being fetched, some cli improvements
1 parent 82771c4 commit fc011b6

File tree

3 files changed

+22
-10
lines changed

3 files changed

+22
-10
lines changed

ui/cli.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func (c *CLI) listAll() {
7474
}
7575

7676
for _, pub := range pubs {
77-
fmt.Printf("%s (%s)\n", pub.Title, pub.URL)
77+
fmt.Printf("%-20s %-60s %4d posts\n", pub.Title, pub.URL, len(pub.Posts))
7878
}
7979
}
8080

usecases/publicationServiceImpl.go

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,7 @@ func (p *PublicationServiceImpl) Find(id string) (entities.Publication, error) {
6060
func (p *PublicationServiceImpl) FetchPublicationPosts(pub entities.Publication) error {
6161
posts := p.reader.Read(pub.URL)
6262
for _, post := range posts {
63-
if post.ID == "" {
64-
post.ID = p.generateID()
65-
}
66-
post.AddedAt = time.Now()
67-
post.UpdatedAt = time.Now()
68-
69-
pub.AddPost(post)
63+
pub = p.addUniquePost(pub, post)
7064
}
7165

7266
pub.FetchedAt = time.Now()
@@ -77,3 +71,21 @@ func (p *PublicationServiceImpl) FetchPublicationPosts(pub entities.Publication)
7771
func (p *PublicationServiceImpl) generateID() string {
7872
return fmt.Sprintf("%x-%x-%x", p.random.Uint32(), p.random.Uint32(), p.random.Uint32())
7973
}
74+
75+
func (p *PublicationServiceImpl) addUniquePost(pub entities.Publication, post entities.Post) entities.Publication {
76+
for _, item := range pub.Posts {
77+
if item.URL == post.URL {
78+
return pub
79+
}
80+
}
81+
82+
if post.ID == "" {
83+
post.ID = p.generateID()
84+
}
85+
post.AddedAt = time.Now()
86+
post.UpdatedAt = time.Now()
87+
88+
pub.AddPost(post)
89+
90+
return pub
91+
}

usecases/publicationServiceImpl_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,8 @@ func TestFetchPublicationPostsAddAndListAll(t *testing.T) {
172172
} else {
173173
t.Run("Make sure all the posts have been added in...", func(t *testing.T) {
174174
matches := 0
175-
for _, wants := range mockFeedReader.Posts {
176-
for _, gots := range pub.Posts {
175+
for _, gots := range pub.Posts {
176+
for _, wants := range mockFeedReader.Posts {
177177
if gots.Title == wants.Title && gots.URL == wants.URL {
178178
t.Logf("Got '%s', want '%s'", gots, wants)
179179
matches++

0 commit comments

Comments
 (0)