Skip to content

Commit

Permalink
chore(app): Refactor types for more consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkJD committed Nov 12, 2023
1 parent 50c6d70 commit a300d09
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 134 deletions.
8 changes: 5 additions & 3 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"io"
"log"
"mime/multipart"
. "munch-o-matic/client/types"
"net/http"
"net/http/cookiejar"
"net/url"
Expand All @@ -29,13 +28,13 @@ func NewClient(Config Config) (*RestClient, error) {

err := ValidateConfig(Config)
if err != nil {
return &RestClient{}, fmt.Errorf("Error validating config: %w", err)
return &RestClient{}, fmt.Errorf("validating config: %w", err)
}
c.Config = Config

jar, err := cookiejar.New(nil)
if err != nil {
return nil, fmt.Errorf("error initializing cookie jar: %v", err)
return nil, fmt.Errorf("initializing cookie jar: %v", err)
}
c.CookieJar = jar
c.Client = &http.Client{
Expand All @@ -47,6 +46,9 @@ func NewClient(Config Config) (*RestClient, error) {
Path: "/",
}
cookieUrl, err := url.Parse("https://rest.tastenext.de")
if err != nil {
return &RestClient{}, fmt.Errorf("perse cookie url: %w", err)
}
c.CookieJar.SetCookies(cookieUrl, []*http.Cookie{cookie})

// Check if the old SessionId works
Expand Down
40 changes: 29 additions & 11 deletions client/types/response_types.go → client/types.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
package types
package client

type CurrentUserResponse struct {
User struct {
ID int `json:"id"`
} `json:"user"`
import "time"

// Client types
type Dish struct {
ID int `json:"id"`
Description string `json:"description"`
Name string `json:"name"`
}

type UpcomingDish struct {
Dummy bool
Date time.Time
OrderId int
Dish Dish
Orders int // We get the total order for each dish from the API ;-)
Booked bool
}

type Bookings struct {
Expand All @@ -15,6 +27,18 @@ type Bookings struct {
} `json:"menuBlockLineEntry"`
}

type MenuDates struct {
Year int
CalendarWeek int
}

// Response types
type CurrentUserResponse struct {
User struct {
ID int `json:"id"`
} `json:"user"`
}

type UserResponse struct {
User struct {
ID int `json:"id"`
Expand Down Expand Up @@ -52,12 +76,6 @@ type UserResponse struct {
} `json:"user"`
}

type Dish struct {
ID int `json:"id"`
Description string `json:"description"`
Name string `json:"name"`
}

type MenuResponse struct {
Status string `json:"status"`
Message string `json:"message"`
Expand Down
12 changes: 0 additions & 12 deletions client/types/client.go

This file was deleted.

75 changes: 0 additions & 75 deletions client/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,9 @@ package client

import (
"fmt"
"math/rand"
. "munch-o-matic/client/types"
"time"
)

type MenuDates struct {
Year int
CalendarWeek int
}

// Calculate the calendar weeks from now to count
func GetNextCalenderWeeks(count int) []MenuDates {
var weeks []MenuDates
Expand Down Expand Up @@ -42,71 +35,3 @@ func GetEmissionDateAsTime(emissionDate interface{}) (time.Time, error) {
return time.Time{}, fmt.Errorf("unknown type for mission_date: %T", v)
}
}

// How often a dish was ordered in the past
func GetOrderCount(Bookings []Bookings, DishId int) (count int, dish Dish, error error) {
for _, booking := range Bookings {
if DishId == booking.MenuBlockLineEntry.Dish.ID {
count++
dish = booking.MenuBlockLineEntry.Dish
}
}

if count > 0 {
return count, dish, nil
}

return 0, Dish{}, fmt.Errorf("Dish ID not found in orders")
}

// Pick dishes automatically based on a few strategies
func ChooseDishesByStrategy(Strategy string, UpcomingDishes map[string][]UpcomingDish) (map[int]UpcomingDish, error) {
retVal := map[int]UpcomingDish{}

// Helper function to decide if menu should be skipped
shouldSkipMenu := func(menu []UpcomingDish) bool {
for _, dish := range menu {
if dish.Booked || dish.Dummy {
return true
}
}
return false
}

// Iterate over the dishes of the day
for _, menu := range UpcomingDishes {
if shouldSkipMenu(menu) {
continue
}
// Choose dish based on the strategy
switch Strategy {

case "SchoolFav":
var maxPos, maxVal int
for i, dish := range menu {
if dish.Orders > maxVal {
maxPos = i
maxVal = dish.Orders
}
}
retVal[menu[maxPos].OrderId] = menu[maxPos]

case "Random":
randomInt := rand.Intn(len(menu))
retVal[menu[randomInt].OrderId] = menu[randomInt]

case "PersonalFav":
/* TODO: Add personal order count in getMenuWeek or structure the code better.
var maxPos, maxVal int
for i, dish := range menu {
GetOrderCount()
}
*/
return map[int]UpcomingDish{}, fmt.Errorf("PersonalFav is not implemented, sorry")

default:
return map[int]UpcomingDish{}, fmt.Errorf("%v is not a valid strategy", Strategy)
}
}
return retVal, nil
}
7 changes: 3 additions & 4 deletions cmd/dish_count.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ package cmd

import (
"fmt"

. "munch-o-matic/client"
"munch-o-matic/core"

"github.com/spf13/cobra"
)
Expand All @@ -17,7 +16,7 @@ var countDish = &cobra.Command{
fmt.Println(err)
}

dishCount, dish, err := GetOrderCount(userResp.User.Customer.Bookings, dishId)
dishCount, dish, err := core.GetOrderCount(userResp.User.Customer.Bookings, dishId)
if err != nil {
fmt.Printf("Error calculating dish count: %v\n", err)
} else {
Expand All @@ -27,6 +26,6 @@ var countDish = &cobra.Command{
}

func init() {
countDish.Flags().IntVar(&dishId, "dish", 0, "DishId")
countDish.Flags().IntVarP(&dishId, "dish-id", "d", 0, "DishId")
dishCmd.AddCommand(countDish)
}
4 changes: 2 additions & 2 deletions cmd/menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cmd

import (
"fmt"
. "munch-o-matic/client/types"
"munch-o-matic/client"
"os"
"sort"

Expand All @@ -19,7 +19,7 @@ func init() {
rootCmd.AddCommand(menuCmd)
}

func renderUpcomingDishesTable(UpcomingDishes map[string][]UpcomingDish) {
func renderUpcomingDishesTable(UpcomingDishes map[string][]client.UpcomingDish) {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Date", "OrderId", "Booked", "Name", "Description", "Orders", "DishId"})
table.SetAutoMergeCells(true)
Expand Down
6 changes: 3 additions & 3 deletions cmd/menu_auto_order.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"log"
"munch-o-matic/client"
. "munch-o-matic/client/types"
"munch-o-matic/core"
"time"

"github.com/spf13/cobra"
Expand All @@ -19,7 +19,7 @@ var autoOrderMenu = &cobra.Command{
fmt.Println("--day and --weeks are mutually exclusive. --week is ignored")
}

var upcomingDishes = map[string][]UpcomingDish{}
var upcomingDishes = map[string][]client.UpcomingDish{}
var err error
if len(menuDay) != 0 {
parsedDate, err := time.Parse("02.01.06", menuDay)
Expand All @@ -39,7 +39,7 @@ var autoOrderMenu = &cobra.Command{
log.Fatal("Please provide --day or --weeks")
}

dishes, err := client.ChooseDishesByStrategy(autoOrderStrategy, upcomingDishes)
dishes, err := core.ChooseDishesByStrategy(autoOrderStrategy, upcomingDishes)
if err != nil {
log.Fatal("Error picking menues")
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/menu_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package cmd
import (
"fmt"
"log"
. "munch-o-matic/client/types"
"munch-o-matic/client"
"time"

"github.com/spf13/cobra"
Expand All @@ -18,7 +18,7 @@ var getMenu = &cobra.Command{
fmt.Println("--day and --weeks are mutually exclusive. --week is ignored")
}

var upcomingDishes = map[string][]UpcomingDish{}
var upcomingDishes = map[string][]client.UpcomingDish{}
var err error
if len(menuDay) != 0 {
parsedDate, err := time.Parse("02.01.06", menuDay)
Expand Down
Loading

0 comments on commit a300d09

Please sign in to comment.