Skip to content

thetnaing-dh/RestAPI_ASP.NET_ADO.NET_SQL_CRUD

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rest_API_ADO_NET

A RESTful API for managing blog posts built with ASP.NET Core, ADO.NET, and SQL Server.

Features

  • CRUD Operations: Create, read, update, and delete blog posts
  • Soft Delete: Posts are marked as deleted instead of being physically removed
  • Partial Updates: Support for PATCH requests to update specific fields
  • SQL Server Integration: Uses ADO.NET for database operations
  • RESTful Design: Follows REST conventions with proper HTTP verbs

API Endpoints

GET /api/blogs

Retrieve all active blog posts (excluding deleted ones).

Response:

    json
    [
        {
            "id": 1,
            "title": "Sample Blog",
            "author": "John Doe",
            "content": "Blog content here..."
        }
    ]

POST /api/blogs

Create a new blog post.

Request Body:

    json
    {
        "title": "New Blog",
        "author": "Jane Smith",
        "content": "Blog content..."
    }

Response:

    text
    "Insertion Successful." or "Insertion Failed."

PUT /api/blogs/{id}

Update an entire blog post by ID.

Request Body:

    json
    {
        "title": "Updated Blog",
        "author": "Updated Author",
        "content": "Updated content..."
    }

Response:

    text
    "Updation Successful." or "Updation Failed."

PATCH /api/blogs/{id}

Partially update a blog post by ID.

Request Body:

    json
    {
        "title": "Only update title"
    }

Response:

    text
    "Updating Successful." or "Updating Failed."

DELETE /api/blogs/{id}

Soft delete a blog post by ID (sets DeleteFlag to 1).

Response:

    text
    "Deletion Successful." or "Deletion Failed."

Project Structure

    text
    Rest_API_ADO_NET/
    ├── Controllers/
    │   └── BlogsController.cs      # Main API controller
    ├── Models/
    │   └── BlogDataModel.cs        # Database model
    ├── ViewModels/
    │   └── BlogViewModel.cs        # API request/response model
    └── Program.cs                  # Application entry point

Models

BlogDataModel (Database Model)

  • BlogId - Primary key
  • BlogTitle - Blog post title
  • BlogAuthor - Author name
  • BlogContent - Blog content
  • DeleteFlag - Soft delete indicator (0 = active, 1 = deleted)

BlogViewModel (API Model)

  • Id - Blog identifier
  • Title - Blog title (nullable)
  • Author - Author name (nullable)
  • Content - Blog content (nullable)

Database Setup

The application expects a SQL Server database with the following table:

    sql
    CREATE TABLE Tbl_Blog (
        BlogId INT IDENTITY(1,1) PRIMARY KEY,
        BlogTitle NVARCHAR(255) NOT NULL,
        BlogAuthor NVARCHAR(100) NOT NULL,
        BlogContent NVARCHAR(MAX) NOT NULL,
        DeleteFlag BIT NOT NULL DEFAULT 0
    );

Configuration

Update the connection string in BlogsController.cs:

    csharp
    private readonly string _connectionString = "Server=.;Database=TrainingBatch5;User Id=sa;Password=your_password;TrustServerCertificate=True;";

Prerequisites

  • .NET 6.0 or later
  • SQL Server
  • ASP.NET Core runtime

Installation

  1. Clone the repository:

     bash
     git clone https://github.com/thetnaing-dh/RestAPI_ASP.NET_ADO.NET_SQL_CRUD
     cd Rest_API_ADO_NET
    
  2. Update the database connection string in BlogsController.cs

  3. Run the application:

     bash
     dotnet run
    

Error Handling

The API returns appropriate HTTP status codes:

  • 200 OK - Successful operation
  • 400 Bad Request - Invalid input (e.g., no fields to update in PATCH)
  • 404 Not Found - Blog post not found

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Commit your changes
  4. Push to the branch
  5. Create a Pull Request

License

This project is for educational purposes.

About

A RESTful API for managing blog posts built with ASP.NET Core, ADO.NET, and SQL Server.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages