Thank you for considering contributing to the Library Management System! This project uses Express.js, TypeScript, and MongoDB to manage book data. Your contributions help us improve and extend this project, and we welcome all types of contributions.
This project follows the Contributor Covenant Code of Conduct, a widely accepted standard in open-source communities. Please read and adhere to these guidelines to foster a welcoming environment for all contributors.
You can contribute by:
- Adding new features
- Improving existing code
- Fixing bugs
- Updating documentation
- Improving code structure and readability
Follow these steps to get the project up and running on your local machine:
-
Fork and Clone the Repository
git clone https://github.com/your-username/library-management-system.git cd library-management-system -
Install Dependencies
npm install
-
Set Up Environment Variables
cp .env.example .env
Update the
.envfile with your MongoDB connection string and other required variables. -
Start the Development Server
npm run dev
-
Run Tests
npm test
The system implements the following access control:
-
Public Access (no authentication required):
- View list of books (
GET /books) - View single book details (
GET /books/:id) - Search books
- View book comments and ratings (
GET /books/:id/comments)
- View list of books (
-
Admin Access (requires authentication with admin role):
- Create new books (
POST /books) - Update book information (
PUT /books/:id) - Delete books (
DELETE /books/:id) - Manage book categories
- Moderate comments and reviews
- Create new books (
-
User Access (requires authentication with user role):
- Create new comments (
POST /books/:id/comments) - Buy book
- Borrow books
- Create new comments (
-
Authentication System:
- User registration and login endpoints
- JWT-based authentication
- Admin role management
- Password reset functionality
-
Book Management (Admin Only):
- Create new book endpoint (
POST /books)
interface Book { title: string; author: string; isbn: string; publishedYear: number; category: string[]; description: string; }
- Update book endpoint (
PUT /books/:id) - Delete book endpoint (
DELETE /books/:id)
- Create new book endpoint (
-
Public Access Features:
- View book details (
GET /books/:id) - List all books with pagination (
GET /books) - Search and filter books
- View reviews and ratings
- View book details (
-
Review System:
- Add book reviews and ratings
- View reviews for a book
- Moderate reviews (admin only)
-
Create a New Branch
git checkout -b feature/your-feature-name
-
Make Your Changes
- Write clean, maintainable code
- Add appropriate comments
- Follow the style guide
- Include tests for new features
-
Commit Your Changes
git add . git commit -m "feat: Add description of your changes"
Follow Conventional Commits for commit messages.
-
Push to Your Fork
git push origin feature/your-feature-name
-
Submit a Pull Request
- Fill out the pull request template
- Reference any related issues
- Provide a clear description of your changes
- Wait for review and address any feedback
When reporting issues, please include:
- Description: Clear description of the problem
- Steps to Reproduce: Detailed steps to reproduce the issue
- Expected Behavior: What you expected to happen
- Actual Behavior: What actually happened
- Environment Details:
- Node.js version
- npm version
- Operating system
- Browser (if applicable)
- Any relevant configuration
interface User {
id: string;
email: string;
role: 'user' | 'admin';
name: string;
googleId?:string
}
// Authentication middleware
const authenticateUser = async (
req: Request,
res: Response,
next: NextFunction
): Promise<void> => {
try {
const token = req.headers.authorization?.split(' ')[1];
if (!token) {
res.status(401).json({ message: 'Authentication required' });
return;
}
req.user = await verifyToken(token);
next();
} catch (error) {
res.status(401).json({ message: 'Invalid token' });
}
};
// Admin authorization middleware
const requireAdmin = (
req: Request,
res: Response,
next: NextFunction
): void => {
if (req.user?.role !== 'admin') {
res.status(403).json({ message: 'Admin access required' });
return;
}
next();
};
// Protected route example
router.post('/books', authenticateUser, requireAdmin, createBook);/**
* Creates a new book (Admin only)
*/
async function createBook(req: Request, res: Response): Promise<void> {
try {
const book = await BookModel.create(req.body);
res.status(201).json(book);
} catch (error) {
res.status(500).json({ message: 'Internal server error' });
}
}
/**
* Updates a book (Admin only)
*/
async function updateBook(req: Request, res: Response): Promise<void> {
try {
const book = await BookModel.findByIdAndUpdate(
req.params.id,
req.body,
{ new: true }
);
if (!book) {
res.status(404).json({ message: 'Book not found' });
return;
}
res.status(200).json(book);
} catch (error) {
res.status(500).json({ message: 'Internal server error' });
}
}Write tests for both authentication and functionality:
describe('Book API', () => {
describe('POST /books', () => {
it('should require authentication', async () => {
const response = await request(app)
.post('/books')
.send(validBookData);
expect(response.status).toBe(401);
});
it('should require admin role', async () => {
const response = await request(app)
.post('/books')
.set('Authorization', `Bearer ${userToken}`)
.send(validBookData);
expect(response.status).toBe(403);
});
it('should create book when admin authenticated', async () => {
const response = await request(app)
.post('/books')
.set('Authorization', `Bearer ${adminToken}`)
.send(validBookData);
expect(response.status).toBe(201);
});
});
});- Use TypeScript's strict mode
- Define interfaces for all data structures
- Use meaningful variable and function names
- Add type annotations where TypeScript cannot infer types
- Document with JSDoc comments
- Implement proper error handling
- Follow RESTful conventions
- Use appropriate HTTP status codes
If you need help or have questions:
- Check existing issues and documentation
- Contact maintainers through the issue tracker
Thank you for contributing to the Library Management System! Your efforts help make this project better for everyone.
