Skip to content

Commit

Permalink
feat(song): history
Browse files Browse the repository at this point in the history
  • Loading branch information
Topvennie committed Nov 27, 2024
1 parent 119420f commit f54fa51
Show file tree
Hide file tree
Showing 21 changed files with 511 additions and 422 deletions.
8 changes: 5 additions & 3 deletions config/development.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
host = "localhost"
port = 3000

[spotify]
client_id = "your_client_id"
client_secret = "your_client_secret"
[song]
spotify_client_id = "your_client_id"
spotify_client_secret = "your_client_secret"
spotify_api = "https://api.spotify.com/v1"
spotify_account = "https://accounts.spotify.com/api/token"

[tap]
api = "https://tap.zeus.gent"
Expand Down
24 changes: 24 additions & 0 deletions db/migrations/20241127162048_add_song_history_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
-- +goose Up
-- +goose StatementBegin
ALTER TABLE spotify
DROP COLUMN created_at;

ALTER TABLE spotify RENAME TO song;

CREATE TABLE song_history (
id INTEGER PRIMARY KEY,
song_id INTEGER NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY(song_id) REFERENCES song(id)
);
-- +goose StatementEnd

-- +goose Down
-- +goose StatementBegin
DROP TABLE IF EXISTS song_history;

ALTER TABLE song RENAME TO spotify;

ALTER TABLE spotify
ADD COLUMN created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP;
-- +goose StatementEnd
33 changes: 33 additions & 0 deletions db/queries/song.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
-- CRUD

-- name: GetAllSongs :many
SELECT *
FROM song;

-- name: GetSongByID :one
SELECT *
FROM song
WHERE id = ?;

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

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

-- name: DeleteSong :execrows
DELETE FROM song
WHERE id = ?;


-- Other

-- name: GetSongBySpotifyID :one
SELECT *
FROM song
WHERE spotify_id = ?;
16 changes: 16 additions & 0 deletions db/queries/song_history.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- CRUD

-- name: CreateSongHistory :one
INSERT INTO song_history (song_id)
VALUES (?)
RETURNING *;


-- Other


-- name: GetLastSongHistory :one
SELECT *
FROM song_history
ORDER BY created_at DESC
LIMIT 1;
33 changes: 0 additions & 33 deletions db/queries/spotify.sql

This file was deleted.

8 changes: 4 additions & 4 deletions internal/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +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"
apiSong "github.com/zeusWPI/scc/internal/api/song"
"github.com/zeusWPI/scc/internal/pkg/db"
"github.com/zeusWPI/scc/internal/pkg/spotify"
"github.com/zeusWPI/scc/internal/pkg/song"
)

// New creates a new API instance
func New(router fiber.Router, db *db.DB, spotify *spotify.Spotify) {
func New(router fiber.Router, db *db.DB, song *song.Song) {
message.New(router, db)
apiSpotify.New(router, db, spotify)
apiSong.New(router, db, song)
}
56 changes: 56 additions & 0 deletions internal/api/song/song.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Package song provides the API regarding songs integration
package song

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/song"
"go.uber.org/zap"
)

// Router is the song API router
type Router struct {
router fiber.Router
db *db.DB
song *song.Song
}

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

return api
}

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

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

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

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

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

return c.SendStatus(fiber.StatusOK)
}
56 changes: 0 additions & 56 deletions internal/api/spotify/spotify.go

This file was deleted.

6 changes: 3 additions & 3 deletions internal/cmd/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ 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/internal/pkg/song"
"github.com/zeusWPI/scc/pkg/config"
"go.uber.org/zap"
)

// API starts the API server
func API(db *db.DB, spotify *spotify.Spotify) {
func API(db *db.DB, song *song.Song) {
app := fiber.New(fiber.Config{
BodyLimit: 1024 * 1024 * 1024,
})
Expand All @@ -29,7 +29,7 @@ func API(db *db.DB, spotify *spotify.Spotify) {
)

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

host := config.GetDefaultString("server.host", "127.0.0.1")
port := config.GetDefaultInt("server.port", 3000)
Expand Down
13 changes: 13 additions & 0 deletions internal/cmd/song.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package cmd

import (
"github.com/zeusWPI/scc/internal/pkg/db"
"github.com/zeusWPI/scc/internal/pkg/song"
)

// Song starts the Song integration
func Song(db *db.DB) (*song.Song, error) {
song, err := song.New(db)

return song, err
}
13 changes: 0 additions & 13 deletions internal/cmd/spotify.go

This file was deleted.

35 changes: 35 additions & 0 deletions internal/pkg/db/dto/song.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package dto

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

// Song is the DTO for the song
type Song 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"`
}

// SongDTO converts a sqlc.Song to a Song
func SongDTO(song sqlc.Song) *Song {
return &Song{
ID: song.ID,
Title: song.Title,
Artists: song.Artists,
SpotifyID: song.SpotifyID,
DurationMS: song.DurationMs,
}
}

// CreateParams converts a Song to sqlc.CreateSongParams
func (s *Song) CreateParams() sqlc.CreateSongParams {
return sqlc.CreateSongParams{
Title: s.Title,
Artists: s.Artists,
SpotifyID: s.SpotifyID,
DurationMs: s.DurationMS,
}
}
39 changes: 0 additions & 39 deletions internal/pkg/db/dto/spotify.go

This file was deleted.

Loading

0 comments on commit f54fa51

Please sign in to comment.