Skip to content

bienherasme/Bookstore-API

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Bookstore API

A RESTful API for managing books in a bookstore. This project provides CRUD operations for books and includes authentication and role-based access control.

Features

  • User Authentication: Register and login with JWT-based authentication.
  • Role-Based Access Control:
    • Admin: Can create, update, and delete books.
    • User: Can view the list of books.
  • CRUD Operations for Books:
    • Add new books (Admin only).
    • Update book details (Admin only).
    • Delete books (Admin only).
    • Retrieve all books (All users).

Getting Started

Follow these steps to set up and run the project locally.

Prerequisites

Installation

  1. Clone the repository:

    git clone https://github.com/bienherasme/Bookstore-API.git
    cd bookstore-api
  2. Install dependencies:

    npm install
  3. Create a .env file in the project root and configure the following variables:

    MONGO_URI=your_mongodb_connection_string
    JWT_SECRET=your_jwt_secret
    PORT=5000
  4. Start the server:

    npx nodemon src/app.js
  5. Use Postman or any REST client to interact with the API.

API Endpoints

Authentication

Method Endpoint Description Access
POST /api/auth/register Register a new user Public
POST /api/auth/login Login and get a token Public

Books

Method Endpoint Description Access
GET /api/books Get all books Authenticated
POST /api/books Add a new book Admin Only
PUT /api/books/:id Update a book by ID Admin Only
DELETE /api/books/:id Delete a book by ID Admin Only

Project Structure

bookstore-api/
├── src/
│   ├── controllers/      # Controllers for business logic
│   ├── models/           # Mongoose models for MongoDB
│   ├── routes/           # Route definitions
│   ├── middlewares/      # Custom middleware
│   ├── config/           # Database connection
│   ├── app.js            # Application entry point
├── package.json          # Project metadata and dependencies
├── .env                  # Environment variables
└── README.md             # Project documentation

Authentication & Authorization

  • Token: Users must send a JWT in the Authorization header:
    Authorization: Bearer <token>
    
  • Roles:
    • Admin: Full access (create, update, delete books).
    • User: Limited access (view books only).

Built With

License

This project is licensed under the MIT License. See the LICENSE file for details.

About

A RESTful API for managing books in a bookstore. Features user authentication, role-based access control (admin and user roles), and CRUD operations for books. Built with Node.js, Express, and MongoDB, it demonstrates secure and scalable backend development.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors