-
Notifications
You must be signed in to change notification settings - Fork 2
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
12 changed files
with
189 additions
and
4 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,17 @@ | ||
import responseUtil from '../utils/responseUtil'; | ||
import strings from '../utils/stringsUtil'; | ||
import findBookmarks from '../services/bookmarksServices'; | ||
|
||
const { YOUR_BOOKMARKS, NO_BOOKMARKS } = strings.bookmarks; | ||
|
||
class BoookmarksController { | ||
static async viewBookmarks(req, res) { | ||
const { id } = req.user.payload; | ||
const bookmarks = await findBookmarks({ userId: id }); | ||
|
||
return responseUtil(res, 200, (!bookmarks.length) | ||
? NO_BOOKMARKS : YOUR_BOOKMARKS, bookmarks); | ||
} | ||
} | ||
|
||
export default BoookmarksController; |
30 changes: 30 additions & 0 deletions
30
src/database/migrations/20191111154330-create-bookmarks.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,30 @@ | ||
'use strict'; | ||
module.exports = { | ||
up: (queryInterface, Sequelize) => { | ||
return queryInterface.createTable('bookmarks', { | ||
id: { | ||
allowNull: false, | ||
autoIncrement: true, | ||
primaryKey: true, | ||
type: Sequelize.INTEGER | ||
}, | ||
userId: { | ||
type: Sequelize.INTEGER | ||
}, | ||
accommodationId: { | ||
type: Sequelize.INTEGER | ||
}, | ||
createdAt: { | ||
allowNull: false, | ||
type: Sequelize.DATEONLY | ||
}, | ||
updatedAt: { | ||
allowNull: false, | ||
type: Sequelize.DATEONLY | ||
} | ||
}); | ||
}, | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
'use strict'; | ||
module.exports = (sequelize, DataTypes) => { | ||
const bookmarks = sequelize.define('bookmarks', { | ||
userId: DataTypes.INTEGER, | ||
accommodationId: DataTypes.INTEGER | ||
}, {}); | ||
bookmarks.associate = function(models) { | ||
// associations can be defined here | ||
bookmarks.belongsTo(models.accommodations, { | ||
as: 'accommodation', | ||
sourceKey: 'accommodationId', | ||
targetKey: 'id', | ||
}); | ||
}; | ||
return 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
'use strict'; | ||
|
||
module.exports = { | ||
up: (queryInterface, Sequelize) => Promise.all([ | ||
queryInterface.bulkInsert('bookmarks', [ | ||
{ | ||
userId: 3, | ||
accommodationId: 1, | ||
createdAt: new Date(), | ||
updatedAt: new Date() | ||
} | ||
]) | ||
]), | ||
down: (queryInterface, Sequelize) => Promise.all([ | ||
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
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 express from 'express'; | ||
import validateToken from '../../middlewares/auth/validateToken'; | ||
import BookmarksController from '../../controllers/bookmarksController'; | ||
|
||
const router = express.Router(); | ||
|
||
const { viewBookmarks } = BookmarksController; | ||
|
||
router.get('/', validateToken, viewBookmarks); | ||
|
||
export default router; |
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 db from '../database/models'; | ||
import responseUtil from '../utils/responseUtil'; | ||
|
||
const findBookmarks = data => { | ||
try { | ||
const myBookmarks = db.bookmarks.findAll({ | ||
where: data, | ||
attributes: { exclude: ['userId', 'accommodationId'] }, | ||
include: [{ | ||
model: db.accommodations, | ||
as: 'accommodation', | ||
attributes: { exclude: ['owner', 'locationId'] }, | ||
include: [ | ||
{ model: db.users, as: 'ownerUser', attributes: ['id', 'username', 'email'] }, | ||
{ model: db.locations, as: 'accommodationLocation', attributes: ['id', 'name'] }, | ||
], | ||
}], | ||
}); | ||
return myBookmarks; | ||
} catch (error) { | ||
return responseUtil(500, 'Ooops Something unexpected happened!'); | ||
} | ||
|
||
}; | ||
|
||
export default findBookmarks; |
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,59 @@ | ||
import chai from 'chai'; | ||
import chaiHttp from 'chai-http'; | ||
import { describe, it } from 'mocha'; | ||
import app from '../index'; | ||
import generateToken from '../utils/generateToken'; | ||
import mockData from './mockData/mockData'; | ||
|
||
chai.should(); | ||
chai.use(chaiHttp); | ||
|
||
const userToken = generateToken(mockData.verifiedUser1); | ||
console.log(userToken); | ||
|
||
const userToken2 = generateToken(mockData.verifiedUser3); | ||
const wrongToken = 'wrongToken' | ||
|
||
describe("Bookmarks tests", ()=> { | ||
|
||
it('it should retrieve all bookmarks for the logged in user', done => { | ||
chai.request(app) | ||
.get('/api/v1/bookmarks') | ||
.set('Authorization', `Bearer ${userToken}`) | ||
.end((err, res) => { | ||
console.log(userToken); | ||
res.should.have.status(200); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('it should reject an invalid token', done => { | ||
chai.request(app) | ||
.get('/api/v1/bookmarks') | ||
.set('Authorization', `Bearer ${wrongToken}`) | ||
.end((err, res) => { | ||
res.should.have.status(400); | ||
done(); | ||
}); | ||
}); | ||
it('it should reject a missing token', done => { | ||
chai.request(app) | ||
.get('/api/v1/bookmarks') | ||
.end((err, res) => { | ||
res.should.have.status(401); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('it should tell the user that no bookmarks were found', done => { | ||
chai.request(app) | ||
.get('/api/v1/bookmarks') | ||
.set('Authorization', `Bearer ${userToken2}`) | ||
.end((err, res) => { | ||
console.log(res.body); | ||
|
||
res.should.have.status(200); | ||
done(); | ||
}); | ||
}); | ||
}); |
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