-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
511 additions
and
422 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = ?; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.