Skip to main content

Posts

Showing posts with the label API

A Demo On Clean Architecture | MediatR CQRS Pattern | .NET7 Web API

In this article, we will learn about 'Clean Architecture', 'CQRS Pattern With MediatR' by implementing in .NET7 Web API. Clean Architecture: The Clean Architecture comprises of: Domain Layer Application Layer Infrastructure Layer UI Layer Clean Architecture lives on the dependency inversion principle. In general business logic depends on the data access layer or infrastructure layer. But in clean architecture, it is inverted, which means the data access layer or infrastructure layers depends on the business logic layer(which means the Application Layer) . Domain Layer: the domain layer project will have 'Entities'(Domain Models), 'Domain Services', and 'Value Object'. This project is a parent project and it is independent on all other projects. Application Layer: the application layer project contains business logic files, DTOs, mapper, etc. This project can depend only on the 'Domain Layer Project'. Infrastructure Layer: the infrastruct

Part-7 | .NET 6 Web API | SQL Database | React JS(v18) | CRUD Example

The main objectives of this article are: Create HTTP delete endpoint in API Project. React application to invoke the delete API. Create HTTP Delete Endpoint In API Project: Let's create an HTTP delete endpoint in the API project. API_Project/Controllers/SuperVillainController.cs: [HttpDelete] [Route("{id:int}")] public async Task<IActionResult> Delete(int id) { var villainToDelete = await _reactJSDemoContext.SuperVillain.FindAsync(id); if (villainToDelete == null) { return NotFound(); } _reactJSDemoContext.SuperVillain.Remove(villainToDelete); await _reactJSDemoContext.SaveChangesAsync(); return Ok(); } (Line: 1) The 'HttpDelete' attribute allows us action method can be consumed for only HTTP delete requests. (Line: 2) The 'Route' attribute defined where we need to pass the integer 'id' value. (Line: 5) Based on the 'id' value trying to fetch the record from the database. (Line: 6-9) Checking whether a record exists in the

Part-6 | .NET 6 Web API | SQL Database | React JS(v18) | CRUD Example

The main objectives of this article are: Create Get By Id Endpoint In API Project. Creating Update Endpoint In API Project. Creating A React Component To Show Update Form Create Get By Id Endpoint In API Project: To update any record, we first need to fetch it by its 'id'. So let's create a get id by an endpoint in our API project. API_Project/Controllers/SuperVillainController.cs: [HttpGet] [Route("{id:int}")] public async Task<IActionResult> Get(int id) { var villainById = await _reactJSDemoContext .SuperVillain.Where(_ => _.Id == id) .FirstOrDefaultAsync(); return Ok(villainById); } (Line: 2)Since it is a second HTTP Get Action method, so we have to explicitly specify the route. The route expression '{id:int}' represent 'id' value must be integer value. (Line: 5-7) Fetching the record by 'id' from the database. Create Update Endpoint In API Project: Let's create the HTTP PUT action method. API_Project/Controllers/Supe

Part-5 | .NET 6 Web API | SQL Database | React JS(v18) | CRUD Example

The main objectives of this article are: Create HTTP Post endpoint in API Project. Create React JS component with a form to add a new item Create Post Endpoint Action Method In API Project: Let's create an HTTP Post action method to save a new record into the database. API_Project/Controllers/SuperVillainController.cs: [HttpPost] public async Task<IActionResult> Post(SuperVillain newSuperVillain) { _reactJSDemoContext.SuperVillain.Add(newSuperVillain); await _reactJSDemoContext.SaveChangesAsync(); return Ok(newSuperVillain); } (Line: 1) The 'HttpPost' attribute makes our action method invoke only for HTTP Post requests. (Line: 2) Here our action method should receive the item to add as a payload input parameter. (Line: 4) Our new record data instance adding to the database context. (Line: 5) The 'SaveChangesAsync()' method inserts the new record into the database. Create 'AddSuperVillain' Component In React App: On the 'pages' folder

Part-4 | .NET 6 Web API | SQL Database | React JS(v18) | CRUD Example

The main objectives of this article are: Create an API endpoint to fetch all records Create ReactJS component to display all records Create 'SuperVillain' Controller In API Project: A Controller is an entity or class that contains logical methods or function that gets executed for an HTTP request from the clients. Let's create a controller 'SuperVillianController.cs'. API_Project/Controllers/SuperVillianController.cs: using Microsoft.AspNetCore.Mvc; using ReactDemo.API.Data; namespace ReactDemo.API.Controllers; [ApiController] [Route("[controller]")] public class SuperVillainController : ControllerBase { private readonly ReactJSDemoContext _reactJSDemoContext; public SuperVillainController(ReactJSDemoContext reactJSDemoContext) { _reactJSDemoContext = reactJSDemoContext; } } (Line: 6) The 'ApiController' attribute applies all API rules to the controller. (Line: 7) The 'Route("[controller]")' attribute