Skip to content

The Web API is created with ASP.NET Core and it's connected to a PostgreSQL database and implemented using Code First CRUD Operations

Notifications You must be signed in to change notification settings

merfuradu/web-api-created-asp.net-core-with-database

Repository files navigation

ASP.NET Core Web API with PostgreSQL

Overview

This project is an ASP.NET Core Web API that provides CRUD operations for managing personal data and associated addresses. The backend uses Entity Framework Core (EF Core) to interact with a PostgreSQL database. The database contains two tables: PersonalData and Addresses, which are related through a foreign key relationship.

Features

  • Personal Data Management: Allows users to create, read, update, and delete personal data records.
  • Address Management: Supports operations for handling address records associated with personal data.
  • RESTful API: Follows REST principles, making it easy to integrate with other systems.
  • Entity Framework Core: Utilizes EF Core for database interactions, including querying and updating the PostgreSQL database.
  • Error Handling: Implements error handling for invalid requests, including invalid IDs and missing data.

Project Structure

  • Controllers: Handles incoming HTTP requests and returns appropriate responses.
    • PersonalDataController: Manages operations related to personal data.
  • Models: Defines the data structure used in the database.
    • PersonalData: Represents a person with fields like Name, PhoneNumber, and AddressId.
    • Address: Represents an address with fields like Street, City, State, and PostalCode.
  • Data: Contains the database context which handles communication with the PostgreSQL database.
    • TodoDb: The database context that includes DbSet<PersonalData> and DbSet<Address> for interacting with the respective tables.

Database

PostgreSQL Database

The project uses PostgreSQL as the database. It contains two tables:

  1. PersonalData:

    • Id (Primary Key): Unique identifier for each person.
    • Name: Name of the person.
    • PhoneNumber: Contact number of the person.
    • IsComplete: A boolean indicating if the record is complete.
    • AddressId (Foreign Key): Links to the Address table.
  2. Addresses:

    • Id (Primary Key): Unique identifier for each address.
    • Street: Street information.
    • City: City information.
    • State: State information.
    • PostalCode: Postal code information.

Relationships

  • Each PersonalData entry is associated with one Address through the AddressId foreign key.
  • The Address entity is a navigation property in the PersonalData model, allowing for easy access and manipulation of related address data.

Endpoints

Personal Data Endpoints

  • GET /api/PersonalData/GetPersonalDatas: Retrieves all personal data entries.
  • GET /api/PersonalData/GetTodoById/{id}: Retrieves a specific personal data entry by ID.
  • GET /api/PersonalData/GetPersonalDataByName/{name}: Retrieves personal data entries by name.
  • POST /api/PersonalData/PostTodo: Creates a new personal data entry.
  • PUT /api/PersonalData/PutTodo/{id}: Updates an existing personal data entry by ID.
  • DELETE /api/PersonalData/DeleteTodo/{id}: Deletes a personal data entry by ID.

Address Endpoints

  • GET /api/Addresses/GetAddresses: Retrieves all address entries.
  • GET /api/Addresses/GetAddressById/{id}: Retrieves a specific address by ID.
  • POST /api/Addresses/PostAddress: Creates a new address entry.
  • PUT /api/Addresses/PutAddress/{id}: Updates an existing address entry by ID.
  • DELETE /api/Addresses/DeleteAddress/{id}: Deletes an address entry by ID.

Prerequisites

  • .NET 7 SDK: Required to build and run the API.
  • PostgreSQL: Ensure PostgreSQL is installed and running. You will need to configure the connection string in the appsettings.json file.

Setup

  1. Clone the repository:

    git clone https://github.com/your-repo/your-project.git
    cd your-project
  2. Configure the Database Connection:

    • Open appsettings.json.
    • Update the connection string to point to your PostgreSQL database.
  3. Apply Migrations:

    • Open the terminal in the project directory.
    • Run the following commands to apply migrations and create the database:
      dotnet ef migrations add InitialCreate
      dotnet ef database update
  4. Run the API:

    dotnet run
  5. Test the API:

    • You can use tools like Postman or Swagger UI (available at http://localhost:<port>/swagger) to interact with the API.

Error Handling

  • Invalid ID Handling: Returns a 400 Bad Request if the provided ID is invalid.
  • Not Found: Returns a 404 Not Found if the requested resource does not exist.
  • Bad Request: Returns a 400 Bad Request if the input data is not valid.

Improvements

  • Validation: Add more robust validation rules for input data.
  • Authentication & Authorization: Secure the API by adding authentication and role-based authorization.
  • Logging: Implement logging to track API usage and errors.
  • Pagination: Add pagination for GET requests to handle large datasets efficiently.

License

This project is licensed under the MIT License - see the LICENSE file for details.


This `README.md` file provides a comprehensive overview of your project, including setup instructions, endpoint descriptions, and potential areas for future improvement.

About

The Web API is created with ASP.NET Core and it's connected to a PostgreSQL database and implemented using Code First CRUD Operations

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages