REST API that manages a library, it includes books, members and loans management
- npm
- node >= 8
- MySQL ~5.7
git clone https://github.com/gonzagrisa/library_backend
cd library_backend
npm install
node index.js
Code | Description |
---|---|
200 | Success! |
400 | The query contains errors. In the event that a request was created using a form and contains user generated data, the user is notified that the data must be corrected before the query is repeated |
401 | There was an unauthorized attempt to use functionality available only to authorized users. |
403 | The request is understood, but it has been refused due to permissions |
404 | An attempt to invoke a non-existent object, such as a method or a book not found |
500 | Something is broken |
Error messages are returned in JSON format. For example, an error might look like this:
{
"error":{
"code": 404
"message": "Sorry, that page does not exist",
}
}
localhost:8000/books
Get the list of books in database
{
"code":200,
"data": [
{
"id": 10,
"title": "Harry Potter",
"amount": 100
},
{
"id": 20,
"title": "Lord of the Rings",
"amount": 10
}
]
}
localhost:8000/books/:id
Gets the book's info by its id
Type | Description | |
---|---|---|
id |
integer | Unique identifier for the object |
{
"code":200,
"data": {
"bookId": 10,
"title": "Harry Potter",
"available": 100
}
}
{
"error": {
"code": 404,
"message": "Book not found"
}
}
localhost:8000/books
Save a new book in the database
{
"title":"Don Quijote",
"amount":100
}
{
"code":201,
"message": "Book Added"
}
{
"error": {
"code": 400,
"message": "Wrong parameters"
}
}
{
"error": {
"code": 401,
"message": "You must be logged in and be an admin to perform this action"
}
}
{
"error": {
"code": 403,
"message": "You must be an admin to perform this action"
}
}
localhost:8000/books/:id
Delete a Book from database by its id
Type | Description | |
---|---|---|
id |
integer | Book's unique identifier |
{
"code":200,
"message": "Book deleted"
}
{
"error": {
"code": 400,
"message": "Cannot delete the book due to there are borrowed copies"
}
}
{
"error": {
"code": 404,
"message": "Book not found"
}
}
{
"error": {
"code": 401,
"message": "You must be logged in and be an admin to perform this action"
}
}
{
"error": {
"code": 403,
"message": "You must be an admin to perform this action"
}
}
localhost:8000/books/{id}
Update a book's amount of copies by its id
Type | Description | |
---|---|---|
id |
integer | Book's unique identifier |
{
"bookId":2,
"amount":100
}
{
"code":200,
"message": "amount of copies of book with id: {id} updated successfully"
}
{
"error": {
"code": 404,
"message": "Book not found"
}
}
{
"error": {
"code": 403,
"message": "You must be an admin to perform this action"
}
}
{
"error": {
"code": 400,
"message": "Wrong parameters"
}
}
localhost:8000/users
Obtain a list of the library's users
{
"code":200,
"data": [
{
"id": 1,
"name": "A"
},
{
"id": 2,
"name": "B"
}
]
}
localhost:8000/users/:id
Get a user's info by its id
Type | Description | |
---|---|---|
id |
integer | User's unique identifier |
{
"code":200,
"data": {
"id": 1,
"name": "A"
}
}
{
"error": {
"code": 400,
"message": "Couldn't Log Out"
}
}
localhost:8000/signup
Create a new user in the database with rol: "USER"
{
"email":"[email protected]",
"username":"abc",
"password":"secret"
}
{
"code":200,
"message": "User Created Successfully"
}
localhost:8000/login
Create a session
{
"email": "[email protected]",
"password": "secret"
}
{
"code": 200,
"userId": 2,
"rol": "ADMIN",
"message": "Logged In"
}
{
"error": {
"code": 400,
"message": "Incorrect User or Password"
}
}
localhost:8000/logout
Delete a user's session
{
"code":200,
"message": "Logged Out"
}
{
"error": {
"code": 400,
"message": "Couldn't Log Out"
}
}
{
"error": {
"code": 400,
"message": "Error destroying session"
}
}
localhost:8000/signup/checkEmail/{email}
Checks if an email is already in database
Type | Description | |
---|---|---|
email |
string | Email to check if it's available |
{
"code":200,
"message": "Email available"
}
{
"error": {
"code": 400,
"message": "Email already in use"
}
}
localhost:8000/signup/checkUsername/{username}
Checks if a username is already in database
Type | Description | |
---|---|---|
username |
string | Username to check if it's available |
{
"code":200,
"message": "Username available"
}
{
"error": {
"code": 400,
"message": "Username already in use"
}
}
localhost:8000/loans
Get a list of all the currently active loans made
{
"code":200,
"data": [
{
"id": 1,
"memberId": 1,
"bookId": 10,
"expiracyDate": 1567900080238
},
{
"id": 2,
"memberId": 2,
"bookId": 10,
"expiracyDate": 1567900080238
}
]
}
{
"error": {
"code": 401,
"message": "You must be logged in and be an admin to perform this action"
}
}
{
"error": {
"code": 403,
"message": "You must be an admin to perform this action"
}
}
localhost:8000/loans/:id
Obtains all the loans made by a member by his id Path Variables:
id: member's id to search all the loans made by him
{
"code":200,
"data": [
{
"bookId": 10,
"expiracyDate": "2019-09-07T23:48:00.238Z"
}
]
}
{
"error": {
"code": 404,
"message": "User not found"
}
}
{
"error": {
"code": 401,
"message": "You must be logged in and be an admin to perform this action"
}
}
{
"error": {
"code": 403,
"message": "You must be an admin to perform this action"
}
}
localhost:8000/loans
Creates a new loan in the database
{
"memberId":1,
"bookId":1,
"days":1
}
{
"code":200,
"message": "Loan of book with id {id} created successfully"
}
{
"error": {
"code": 400,
"message": "User 1 has unreturned books"
}
}
{
"error": {
"code": 400,
"message": "Wrong Number of Days"
}
}
{
"error": {
"code": 401,
"message": "You must be logged in and be an admin to perform this action"
}
}
localhost:8000/loans/{id}
Deletes a loan from database
Type | Description | |
---|---|---|
id |
integer | Book's unique identifier |
{
"code":200,
"message": "Loan deleted successfully"
}
{
"error": {
"code": 404,
"message": "loan not found"
}
}
{
"error": {
"code": 401,
"message": "You must be logged in and be an admin to perform this action"
}
}