-
Notifications
You must be signed in to change notification settings - Fork 4
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
Showing
14 changed files
with
474 additions
and
142 deletions.
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
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,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; |
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
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
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
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,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') | ||
}; |
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
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,17 @@ | ||
module.exports = (sequelize, DataTypes) => { | ||
const Bookmark = sequelize.define( | ||
'Bookmark', | ||
{ | ||
userId: { | ||
type: DataTypes.INTEGER, | ||
allowNull: false | ||
}, | ||
recipeId: { | ||
type: DataTypes.INTEGER, | ||
allowNull: false | ||
} | ||
}, | ||
{} | ||
); | ||
return Bookmark; | ||
}; |
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
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
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
Oops, something went wrong.