diff --git a/cmd/delete.go b/cmd/delete.go new file mode 100644 index 0000000..62987c2 --- /dev/null +++ b/cmd/delete.go @@ -0,0 +1,34 @@ +/* +Copyright © 2024 NAME HERE +*/ +package cmd + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +// deleteCmd represents the delete command +var deleteCmd = &cobra.Command{ + Use: "delete", + Short: "Deletes a favorite mensa", + Long: `Returns a list of your favorite mensas and prompts you to select one to delete.`, + Run: func(cmd *cobra.Command, args []string) { + fmt.Println("delete called") + }, +} + +func init() { + favCmd.AddCommand(deleteCmd) + + // Here you will define your flags and configuration settings. + + // Cobra supports Persistent Flags which will work for this command + // and all subcommands, e.g.: + // deleteCmd.PersistentFlags().String("foo", "", "A help for foo") + + // Cobra supports local flags which will only run when this command + // is called directly, e.g.: + // deleteCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") +} diff --git a/cmd/fav.go b/cmd/fav.go index 729204a..4069595 100644 --- a/cmd/fav.go +++ b/cmd/fav.go @@ -5,8 +5,10 @@ package cmd import ( "fmt" - "strings" + "github.com/LeRoid-hub/Mensa-CLI/internal" + "github.com/fatih/color" + "github.com/rodaine/table" "github.com/spf13/cobra" "github.com/spf13/viper" ) @@ -18,13 +20,40 @@ var favCmd = &cobra.Command{ Long: `Retrieves the favorite mensa menus for the current day. You can add mensas to your favorites using the search command.`, Run: func(cmd *cobra.Command, args []string) { - favorites := viper.GetStringSlice("favorites") - if len(favorites) == 0 { - fmt.Println("You have no favorites") - return - } + favorites := viper.Get("favorites").([]interface{}) + fmt.Println(favorites) - fmt.Println("Your favorites: " + strings.Join(favorites, ", ")) + s := make([]string, len(favorites)) + for i, v := range favorites { + s[i] = v.(string) + } + fmt.Println(s, len(s), s[0]) + + for i := 0; i < len(s); i++ { + data, err := internal.GetMenu(s[i]) + if err != nil { + fmt.Println("Error fetching data") + } + headerFmt := color.New(color.FgGreen, color.Underline).SprintfFunc() + columnFmt := color.New(color.FgYellow).SprintfFunc() + + tbl := table.New("Offering", "Dish", "Price") + tbl.WithHeaderFormatter(headerFmt).WithFirstColumnFormatter(columnFmt) + + color.Green("Mensa: %s", data.Name) + + for _, day := range data.Days { + for _, menu := range day.Menu { + for _, meal := range menu.Meal { + tbl.AddRow(menu.Name, meal.Name, meal.Price) + } + } + } + + tbl.Print() + + fmt.Println() + } }, } diff --git a/cmd/root.go b/cmd/root.go index 56b9391..733714a 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -53,8 +53,9 @@ func init() { } func setDefaults() { + favorites := []string{} viper.SetDefault("Server", "https://mensa.barfuss.email") - viper.SetDefault("favorites", []string{}) + viper.SetDefault("favorites", favorites) } func initConfig() { diff --git a/cmd/search.go b/cmd/search.go index 40c7341..d8ea1db 100644 --- a/cmd/search.go +++ b/cmd/search.go @@ -91,7 +91,7 @@ var searchCmd = &cobra.Command{ } if result == "menu" { - data, err := internal.GetMenu(selectedCity, selectedMensa) + data, err := internal.GetSearchMenu(selectedCity, selectedMensa) if err != nil { fmt.Println("Error fetching data") } @@ -113,12 +113,19 @@ var searchCmd = &cobra.Command{ } if result == "favorites" { - favorites := viper.Get("favorites").([]string) - favorites = append(favorites, selectedCity+"/"+selectedMensa) + favorites := viper.Get("favorites").([]interface{}) + + s := make([]string, len(favorites)) + for i, v := range favorites { + s[i] = v.(string) + } + + s = append(s, selectedCity+"/"+selectedMensa) + viper.Set("favorites", s) + fmt.Println("Added " + selectedCity + "/" + selectedMensa + " to your favorites") - viper.Set("favorites", favorites) fmt.Println(viper.Get("favorites")) - viper.SafeWriteConfig() + viper.WriteConfig() } }, diff --git a/internal/getMenu.go b/internal/getMenu.go index 88a6375..ba01a1b 100644 --- a/internal/getMenu.go +++ b/internal/getMenu.go @@ -9,8 +9,8 @@ import ( "github.com/LeRoid-hub/Mensa-CLI/models" ) -func GetMenu(city string, mensa string) (models.Mensa, error) { - http, err := http.Get("https://mensa.barfuss.email/mensa/" + city + "/" + mensa) +func GetMenu(mensa string) (models.Mensa, error) { + http, err := http.Get("https://mensa.barfuss.email/mensa/" + mensa) if err != nil { return models.Mensa{}, err } @@ -31,3 +31,7 @@ func GetMenu(city string, mensa string) (models.Mensa, error) { return data, nil } + +func GetSearchMenu(city string, mensa string) (models.Mensa, error) { + return GetMenu(city + "/" + mensa) +}