Skip to content

Commit

Permalink
chore: finalisation of tap
Browse files Browse the repository at this point in the history
  • Loading branch information
Topvennie committed Nov 20, 2024
1 parent 086b13b commit e649034
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 85 deletions.
64 changes: 44 additions & 20 deletions makefile
Original file line number Diff line number Diff line change
@@ -1,40 +1,64 @@
# Variables
BACKEND_BIN := backend
TUI_BIN := tui
BACKEND_SRC := cmd/backend/backend.go
TUI_SRC := cmd/tui/tui.go
DB_DIR := ./db/migrations
DB_FILE := ./sqlite.db

# Phony targets
.PHONY: all build clean run run-backend run-tui sqlc create-migration goose migrate watch

# Default target: build everything
all: build

build: clean backend tui
# Build targets
build: clean build-backend build-tui

build-backend:
@echo "Building $(BACKEND_BIN)..."
@rm -f $(BACKEND_BIN)
@go build -o $(BACKEND_BIN) $(BACKEND_SRC)

run: backend tui
@./backend & ./tui
build-tui:
@echo "Building $(TUI_BIN)..."
@rm -f $(TUI_BIN)
@go build -o $(TUI_BIN) $(TUI_SRC)

run-backend: backend
@./backend
# Run targets
run: run-backend run-tui

run-tui: tui
run-backend:
@[ -f $(BACKEND_BIN) ] || $(MAKE) build-backend
@./$(BACKEND_BIN)

run-tui:
@[ -f $(TUI_BIN) ] || $(MAKE) build-tui
@read -p "Enter screen name: " screen; \
./tui $$screen
./$(TUI_BIN) $$screen

backend:
@[ -f backend ] || (echo "Building backend..." && go build -o backend cmd/backend/backend.go)
# Clean targets
clean: clean-backend clean-tui

tui:
@[ -f tui ] || (echo "Building tui..." && go build -o tui cmd/tui/tui.go)
clean-backend:
@echo "Cleaning $(BACKEND_BIN)..."
@rm -f $(BACKEND_BIN)

clean:
@rm -f backend tui
clean-tui:
@echo "Cleaning $(TUI_BIN)..."
@rm -f $(TUI_BIN)

# SQL and migration targets
sqlc:
sqlc generate

create-migration:
@read -p "Enter migration name: " name; \
goose -dir ./db/migrations create $$name sql
goose -dir $(DB_DIR) create $$name sql

goose:
@read -p "Action: " action; \
goose -dir ./db/migrations sqlite3 ./sqlite.db $$action
goose -dir $(DB_DIR) sqlite3 $(DB_FILE) $$action

migrate:
@goose -dir ./db/migrations sqlite3 ./sqlite.db up

watch:
@echo "Starting the watch script..."
./watch.sh
@goose -dir $(DB_DIR) sqlite3 $(DB_FILE) up
45 changes: 25 additions & 20 deletions ui/view/tap.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,22 +52,27 @@ func (t *TapModel) Init() tea.Cmd {

// Update updates the tap model
func (t *TapModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
t.lastOrderID = msg.(TapMessage).lastOrderID

for _, msg := range msg.(TapMessage).items {
switch msg.category {
case "Mate":
t.mate += msg.amount
case "Soft":
t.soft += msg.amount
case "Beer":
t.beer += msg.amount
case "Food":
t.food += msg.amount
switch msg := msg.(type) {
case TapMessage:
t.lastOrderID = msg.lastOrderID

for _, msg := range msg.items {
switch msg.category {
case "Mate":
t.mate += msg.amount
case "Soft":
t.soft += msg.amount
case "Beer":
t.beer += msg.amount
case "Food":
t.food += msg.amount
}
}

return t, updateOrders(t.db, t.lastOrderID)
}

return t, updateOrders(t.db, t.lastOrderID)
return t, nil
}

// View returns the tap view
Expand All @@ -85,23 +90,23 @@ func (t *TapModel) View() string {
barSoft := barchart.BarData{
Label: "Soft",
Values: []barchart.BarValue{{
Name: "Item1",
Name: "Soft",
Value: t.soft,
Style: lipgloss.NewStyle().Foreground(tapCategoryColor["Soft"]),
}},
}
barBeer := barchart.BarData{
Label: "Beer",
Values: []barchart.BarValue{{
Name: "Item1",
Name: "Beer",
Value: t.beer,
Style: lipgloss.NewStyle().Foreground(tapCategoryColor["Beer"]),
}},
}
barFood := barchart.BarData{
Label: "Food",
Values: []barchart.BarValue{{
Name: "Item1",
Name: "Food",
Value: t.food,
Style: lipgloss.NewStyle().Foreground(tapCategoryColor["Food"]),
}},
Expand All @@ -114,21 +119,21 @@ func (t *TapModel) View() string {
}

func updateOrders(db *db.DB, lastOrderID int64) tea.Cmd {
return tea.Tick(time.Second, func(_ time.Time) tea.Msg {
return tea.Tick(60*time.Second, func(_ time.Time) tea.Msg {
order, err := db.Queries.GetLastOrderByOrderID(context.Background())
if err != nil {
zap.S().Error("DB: Failed to get last order", err)
return nil
return TapMessage{lastOrderID: lastOrderID, items: nil}
}

if order.OrderID <= lastOrderID {
return nil
return TapMessage{lastOrderID: lastOrderID, items: nil}
}

orders, err := db.Queries.GetOrderCountByCategorySinceOrderID(context.Background(), lastOrderID)
if err != nil {
zap.S().Error("DB: Failed to get tap orders", err)
return nil
return TapMessage{lastOrderID: lastOrderID, items: nil}
}

mate, soft, beer, food := 0.0, 0.0, 0.0, 0.0
Expand Down
45 changes: 0 additions & 45 deletions watch.sh

This file was deleted.

0 comments on commit e649034

Please sign in to comment.