Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Landry irakoze saved strains #8

Merged
merged 4 commits into from
Sep 26, 2019
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
4 changes: 2 additions & 2 deletions api/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const KnexSessionStore = require('connect-session-knex')(session);
const dbConnection = require('../database/dbConfig');
const authRouter = require('../auth/authRouter');
const searchRouter = require('../search/searchRouter');
const savedStrainsRouter = require('../saved-strains/savedStrainsRouter');
const usersRouter = require('../saved-strains/savedStrainsRouter');

const server = express();

Expand Down Expand Up @@ -36,6 +36,6 @@ server.use(session(sessionConfig));

server.use('/api/auth', authRouter);
server.use('/api/search', searchRouter);
server.use('api/user/strains', savedStrainsRouter);
server.use('/api/users', usersRouter);

module.exports = server;
Binary file modified database/med_cabinet.db
Binary file not shown.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const server = require('./api/server');

const PORT = process.env.PORT || 8000;
const PORT = process.env.PORT || 3333;

server.listen(PORT, () => {
console.log(`\n** Server listening on port ${PORT}**\n`)
Expand Down
Empty file.
59 changes: 59 additions & 0 deletions saved-strains/savedStrainsModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const db = require('../database/dbConfig');

module.exports = {
find,
findRow,
findBy,
findByStrainId,
add,
remove
}
function find(user_Id) {
return (
db('user_strains')
.where({ user_Id })
.join('strains', 'user_strains.strain_id', 'strains.id')
.select('strain', 'type', 'rating', 'effects', 'flavor', 'description') // add all strain information
)
} //edit to include savedstrainID in response for frontend
function findBy(user_Id, filter) {
return (
db('user_strains')
.where({ user_Id })
.join('strains', 'user_strains.strain_id', 'strains.id')
.where(filter)
.select('strain')
)
} //fix
function findByStrainId(user_Id, Id) {
return (
db('user_strains')
.where({ user_Id, Id })
// .join('strains', 'user_strains.strain_id', 'strains.id')
// .select('strain')
)
} //fix
function findRow(row) {
return (
db('user_strains')
.where(row.user_Id)
.where(row.strain_Id)
)
}
function add(row) {
return (
db('user_strains')
.insert(row)
// .then(ids => {
// const [id] = ids;
// return findById(id)
// })
)
}
function remove(user_Id, Id) {
return (
db('user_strains')
.where({ user_Id, Id })
.del()
)
}
67 changes: 61 additions & 6 deletions saved-strains/savedStrainsRouter.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,71 @@
const router = require("express").Router();
const authMiddleware = require('../auth/authMiddleware');
const Strains = require('./savedStrainsModel');
//add middleware for nonexistent routes ie when id's strains don't exist or are empty etc
//add auth middleware
//mention discrepancy between strains table id and suggestions ID
//add documentation

// get all saved strains for a user route
router.get("/", authMiddleware, async (req, res) => {
// add a strain
router.post('/:id/strains', async (req, res) => {
try {
const { id } = req.params;
const { index } = req.body;
const savedStrainObj = { user_Id: id, strain_Id: index };
if (Strains.findRow(savedStrainObj)) { //fix this since it allows duplicates
const response = await Strains.add(savedStrainObj);
res.status(200).json(response);
}
else {
res.status(400).json({ message: 'Strain is already saved' });
}
} catch (err) {
console.log(err);
}
});

// save a strain route
router.post("/save", authMiddleware, async (req, res) => {
// get all saved strains for a user
router.get('/:id/strains', async (req, res) => {
try {
const { id } = req.params;
const response = await Strains.find(id);
res.status(200).json(response)
} catch {
res.status(400).json({ message: 'error retrieving saved strains' })
}
});

// unsave a strain route
router.post("/unsave", authMiddleware, async (req, res) => {
// get a particular saved strain by ID
router.get('/:id/strains/:strainId', async (req, res) => {
try {
const { id, strainId } = req.params;
const response = await Strains.findByStrainId(id, strainId);
res.status(200).json(response);
} catch {
res.status(400).json({ message: 'error retrieving saved strain' });
}
}); //fix

// // get a particular saved strain by Name
router.get('/:id/strains/:strainName', async (req, res) => {
try {
const { id, strainName } = req.params;
const response = await Strains.findBy(id, strainName);
res.status(200).json(response)
} catch {
res.status(400).json({ message: 'error retrieving saved strain' })
}
}); //fix

// remove a saved strain
router.delete("/:id/strains/:strainId", async (req, res) => {
try {
const { id, strainId } = req.params;
const response = await Strains.remove(id, strainId);
res.status(200).json(response)
} catch {
res.status(400).json({ message: 'error retrieving saved strain' })
}
});


Expand Down