# Cortex Mediator Demo API

This project demonstrates the implementation of the [Cortex Mediator](https://www.nuget.org/packages/Cortex.Mediator/) pattern in a .NET API application. Cortex Mediator is an alternative to the popular MediatR library, providing a clean and efficient way to implement the mediator pattern in .NET applications.

## Features

- Cortex Mediator for CQRS (Command Query Responsibility Segregation) implementation
- Pipeline Behaviors for cross-cutting concerns
- Notification Pattern implementation
- Logging integration with Serilog

## Setup

1. Clone the repository
2. Ensure you have .NET 9.0 SDK installed
3. Restore NuGet packages (Build/Rebuild)
4. Run the application
5. Run the request at the BookRequests.http

## Project Structure

The project implements several key components:

### Commands
- `BookPurchaseCommand` - Handles the creation of new books

### Queries
- `BookByIdQuery` - Retrieves book information by ID

### Pipeline Behaviors
- `LoggingPipelineBehavior` - Provides logging for all commands and queries
- `NotificationPipelineBehavior` - Handles notifications for specific commands

### Notification Handlers
- `BookPublisherNotificationHandler` - Demonstrates notification handling

### Pipeline Behaviors
- `LoggingPipelineBehavior` - Provides logging for all commands and queries
- `NotificationPipelineBehavior` - Handles notifications for specific commands

### Notification Handlers
- `BookPublisherNotificationHandler` - Demonstrates notification handling

## Pipeline Behaviors Deep Dive

Pipeline behaviors in Cortex Mediator allow you to implement cross-cutting concerns that execute before and after command/query handling. This project demonstrates two key pipeline behaviors:

### 1. LoggingPipelineBehavior

The logging pipeline behavior wraps both commands and queries, providing consistent logging across all operations:

### 2. Using PublishAsync

In the BookController, notifications are published using the mediator:

```csharp
await _mediator.PublishAsync(query);
```

### Benefits of This Approach

1. **Separation of Concerns**: Pipeline behaviors separate cross-cutting concerns from business logic
2. **Maintainability**: Each behavior has a single responsibility
3. **Reusability**: Behaviors can be applied to multiple commands/queries
4. **Flexibility**: Easy to add or remove behaviors without changing business logic
5. **Testability**: Each component can be tested in isolation

## API Endpoints

### GET /Book/by-id/{id}
Retrieves a book by its ID.

### POST /Book
Creates a new book entry.

Request body example:
```json
{
    "title": "Sample Book",
    "author": "John Doe",
    "publisher": "Sample Publisher",
    "isbn": "123-456-789",
    "year": 2024,
    "language": "English",
    "description": "Sample description",
    "category": "Fiction"
}
```

## How to test it

1. Start the project.
2. Open the BookRequests.http inside the HttpRequestTests.
3. Update the URL to match your localhost.
4. Run the requests.
5. Check the logs inside the Logs file at the root folder of the project.
6. Then look at the handlers for the command, query, pipeline and notification to identify the logs. usually starts with the prefix '##'