-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ft(accomodation): like/unlike an accommodation
- add new table/model `AccommodationLike` - add controller to like/unlike an accommodation - add endpoint to like/unlike an accommodation - `PATCH: /accommodation/:accommodationId/like [Finishes (#167727755)]
- Loading branch information
Showing
9 changed files
with
255 additions
and
10 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
41 changes: 41 additions & 0 deletions
41
src/database/migrations/20190907143807-create-accommodation-likes.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,41 @@ | ||
module.exports = { | ||
up: (queryInterface, Sequelize) => { | ||
return queryInterface.createTable('AccommodationLikes', { | ||
id: { | ||
allowNull: false, | ||
primaryKey: true, | ||
type: Sequelize.UUID, | ||
}, | ||
liked: { | ||
type: Sequelize.BOOLEAN, | ||
allowNull: false, | ||
defaultValue: true | ||
}, | ||
userId: { | ||
type: Sequelize.UUID, | ||
references: { | ||
model: 'Users', | ||
key: 'id', | ||
}, | ||
}, | ||
accommodationId: { | ||
type: Sequelize.UUID, | ||
references: { | ||
model: 'Accommodation', | ||
key: 'id', | ||
}, | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATE | ||
} | ||
}); | ||
}, | ||
down: (queryInterface, Sequelize) => { | ||
return queryInterface.dropTable('AccommodationLikes'); | ||
} | ||
}; |
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,31 @@ | ||
import response from '../utils/response'; | ||
import messages from '../utils/messages'; | ||
import DbServices from '../services/dbServices'; | ||
import models from '../models'; | ||
|
||
const { Accommodation } = models; | ||
const { getById } = DbServices; | ||
|
||
/** | ||
* @method checkAccommodationId | ||
* @param {object} req request object | ||
* @param {object} res request object | ||
* @param {function} next next function | ||
* @returns {object} custom response | ||
* @description checks if accommodationId passed to params is valid | ||
*/ | ||
export const checkAccommodationId = async (req, res, next) => { | ||
try { | ||
const { accommodationId } = req.params; | ||
if (accommodationId) { | ||
const accommodation = await getById(Accommodation, accommodationId, {}); | ||
if (!accommodation) { | ||
return response(res, 404, 'error', { message: messages.notExistAccommodation }); | ||
} | ||
return next(); | ||
} | ||
return next(); | ||
} catch (error) { | ||
return response(res, 500, 'error', { message: messages.serverError }); | ||
} | ||
}; |
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,28 @@ | ||
export default (sequelize, DataTypes) => { | ||
const AccommodationLike = sequelize.define('AccommodationLike', { | ||
id: { | ||
allowNull: false, | ||
primaryKey: true, | ||
type: DataTypes.UUID, | ||
defaultValue: DataTypes.UUIDV4, | ||
}, | ||
liked: { | ||
type: DataTypes.BOOLEAN, | ||
allowNull: false, | ||
defaultValue: true | ||
} | ||
}, {}); | ||
AccommodationLike.associate = (models) => { | ||
// associations can be defined here | ||
AccommodationLike.belongsTo(models.Accommodation, { | ||
foreignKey: 'accommodationId', | ||
as: 'accommodation' | ||
}); | ||
|
||
AccommodationLike.belongsTo(models.User, { | ||
foreignKey: 'userId', | ||
as: 'user' | ||
}); | ||
}; | ||
return AccommodationLike; | ||
}; |
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
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