Skip to content

Commit

Permalink
Add playlist service support routes and DB tables.
Browse files Browse the repository at this point in the history
  • Loading branch information
feserr committed Oct 4, 2023
1 parent 916952e commit 3e3f65b
Show file tree
Hide file tree
Showing 21 changed files with 835 additions and 145 deletions.
3 changes: 2 additions & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
ORIGINS=
ORIGINS=
MAX_SONGS=
13 changes: 11 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,21 @@ module.exports = {
},
extends: 'airbnb-base',
overrides: [
{
env: {
node: true,
},
files: [
'.eslintrc.{js,cjs}',
],
parserOptions: {
sourceType: 'script',
},
},
],
parserOptions: {
ecmaVersion: 'latest',
},
rules: {
'consistent-return': 'off',
'import/no-extraneous-dependencies': 'off',
},
};
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ haters
.env*.*
.vscode
.cache
seedData.js
seedDataDev.js

# package managers
npm-debug.log*
Expand All @@ -29,4 +29,4 @@ yarn-error.log*
*.sqlite

# IDEs
.vscode/
.vscode/
12 changes: 12 additions & 0 deletions db/Like.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const { Sequelize, sequelize } = require('./db');

const Like = sequelize.define('Like', {
id: {
type: Sequelize.UUID,
defaultValue: Sequelize.UUIDV4,
allowNull: false,
primaryKey: true,
},
});

module.exports = { Like };
32 changes: 32 additions & 0 deletions db/Track.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const { Sequelize, sequelize } = require('./db');

const Track = sequelize.define('track', {
id: {
type: Sequelize.UUID,
defaultValue: Sequelize.UUIDV4,
allowNull: false,
primaryKey: true,
},
spotifyTrackId: {
type: Sequelize.STRING,
allowNull: false,
},
uri: {
type: Sequelize.STRING,
allowNull: false,
},
name: {
type: Sequelize.STRING,
allowNull: false,
},
album: {
type: Sequelize.STRING,
allowNull: false,
},
artist: {
type: Sequelize.STRING,
allowNull: false,
},
});

module.exports = { Track };
16 changes: 16 additions & 0 deletions db/User.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const { Sequelize, sequelize } = require('./db');

const User = sequelize.define('user', {
id: {
type: Sequelize.UUID,
defaultValue: Sequelize.UUIDV4,
allowNull: false,
primaryKey: true,
},
spotifyUserId: {
type: Sequelize.STRING,
allowNull: false,
},
});

module.exports = { User };
15 changes: 15 additions & 0 deletions db/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,23 @@ require('../src/utils/logger');

const { Bank } = require('./Bank');
const { Key } = require('./Key');
const { Track } = require('./Track');
const { User } = require('./User');
const { Like } = require('./Like');

User.hasMany(Track, { foreignKey: 'userId' });
Track.belongsTo(User, { foreignKey: 'userId', onDelete: 'CASCADE' });

User.hasMany(Like, { foreignKey: 'userId' });
Like.belongsTo(User, { foreignKey: 'userId', onDelete: 'CASCADE' });

Track.hasMany(Like, { foreignKey: 'trackId' });
Like.belongsTo(Track, { foreignKey: 'trackId', onDelete: 'CASCADE' });

module.exports = {
Bank,
Key,
Track,
User,
Like,
};
17 changes: 17 additions & 0 deletions db/seedData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const banks = [];

const keys = [];

const tracks = [];

const users = [];

const likes = [];

module.exports = {
banks,
keys,
tracks,
users,
likes,
};
38 changes: 28 additions & 10 deletions db/seedFn.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,41 @@
const { sequelize } = require('./db');
const { Bank, Key } = require('.');
const { banks, keys } = require('./seedData');

const {
Bank, Key, Track, User, Like,
} = require('.');

const {
banks, keys, tracks, users, likes,
} = require('./seedData');

const seed = async () => {
try {
await sequelize.sync({ force: true }); // recreate db

await Promise.all(banks.map(async (bank) => {
await Bank.create({
holder: bank.holder,
account: bank.account,
country: bank.country,
});
await Bank.create(bank);
}));

await Promise.all(keys.map(async (key) => {
await Key.create({
key: key.key,
});
await Key.create(key);
}));

const createdUsers = [];
await Promise.all(users.map(async (user) => {
createdUsers.push(await User.create(user));
}));

const createdTracks = [];
await Promise.all(tracks.map(async (track) => {
const createdTrack = await Track.create(track);
await createdUsers[0].addTrack(createdTrack);
createdTracks.push(createdTrack);
}));

await Promise.all(likes.map(async (like) => {
const createdLike = await Like.create(like);
await createdUsers[0].addLike(createdLike);
await createdTracks[0].addLike(createdLike);
}));
} catch (error) {
global.logger.error(error);
Expand Down
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@ app.use(express.json());
app.use(express.urlencoded({ extended: true }));

app.use('/api/bank', require('./src/routes/bankRoutes'));
app.use('/api/track', require('./src/routes/trackRoutes'));
app.use('/api/like', require('./src/routes/likeRoutes'));
app.use('/api/user', require('./src/routes/userRoutes'));

module.exports = app;
Loading

0 comments on commit 3e3f65b

Please sign in to comment.