-
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
29 changed files
with
3,686 additions
and
102 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,50 @@ | ||
import { Article, Like } from '../db/models'; | ||
|
||
/** | ||
*@description This class handles all like requests | ||
* @class LikeController | ||
*/ | ||
class LikeController { | ||
/** | ||
*@description This function allows a user to like articles | ||
* @static | ||
* @param {object} req the request body | ||
* @param {object} res the response body | ||
* @returns {object} res | ||
* @memberof LikeController | ||
*/ | ||
static async likeArticle(req, res) { | ||
const { slug } = req.params; | ||
const userId = req.user; | ||
|
||
try { | ||
const article = await Article.findOne({ | ||
attributes: ['id', 'slug', 'body', | ||
'tagList'], | ||
where: { slug }, | ||
}); | ||
|
||
if (!article) { | ||
return res.status(404).json({ | ||
status: 404, | ||
error: 'Article not found', | ||
}); | ||
} | ||
|
||
await Like.create({ userId, articleId: article.id }); | ||
|
||
return res.status(201).json({ | ||
status: 201, | ||
data: article, | ||
message: 'You just liked this article', | ||
}); | ||
} catch (error) { | ||
return res.status(500).json({ | ||
status: 500, | ||
error: error.message, | ||
}); | ||
} | ||
} | ||
} | ||
|
||
export default LikeController; |
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,111 @@ | ||
import { User } from '../db/models'; | ||
|
||
/** | ||
* @description This class handles user requests | ||
* @class UserController | ||
*/ | ||
class UserController { | ||
/** | ||
* @static | ||
* @description this function gets all registered users | ||
* @param {object} req the request body | ||
* @param {object} res the response body | ||
* @returns {object} res | ||
* @memberof UserController | ||
*/ | ||
static async getUserProfile(req, res) { | ||
const { userName } = req.params; | ||
|
||
try { | ||
const userData = await User.findOne({ | ||
attributes: ['firstName', 'lastName', 'userName', | ||
'email', 'bio', 'imageUrl'], | ||
where: { userName }, | ||
}); | ||
|
||
if (!userData) { | ||
return res.status(404).json({ | ||
status: 404, | ||
error: 'User not found', | ||
}); | ||
} | ||
|
||
return res.status(200).json({ | ||
status: 200, | ||
users: userData, | ||
}); | ||
} catch (error) { | ||
return res.status(500).json({ | ||
status: 500, | ||
error: error.message | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
*@static | ||
*@description this function creates and updates user profile | ||
* @param {object} req the request body | ||
* @param {object} res the response body | ||
* @returns {object} res | ||
* @memberof UserController | ||
*/ | ||
static async updateProfile(req, res) { | ||
try { | ||
const { | ||
firstName, lastName, userName, bio, | ||
} = req.body; | ||
|
||
const avatar = req.file; | ||
const userId = req.user; | ||
let { id } = req.params; | ||
id = Number(id); | ||
|
||
let avatarValue; | ||
|
||
if (avatar) avatarValue = avatar.url; | ||
|
||
const userDetails = { | ||
firstName, | ||
lastName, | ||
userName, | ||
bio, | ||
imageUrl: avatarValue, | ||
}; | ||
|
||
const where = { | ||
id, | ||
}; | ||
|
||
const attributes = ['id', 'firstName', 'lastName', 'userName', 'bio', 'imageUrl']; | ||
|
||
const userData = await User.findOne({ attributes, where }); | ||
|
||
if (id === userId) { | ||
await userData.update(userDetails, { where }); | ||
|
||
return res.status(200).json({ | ||
status: 200, | ||
user: userDetails, | ||
}); | ||
} | ||
return res.status(401).json({ | ||
status: 401, | ||
error: 'You do not have permission to perform that operation', | ||
}); | ||
} catch (error) { | ||
if (error.routine === '_bt_check_unique') { | ||
return res.status(400).json({ | ||
status: 400, | ||
error: 'User with that username already exists', | ||
}); | ||
} | ||
return res.status(500).json({ | ||
status: 500, | ||
error: error.message, | ||
}); | ||
} | ||
} | ||
} | ||
|
||
export default UserController; |
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,41 @@ | ||
module.exports = { | ||
up: (queryInterface, Sequelize) => queryInterface.createTable('Likes', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER | ||
}, | ||
userId: { | ||
type: Sequelize.INTEGER, | ||
required: true, | ||
onUpdate: 'CASCADE', | ||
onDelete: 'CASCADE', | ||
references: { | ||
model: 'Users', | ||
key: 'id', | ||
as: 'userId' | ||
} | ||
}, | ||
articleId: { | ||
type: Sequelize.INTEGER, | ||
required: true, | ||
onUpdate: 'CASCADE', | ||
onDelete: 'CASCADE', | ||
references: { | ||
model: 'Articles', | ||
key: 'id', | ||
as: 'articleId' | ||
} | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
} | ||
}), | ||
down: (queryInterface, Sequelize) => queryInterface.dropTable('Likes'), | ||
}; |
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,34 @@ | ||
module.exports = { | ||
up: (queryInterface, Sequelize) => queryInterface.createTable('Tags', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER | ||
}, | ||
tagName: { | ||
type: Sequelize.STRING | ||
}, | ||
articleId: { | ||
type: Sequelize.INTEGER, | ||
allowNull: false, | ||
required: true, | ||
references: { | ||
model: 'Articles', | ||
key: 'id', | ||
as: 'article' | ||
}, | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE, | ||
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP') | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE, | ||
defaultValue: Sequelize.literal('CURRENT_TIMESTAMP') | ||
} | ||
}), | ||
down: (queryInterface, Sequelize) => queryInterface.dropTable('Tags') | ||
}; |
Oops, something went wrong.