Skip to content

jarDotNet/PostgresEFSample

Repository files navigation

TodoApi - .NET Core Web API with PostgreSQL & FastEndpoints

A complete Todo API built with .NET Core 8, Entity Framework Core, PostgreSQL, and FastEndpoints, containerized with Docker.

Features

  • FastEndpoints - High-performance endpoint-based architecture
  • Vertical Slice Architecture - Feature-based organization
  • Built-in Validation - FluentValidation integration
  • Entity Framework Core with PostgreSQL
  • Database Migrations with automatic application
  • Docker Containerization - Multi-container setup
  • Swagger Documentation - Auto-generated API docs
  • .http File - API testing and exploration with Visual Studio
  • Razor Page served as the Home Page
  • Priority System - Low, Medium, High priorities
  • Filtering & Querying - Filter by status and priority

Architecture

Vertical Slice Structure

Features/Todos/
├── GetTodos/GetTodosEndpoint.cs           # GET /api/todos
├── GetTodoById/GetTodoByIdEndpoint.cs     # GET /api/todos/{id}
├── CreateTodo/CreateTodoEndpoint.cs       # POST /api/todos
├── UpdateTodo/UpdateTodoEndpoint.cs       # PUT /api/todos/{id}
├── CompleteTodo/CompleteTodoEndpoint.cs   # PATCH /api/todos/{id}/complete
└── DeleteTodo/DeleteTodoEndpoint.cs       # DELETE /api/todos/{id}

Each endpoint contains:

  • Request/Response Models - Data contracts
  • Validation Rules - FluentValidation rules
  • Handler Logic - Business logic implementation
  • Configuration - Route, security, and documentation

Quick Start

Using Docker Compose (Recommended)

  1. Clone the repository.
  2. From your project directory, run the application:
    docker-compose up --build
  3. Access the services:
    • API: http://localhost:6060
    • Swagger Documentation: http://localhost:6060//swagger
    • pgAdmin: http://localhost:5050

pgAdmin Setup

  1. Open pgAdmin at http://localhost:5050
  2. Login with:
    • Email: admin@todoapi.com
    • Password: admin123
  3. Add a new server connection:
    • Host: postgres
    • Port: 5432
    • Database: TodoDb
    • Username: postgres
    • Password: postgres123

Local Development

  1. Install .NET 8 SDK
  2. Install PostgreSQL
  3. Update connection string in appsettings.json
  4. If needed, install the dotnet ef tool:
    dotnet tool install --global dotnet-ef
  5. Run migrations:
    dotnet ef database update
    This will create the Migrations folder and apply the initial migration to set up the database schema.
  6. Start the application:
    dotnet run

API Endpoints

Method Endpoint Description
GET /api/todos Get all todos (with filtering)
GET /api/todos/{id} Get todo by ID
POST /api/todos Create new todo
PUT /api/todos/{id} Update todo
PATCH /api/todos/{id}/complete Mark todo as complete
DELETE /api/todos/{id} Delete todo

Query Parameters

  • isCompleted (bool) - Filter by completion status
  • priority (enum) - Filter by priority level

FastEndpoints Benefits

  1. Performance - ~2x faster than traditional controllers
  2. Type Safety - Strongly typed requests/responses
  3. Built-in Validation - Automatic request validation
  4. Minimal Boilerplate - Less code, more functionality
  5. Self-Documenting - Integrated Swagger generation
  6. Testability - Easy unit testing without mocking

Database Schema

The TodoItem entity includes:

  • Id (Primary Key)
  • Title (Required, max 200 chars)
  • Description (Optional, max 1000 chars)
  • IsCompleted (Boolean, default false)
  • CreatedAt (Timestamp)
  • CompletedAt (Nullable timestamp)
  • Priority (Enum: Low=1, Medium=2, High=3)

Environment Variables

  • ConnectionStrings__DefaultConnection - PostgreSQL connection string
  • ASPNETCORE_ENVIRONMENT - Environment (Development/Production)

Development Commands

# Add new migration
dotnet ef migrations add MigrationName

# Update database
dotnet ef database update

# Remove last migration
dotnet ef migrations remove

# Generate SQL script
dotnet ef migrations script