Skip to content

Commit

Permalink
feat: add spotify integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Topvennie committed Nov 14, 2024
1 parent 715ae2a commit cc1cbe1
Show file tree
Hide file tree
Showing 15 changed files with 533 additions and 10 deletions.
4 changes: 4 additions & 0 deletions config/development.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
host = "localhost"
port = 3000

[spotify]
client_id = "your_client_id"
client_secret = "your_client_secret"

[buzzer]
song = [
"-n", "-f880", "-l100", "-d0",
Expand Down
16 changes: 16 additions & 0 deletions db/migrations/20241114135818_add_spotify_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 spotify (
id INTEGER PRIMARY KEY,
title TEXT NOT NULL,
artists TEXT NOT NULL,
spotify_id TEXT NOT NULL,
duration_ms INTEGER NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
-- +goose StatementEnd

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

-- name: GetAllSpotify :many
SELECT *
FROM spotify;

-- name: GetSpotifyByID :one
SELECT *
FROM spotify
WHERE id = ?;

-- name: CreateSpotify :one
INSERT INTO spotify (title, artists, spotify_id, duration_ms)
VALUES (?, ?, ?, ?)
RETURNING *;

-- name: UpdateSpotify :one
UPDATE spotify
SET title = ?, artists = ?, spotify_id = ?, duration_ms = ?
WHERE id = ?
RETURNING *;

-- name: DeleteSpotify :execrows
DELETE FROM spotify
WHERE id = ?;


-- Other

-- name: GetSpotifyBySpotifyID :one
SELECT *
FROM spotify
WHERE spotify_id = ?;
5 changes: 4 additions & 1 deletion internal/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ package api
import (
"github.com/gofiber/fiber/v2"
"github.com/zeusWPI/scc/internal/api/message"
apiSpotify "github.com/zeusWPI/scc/internal/api/spotify"
"github.com/zeusWPI/scc/internal/pkg/db"
"github.com/zeusWPI/scc/internal/pkg/spotify"
)

// New creates a new API instance
func New(router fiber.Router, db *db.DB) {
func New(router fiber.Router, db *db.DB, spotify *spotify.Spotify) {
message.New(router, db)
apiSpotify.New(router, db, spotify)
}
10 changes: 5 additions & 5 deletions internal/api/message/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (r *Router) createRoutes() {
func (r *Router) getAll(c *fiber.Ctx) error {
messages, err := r.db.Queries.GetAllMessages(c.Context())
if err != nil {
zap.S().Error("DB: Get all messages", err)
zap.S().Error("DB: Get all messages\n", err)
return c.SendStatus(fiber.StatusInternalServerError)
}

Expand All @@ -45,20 +45,20 @@ func (r *Router) create(c *fiber.Ctx) error {
message := new(dto.Message)

if err := c.BodyParser(message); err != nil {
zap.S().Error("Body parser", err)
zap.S().Error("API: Message body parser\n", err)
return c.SendStatus(fiber.StatusBadRequest)
}

if err := dto.Validate.Struct(message); err != nil {
zap.S().Error("Validation", err)
zap.S().Error("API: Message validation\n", err)
return c.SendStatus(fiber.StatusBadRequest)
}

messageDB, err := r.db.Queries.CreateMessage(c.Context(), message.CreateParams())
if err != nil {
zap.S().Error("DB: Create message", err)
zap.S().Error("DB: Create message\n", err)
return c.SendStatus(fiber.StatusInternalServerError)
}

return c.Status(fiber.StatusOK).JSON(dto.MessageDTO(messageDB))
return c.Status(fiber.StatusCreated).JSON(dto.MessageDTO(messageDB))
}
56 changes: 56 additions & 0 deletions internal/api/spotify/spotify.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Package spotify provides the API regarding spotify integration
package spotify

import (
"github.com/gofiber/fiber/v2"
"github.com/zeusWPI/scc/internal/pkg/db"
"github.com/zeusWPI/scc/internal/pkg/db/dto"
"github.com/zeusWPI/scc/internal/pkg/spotify"
"go.uber.org/zap"
)

// Router is the spotify API router
type Router struct {
router fiber.Router
db *db.DB
spotify *spotify.Spotify
}

// New creates a new spotify API instance
func New(router fiber.Router, db *db.DB, spotify *spotify.Spotify) *Router {
api := &Router{
router: router.Group("/spotify"),
db: db,
spotify: spotify,
}
api.createRoutes()

return api
}

func (r *Router) createRoutes() {
r.router.Post("/", r.new)
}

func (r *Router) new(c *fiber.Ctx) error {
spotify := new(dto.Spotify)

if err := c.BodyParser(spotify); err != nil {
zap.S().Error("API: Spotify body parser\n", err)
return c.SendStatus(fiber.StatusBadRequest)
}

if err := dto.Validate.Struct(spotify); err != nil {
zap.S().Error("API: Spotify validation\n", err)
return c.SendStatus(fiber.StatusBadRequest)
}

go func() {
err := r.spotify.Track(spotify)
if err != nil {
zap.S().Error("Spotify: Get Track\n", err)
}
}()

return c.SendStatus(fiber.StatusOK)
}
5 changes: 3 additions & 2 deletions internal/cmd/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import (
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/zeusWPI/scc/internal/api"
"github.com/zeusWPI/scc/internal/pkg/db"
"github.com/zeusWPI/scc/internal/pkg/spotify"
"github.com/zeusWPI/scc/pkg/config"
"go.uber.org/zap"
)

func apiCmd(db *db.DB) {
func apiCmd(db *db.DB, spotify *spotify.Spotify) {
app := fiber.New(fiber.Config{
BodyLimit: 1024 * 1024 * 1024,
})
Expand All @@ -27,7 +28,7 @@ func apiCmd(db *db.DB) {
)

apiGroup := app.Group("/api")
api.New(apiGroup, db)
api.New(apiGroup, db, spotify)

host := config.GetDefaultString("server.host", "127.0.0.1")
port := config.GetDefaultInt("server.port", 3000)
Expand Down
10 changes: 8 additions & 2 deletions internal/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd

import (
"github.com/zeusWPI/scc/internal/pkg/db"
"github.com/zeusWPI/scc/internal/pkg/spotify"
"github.com/zeusWPI/scc/pkg/config"
"github.com/zeusWPI/scc/pkg/logger"
"go.uber.org/zap"
Expand All @@ -23,8 +24,13 @@ func Execute() {

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

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

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

import (
"time"

"github.com/zeusWPI/scc/internal/pkg/db/sqlc"
)

// Spotify is the DTO for the spotify
type Spotify struct {
ID int64 `json:"id"`
Title string `json:"title"`
Artists string `json:"artists"`
SpotifyID string `json:"spotify_id" validate:"required"`
DurationMS int64 `json:"duration_ms"`
CreatedAt time.Time `json:"created_at"`
}

// SpotifyDTO converts a sqlc.Spotify to a Spotify
func SpotifyDTO(spotify sqlc.Spotify) *Spotify {
return &Spotify{
ID: spotify.ID,
Title: spotify.Title,
Artists: spotify.Artists,
SpotifyID: spotify.SpotifyID,
DurationMS: spotify.DurationMs,
CreatedAt: spotify.CreatedAt,
}
}

// CreateParams converts a Spotify to sqlc.CreateSpotifyParams
func (s *Spotify) CreateParams() sqlc.CreateSpotifyParams {
return sqlc.CreateSpotifyParams{
Title: s.Title,
Artists: s.Artists,
SpotifyID: s.SpotifyID,
DurationMs: s.DurationMS,
}
}
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 cc1cbe1

Please sign in to comment.