Skip to content

Commit

Permalink
Merge 07bc347 into 35aa182
Browse files Browse the repository at this point in the history
  • Loading branch information
mezlet committed Mar 23, 2019
2 parents 35aa182 + 07bc347 commit fe4cdd9
Show file tree
Hide file tree
Showing 14 changed files with 474 additions and 142 deletions.
27 changes: 15 additions & 12 deletions server/controllers/auth-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,17 @@ class UsersController {
'invalid token'
].includes(e.message)
) {
errorResponse(res, 'Invalid token, verification unsuccessful', 400);
} else {
errorResponse(
return errorResponse(
res,
'Something went wrong, verification unsuccessful',
500
'Invalid token, verification unsuccessful',
400
);
}
return errorResponse(
res,
'Something went wrong, verification unsuccessful',
500
);
}
}

Expand Down Expand Up @@ -158,7 +161,7 @@ class UsersController {
const token = generateToken({ id, username });
rows.dataValues.token = token;
delete rows.dataValues.hash;
successResponse(res, { user: rows.dataValues }, 200);
return successResponse(res, { user: rows.dataValues }, 200);
} catch (error) {
return errorResponse(res, error.message);
}
Expand Down Expand Up @@ -189,7 +192,7 @@ class UsersController {
resetPasswordLink: generateResetLink(token)
})
.then(() => {
successResponse(
return successResponse(
res,
{
message: 'Password reset link sent successfully'
Expand All @@ -198,7 +201,7 @@ class UsersController {
);
})
.catch(() => {
errorResponse(
return errorResponse(
res,
{ message: 'Cannot send password reset link' },
500
Expand All @@ -207,7 +210,7 @@ class UsersController {
});
})
.catch(({ details }) => {
validationErrorResponse(res, details, 400);
return validationErrorResponse(res, details, 400);
});
}

Expand Down Expand Up @@ -240,14 +243,14 @@ class UsersController {
200
);
}
errorResponse(
return errorResponse(
res,
{ message: 'An error occured! Kindly try again' },
400
);
})
.catch(() => {
errorResponse(
return errorResponse(
res,
{ message: 'An error occurred! kindly try again' },
400
Expand All @@ -258,7 +261,7 @@ class UsersController {
validationErrorResponse(res, details, 400);
});
} catch (err) {
errorResponse(res, err, 400);
return errorResponse(res, err, 400);
}
}
}
Expand Down
71 changes: 71 additions & 0 deletions server/controllers/bookmark-controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import db from '../models';
import { successResponse, errorResponse } from '../utils/helpers';

const { Recipe, User } = db;

/**
* The controllers for user bookmarks
*
* @class BookmarkController
*/
class BookmarkController {
/**
* toggle bookmark controller
*
* @static
* @param {*} req
* @param {*} res
* @param {Function} next
* @memberof RecipeController
* @returns {undefined}
*/
static async toggleBookmark(req, res) {
const { id } = req.user;
const { slug } = req.params;
try {
const recipe = await Recipe.findOne({ where: { slug } });
if (!recipe) {
return errorResponse(res, 'recipe not found', 404);
}
const check = await recipe.hasBookmarkers(id);
if (check) {
const deleted = await recipe.removeBookmarkers(id);
return deleted
? successResponse(res, 'delete successful')
: errorResponse(res, 'could not delete', 500);
}
await recipe.addBookmarkers(id);
return successResponse(res, 'bookmarked successful');
} catch (error) {
return errorResponse(res, error.message);
}
}

/**
* Get user bookmark controller
*
* @static
* @param {*} req
* @param {*} res
* @param {Function} next
* @memberof RecipeController
* @returns {undefined}
*/
static async getBookmarks(req, res) {
try {
const { id } = req.user;
const user = await User.findOne({ where: { id } });
const bookmarks = await user.getBookmarks();
if (!bookmarks) {
return successResponse(res, 'you do not have any bookmark');
}
const bookmarkArr = bookmarks.map(lists => ({
slug: lists.slug
}));
return successResponse(res, bookmarkArr);
} catch (err) {
return errorResponse(res, err.message);
}
}
}
export default BookmarkController;
3 changes: 1 addition & 2 deletions server/migrations/20190305132457-create-user.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,5 @@ module.exports = {
type: Sequelize.DATE
}
}),
// eslint-disable-next-line no-unused-vars
down: (queryInterface, Sequelize) => queryInterface.dropTable('Users')
down: queryInterface => queryInterface.dropTable('Users')
};
3 changes: 1 addition & 2 deletions server/migrations/20190312121718-create-recipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,5 @@ module.exports = {
type: Sequelize.DATE
}
}),
down: (queryInterface /* , Sequelize */) =>
queryInterface.dropTable('Recipes')
down: queryInterface => queryInterface.dropTable('Recipes')
};
2 changes: 1 addition & 1 deletion server/migrations/20190319030801-create-comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ module.exports = {
type: Sequelize.DATE
}
}),
down: (queryInterface /* Sequelize */) => queryInterface.dropTable('Comments')
down: queryInterface => queryInterface.dropTable('Comments')
};
38 changes: 38 additions & 0 deletions server/migrations/20190319180606-create-bookmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
module.exports = {
up: (queryInterface, Sequelize) =>
queryInterface.createTable('Bookmarks', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
userId: {
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: 'Users',
key: 'id',
as: 'bookmarks'
}
},
recipeId: {
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: 'Recipe',
key: 'id',
as: 'bookmakers'
}
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
}),
down: queryInterface => queryInterface.dropTable('Bookmarks')
};
6 changes: 6 additions & 0 deletions server/models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ module.exports = (sequelize, DataTypes) => {
foreignKey: 'userId',
as: 'comments'
});

