Skip to content

haider-majid/Book-Store-Management-API

Repository files navigation

Book Store Management API

A comprehensive RESTful API for managing a book store with books, authors, categories, and user authentication.

Features

  • RESTful APIs - Complete CRUD operations for all entities
  • Entity Framework Core - Database-first approach with PostgreSQL
  • Repository Pattern - Clean separation of data access logic
  • AutoMapper - Object-to-object mapping between models and DTOs
  • FluentValidation - Comprehensive input validation
  • JWT Authentication - Secure user authentication and authorization
  • Swagger Documentation - Interactive API documentation
  • CORS Support - Cross-origin resource sharing enabled

Database Schema

Tables

  • Users - User accounts with authentication
  • Categories - Book categories/genres
  • Authors - Book authors with biographical information
  • Books - Book information with relationships to categories and authors

Relationships

  • Books → Categories (Many-to-One)
  • Books → Authors (Many-to-One)
  • Books → Users (Many-to-One) - Track who added the book

Prerequisites

  • .NET 9.0 SDK
  • PostgreSQL Database
  • Your database credentials:
    • Host: localhost
    • Port: 5555
    • Database: bookstore
    • Username: postgres
    • Password: 9900

Setup Instructions

  1. Clone the repository

    git clone <repository-url>
    cd bookapi
  2. Install dependencies

    dotnet restore
  3. Update database connection (if needed)

    • Edit appsettings.json to match your PostgreSQL configuration
  4. Run the application

    dotnet run
  5. Access the API

API Endpoints

Authentication

  • POST /api/auth/register - Register a new user
  • POST /api/auth/login - Login user

Books

  • GET /api/books - Get all books
  • GET /api/books/{id} - Get book by ID
  • POST /api/books - Create new book (requires authentication)
  • PUT /api/books/{id} - Update book (requires authentication)
  • DELETE /api/books/{id} - Delete book (requires authentication)
  • GET /api/books/search?q={term} - Search books
  • GET /api/books/category/{categoryId} - Get books by category
  • GET /api/books/author/{authorId} - Get books by author
  • GET /api/books/price-range?minPrice={min}&maxPrice={max} - Get books by price range

Categories

  • GET /api/categories - Get all categories
  • GET /api/categories/{id} - Get category by ID
  • POST /api/categories - Create new category
  • PUT /api/categories/{id} - Update category
  • DELETE /api/categories/{id} - Delete category

Authors

  • GET /api/authors - Get all authors
  • GET /api/authors/{id} - Get author by ID
  • POST /api/authors - Create new author
  • PUT /api/authors/{id} - Update author
  • DELETE /api/authors/{id} - Delete author

Authentication

The API uses JWT (JSON Web Tokens) for authentication. To access protected endpoints:

  1. Register or login to get a token
  2. Include the token in the Authorization header:
    Authorization: Bearer <your-token>
    

Validation Rules

User Registration

  • Username: 3-50 characters, alphanumeric and underscores only
  • Email: Valid email format, max 100 characters
  • Password: Min 6 characters, must contain uppercase, lowercase, and number
  • First/Last Name: Optional, letters and spaces only

Book Creation/Update

  • Title: Required, 2-200 characters
  • Price: Required, greater than 0, less than 10,000
  • ISBN: Optional, alphanumeric with hyphens and X
  • Publication Year: Optional, between 1800 and current year
  • Pages: Optional, greater than 0, less than 10,000
  • Category and Author IDs: Required, must exist in database

Category Creation/Update

  • Name: Required, 2-100 characters, unique
  • Description: Optional, max 500 characters

Author Creation/Update

  • Full Name: Required, 2-100 characters, letters and spaces only
  • Bio: Optional, max 1000 characters
  • Date of Birth: Optional, cannot be in the future
  • Nationality: Optional, max 50 characters, letters and spaces only

Project Structure

bookapi/
├── Controllers/          # API Controllers
├── Data/                # Entity Framework DbContext
├── DTOs/                # Data Transfer Objects
├── Models/              # Entity Models
├── Repositories/        # Repository Pattern Implementation
├── Services/            # Business Logic Services
├── Validators/          # FluentValidation Rules
├── wwwroot/             # Static Files
├── Program.cs           # Application Entry Point
├── appsettings.json     # Configuration
└── README.md           # This file

Technologies Used

  • .NET 9.0 - Latest .NET framework
  • Entity Framework Core - ORM for database operations
  • PostgreSQL - Relational database
  • AutoMapper - Object mapping
  • FluentValidation - Input validation
  • JWT Bearer - Authentication
  • Swagger/OpenAPI - API documentation
  • Repository Pattern - Data access abstraction

Learning Outcomes

This project demonstrates:

  • RESTful API design principles
  • Entity Framework Core with Fluent API
  • Repository pattern implementation
  • DTOs and AutoMapper usage
  • Comprehensive validation with FluentValidation
  • JWT authentication implementation
  • Swagger documentation setup
  • Clean architecture principles

Contributing

Feel free to contribute to this project by:

  • Adding new features
  • Improving validation rules
  • Enhancing error handling
  • Adding unit tests
  • Improving documentation

License

This project is for educational purposes.

About

A comprehensive RESTful API for managing a book store with books, authors, categories, and user authentication.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages