-
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
15 changed files
with
533 additions
and
10 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,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 |
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: 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 = ?; |
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
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 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) | ||
} |
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
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,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, | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.