Skip to content

BackEnd of the Library Project, a REST API made with Express

Notifications You must be signed in to change notification settings

gonzalogrisafi/library_backend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

REST API LIBRARY

REST API that manages a library, it includes books, members and loans management

Installation

Requirements

  • npm
  • node >= 8
  • MySQL ~5.7
 git clone https://github.com/gonzagrisa/library_backend
 cd library_backend
 npm install

Run application

node index.js

Response Status Codes

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

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",
    }
}

Endpoints

Books

Users

Loans

Books

Get /books

localhost:8000/books

Get the list of books in database

Example Response

{
    "code":200,
    "data": [
        {
            "id": 10,
            "title": "Harry Potter",
            "amount": 100
        },
        {
            "id": 20,
            "title": "Lord of the Rings",
            "amount": 10
        }
    ]
}

GET /books/{id}

localhost:8000/books/:id

Gets the book's info by its id

  • Parameters

Type Description
id integer Unique identifier for the object

Example Response

  • 200 OK

{
    "code":200,
    "data": {
        "bookId": 10,
        "title": "Harry Potter",
        "available": 100
    }
}
  • 404 NOT FOUND

{
    "error": {
        "code": 404,
        "message": "Book not found"
    }
}

POST /books

localhost:8000/books

Save a new book in the database

Request Body

{
	"title":"Don Quijote",
	"amount":100
}

Example Response

  • 200 OK

{
    "code":201,
    "message": "Book Added"
}
  • 400 BAD REQUEST

{
    "error": {
        "code": 400,
        "message": "Wrong parameters"
    }
}
  • 401 UNAUTHORIZED

{
    "error": {
        "code": 401,
        "message": "You must be logged in and be an admin to perform this action"
    }
}
  • 403 FORBIDDEN

{
    "error": {
        "code": 403,
        "message": "You must be an admin to perform this action"
    }
}

DELETE /books/{id}

localhost:8000/books/:id

Delete a Book from database by its id

  • Parameters

Type Description
id integer Book's unique identifier

Example Response

  • 200 OK

{
    "code":200,
    "message": "Book deleted"
}
  • 400 BAD REQUEST

{
    "error": {
        "code": 400,
        "message": "Cannot delete the book due to there are borrowed copies"
    }
}
  • 404 NOT FOUND

{
    "error": {
        "code": 404,
        "message": "Book not found"
    }
}
  • 401 UNAUTHORIZED

{
    "error": {
        "code": 401,
        "message": "You must be logged in and be an admin to perform this action"
    }
}
  • 403 FORBIDDEN

{
    "error": {
        "code": 403,
        "message": "You must be an admin to perform this action"
    }
}

PUT /books/{id}

localhost:8000/books/{id}

Update a book's amount of copies by its id

  • Parameters

Type Description
id integer Book's unique identifier

Request Body

{
	"bookId":2,
	"amount":100
}

Example Response

  • 200 OK

{
    "code":200,
    "message": "amount of copies of book with id: {id} updated successfully"
}
  • 404 NOT FOUND

{
    "error": {
        "code": 404,
        "message": "Book not found"
    }
}
  • 403 FORBIDDEN

{
    "error": {
        "code": 403,
        "message": "You must be an admin to perform this action"
    }
}
  • 400 BAD REQUEST

{
    "error": {
        "code": 400,
        "message": "Wrong parameters"
    }
}

GET /users

localhost:8000/users

Obtain a list of the library's users

Example Response

{
    "code":200,
    "data": [
        {
            "id": 1,
            "name": "A"
        },
        {
            "id": 2,
            "name": "B"
        }
    ]
}

GET /users/{id}

localhost:8000/users/:id

Get a user's info by its id

  • Parameters

Type Description
id integer User's unique identifier

Example Response

  • 200 OK

{
    "code":200,
    "data": {
        "id": 1,
        "name": "A"
    }
}
  • 400 BAD REQUEST

{
    "error": {
        "code": 400,
        "message": "Couldn't Log Out"
    }
}

POST /signup

localhost:8000/signup

Create a new user in the database with rol: "USER"

Request Body

{
	"email":"[email protected]",
	"username":"abc",
	"password":"secret"
}

Example Response

