-
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
10 changed files
with
1,549 additions
and
0 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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,23 @@ | ||
{ | ||
"name": "jukebig", | ||
"version": "1.0.0", | ||
"description": "Introducing Jukebox, the successor to Jukebox Mini! We've secured some initial tracks for our digital music platform, so now users can add tracks to their playlists.", | ||
"main": "index.js", | ||
"directories": { | ||
"doc": "docs" | ||
}, | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"seed": "node prisma/seed.js", | ||
"dev": "nodemon server.js" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"@prisma/client": "^6.2.1", | ||
"express": "^4.21.2", | ||
"nodemon": "^3.1.9", | ||
"prisma": "^6.2.1" | ||
} | ||
} |
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,45 @@ | ||
-- CreateTable | ||
CREATE TABLE "User" ( | ||
"id" SERIAL NOT NULL, | ||
"username" TEXT NOT NULL, | ||
|
||
CONSTRAINT "User_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateTable | ||
CREATE TABLE "Playlist" ( | ||
"id" SERIAL NOT NULL, | ||
"name" TEXT NOT NULL, | ||
"description" TEXT NOT NULL, | ||
"ownerId" INTEGER NOT NULL, | ||
|
||
CONSTRAINT "Playlist_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateTable | ||
CREATE TABLE "Track" ( | ||
"id" SERIAL NOT NULL, | ||
"name" TEXT NOT NULL, | ||
|
||
CONSTRAINT "Track_pkey" PRIMARY KEY ("id") | ||
); | ||
|
||
-- CreateTable | ||
CREATE TABLE "_PlaylistTracks" ( | ||
"A" INTEGER NOT NULL, | ||
"B" INTEGER NOT NULL, | ||
|
||
CONSTRAINT "_PlaylistTracks_AB_pkey" PRIMARY KEY ("A","B") | ||
); | ||
|
||
-- CreateIndex | ||
CREATE INDEX "_PlaylistTracks_B_index" ON "_PlaylistTracks"("B"); | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "Playlist" ADD CONSTRAINT "Playlist_ownerId_fkey" FOREIGN KEY ("ownerId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "_PlaylistTracks" ADD CONSTRAINT "_PlaylistTracks_A_fkey" FOREIGN KEY ("A") REFERENCES "Playlist"("id") ON DELETE CASCADE ON UPDATE CASCADE; | ||
|
||
-- AddForeignKey | ||
ALTER TABLE "_PlaylistTracks" ADD CONSTRAINT "_PlaylistTracks_B_fkey" FOREIGN KEY ("B") REFERENCES "Track"("id") ON DELETE CASCADE ON UPDATE CASCADE; |
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,3 @@ | ||
# Please do not edit this file manually | ||
# It should be added in your version-control system (e.g., Git) | ||
provider = "postgresql" |
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 @@ | ||
// This is your Prisma schema file, | ||
// learn more about it in the docs: https://pris.ly/d/prisma-schema | ||
|
||
// Looking for ways to speed up your queries, or scale easily with your serverless or edge functions? | ||
// Try Prisma Accelerate: https://pris.ly/cli/accelerate-init | ||
|
||
generator client { | ||
provider = "prisma-client-js" | ||
} | ||
|
||
datasource db { | ||
provider = "postgresql" | ||
url = env("DATABASE_URL") | ||
} | ||
|
||
model User { | ||
id Int @id @default(autoincrement()) | ||
username String | ||
playlists Playlist[] | ||
} | ||
|
||
model Playlist { | ||
id Int @id @default(autoincrement()) | ||
name String | ||
description String | ||
owner User @relation(fields: [ownerId], references: [id]) | ||
ownerId Int | ||
tracks Track[] @relation("PlaylistTracks") | ||
} | ||
|
||
model Track { | ||
id Int @id @default(autoincrement()) | ||
name String | ||
playlists Playlist[] @relation("PlaylistTracks") | ||
} |
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 @@ | ||
const {PrismaClient} = require('@prisma/client'); | ||
const prisma = new PrismaClient(); | ||
|
||
|
||
const seed = async() => { | ||
try{ | ||
const userCount = 5; | ||
const users = []; | ||
for(let i =0; i < userCount; i++){ | ||
const user = await prisma.user.create({ | ||
data: { | ||
username: `user${i+1}`, | ||
}, | ||
}); | ||
users.push(user); | ||
} | ||
const trackCount = 20; | ||
const tracks = []; | ||
for ( let i = 0; i < trackCount; i++ ) { | ||
const track = await prisma.track.create({ | ||
data: { | ||
name: `track${i+1}`, | ||
}, | ||
}); | ||
tracks.push(track); | ||
} | ||
const playlistCount = 10; | ||
for ( let i = 0; i < playlistCount; i++ ) { | ||
const randomUser = users[Math.floor(Math.random() * users.length)] | ||
const trackSet = tracks | ||
.sort(() => 0.5 - Math.random()) | ||
.slice(0, Math.floor(Math.random() * 5) + 1); | ||
await prisma.playlist.create({ | ||
data: { | ||
name: `playlist${i+1}`, | ||
description: `Description for playlist${i+1}`, | ||
owner: { | ||
connect: {id: randomUser.id}, | ||
}, | ||
tracks: { | ||
connect: trackSet.map((track) => ({ id: track.id})), | ||
}, | ||
}, | ||
}); | ||
} | ||
} catch (error) { | ||
console.error(error) | ||
} finally { | ||
await prisma.$disconnect(); | ||
} | ||
} | ||
|
||
|
||
|
||
|
||
seed(); |
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,47 @@ | ||
const express = require('express'); | ||
const { PrismaClient } = require('@prisma/client'); | ||
const prisma = new PrismaClient(); | ||
const router = express.Router(); | ||
|
||
router.get('/', async (req, res, next) => { | ||
try{ | ||
const playlists = await prisma.playlist.findMany({ | ||
include: { owner: true, tracks: true}, | ||
}); | ||
res.json(playlists); | ||
} catch (error) { | ||
next(error) | ||
} | ||
}); | ||
|
||
router.post('/', async (req,res,next) => { | ||
try{ | ||
const { name, description, ownerId, trackIds} = req.body; | ||
const playlist = await prisma.playlist.create({ | ||
data: { | ||
name, | ||
description, | ||
owner: { connect: {id: ownerId} }, | ||
tracks: { connect: trackIds.map((id) => ({id}))}, | ||
}, | ||
}); | ||
res.status(201).json(playlist); | ||
}catch (error) { | ||
next(error) | ||
} | ||
}); | ||
|
||
router.get('/:id', async (req, res, next) => { | ||
try{ | ||
const playlist = await prisma.playlist.findUnique({ | ||
where: { id: parseInt(req.params.id)}, | ||
include: {tracks: true, owner: true}, | ||
}); | ||
if (!playlist) return res.status(404).json({error: 'Playlist not found?'}) | ||
res.json(playlist); | ||
} catch (error) { | ||
next(error) | ||
} | ||
}); | ||
|
||
module.exports = router; |
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,27 @@ | ||
const express = require('express'); | ||
const { PrismaClient } = require('@prisma/client'); | ||
const prisma = new PrismaClient(); | ||
const router = express.Router(); | ||
|
||
router.get('/', async (req, res, next) => { | ||
try{ | ||
const tracks = await prisma.track.findMany(); | ||
res.json(tracks); | ||
} catch (error) { | ||
next(error) | ||
} | ||
}); | ||
|
||
router.get('/:id', async (req, res, next) => { | ||
try{ | ||
const track = await prisma.track.findUnique({ | ||
where: { id: parseInt(req.params.id)}, | ||
}); | ||
if (!track) return res.status(404).json({ error: 'Track not found'}) | ||
res.json(track); | ||
} catch (error) { | ||
next(error) | ||
} | ||
}); | ||
|
||
module.exports= router; |
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,29 @@ | ||
const express = require('express'); | ||
const { PrismaClient } = require('@prisma/client'); | ||
const prisma = new PrismaClient(); | ||
const router = express.Router(); | ||
|
||
router.get('/', async (req, res, next) => { | ||
try { | ||
const users = await prisma.user.findMany(); | ||
console.log(users) | ||
res.json(users); | ||
} catch (error) { | ||
next(error); | ||
} | ||
}); | ||
|
||
router.get('/:id', async (req, res, next) => { | ||
try{ | ||
const user = await prisma.user.findUnique({ | ||
where: { id: parseInt(req.params.id)}, | ||
include: {playlists: true}, | ||
}); | ||
if (!user) return res.status(404).json({ error: 'user not found'}) | ||
res.json(user); | ||
} catch (error){ | ||
next(error); | ||
} | ||
}); | ||
|
||
module.exports = router; |
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,23 @@ | ||
const express = require('express'); | ||
const usersRouter = require('./routes/users'); | ||
const playlistsRouter = require('./routes/playlists'); | ||
const tracksRouter = require('./routes/tracks'); | ||
const PORT = 3900; | ||
|
||
const server = express(); | ||
server.listen(PORT, () => { | ||
console.log(`Listening on ${PORT}`) | ||
}); | ||
server.use(express.json()); | ||
|
||
server.use('/users', usersRouter ) | ||
server.use('/playlists',playlistsRouter ) | ||
server.use('/tracks', tracksRouter ) | ||
|
||
server.get('/', (req, res) => { | ||
res.send('Welcome to the JukeBox API!'); | ||
}); | ||
|
||
|
||
module.exports = server; | ||
|