This project is a simple ASP.NET Core Web API that manages a collection of books using Entity Framework Core with PostgreSQL.
The API allows you to retrieve a list of books from a PostgreSQL database. It uses Entity Framework Core for database operations and includes automatic migrations on startup.
- .NET 8.0 SDK or later
- PostgreSQL database server
-
Clone the repository to your local machine.
-
For development, update the connection string in
appsettings.Development.json
:"ConnectionStrings": { "DefaultConnection": "Host=localhost;Database=your_database_name;Username=your_username;Password=your_password" }
Replace
your_database_name
,your_username
, andyour_password
with your PostgreSQL database details. -
Open a terminal in the project directory and run the following commands:
dotnet restore dotnet build dotnet run --environment Development
-
The application will start, automatically apply any pending migrations, and seed the database with sample data.
-
The API will be available at
http://localhost:5073
by default.
- URL:
/api/books
- Method: GET
- Description: Retrieves a list of all books in the database.
- Sample Response:
[ { "id": 1, "title": "1984", "author": "George Orwell", "publicationYear": 1949 }, { "id": 2, "title": "To Kill a Mockingbird", "author": "Harper Lee", "publicationYear": 1960 }, { "id": 3, "title": "The Great Gatsby", "author": "F. Scott Fitzgerald", "publicationYear": 1925 } ]
You can test the API using tools like curl, Postman, or by accessing the Swagger UI at http://localhost:5073/swagger
when running in development mode.
Example curl command:
curl -X GET "http://localhost:5073/api/books" -H "accept: application/json"
Models/Book.cs
: Defines the Book entity.Data/AppDbContext.cs
: Contains the database context and seeding logic.Controllers/BooksController.cs
: Handles the API endpoints.Program.cs
: Configures the application and services.appsettings.json
: Contains application settings, including the database connection string.