{
    "code":200,
    "message": "User Created Successfully"
}

POST /login

localhost:8000/login

Create a session

Request Body

{
    "email": "[email protected]",
    "password": "secret"
}

Example Response

  • 200 OK

{
    "code": 200,
    "userId": 2,
    "rol": "ADMIN",
    "message": "Logged In"
}
  • 400 BAD REQUEST

{
    "error": {
        "code": 400,
        "message": "Incorrect User or Password"
    }
}

POST /logout

localhost:8000/logout

Delete a user's session

Example Response

  • 200 OK

{
    "code":200,
    "message": "Logged Out"
}
  • 400 BAD REQUEST

{
    "error": {
        "code": 400,
        "message": "Couldn't Log Out"
    }
}
  • 500 INTERNAL SERVER ERROR

{
    "error": {
        "code": 400,
        "message": "Error destroying session"
    }
}

GET /signup/checkEmail/ {email}

localhost:8000/signup/checkEmail/{email}

Checks if an email is already in database

  • Parameters

Type Description
email string Email to check if it's available

Example Response

  • 200 OK

{
    "code":200,
    "message": "Email available"
}
  • 400 BAD REQUEST

{
    "error": {
        "code": 400,
        "message": "Email already in use"
    }
}

GET /signup/checkUsername/ {username}

localhost:8000/signup/checkUsername/{username}

Checks if a username is already in database

  • Parameters

Type Description
username string Username to check if it's available

Example Response

  • 200 OK

{
    "code":200,
    "message": "Username available"
}
  • 400 BAD REQUEST

{
    "error": {
        "code": 400,
        "message": "Username already in use"
    }
}

GET /loans

localhost:8000/loans

Get a list of all the currently active loans made

Example Response

  • 200 OK

{
    "code":200,
    "data": [
        {
            "id": 1,
            "memberId": 1,
            "bookId": 10,
            "expiracyDate": 1567900080238
        },
        {
            "id": 2,
            "memberId": 2,
            "bookId": 10,
            "expiracyDate": 1567900080238
        }
    ]
}
  • 401 UNAUTHORIZED

{
    "error": {
        "code": 401,
        "message": "You must be logged in and be an admin to perform this action"
    }
}
  • 403 FORBIDDEN

{
    "error": {
        "code": 403,
        "message": "You must be an admin to perform this action"
    }
}

GET /loans/{id}

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

Example Response

  • 200 OK

{
    "code":200,
    "data": [
        {
            "bookId": 10,
            "expiracyDate": "2019-09-07T23:48:00.238Z"
        }
    ]
}
  • 400 BAD REQUEST

{
    "error": {
        "code": 404,
        "message": "User not found"
    }
}
  • 401 UNAUTHORIZED

{
    "error": {
        "code": 401,
        "message": "You must be logged in and be an admin to perform this action"
    }
}
  • 403 FORBIDDEN

{
    "error": {
        "code": 403,
        "message": "You must be an admin to perform this action"
    }
}

POST /loans/

localhost:8000/loans

Creates a new loan in the database

Request Body

{
	"memberId":1,
	"bookId":1,
	"days":1
}

Example Response

  • 200 OK

{
    "code":200,
    "message": "Loan of book with id {id} created successfully"
}
  • 400 BAD REQUEST

{
    "error": {
        "code": 400,
        "message": "User 1 has unreturned books"
    }
}
{
    "error": {
        "code": 400,
        "message": "Wrong Number of Days"
    }
}
  • 401 UNAUTHORIZED

{
    "error": {
        "code": 401,
        "message": "You must be logged in and be an admin to perform this action"
    }
}

DELETE /loans/{id}

localhost:8000/loans/{id}

Deletes a loan from database

  • Parameters

Type Description
id integer Book's unique identifier

Example Response

  • 200 OK

{
    "code":200,
    "message": "Loan deleted successfully"
}
  • 404 NOT FOUND

{
    "error": {
        "code": 404,
        "message": "loan not found"
    }
}
  • 401 UNAUTHORIZED

{
    "error": {
        "code": 401,
        "message": "You must be logged in and be an admin to perform this action"
    }
}

About

BackEnd of the Library Project, a REST API made with Express

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published