Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 15 additions & 20 deletions database/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
require("dotenv").config();
const Sequelize = require("sequelize");
const PG_URL =
process.env.PG_URL ||
`postgres://${process.env.PG_USER}:${
process.env.PG_PASS
}@localhost:5432/test`;
const PG_URL = process.env.PG_URL || `postgres://${process.env.PG_USER}:${process.env.PG_PASS}@localhost:5432/test`;
const sequelize = new Sequelize(PG_URL);

sequelize
Expand Down Expand Up @@ -120,15 +116,7 @@ const addBookFromScrape = (bookObj, genre_id, name) => {
};

const addGenre = genre_id => {
// genreList.forEach(genre => {
// Genre.upsert({genre_id: genre})
// })
return Genre.upsert({ genre_id });
// return Promise.all([
// Genre.upsert({genre_id: genreList[0]}),
// Genre.upsert({genre_id: genreList[1]}),
// Genre.upsert({genre_id: genreList[2]})
// ]);
};

const addGenreToUser = async (genreList, id_token) => {
Expand Down Expand Up @@ -221,19 +209,25 @@ const getGenreByMedium = moviedb_id => {
.catch(console.log);
};

const getMediumByGenre = genre_id => {
return Genre.findOne({ where: { genre_id } }).then(genre => {
genre.getMedia({ limit: 3 }).then(results => {
console.log(`==========\nresults\n==========\n`, results);
});
});
const getMediumByGenre = async genre_id => {
let genre = await Genre.findOne({ where: { genre_id }})
return genre.getMedia({ limit: 3 })
// return Genre.findOne({ where: { genre_id } }).then(genre => {
// genre.getMedia({ limit: 3 }).then(results => {
// console.log(`==========\nresults\n==========\n`, results);
// });
// });
};

const getBooksByGenre = async genre_id => {
let genre = await Genre.findOne({ where: { genre_id } });
return genre.getBooks()
};

const getAllMediaByUser = async id_token => {
let user = await findOneUserByToken(id_token);
return user.getMedia();
}
// addMedium(myObj,'1')
// addGenre(
// [{genre_id:2000,
Expand Down Expand Up @@ -276,7 +270,8 @@ module.exports = {
getGenreByMedium,
getMediumByGenre,
addBookFromScrape,
getBooksByGenre
getBooksByGenre,
getAllMediaByUser
};

// User.sync({ force: true })
Expand Down
15 changes: 11 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,8 @@ app.post('/db/addMedium', (req, res) => {
db.addMedium(mediumObj, id_token)
.then(() => {
db.addGenreToMedium(genre_id, moviedb_id)
// res.sendStatus(200);
})
.then(results => {
console.log('Made it to then catch', results);
res.sendStatus(200)
})
.catch(err => {
Expand Down Expand Up @@ -96,8 +94,7 @@ app.post('/db/getTopGenres', (req, res) => {
});
res.send(body);
})
.catch(err => {
console.log('----------\nError getting Top Genres\n----------\n', err);
.catch(() => {
res.sendStatus(500);
})
});
Expand All @@ -124,6 +121,16 @@ app.get('/db/getBookRecsByGenre/:genre_id', async (req, res) => {
}
});

app.get('/db/getAllMedia/:id_token', async (req, res) => {
const { id_token } = req.params;
try {
const media = await db.getAllMediaByUser(id_token);
res.json(media);
} catch (err) {
res.sendStatus(500);
}
});

app.listen(PORT, () => {
console.log(`Listening on port ${PORT}`);
});