Skip to content

Commit

Permalink
Implement-API-documentation
Browse files Browse the repository at this point in the history
- Implemented using Slate

- Implemented more test cases

- Removed helper class
  • Loading branch information
Benny Ogidan authored and Benny Ogidan committed Oct 19, 2017
1 parent e629dfa commit 176814b
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 139 deletions.
19 changes: 0 additions & 19 deletions server/src/Helper/helper.js

This file was deleted.

21 changes: 4 additions & 17 deletions server/src/controllers/books.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import models from '../models';
import Helper from '../Helper/helper';
import paginationfunc from '../controllers/middleware/pagination';

const Books = models.Books;
Expand Down Expand Up @@ -39,10 +38,7 @@ export default {
description: req.body.description,
bookimage: req.body.bookimage
})
.then(books =>
res.status(201).send({
message: `${books.title} has been added to the library`
}))
.then(books => res.status(201).send({ message: `${books.title} has been added to the library` }))
.catch(error => res.status(401).send(error));
});
},
Expand Down Expand Up @@ -81,17 +77,9 @@ export default {
Description: book.description,
Image: book.bookimage
}))
.catch((error) => {
res
.status(400)
.send({
Errors: Helper.errorArray(error)
});
});
.catch((error) => { res.status(400).send({ success: false, error }); });
})
.catch(error => res.status(400).send({
Errors: Helper.errorArray(error)
}));
.catch(error => res.status(400).send({ success: false, error }));
},

