Skip to content

A secure ASP.NET Core Web API for managing books and users, with full authentication, role-based authorization, and audit logging.

License

Notifications You must be signed in to change notification settings

m-azra3l/LibraryApplication

Repository files navigation

📚 Library Management System

A secure ASP.NET Core Web API for managing books and users, with full authentication, role-based authorization, and audit logging.


📑 Table of Contents


🔎 Overview

This API allows users to register, log in, and perform CRUD operations on books. It supports multiple roles (Reader, Author, Admin) with role-based access controls. All data changes are logged for auditing purposes.


✨ Features

  • User Management: Registration and login with JWT authentication
  • Role-Based Authorization: Reader, Author, Admin roles
  • Book Management: Create, read, update, delete books with validation
  • Audit Logging: Tracks all data modifications with user, timestamp, and changed columns
  • JWT Authentication: Secure token generation and validation
  • Swagger/OpenAPI: Interactive API documentation

🛠 Technologies

  • .NET 9 / ASP.NET Core
  • Entity Framework Core
  • SQL Server
  • JWT (JSON Web Tokens)
  • HMACSHA512 for password hashing
  • Swagger / Scalar UI for API docs

🏗 Architecture

The project follows a layered architecture:

  1. Domain Layer: Entities, DTOs, interfaces, and utilities (User, Book, AuditLog, RoleEnums)
  2. Application Layer: Helpers, extensions, and middlewares (Jwt, AuditEntry, EnumExtension, ExceptionMiddleware)
  3. Infrastructure Layer: Repositories, database context, migrations, filters, and unit of work (AppDbContext, DbInitializer, AuthRepo, BookRepo, AuditRepo)
  4. Presentation Layer: API controllers inheriting from BaseController with reusable authentication helpers

🚀 Getting Started

Prerequisites

Setup

  1. Clone the repository:
    git clone https://github.com/m-azra3l/LibraryApplication.git
  2. Configure database connection:
    Rename .env.sample.env to .env and provide the required information inside the Presentation folder.
  3. Create and apply migrations:
    dotnet-ef migrations add InitialCreate --project Infrastructure --startup-project Presentation
    dotnet-ef database update --project Infrastructure --startup-project Presentation
  4. Run the project:
    cd Presentation
    dotnet run

🔐 Authentication & Authorization

  • JWT tokens are generated on registration and login.
  • Roles are defined in RoleEnums: Reader, Author, Admin.
  • Authorization is enforced via [Authorize] and a custom [Auth] filter.
  • BaseController provides helper methods for accessing user claims:
    • GetUserId()
    • GetName()
    • GetEmail()
    • GetRole()
    • CheckTokenExpires()

📝 Audit Logging

Every database change is tracked in the AuditLogs table:

  • User performing the action
  • Table and record affected
  • Action type (Added, Modified, Deleted)
  • Changed columns, old and new values
  • Timestamp

Audit logging is handled automatically in AppDbContext.SaveChangesAsync.


🗄 Database Initialization

DbInitializer seeds the database with:

  • Users: Test Reader, Test Author, Test Admin
  • Books: Sample books authored by Test Author
  • Passwords hashed using HMACSHA512

🌐 API Endpoints

You can access endpoints via Swagger at /documentations/index.html or Scalar UI at /docs.

Auth

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

Books

  • GET /api/books → Get paginated books
  • GET /api/books/{id} → Get book by ID
  • POST /api/books → Create book (Author only)
  • PUT /api/books/{id} → Update book (Author only)
  • DELETE /api/books/{id} → Delete book (Author/Admin)

Audit Logs

  • GET /api/audits → Get paginated audit logs
  • GET /api/audits/{id} → Get specific audit log
  • DELETE /api/audits/{id} → Delete audit log

🧪 Test Suites

Structure

The solution includes comprehensive test coverage:

  • AuthRepoTests → Validates registration, login, JWT generation, and password hashing.
  • AuthControllerTests → Ensures correct HTTP responses for login/register endpoints.
  • BookRepoTests → Covers CRUD operations, search, pagination, and role-based restrictions.
  • BookControllerTests → Validates controller responses for book operations.
  • AuditRepoTests → Confirms audit logging is triggered correctly.

Tests use:

  • xUnit as the test framework
  • Moq for mocking dependencies
  • EF Core InMemory for repository tests

Running Tests

From the solution root:

dotnet test

Run tests by navigating to the TestProject:

cd TestProject
dotnet test 

Run only a specific test class or method:

dotnet test --filter FullyQualifiedName~BookRepoTests
dotnet test --filter FullyQualifiedName~BookRepoTests.UpdateBook_ShouldReturn200_WhenAuthorUpdatesOwnBook

Run tests continuously while coding:

dotnet watch test

📜 License

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

About

A secure ASP.NET Core Web API for managing books and users, with full authentication, role-based authorization, and audit logging.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published