# Library Management System (LMS)

A full-stack Library Management System built with **React + Tailwind CSS**, **Node.js/Express**, and **MySQL**, featuring JWT authentication, CRUD operations, and report generation.

## Features

- **Authentication** — Register & login with JWT tokens
- **Books** — Full CRUD (Create, Read, Update, Delete)
- **Members** — Manage library members
- **Borrowings** — Issue books, return books, automatic fine calculation
- **Reports** — Inventory, borrowings, overdue, and members reports (JSON + PDF export)
- **Dashboard** — Overview stats and recent activity

## Tech Stack

| Layer    | Technology                          |
|----------|-------------------------------------|
| Frontend | React 18, Vite, Tailwind CSS, Axios |
| Backend  | Node.js, Express.js                 |
| Database | MySQL                               |
| Auth     | JWT (jsonwebtoken), bcryptjs        |

## Prerequisites

- [Node.js](https://nodejs.org/) v18+
- [MySQL](https://dev.mysql.com/downloads/) v8+

## Setup

### 1. Database

Create the database and tables:

```bash
mysql -u root -p < backend/database/schema.sql
```

### 2. Backend

```bash
cd backend
cp .env.example .env
```

Edit `.env` with your MySQL credentials:

```env
PORT=5000
DB_HOST=localhost
DB_USER=root
DB_PASSWORD=your_password
DB_NAME=lms_db
JWT_SECRET=your_super_secret_jwt_key_change_this
JWT_EXPIRES_IN=7d
```

Install and start:

```bash
npm install
npm run dev
```

Backend runs at **http://localhost:5000**

### 3. Frontend

```bash
cd frontend
npm install
npm run dev
```

Frontend runs at **http://localhost:3000**

## API Endpoints

### Auth (Public)
| Method | Endpoint           | Description     |
|--------|--------------------|-----------------|
| POST   | /api/auth/register | Register user   |
| POST   | /api/auth/login    | Login           |
| GET    | /api/auth/me       | Current user    |

### Books (Protected)
| Method | Endpoint         | Description   |
|--------|------------------|---------------|
| GET    | /api/books       | List books    |
| POST   | /api/books       | Create book   |
| PUT    | /api/books/:id   | Update book   |
| DELETE | /api/books/:id   | Delete book   |

### Members (Protected)
| Method | Endpoint           | Description     |
|--------|--------------------|-----------------|
| GET    | /api/members       | List members    |
| POST   | /api/members       | Create member   |
| PUT    | /api/members/:id   | Update member   |
| DELETE | /api/members/:id   | Delete member   |

### Borrowings (Protected)
| Method | Endpoint                    | Description    |
|--------|-----------------------------|----------------|
| GET    | /api/borrowings             | List borrowings|
| POST   | /api/borrowings             | Issue book     |
| PUT    | /api/borrowings/:id/return  | Return book    |
| DELETE | /api/borrowings/:id         | Delete record  |

### Reports (Protected)
| Method | Endpoint                    | Description         |
|--------|-----------------------------|---------------------|
| GET    | /api/reports/dashboard      | Dashboard stats     |
| GET    | /api/reports/inventory      | Inventory report    |
| GET    | /api/reports/borrowings     | Borrowings report   |
| GET    | /api/reports/overdue        | Overdue report      |
| GET    | /api/reports/members        | Members report      |
| GET    | /api/reports/pdf/inventory  | PDF inventory       |
| GET    | /api/reports/pdf/borrowings | PDF borrowings      |
| GET    | /api/reports/pdf/overdue    | PDF overdue         |

## Project Structure

```
LMS/
├── backend/
│   ├── config/db.js
│   ├── database/schema.sql
│   ├── middleware/auth.js
│   ├── routes/
│   │   ├── auth.js
│   │   ├── books.js
│   │   ├── members.js
│   │   ├── borrowings.js
│   │   └── reports.js
│   ├── server.js
│   └── package.json
├── frontend/
│   ├── src/
│   │   ├── components/
│   │   ├── context/AuthContext.jsx
│   │   ├── pages/
│   │   ├── services/api.js
│   │   ├── App.jsx
│   │   └── main.jsx
│   └── package.json
└── README.md
```

## Usage

1. Open **http://localhost:3000**
2. Click **Register** to create an account
3. Log in and use the sidebar to navigate:
   - **Dashboard** — Overview
   - **Books** — Manage book catalog
   - **Members** — Manage library members
   - **Borrowings** — Issue and return books
   - **Reports** — Generate and download reports

## License

MIT
