-
Notifications
You must be signed in to change notification settings - Fork 2
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
1 parent
af554d0
commit 09d5dc3
Showing
2 changed files
with
64 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
'use strict'; | ||
|
||
const express = require('express'); | ||
const router = express.Router(); | ||
|
||
const ev = require('express-validation'); | ||
// const validations = require('../validations/playlists_tracks'); | ||
const knex = require('../knex'); | ||
|
||
router.get('/playlists_tracks', (req, res, next) => { | ||
knex('playlists_tracks') | ||
.orderBy('id') | ||
.then((playlists_tracks) => { | ||
res.send(playlists_tracks); | ||
}) | ||
.catch((err) => { | ||
next(err); | ||
}); | ||
}); | ||
|
||
router.post('/playlists_tracks', (req, res, next) => { | ||
const track_id = Number.parseInt(req.body.track_id); | ||
const playlist_id = Number.parseInt(req.body.playlist_id); | ||
|
||
knex('tracks') | ||
.where('id', track_id) | ||
.first() | ||
.then((track) => { | ||
if (!track) { | ||
const err = new Error('Invalid track id.'); | ||
err.status = 400; | ||
|
||
throw err; | ||
} | ||
|
||
return knex('playlists') | ||
.where('id', playlist_id) | ||
.first() | ||
.then((playlist) => { | ||
if (!playlist) { | ||
const err = new Error('Invalid playlist id.') | ||
err.status = 400; | ||
|
||
throw err; | ||
} | ||
}); | ||
}) | ||
.then(() => { | ||
return knex('playlists_tracks') | ||
.insert(req.body, '*') | ||
.then((playlists_tracks) => { | ||
res.send(playlists_tracks[0]); | ||
}) | ||
.catch((err) => { | ||
throw err; | ||
}); | ||
}) | ||
.catch((err) => { | ||
next(err); | ||
}); | ||
}); | ||
|
||
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