#13 - browse articles

This commit is contained in:
2025-03-11 16:08:34 +02:00
parent f561eb4c34
commit 80602bf04f
10 changed files with 265 additions and 1 deletions

View File

@@ -0,0 +1,36 @@
package handlers
import (
"context"
"fmt"
"github.com/rdarius/boot-dev-blog-aggregator/internal/config"
"github.com/rdarius/boot-dev-blog-aggregator/internal/database"
"strconv"
)
func BrowseHandler(s *config.State, cmd config.Command, user database.User) error {
var limit int32
if len(cmd.Args) < 1 {
limit = 2
} else {
l, err := strconv.Atoi(cmd.Args[0])
if err != nil {
return err
}
limit = int32(l)
}
posts, err := s.DB.GetPostsForUser(context.Background(), database.GetPostsForUserParams{
UserID: user.ID,
Limit: limit,
})
if err != nil {
return err
}
for _, post := range posts {
fmt.Printf("Title: %s\nURL: %s\nArticle: %sPublished: %s\n\n", post.Title, post.Url, post.Description, post.PublishedAt)
}
return nil
}

View File

@@ -3,8 +3,12 @@ package handlers
import (
"context"
"fmt"
"github.com/google/uuid"
"github.com/rdarius/boot-dev-blog-aggregator/internal/config"
"github.com/rdarius/boot-dev-blog-aggregator/internal/database"
"github.com/rdarius/boot-dev-blog-aggregator/internal/parser"
"github.com/rdarius/boot-dev-blog-aggregator/internal/rss"
"time"
)
func ScrapeFeedsHandler(s *config.State, cmd config.Command) error {
@@ -29,7 +33,24 @@ func ScrapeFeedsHandler(s *config.State, cmd config.Command) error {
fmt.Println("Feed Title: " + feedData.Channel.Title)
for _, i := range feedData.Channel.Item {
fmt.Println("Item Title: " + i.Title)
pubTime, err := parser.ParseTimestamp(i.PubDate)
if err != nil {
return err
}
_, err = s.DB.CreatePost(ctx, database.CreatePostParams{
ID: uuid.New(),
CreatedAt: time.Time{},
UpdatedAt: time.Time{},
FeedID: nextFeed.ID,
Url: i.Link,
Title: i.Title,
Description: i.Description,
PublishedAt: pubTime,
})
if err != nil {
return err
}
}
return nil