Skip to content

Commit

Permalink
feat: tap integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Topvennie committed Nov 14, 2024
1 parent cc1cbe1 commit 893f617
Show file tree
Hide file tree
Showing 10 changed files with 531 additions and 6 deletions.
16 changes: 16 additions & 0 deletions config/development.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,22 @@ port = 3000
client_id = "your_client_id"
client_secret = "your_client_secret"

[tap]
interval_s = 60
beers = [
"Schelfaut",
"Duvel",
"Fourchette",
"Jupiler",
"Karmeliet",
"Kriek",
"Chouffe",
"Maes",
"Somersby",
"Sportzot",
"Stella",
]

[buzzer]
song = [
"-n", "-f880", "-l100", "-d0",
Expand Down
16 changes: 16 additions & 0 deletions db/migrations/20241114152122_add_tap_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- +goose Up
-- +goose StatementBegin
CREATE TABLE IF NOT EXISTS tap (
id INTEGER PRIMARY KEY,
order_id INTEGER NOT NULL,
order_created_at TIMESTAMP NOT NULL,
name TEXT NOT NULL,
category TEXT NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
)
-- +goose StatementEnd

-- +goose Down
-- +goose StatementBegin
DROP TABLE IF EXISTS tap;
-- +goose StatementEnd
43 changes: 43 additions & 0 deletions db/queries/tap.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
-- CRUD

-- name: GetAllTaps :many
SELECT *
FROM tap;

-- name: GetTapByID :one
SELECT *
FROM tap
WHERE id = ?;

-- name: CreateTap :one
INSERT INTO tap (order_id, order_created_at, name, category)
VALUES (?, ?, ?, ?)
RETURNING *;

-- name: UpdateTap :one
UPDATE tap
SET order_id = ?, order_created_at = ?, name = ?, category = ?
WHERE id = ?
RETURNING *;

-- name: DeleteTap :execrows
DELETE FROM tap
WHERE id = ?;

-- Other

-- name: GetTapByOrderID :one
SELECT *
FROM tap
WHERE order_id = ?;

-- name: GetTapByCategory :many
SELECT *
FROM tap
WHERE category = ?;

-- name: GetLastOrder :one
SELECT *
FROM tap
ORDER BY id DESC
LIMIT 1;
16 changes: 16 additions & 0 deletions internal/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,49 @@ package cmd
import (
"github.com/zeusWPI/scc/internal/pkg/db"
"github.com/zeusWPI/scc/internal/pkg/spotify"
"github.com/zeusWPI/scc/internal/pkg/tap"
"github.com/zeusWPI/scc/pkg/config"
"github.com/zeusWPI/scc/pkg/logger"
"go.uber.org/zap"
)

// Execute starts the entire application
func Execute() {
zap.S().Info("Initializing application")

// Config
err := config.Init()
if err != nil {
panic(err)
}

// Logger
zapLogger, err := logger.New()
if err != nil {
panic(err)
}
zap.ReplaceGlobals(zapLogger)

// Database
db, err := db.New()
if err != nil {
zap.S().Fatal("DB: Fatal error\n", err)
}

// Spotify
spotify, err := spotify.New(db)
if err != nil {
zap.S().Error("Spotify: Initiating error, integration will not work.\n", err)
}

// Tap
tap := tap.New(db)

// Everything that needs to be initialized is done
// Time to start all parts of the application
zap.S().Info("Starting application")

_ = tapCmd(tap)

apiCmd(db, spotify)
}
41 changes: 41 additions & 0 deletions internal/cmd/tap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package cmd

import (
"time"

"github.com/zeusWPI/scc/internal/pkg/tap"
"github.com/zeusWPI/scc/pkg/config"
"go.uber.org/zap"
)

func tapCmd(tap *tap.Tap) chan bool {
done := make(chan bool)

go tapPeriodicUpdate(tap, done)

return done
}

func tapPeriodicUpdate(tap *tap.Tap, done chan bool) {
interval := config.GetDefaultInt("tap.interval_s", 60)
zap.S().Info("Tap: Starting periodic update with an interval of ", interval, " seconds")

ticker := time.NewTicker(time.Duration(interval) * time.Second)
defer ticker.Stop()

for {
select {
case <-done:
zap.S().Info("Tap: Stopping periodic update")
return
case <-ticker.C:
// Update tap
zap.S().Info("Tap: Updating tap")
err := tap.Update()
if err != nil {
zap.S().Error("Tap: Error updating tap\n", err)
}
}
}

}
9 changes: 9 additions & 0 deletions internal/pkg/db/sqlc/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 893f617

Please sign in to comment.