From d4e79ed688778a42e3552a9e7bf7acb6a7ef8b1e Mon Sep 17 00:00:00 2001 From: Jan Date: Wed, 18 Sep 2024 12:08:11 +0200 Subject: [PATCH] mostly os search --- cmd/mensa.go | 1 - cmd/search.go | 82 ++++++++++++++++++++++++++++- internal/getCity.go | 38 +++++++++++++ internal/getMensa.go | 38 +++++++++++++ internal/getMenu.go | 5 ++ internal/{getAll.go => getState.go} | 0 6 files changed, 162 insertions(+), 2 deletions(-) create mode 100644 internal/getMenu.go rename internal/{getAll.go => getState.go} (100%) diff --git a/cmd/mensa.go b/cmd/mensa.go index deac887..b0d2c19 100644 --- a/cmd/mensa.go +++ b/cmd/mensa.go @@ -1,6 +1,5 @@ /* Copyright © 2024 NAME HERE - */ package cmd diff --git a/cmd/search.go b/cmd/search.go index 6a48623..987a026 100644 --- a/cmd/search.go +++ b/cmd/search.go @@ -5,7 +5,10 @@ package cmd import ( "fmt" + "strings" + "github.com/LeRoid-hub/Mensa-CLI/internal" + "github.com/manifoldco/promptui" "github.com/spf13/cobra" ) @@ -20,7 +23,84 @@ Cobra is a CLI library for Go that empowers applications. This application is a tool to generate the needed files to quickly create a Cobra application.`, Run: func(cmd *cobra.Command, args []string) { - fmt.Println("search called") + prompt := promptui.Select{ + Label: "Select Day", + Items: []string{"baden-wuerttemberg", "bayern", "berlin", "brandenburg", "bremen", + "hamburg", "hessen", "mecklenburg-vorpommern", "niedersachsen", "nordrhein-westfalen", "rheinland-pfalz", "saarland", "sachsen", "sachsen-anhalt", "schleswig-holstein", "thueringen"}, + } + + _, result, err := prompt.Run() + + if err != nil { + fmt.Printf("Prompt failed %v\n", err) + return + } + + cities, err := internal.GetState(result) + if err != nil { + fmt.Println("Error fetching data") + } + + city := strings.Split(cities, ",") + + prompt = promptui.Select{ + Label: "Select City", + Items: city, + } + + _, result, err = prompt.Run() + + if err != nil { + fmt.Printf("Prompt failed %v\n", err) + return + } + + mensen, err := internal.GetMensen(result) + if err != nil { + fmt.Println("Error fetching data") + } + + mensa := strings.Split(mensen, ",") + + prompt = promptui.Select{ + Label: "Select Mensa", + Items: mensa, + } + + _, result, err = prompt.Run() + + if err != nil { + fmt.Printf("Prompt failed %v\n", err) + return + } + + prompt = promptui.Select{ + Label: "Do you want to see the menu or save the mensa to your favorites?", + Items: []string{"menu", "favorites"}, + } + + _, result, err = prompt.Run() + + if err != nil { + fmt.Printf("Prompt failed %v\n", err) + return + } + + fmt.Printf("You choose %q\n", result) + + if result == "menu" { + menu, err := internal.GetMenu(result) + if err != nil { + fmt.Println("Error fetching data") + } + + fmt.Println(menu) + } + + if result == "favorites" { + // viper + } + }, } diff --git a/internal/getCity.go b/internal/getCity.go index 5bf0569..5cf8d07 100644 --- a/internal/getCity.go +++ b/internal/getCity.go @@ -1 +1,39 @@ package internal + +import ( + "encoding/json" + "fmt" + "io" + "net/http" +) + +func GetState(state string) (string, error) { + fmt.Println("Get all called") + + http, err := http.Get("https://mensa.barfuss.email/state/" + state) + if err != nil { + return "", err + } + + defer http.Body.Close() + body, err := io.ReadAll(http.Body) + if err != nil { + return "", err + } + data := string(body) + + var cities []string + err = json.Unmarshal([]byte(data), &cities) + if err != nil { + return "", err + } + + var result string + for _, city := range cities { + result += city + "," + + } + city := result[:len(result)-1] + + return city, nil +} diff --git a/internal/getMensa.go b/internal/getMensa.go index 5bf0569..137f658 100644 --- a/internal/getMensa.go +++ b/internal/getMensa.go @@ -1 +1,39 @@ package internal + +import ( + "encoding/json" + "fmt" + "io" + "net/http" +) + +func GetMensen(city string) (string, error) { + fmt.Println("Get all called") + + http, err := http.Get("https://mensa.barfuss.email/city/" + city) + if err != nil { + return "", err + } + + defer http.Body.Close() + body, err := io.ReadAll(http.Body) + if err != nil { + return "", err + } + data := string(body) + + var mensen []string + err = json.Unmarshal([]byte(data), &mensen) + if err != nil { + return "", err + } + + var result string + for _, mensa := range mensen { + result += mensa + "," + + } + mensa := result[:len(result)-1] + + return mensa, nil +} diff --git a/internal/getMenu.go b/internal/getMenu.go new file mode 100644 index 0000000..9647c51 --- /dev/null +++ b/internal/getMenu.go @@ -0,0 +1,5 @@ +package internal + +func GetMenu(mensa string) (string, error) { + return "", nil +} diff --git a/internal/getAll.go b/internal/getState.go similarity index 100% rename from internal/getAll.go rename to internal/getState.go