/**
Expand Down Expand Up @@ -144,8 +132,7 @@ export default {
res.json({ error: 'Empty', message: 'There are no books present in the database' });
} else {
res
.status(200)
.send({
.status(200).send({
books: books.rows,
pagination: paginationfunc(offset, limit, books)
});
Expand Down
6 changes: 3 additions & 3 deletions server/src/controllers/middleware/authenticate.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ const getJWT = (id, email, username, firstname) => new Promise((resolve, reject)
firstname
}, process.env.JWT_SECRET,
{
expiresIn: '24h'
expiresIn: '4h'
}, (error, token) => {
if (error) {
reject(new Error({
status: 'Error',
message: error
message: 'Error generating token'
}));
} else if (token) {
resolve({
Expand All @@ -61,7 +61,7 @@ const getJWT = (id, email, username, firstname) => new Promise((resolve, reject)
} else {
reject(new Error({
status: 'Error',
message: 'Token error'
message: 'Error generating token'
}));
}
}
Expand Down
43 changes: 9 additions & 34 deletions server/src/controllers/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,47 +15,28 @@ export default {
* @returns {void|Response} status, send
*/
create(req, res) {
if (!req.body.firstname ||
!req.body.lastname ||
!req.body.username ||
!req.body.password ||
if (!req.body.firstname || !req.body.lastname || !req.body.username || !req.body.password ||
!req.body.email ||
!req.body.passwordConfirmation) {
return res
.status(400)
.send({ message: 'All fields are required' });
return res.status(400).send({ message: 'All fields are required' });
}
if (req.body.password !== req.body.passwordConfirmation) {
return res
.status(422)
.send({ message: 'Password and Password confirmation do not match' });
return res.status(422).send({ message: 'Password and Password confirmation do not match' });
}

User
.findOne({
where: { username: req.body.username }
})
.findOne({ where: { username: req.body.username } })
.then((usernameExists) => {
if (usernameExists) {
res
.status(409)
.json({
success: false,
message: 'This username is already in use'
});
.status(409).json({ success: false, message: 'This username is already in use' });
} else {
User
.findOne({
where: { email: req.body.email }
})
.then((userExists) => {
if (userExists) {
res
.status(409)
.json({
success: false,
message: 'This email is already in use'
});
res.status(409).json({ success: false, message: 'This email is already in use' });
} else {
User
.create({
Expand All @@ -77,21 +58,15 @@ export default {
}
})
.catch((error) => {
res
.status(400)
.send({ success: false, message: ` ${error.message}` });
res.status(400).send({ success: false, message: ` ${error.message}` });
});
}
})
.catch((error) => {
res
.status(400)
.send({ success: false, message: ` ${error.message}` });
res.status(400).send({ success: false, message: ` ${error.message}` });
})
.catch((error) => {
res
.status(400)
.send({ success: false, message: ` ${error.message}` });
res.status(400).send({ success: false, message: ` ${error.message}` });
});
}
});
Expand Down
71 changes: 16 additions & 55 deletions server/src/controllers/userbooks.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { toDate } from 'validator';
import models from '../models';
import Helper from '../Helper/helper';
import paginationfunc from '../controllers/middleware/pagination';


Expand All @@ -26,81 +25,45 @@ export default {
return res.status(422).send({ message: 'Please provide a valid return date' });
}
UserBooks.findOne({
where: {
userid: req.params.userId,
bookid: req.body.bookId,
returnstatus: false
},
where: { userid: req.params.userId, bookid: req.body.bookId, returnstatus: false },
include: [
{
model: Books,
as: 'book',
required: true
}
{ model: Books, as: 'book', required: true }
]
}).then((bookfound) => {
if (bookfound) {
return res
.status(409)
.send({
success: false,
messsage: 'You have already borrowed this book',
});
return res.status(409).send({ success: false, messsage: 'You have already borrowed this book' });
}
return UserBooks
UserBooks
.create({
userid: req.params.userId,
bookid: req.body.bookId,
returndate
userid: req.params.userId, bookid: req.body.bookId, returndate
})
.then(() => {
Books
.findOne({
where: {
id: req.body.bookId
}
})
.findOne({ where: { id: req.body.bookId } })
.then((booktoborrow) => {
if (!booktoborrow || booktoborrow.quantity === 0) {
return res
.status(404)
.send({ success: false, message: 'Sorry we can\'t find this book or all copies of this book are on loan' });
return res.status(404).send({ success: false, message: 'Sorry we can\'t find this book or all copies of this book are on loan' });
}

booktoborrow
.update({
quantity: booktoborrow.quantity -= 1
})
.then((borrowedbook) => {
res
.status(201)
.send({ success: true, message: `${borrowedbook.title} succesfully loaned` });
res.status(202).send({ success: true, message: `${borrowedbook.title} succesfully loaned` });
})
.catch((error) => {
res
.status(500)
.send({
Errors: Helper.errorArray(error)
});
.catch(() => {
res.status(500).send({ success: false, message: 'Error from the your end' });
});
})
.catch((error) => {
res
.status(500)
.send({
Errors: Helper.errorArray(error)
});
.catch(() => {
res.status(500).send({ success: false, message: 'Error from the your end' });
});
})
.catch(() => {
res
.status(422)
.send({ success: false, message: 'This book does not exist in the library' });
res.status(404).send({ success: false, message: 'There is a problem with this user or book, Please contact the administrator' });
});
}).catch((error) => {
res
.status(400)
.send({ success: false, message: ` ${error.message}` });
res.status(400).send({ success: false, message: ` ${error.message}` });
});
},

Expand All @@ -117,8 +80,7 @@ export default {
const limit = req.query.limit || 3;
if (!req.query.returned) {
return res
.status(404)
.send({ message: 'Please specify a value for returned books' });
.status(404).send({ message: 'Please specify a value for returned books' });
}
return UserBooks.findAndCountAll({
where: {
Expand All @@ -137,8 +99,7 @@ export default {
}).then((book) => {
if (book.length === 0) {
return res
.status(404)
.send({ success: false, message: 'You have no books on your loan list' });
.status(404).send({ success: false, message: 'You have no books on your loan list' });
}
res
.status(200)
Expand Down
23 changes: 16 additions & 7 deletions server/src/test/books.js → server/src/test/booksSpec.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/*
eslint-disable no-console
*/

import faker from 'faker';
import chai from 'chai';
import chaiHttp from 'chai-http';
Expand Down Expand Up @@ -33,6 +37,9 @@ describe('HelloBooks', () => {
})
.then((book) => {
bookId = book.id;
})
.catch(() => {
console.log('Error in the Book seeding');
});

User.create({
Expand All @@ -43,20 +50,23 @@ describe('HelloBooks', () => {
.name
.lastName(),
username: 'Benny',
password: 'benny',
passwordConfirmation: 'benny',
password: 'bennyogidan',
passwordConfirmation: 'bennyogidan',
email: faker
.internet
.email()
});
})
.catch(() => {
console.log('Error in the User seeding');
});

chai
.request(app)
.post('/api/v1/auth/users/signin')
.set('Accept', 'application/x-www-form-urlencoded')
.send({
username: 'Benny',
password: 'benny',
password: 'bennyogidan',
})
.end((err, res) => {
token = res.body.token;
Expand All @@ -70,7 +80,7 @@ describe('HelloBooks', () => {
/*
* Unauthenticated user tests
*/
describe('/POST', () => {
describe('/GET', () => {
it('It retrieves all books from the data', (done) => {
chai
.request(app)
Expand All @@ -85,7 +95,7 @@ describe('HelloBooks', () => {
});
});
});
describe('/GET', () => {
describe('/GET should return a book list', () => {
it('should return books when given a limit and an offset', (done) => {
chai
.request(app)
Expand All @@ -104,7 +114,6 @@ describe('HelloBooks', () => {
});
});

// Edit a book
describe('/PUT', () => {
it('Edit a select book from the data', (done) => {
chai
Expand Down
Loading

0 comments on commit 176814b

Please sign in to comment.