feat: added new commands

- added follow command
- added unfollow command
- following command
- introduced logged in user middleware
This commit is contained in:
Stevan Freeborn
2026-08-02 07:49:57 -05:00
parent c012f6008c
commit f51b1b941a
8 changed files with 331 additions and 9 deletions
+101 -9
View File
@@ -51,6 +51,9 @@ func NewRegistry() *CommandRegistry {
aggCommand(),
addFeedCommand(),
feedsCommand(),
followCommand(),
followingCommand(),
unfollowCommand(),
}
for _, cmd := range commands {
@@ -194,7 +197,7 @@ func aggCommand() *Command {
}
func addFeedCommand() *Command {
return newCommand("addfeed", func(s *state.State) error {
return newCommand("addfeed", requiresLoggedInUser(func(s *state.State, currentUser database.User) error {
if len(s.Arguments) < 2 {
return fmt.Errorf("Did not receive expected feed name and url")
}
@@ -212,12 +215,6 @@ func addFeedCommand() *Command {
return fmt.Errorf("Feed url '%s' is not a valid url", feedUrl)
}
currentUser, err := s.GetCurrentUser(context.Background())
if err != nil {
return fmt.Errorf("Currently logged in user does not exist. Cannot add feed for non-existent user.")
}
createFeedParams := database.CreateFeedParams{
ID: uuid.New(),
UserID: currentUser.ID,
@@ -233,14 +230,28 @@ func addFeedCommand() *Command {
return err
}
fmt.Printf("Successfully added feed '%s' with url '%s'\n", createdFeed.Name, createdFeed.Url)
createFollowParams := database.CreateFollowParams{
ID: uuid.New(),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
UserID: currentUser.ID,
FeedID: createdFeed.ID,
}
_, err = s.Database.CreateFollow(context.Background(), createFollowParams)
if err != nil {
return err
}
fmt.Printf("Successfully added and followed feed '%s' with url '%s'\n", createdFeed.Name, createdFeed.Url)
fmt.Printf(" Id => %s\n", createdFeed.ID)
fmt.Printf(" UserId => %s\n", createdFeed.UserID)
fmt.Printf(" CreatedAt => %s\n", createdFeed.CreatedAt)
fmt.Printf(" UpdatedAt => %s\n", createdFeed.UpdatedAt)
return nil
})
}))
}
func feedsCommand() *Command {
@@ -266,3 +277,84 @@ func feedsCommand() *Command {
return nil
})
}
func followCommand() *Command {
return newCommand("follow", requiresLoggedInUser(func(s *state.State, currentUser database.User) error {
if len(s.Arguments) == 0 {
return fmt.Errorf("Did not receive expected url argument")
}
urlForFeedToFollow := s.Arguments[0]
feed, err := s.Database.GetFeedByUrl(context.Background(), urlForFeedToFollow)
if err != nil {
return fmt.Errorf("Unable to find feed with url '%s'", urlForFeedToFollow)
}
createFollowParams := database.CreateFollowParams{
ID: uuid.New(),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
FeedID: feed.ID,
UserID: currentUser.ID,
}
createdFollow, err := s.Database.CreateFollow(context.Background(), createFollowParams)
if err != nil {
return fmt.Errorf("Unable to follow feed '%s'", feed.Name)
}
fmt.Printf("'%s' successfully followed feed '%s'", createdFollow.UserName, createdFollow.FeedName)
return nil
}))
}
func followingCommand() *Command {
return newCommand("following", requiresLoggedInUser(func(s *state.State, currentUser database.User) error {
follows, err := s.Database.GetFollowsForUser(context.Background(), currentUser.ID)
if err != nil {
return fmt.Errorf("Unable to find follows for user")
}
for _, follow := range follows {
fmt.Printf("* %s", follow.FeedName)
}
return nil
}))
}
func unfollowCommand() *Command {
return newCommand("unfollow", requiresLoggedInUser(func(s *state.State, currentUser database.User) error {
if len(s.Arguments) == 0 {
return fmt.Errorf("Did not receive expected feed url argument")
}
feedUrl := s.Arguments[0]
feed, err := s.Database.GetFeedByUrl(context.Background(), feedUrl)
if err != nil {
return fmt.Errorf("Unable to find feed with url '%s'", feedUrl)
}
deleteFollowParams := database.DeleteFollowForUserParams{
UserID: currentUser.ID,
FeedID: feed.ID,
}
err = s.Database.DeleteFollowForUser(context.Background(), deleteFollowParams)
if err != nil {
return fmt.Errorf("Unable to unfollow feed '%s'", feed.Name)
}
fmt.Printf("Successfully unfollowed feed '%s'", feed.Name)
return nil
}))
}
+20
View File
@@ -0,0 +1,20 @@
package command
import (
"context"
"github.com/StevanFreeborn/gator/internal/database"
"github.com/StevanFreeborn/gator/internal/state"
)
func requiresLoggedInUser(handler func(*state.State, database.User) error) CommandHandler {
return func(s *state.State) error {
loggedInUser, err := s.GetCurrentUser(context.Background())
if err != nil {
return err
}
return handler(s, loggedInUser)
}
}