-
Notifications
You must be signed in to change notification settings - Fork 8
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
13 changed files
with
391 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import status from '../../helpers/constants/status.codes'; | ||
import errorSender from '../../helpers/error.sender'; | ||
import errorMessage from '../../helpers/constants/error.messages'; | ||
import model from '../models/index'; | ||
import generatePagination from '../../helpers/generate.pagination.details'; | ||
|
||
const { | ||
Bookmark, | ||
User, | ||
Article, | ||
Category | ||
} = model; | ||
/** | ||
* @class | ||
*/ | ||
export default class BookmarkController { | ||
/** | ||
* @author Daniel Mwangila | ||
* @description This functions stores the bookmarked articles | ||
* If the user request this operation for the second time it will delete the boorkmark | ||
* This is to create the toggle effect. | ||
* @static | ||
* @param {object} req the request sent to be excuted | ||
* @param {object} res the response after execution | ||
* @return {*} void | ||
* @memberof BookmarkController | ||
*/ | ||
static async addBookmark(req, res) { | ||
const { username } = req.user.user; | ||
const { slug } = req.params; | ||
|
||
try { | ||
await Bookmark.create({ | ||
articleSlug: slug, | ||
username | ||
}); | ||
res.status(status.CREATED).json({ | ||
status: 201, | ||
message: 'Bookmarked successfully' | ||
}); | ||
} catch (error) { | ||
const duplicationErrorCode = '23505'; | ||
if (error.original.code === duplicationErrorCode) { | ||
await Bookmark.destroy({ | ||
where: { | ||
username, | ||
articleSlug: slug | ||
} | ||
}); | ||
res.status(status.OK).json({ | ||
status: 200, | ||
message: 'Successfully removed bookmark' | ||
}); | ||
} else { | ||
errorSender(status.NOT_FOUND, res, 'Slug', errorMessage.noSlug); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @author Daniel Mwangila | ||
* @description This funcrtion gets the users bookmarks from the database. | ||
* @static | ||
* @param {object} req the request sent to be excuted | ||
* @param {object} res the response after execution | ||
* @return {*} void | ||
* @memberof BookmarkController | ||
*/ | ||
static async getBookmarks(req, res) { | ||
const { username } = req.user.user; | ||
const emptyBookmarkList = 0; | ||
const defaultOffset = 0; | ||
const defaultLimit = 10; | ||
const data = {}; | ||
const offset = req.query.offset || defaultOffset; | ||
const limit = req.query.limit || defaultLimit; | ||
|
||
const bookmarkList = await Bookmark.findAll({ | ||
attributes: ['articleSlug'], | ||
where: { | ||
username | ||
}, | ||
include: { | ||
model: Article, | ||
required: true, | ||
attributes: ['id', 'title', 'description'], | ||
include: [{ | ||
model: User, | ||
required: true, | ||
attributes: ['firstName', 'lastName', 'profileImage'] | ||
}, { | ||
model: Category, | ||
as: 'Category', | ||
attributes: ['name'] | ||
}] | ||
}, | ||
offset, | ||
limit | ||
}); | ||
const count = bookmarkList.length; | ||
|
||
const paginationDetail = generatePagination(count, bookmarkList, offset, limit); | ||
data.paginationDetail = paginationDetail; | ||
data.bookmarks = bookmarkList; | ||
|
||
if (count > emptyBookmarkList) { | ||
res.status(status.OK).json({ | ||
status: 200, | ||
data | ||
}); | ||
} else { | ||
errorSender(status.NOT_FOUND, res, 'Bookmark', errorMessage.noBookmark); | ||
} | ||
} | ||
} |
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,44 @@ | ||
export default { | ||
up: (queryInterface, Sequelize) => { | ||
return queryInterface.createTable('Bookmarks', { | ||
articleSlug: { | ||
type: Sequelize.STRING, | ||
references: { | ||
model: 'Articles', | ||
key: 'slug' | ||
}, | ||
onDelete: 'CASCADE', | ||
onUpdate: 'CASCADE', | ||
allowNull: false | ||
}, | ||
username: { | ||
type: Sequelize.STRING, | ||
references: { | ||
model: 'Users', | ||
key: 'username' | ||
}, | ||
onDelete: 'CASCADE', | ||
onUpdate: 'CASCADE', | ||
allowNull: false | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
} | ||
}) | ||
.then(() => | ||
queryInterface | ||
.addConstraint('Bookmarks', ['articleSlug', 'username'], { | ||
type: 'primary key', | ||
name: 'bookmarkId' | ||
}) | ||
); | ||
}, | ||
down: (queryInterface, Sequelize) => { | ||
return 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,40 @@ | ||
export default (sequelize, DataTypes) => { | ||
const bookmark = sequelize.define('Bookmark', { | ||
articleSlug: { | ||
type: DataTypes.STRING, | ||
references: { | ||
model: 'Article', | ||
key: 'slug' | ||
}, | ||
onDelete: 'CASCADE', | ||
onUpdate: 'CASCADE', | ||
allowNull: false | ||
}, | ||
username: { | ||
type: DataTypes.STRING, | ||
references: { | ||
model: 'Users', | ||
key: 'username' | ||
}, | ||
onDelete: 'CASCADE', | ||
onUpdate: 'CASCADE', | ||
allowNull: false | ||
} | ||
}, {}); | ||
|
||
bookmark.removeAttribute('id'); | ||
|
||
bookmark.associate = (models) => { | ||
bookmark.belongsTo(models.Article,{ | ||
foreignKey: 'articleSlug', | ||
targetKey: 'slug', | ||
onDelete: 'CASCADE', | ||
hooks: true | ||
}); | ||
bookmark.belongsTo(models.User,{ | ||
foreignKey: 'username' | ||
}); | ||
|
||
}; | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Router } from 'express'; | ||
import bookmarkController from '../controllers/bookmarkController'; | ||
import authorization from '../../middlewares/checkValidToken'; | ||
|
||
const route = new Router(); | ||
|
||
route.post('/:slug', authorization, bookmarkController.addBookmark); | ||
route.get('/', authorization, bookmarkController.getBookmarks); | ||
|
||
|
||
export default route; |
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,20 @@ | ||
export default { | ||
up: (queryInterface, Sequelize) => { | ||
return queryInterface.bulkInsert('Bookmarks', [{ | ||
articleSlug:'How-to-create-sequalize-seeds', | ||
username:'BurindiAlain2', | ||
createdAt: new Date(), | ||
updatedAt: new Date() | ||
}, | ||
{ | ||
articleSlug:'How-to-create-sequalize-seedss', | ||
username:'BurindiAlain2', | ||
createdAt: new Date(), | ||
updatedAt: new Date() | ||
}], {}); | ||
}, | ||
|
||
down: (queryInterface, Sequelize) => { | ||
return queryInterface.bulkDelete('Bookmarks', null, {}); | ||
} | ||
}; |
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.