-
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
16 changed files
with
452 additions
and
26 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,138 @@ | ||
import db from '../models'; | ||
import response from '../helpers/response'; | ||
|
||
|
||
const { FollowedTags, Tag } = db; | ||
|
||
/** | ||
* @class TagController | ||
*/ | ||
class TagController { | ||
/** | ||
* @static | ||
* @description a function that handles following of a tag by user | ||
* @param {object} req HTTP request object | ||
* @param {object} res HTTP response object | ||
* @returns {object} api route response | ||
*/ | ||
static async follow(req, res) { | ||
try { | ||
const { userId } = req.user; | ||
const { tagName } = req.params; | ||
const tag = await Tag.findOne({ where: { name: tagName } }); | ||
|
||
if (!tag) { | ||
return response(res, 404, 'failure', `${tagName} tag does not exist`); | ||
} | ||
|
||
FollowedTags.findOrCreate({ | ||
where: { tagName, followerId: userId, tagId: tag.id }, | ||
attributes: ['id', 'tagId', 'tagName', 'followerId'] | ||
}).spread((follow, created) => { | ||
if (created) { | ||
return response( | ||
res, | ||
201, | ||
'success', | ||
`You are now following the ${tagName} tag` | ||
); | ||
} | ||
return response( | ||
res, | ||
409, | ||
'failure', | ||
`You are already following the ${tagName} tag` | ||
); | ||
}); | ||
} catch (error) { | ||
return response( | ||
res, | ||
500, | ||
'failure', | ||
'Something went wrong on the server' | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* @static | ||
* @description a function that handles getting all tags a user is following | ||
* @param {object} req HTTP request object | ||
* @param {object} res HTTP response object | ||
* @returns {object} api route response | ||
*/ | ||
static async getFollowedTags(req, res) { | ||
try { | ||
const { userId } = req.user; | ||
const followedTags = await FollowedTags.findAll({ | ||
where: { followerId: userId } | ||
}); | ||
|
||
if (followedTags.length === 0) { | ||
return response(res, 200, 'success', 'You are currenly not following any tag'); | ||
} | ||
|
||
response( | ||
res, | ||
200, | ||
'success', | ||
'Tags you are following', | ||
null, | ||
followedTags | ||
); | ||
} catch (error) { | ||
return response( | ||
res, | ||
500, | ||
'failure', | ||
'Something went wrong on the server' | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* @static | ||
* @description a function that handles unfollowing a tag | ||
* @param {object} req HTTP request object | ||
* @param {object} res HTTP response object | ||
* @returns {object} api route response | ||
*/ | ||
static async unFollow(req, res) { | ||
try { | ||
const { tagName } = req.params; | ||
const { userId } = req.user; | ||
const tag = await Tag.findOne({ | ||
where: { name: tagName } | ||
}); | ||
|
||
if (!tag) { | ||
return response(res, 404, 'failure', `${tagName} tag does not exist`); | ||
} | ||
|
||
const followingTag = await FollowedTags.findOne({ | ||
where: { followerId: userId, tagName } | ||
}); | ||
|
||
if (!followingTag) { | ||
return response( | ||
res, | ||
400, | ||
'failure', | ||
`You are not following the ${tagName} tag` | ||
); | ||
} | ||
|
||
followingTag.destroy(); | ||
response(res, 200, 'success', `You have now unfollowed the ${tagName} tag`); | ||
} catch (error) { | ||
return response( | ||
res, | ||
500, | ||
'failure', | ||
'Something went wrong on the server' | ||
); | ||
} | ||
} | ||
} | ||
|
||
export default TagController; |
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
38 changes: 38 additions & 0 deletions
38
server/migrations/20190130065911-create-user-followed-tags.js
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 @@ | ||
export default { | ||
up: (queryInterface, Sequelize) => { | ||
return queryInterface.sequelize.query('CREATE EXTENSION IF NOT EXISTS "uuid-ossp";') | ||
.then(() => { | ||
return queryInterface.createTable('FollowedTags', { | ||
id: { | ||
allowNull: false, | ||
primaryKey: true, | ||
type: Sequelize.UUID, | ||
defaultValue: Sequelize.literal('uuid_generate_v4()') | ||
}, | ||
tagId: { | ||
type: Sequelize.UUID, | ||
allowNull: false | ||
}, | ||
tagName: { | ||
type: Sequelize.STRING, | ||
allowNull: false | ||
}, | ||
followerId: { | ||
type: Sequelize.UUID, | ||
allowNull: false | ||
}, | ||
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('FollowedTags') | ||
}; |
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,32 @@ | ||
module.exports = (sequelize, DataTypes) => { | ||
const FollowedTags = sequelize.define( | ||
'FollowedTags', | ||
{ | ||
tagId: { | ||
type: DataTypes.UUID, | ||
allowNull: false | ||
}, | ||
tagName: { | ||
type: DataTypes.STRING, | ||
allowNull: false, | ||
}, | ||
followerId: { | ||
type: DataTypes.UUID, | ||
allowNull: false | ||
} | ||
}, | ||
{} | ||
); | ||
FollowedTags.associate = (models) => { | ||
const { Tag, User } = models; | ||
FollowedTags.belongsTo(Tag, { | ||
foreignKey: 'tagId', | ||
onDelete: 'CASCADE' | ||
}); | ||
FollowedTags.belongsTo(User, { | ||
foreignKey: 'followerId', | ||
onDelete: 'CASCADE' | ||
}); | ||
}; | ||
return FollowedTags; | ||
}; |
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,26 @@ | ||
import { Router } from 'express'; | ||
import TagController from '../../controllers/TagController'; | ||
import AuthMiddleware from '../../middlewares/AuthMiddleware'; | ||
|
||
|
||
const tagRoutes = Router(); | ||
|
||
tagRoutes.post( | ||
'/tags/:tagName/follow', | ||
AuthMiddleware.checkIfUserIsAuthenticated, | ||
TagController.follow | ||
); | ||
|
||
tagRoutes.delete( | ||
'/tags/:tagName/unfollow', | ||
AuthMiddleware.checkIfUserIsAuthenticated, | ||
TagController.unFollow | ||
); | ||
|
||
tagRoutes.get( | ||
'/tags/followedTags', | ||
AuthMiddleware.checkIfUserIsAuthenticated, | ||
TagController.getFollowedTags | ||
); | ||
|
||
export default tagRoutes; |
Oops, something went wrong.