Skip to content

Commit

Permalink
Issue #77: Adjust notes route
Browse files Browse the repository at this point in the history
Remove user name from notes route and get name from jwt token.
Use filter type=all to get all notes from a session.
  • Loading branch information
CarlosRodrigo authored and viniciusalbuquerque committed Jun 20, 2019
1 parent 3922126 commit 227e551
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 61 deletions.
8 changes: 4 additions & 4 deletions src/controllers/notesController.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ class NotesController {
}

async getNotesFromSession (note) {
return this.notesRepository.getNotes(note)
return this.notesRepository.getNotes(note.getNote())
}

async addNewNoteToSession (note) {
return this.notesRepository.addNewNoteToSession(note)
return this.notesRepository.addNewNoteToSession(note.getNote())
}

async deleteNote (note) {
return this.notesRepository.deleteNote(note)
return this.notesRepository.deleteNote(note.id)
}

async editNote (note) {
return this.notesRepository.editNote(note)
return this.notesRepository.editNote(note.id, note.getNote())
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/repositories/notesRepository/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ class NotesRepository {
this.db = db
}

async getNotes (note) {
return this.db.executeGetDB('notes', note.getNote())
async getNotes (filter) {
return this.db.executeGetDB('notes', filter)
}

async addNewNoteToSession (note) {
return this.db.executeInsert('notes', note.getNote())
return this.db.executeInsert('notes', note)
}

async deleteNote (note) {
return this.db.executeDelete('notes', note.id)
async deleteNote (noteId) {
return this.db.executeDelete('notes', noteId)
}

async editNote (note) {
return this.db.executeUpdateDB('notes', note.id, note.getNote())
async editNote (noteId, note) {
return this.db.executeUpdateDB('notes', noteId, note)
}
}

Expand Down
20 changes: 0 additions & 20 deletions src/router/notesRouter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,6 @@ class NotesRouter {
res.send(response)
})

this.app.get('/:session_id/:user?', async (req, res) => {
var note
try {
note = this.noteMapper.mapGetNotesToDomain(req)
} catch (error) {
res.status(400)
res.send({ 'error': error })
return
}
var response = await this.notesController.getNotesFromSession(note)

if (response.message) {
res.status(500)
res.send({ 'error': response.message })
return
}

res.send(response)
})

this.app.post('/', async (req, res) => {
var note
try {
Expand Down
18 changes: 6 additions & 12 deletions src/router/notesRouter/mapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ class NotesMapper {
null,
req.body.topic,
req.body.description,
req.body.user,
req.name,
req.body.session_id
)
}

bodyFullJoiSchema () {
return Joi.object().keys({
description: Joi.string().required().max(100),
user: Joi.string().required(),
session_id: Joi.string().required(),
topic: Joi.string().required()
})
Expand All @@ -30,21 +29,16 @@ class NotesMapper {
if (error) {
throw error.details[0].message
}
return new Note(null, null, null, req.query.user, req.query.session_id)
}

mapGetNotesToDomain (req) {
const { error } = Joi.validate(req.params, this.getNotesJoiSchema())
if (error) {
throw error.details[0].message
if (req.query.type === 'all') {
return new Note(null, null, null, null, req.query.session_id)
}
return new Note(null, null, null, req.params.user, req.params.session_id)
return new Note(null, null, null, req.name, req.query.session_id)
}

getNotesJoiSchema () {
return Joi.object().keys({
session_id: Joi.string().required(),
user: Joi.string()
type: Joi.string().allow()
})
}

Expand Down Expand Up @@ -77,7 +71,7 @@ class NotesMapper {
req.params.id,
req.body.topic,
req.body.description,
req.body.user,
req.name,
req.body.session_id
)
}
Expand Down
15 changes: 1 addition & 14 deletions test/api/notes/notes.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ chai.should()
const auth = require('../../../src/middlewares/auth')
sinon.stub(auth, 'verifyToken')
.callsFake(function (req, res, next) {
req.name = 'Tester'
return next()
})

Expand All @@ -26,7 +27,6 @@ var noteId
let note = {
'topic': 'test',
'description': 'API Testing',
'user': 'Tester',
'session_id': 'test'
}

Expand All @@ -47,19 +47,6 @@ describe('Notes API', function () {
})
})

describe('/GET notes from sessions', function () {
it('Should get notes from session', function () {
return chai.request(app)
.get('/notes/test')
.then((res) => {
res.should.have.status(200)
res.body.should.be.a('array')

expect(res.body[0].description).to.equal(note.description)
})
})
})

describe('/GET notes from sessions using query params', function () {
it('Should get notes from session', function () {
return chai.request(app)
Expand Down
12 changes: 9 additions & 3 deletions test/mappers/reqMock.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ class ReqMock {
mockAddNoteCorrectReq () {
return {
body: {
user: 'userTest',
description: 'test note',
session_id: 'abcz',
topic: 'start'
Expand All @@ -13,7 +12,6 @@ class ReqMock {
mockAddNoteNotCorrectReq () {
return {
body: {
user: 'userTest',
description: 'test note',
topic: 'start'
}
Expand All @@ -23,12 +21,20 @@ class ReqMock {
mockGetNoteCorrectReq () {
return {
query: {
user: 'userTest',
session_id: 'abcz'
}
}
}

mockGetNoteTypeFilterCorrectReq () {
return {
query: {
session_id: 'abcz'
},
name: 'Tester'
}
}

mockGetNoteNotCorrectReq () {
return {
query: {
Expand Down
4 changes: 4 additions & 0 deletions test/mappers/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ describe('Notes Router Mapper', function () {
expect(() => notesMapper.mapGetNotesQueryToDomain(reqMock.mockGetNoteCorrectReq())).to.not.throw()
})

it('Should return a note', () => {
expect(() => notesMapper.mapGetNotesQueryToDomain(reqMock.mockGetNoteTypeFilterCorrectReq())).to.not.throw()
})

it('Should throw an Validation Error', () => {
expect(() => notesMapper.mapGetNotesQueryToDomain(reqMock.mockGetNoteNotCorrectReq())).to.throw()
})
Expand Down
2 changes: 1 addition & 1 deletion test/repositories/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('Notes Repository', function () {
it('Should get notes from session', async () => {
var note = new Note()
note.sessionId = 1
const notes = await notesRepository.getNotes(note)
const notes = await notesRepository.getNotes(note.getNote())
expect(notes).deep.equal([1, 2, 3])
})
})
Expand Down

0 comments on commit 227e551

Please sign in to comment.