-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler_feed.go
81 lines (68 loc) · 1.85 KB
/
handler_feed.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main
import (
"context"
"fmt"
"time"
"github.com/google/uuid"
"github.com/katsuikeda/gator/internal/database"
)
func handlerAddFeed(s *state, cmd command, currentUser database.User) error {
if len(cmd.args) != 2 {
return fmt.Errorf("usage: %s <feed_name> <url>", cmd.name)
}
feedName := cmd.args[0]
feedURL := cmd.args[1]
feed, err := s.db.CreateFeed(context.Background(), database.CreateFeedParams{
ID: uuid.New(),
CreatedAt: time.Now().UTC(),
UpdatedAt: time.Now().UTC(),
Name: feedName,
Url: feedURL,
UserID: currentUser.ID,
})
if err != nil {
return fmt.Errorf("couldn't create feed %w", err)
}
_, err = s.db.CreateFeedFollow(context.Background(), database.CreateFeedFollowParams{
ID: uuid.New(),
CreatedAt: time.Now().UTC(),
UpdatedAt: time.Now().UTC(),
UserID: currentUser.ID,
FeedID: feed.ID,
})
if err != nil {
return fmt.Errorf("couldn't follow feed %w", err)
}
fmt.Println("Feed created and followed successfully:")
printFeed(feed)
return nil
}
func printFeed(feed database.Feed) {
fmt.Printf("* ID: %v\n", feed.ID)
fmt.Printf("* Name: %v\n", feed.Name)
fmt.Printf("* URL: %v\n", feed.Url)
}
func handlerFeeds(s *state, cmd command) error {
if len(cmd.args) != 0 {
return fmt.Errorf("usage: %s", cmd.name)
}
feeds, err := s.db.GetFeeds(context.Background())
if err != nil {
return fmt.Errorf("couldn't get feeds: %w", err)
}
if len(feeds) == 0 {
fmt.Println("No feeds found")
return nil
}
fmt.Printf("Found %d feeds:\n", len(feeds))
for _, feed := range feeds {
user, err := s.db.GetUserById(context.Background(), feed.UserID)
if err != nil {
return fmt.Errorf("couldn't find user with the id")
}
fmt.Printf("* %s\n", feed.Name)
fmt.Printf(" - URL: %s\n", feed.Url)
fmt.Printf(" - Added by: %s\n", user.Name)
}
return nil
}