User.belongsToMany(models.Recipe, {
through: 'Bookmark',
as: 'bookmarks',
foreignKey: 'userId'
});
};
return User;
};
17 changes: 17 additions & 0 deletions server/models/bookmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module.exports = (sequelize, DataTypes) => {
const Bookmark = sequelize.define(
'Bookmark',
{
userId: {
type: DataTypes.INTEGER,
allowNull: false
},
recipeId: {
type: DataTypes.INTEGER,
allowNull: false
}
},
{}
);
return Bookmark;
};
7 changes: 7 additions & 0 deletions server/models/recipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,17 @@ module.exports = (sequelize, DataTypes) => {
foreignKey: 'userId',
onDelete: 'CASCADE'
});

Recipe.hasMany(models.Comment, {
foreignKey: 'recipeId',
as: 'comments'
});

Recipe.belongsToMany(models.User, {
through: 'Bookmark',
foreignKey: 'recipeId',
as: 'bookmarkers'
});
};

return Recipe;
Expand Down
1 change: 1 addition & 0 deletions server/routes/recipe-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ router.get('/:slug', controller.getRecipeBySlug);
router.post('/:slug/comments', User.validUser, CommentController.create);
router.get('/:slug/comments', CommentController.getAllComments);
router.put('/:slug', User.validUser, controller.updateRecipe);

router.delete('/:slug', User.validUser, controller.deleteRecipe);

export default router;
7 changes: 7 additions & 0 deletions server/routes/user-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Router } from 'express';
import User from '../middlewares/users-middleware';
import userController from '../controllers/user-controller';
import followController from '../controllers/follow-controller';
import bookmarkController from '../controllers/bookmark-controller';

const router = new Router();

Expand All @@ -10,5 +11,11 @@ router.put('/', User.validUser, userController.updateUser);
router.get('/', User.validUser, userController.getProfile);
router.get('/followers', User.validUser, followController.fetchFollowers);
router.get('/following', User.validUser, followController.fetchFollowing);
router.post(
'/:slug/bookmark',
User.validUser,
bookmarkController.toggleBookmark
);
router.get('/bookmarks', User.validUser, bookmarkController.getBookmarks);

export default router;
Loading

0 comments on commit fe4cdd9

Please sign in to comment.