├── .github └── workflows │ └── gitleaks.yaml ├── .gitignore ├── DotNetCRUD.sln ├── DotNetCRUD ├── Controllers │ └── EmployeesController.cs ├── DotNetCRUD.csproj ├── Models │ └── Employee.cs ├── Program.cs ├── Properties │ └── launchSettings.json ├── Services │ ├── DbService.cs │ ├── EmployeeService.cs │ ├── IDbService.cs │ └── IEmployeeService.cs ├── appsettings.Development.json └── appsettings.json └── README.md /.github/workflows/gitleaks.yaml: -------------------------------------------------------------------------------- 1 | name: Secret Value found!! 2 | on: 3 | push: 4 | public: 5 | jobs: 6 | scan: 7 | name: gitleaks 8 | runs-on: ubuntu-latest 9 | steps: 10 | - name: Checkout 11 | uses: actions/checkout@v3 12 | - name: Install the gitleaks 13 | run: wget https://github.com/zricethezav/gitleaks/releases/download/v8.15.2/gitleaks_8.15.2_linux_x64.tar.gz 14 | shell: pwsh 15 | - name: Extract the tar file 16 | run: tar xzvf gitleaks_8.15.2_linux_x64.tar.gz 17 | - name: Generate the report 18 | id: gitleaks 19 | run: $GITHUB_WORKSPACE/gitleaks detect -s $GITHUB_WORKSPACE -f json -r $GITHUB_WORKSPACE/leaksreport.json 20 | shell: bash 21 | continue-on-error: true 22 | - name: Setup NuGet.exe 23 | if: steps.gitleaks.outcome != 'success' 24 | uses: nuget/setup-nuget@v1 25 | with: 26 | nuget-version: latest 27 | - name: Install the dotnet 28 | if: steps.gitleaks.outcome != 'success' 29 | uses: actions/setup-dotnet@v3 30 | with: 31 | dotnet-version: '3.1.x' 32 | - name: Install the report tool packages 33 | if: steps.gitleaks.outcome != 'success' 34 | run: | 35 | nuget install "Syncfusion.Email" -source ${{ secrets.NexusFeedLink }} -ExcludeVersion 36 | dir $GITHUB_WORKSPACE/Syncfusion.Email/lib/netcoreapp3.1 37 | dotnet $GITHUB_WORKSPACE/Syncfusion.Email/lib/netcoreapp3.1/GitleaksReportMail.dll ${{ secrets.CITEAMCREDENTIALS }} "$GITHUB_REF_NAME" ${{ secrets.NETWORKCREDENTIALS }} ${{ secrets.NETWORKKEY }} "$GITHUB_WORKSPACE" ${{ secrets.ORGANIZATIONNAME }} 38 | exit 1 -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # This .gitignore file was automatically created by Microsoft(R) Visual Studio. 3 | ################################################################################ 4 | 5 | /.vs/DotNetCRUD 6 | /DotNetCRUD/bin/Debug/net6.0 7 | /DotNetCRUD/obj 8 | /.idea/.idea.DotNetCRUD/.idea 9 | -------------------------------------------------------------------------------- /DotNetCRUD.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DotNetCRUD", "DotNetCRUD\DotNetCRUD.csproj", "{40EF040F-6A23-4B4E-8DE8-091D4513B90C}" 4 | EndProject 5 | Global 6 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 7 | Debug|Any CPU = Debug|Any CPU 8 | Release|Any CPU = Release|Any CPU 9 | EndGlobalSection 10 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 11 | {40EF040F-6A23-4B4E-8DE8-091D4513B90C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 12 | {40EF040F-6A23-4B4E-8DE8-091D4513B90C}.Debug|Any CPU.Build.0 = Debug|Any CPU 13 | {40EF040F-6A23-4B4E-8DE8-091D4513B90C}.Release|Any CPU.ActiveCfg = Release|Any CPU 14 | {40EF040F-6A23-4B4E-8DE8-091D4513B90C}.Release|Any CPU.Build.0 = Release|Any CPU 15 | EndGlobalSection 16 | EndGlobal 17 | -------------------------------------------------------------------------------- /DotNetCRUD/Controllers/EmployeesController.cs: -------------------------------------------------------------------------------- 1 | using System.Threading.Tasks; 2 | using DotNetCRUD.Models; 3 | using DotNetCRUD.Services; 4 | using Microsoft.AspNetCore.Mvc; 5 | 6 | namespace DotNetCRUD.Controllers; 7 | 8 | [ApiController] 9 | [Route("[controller]")] 10 | public class EmployeesController : Controller 11 | { 12 | private readonly IEmployeeService _employeeService; 13 | 14 | public EmployeesController(IEmployeeService employeeService) 15 | { 16 | _employeeService = employeeService; 17 | } 18 | 19 | [HttpGet] 20 | public async Task Get() 21 | { 22 | var result = await _employeeService.GetEmployeeList(); 23 | 24 | return Ok(result); 25 | } 26 | 27 | [HttpGet("{id:int}")] 28 | public async Task GetEmployee(int id) 29 | { 30 | var result = await _employeeService.GetEmployee(id); 31 | 32 | return Ok(result); 33 | } 34 | 35 | [HttpPost] 36 | public async Task AddEmployee([FromBody]Employee employee) 37 | { 38 | var result = await _employeeService.CreateEmployee(employee); 39 | 40 | return Ok(result); 41 | } 42 | 43 | [HttpPut] 44 | public async Task UpdateEmployee([FromBody]Employee employee) 45 | { 46 | var result = await _employeeService.UpdateEmployee(employee); 47 | 48 | return Ok(result); 49 | } 50 | 51 | [HttpDelete("{id:int}")] 52 | public async Task DeleteEmployee(int id) 53 | { 54 | var result = await _employeeService.DeleteEmployee(id); 55 | 56 | return Ok(result); 57 | } 58 | } -------------------------------------------------------------------------------- /DotNetCRUD/DotNetCRUD.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net6.0 5 | enable 6 | enable 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /DotNetCRUD/Models/Employee.cs: -------------------------------------------------------------------------------- 1 | namespace DotNetCRUD.Models; 2 | 3 | public class Employee 4 | { 5 | public int Id { get; set; } 6 | public string Name { get; set; } 7 | public int Age { get; set; } 8 | public string Address { get; set; } 9 | public string MobileNumber { get; set; } 10 | } -------------------------------------------------------------------------------- /DotNetCRUD/Program.cs: -------------------------------------------------------------------------------- 1 | using DotNetCRUD.Services; 2 | using Microsoft.AspNetCore.Builder; 3 | using Microsoft.Extensions.DependencyInjection; 4 | using Microsoft.Extensions.Hosting; 5 | 6 | var builder = WebApplication.CreateBuilder(args); 7 | 8 | // Add services to the container. 9 | 10 | builder.Services.AddControllers(); 11 | 12 | // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle 13 | builder.Services.AddEndpointsApiExplorer(); 14 | builder.Services.AddSwaggerGen(); 15 | 16 | builder.Services.AddScoped(); 17 | builder.Services.AddScoped(); 18 | 19 | var app = builder.Build(); 20 | 21 | // Configure the HTTP request pipeline. 22 | if (app.Environment.IsDevelopment()) 23 | { 24 | app.UseSwagger(); 25 | app.UseSwaggerUI(); 26 | } 27 | 28 | app.UseHttpsRedirection(); 29 | 30 | app.UseAuthorization(); 31 | 32 | app.MapControllers(); 33 | 34 | app.Run(); -------------------------------------------------------------------------------- /DotNetCRUD/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://json.schemastore.org/launchsettings.json", 3 | "iisSettings": { 4 | "windowsAuthentication": false, 5 | "anonymousAuthentication": true, 6 | "iisExpress": { 7 | "applicationUrl": "http://localhost:61996", 8 | "sslPort": 44393 9 | } 10 | }, 11 | "profiles": { 12 | "DotNetCRUD": { 13 | "commandName": "Project", 14 | "dotnetRunMessages": true, 15 | "launchBrowser": true, 16 | "launchUrl": "swagger", 17 | "applicationUrl": "https://localhost:7283;http://localhost:5283", 18 | "environmentVariables": { 19 | "ASPNETCORE_ENVIRONMENT": "Development" 20 | } 21 | }, 22 | "IIS Express": { 23 | "commandName": "IISExpress", 24 | "launchBrowser": true, 25 | "launchUrl": "swagger", 26 | "environmentVariables": { 27 | "ASPNETCORE_ENVIRONMENT": "Development" 28 | } 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /DotNetCRUD/Services/DbService.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Data; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | using Dapper; 6 | using Microsoft.Extensions.Configuration; 7 | using Npgsql; 8 | 9 | namespace DotNetCRUD.Services; 10 | 11 | public class DbService : IDbService 12 | { 13 | private readonly IDbConnection _db; 14 | 15 | public DbService(IConfiguration configuration) 16 | { 17 | _db = new NpgsqlConnection(configuration.GetConnectionString("Employeedb")); 18 | } 19 | 20 | public async Task GetAsync(string command, object parms) 21 | { 22 | T result; 23 | 24 | result = (await _db.QueryAsync(command, parms).ConfigureAwait(false)).FirstOrDefault(); 25 | 26 | return result; 27 | 28 | } 29 | 30 | public async Task> GetAll(string command, object parms) 31 | { 32 | 33 | List result = new List(); 34 | 35 | result = (await _db.QueryAsync(command, parms)).ToList(); 36 | 37 | return result; 38 | } 39 | 40 | public async Task EditData(string command, object parms) 41 | { 42 | int result; 43 | 44 | result = await _db.ExecuteAsync(command, parms); 45 | 46 | return result; 47 | } 48 | } -------------------------------------------------------------------------------- /DotNetCRUD/Services/EmployeeService.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Threading.Tasks; 3 | using DotNetCRUD.Models; 4 | 5 | namespace DotNetCRUD.Services; 6 | 7 | public class EmployeeService : IEmployeeService 8 | { 9 | private readonly IDbService _dbService; 10 | 11 | public EmployeeService(IDbService dbService) 12 | { 13 | _dbService = dbService; 14 | } 15 | 16 | public async Task CreateEmployee(Employee employee) 17 | { 18 | var result = 19 | await _dbService.EditData( 20 | "INSERT INTO public.employee (id,name, age, address, mobile_number) VALUES (@Id, @Name, @Age, @Address, @MobileNumber)", 21 | employee); 22 | return true; 23 | } 24 | 25 | public async Task> GetEmployeeList() 26 | { 27 | var employeeList = await _dbService.GetAll("SELECT * FROM public.employee", new { }); 28 | return employeeList; 29 | } 30 | 31 | 32 | public async Task GetEmployee(int id) 33 | { 34 | var employeeList = await _dbService.GetAsync("SELECT * FROM public.employee where id=@id", new {id}); 35 | return employeeList; 36 | } 37 | 38 | public async Task UpdateEmployee(Employee employee) 39 | { 40 | var updateEmployee = 41 | await _dbService.EditData( 42 | "Update public.employee SET name=@Name, age=@Age, address=@Address, mobile_number=@MobileNumber WHERE id=@Id", 43 | employee); 44 | return employee; 45 | } 46 | 47 | public async Task DeleteEmployee(int id) 48 | { 49 | var deleteEmployee = await _dbService.EditData("DELETE FROM public.employee WHERE id=@Id", new {id}); 50 | return true; 51 | } 52 | } -------------------------------------------------------------------------------- /DotNetCRUD/Services/IDbService.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Threading.Tasks; 3 | 4 | namespace DotNetCRUD.Services; 5 | 6 | public interface IDbService 7 | { 8 | Task GetAsync(string command, object parms); 9 | Task> GetAll(string command, object parms ); 10 | Task EditData(string command, object parms); 11 | } -------------------------------------------------------------------------------- /DotNetCRUD/Services/IEmployeeService.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Threading.Tasks; 3 | using DotNetCRUD.Models; 4 | 5 | namespace DotNetCRUD.Services; 6 | 7 | public interface IEmployeeService 8 | { 9 | Task CreateEmployee(Employee employee); 10 | Task GetEmployee(int id); 11 | Task> GetEmployeeList(); 12 | Task UpdateEmployee(Employee employee); 13 | Task DeleteEmployee(int key); 14 | 15 | } -------------------------------------------------------------------------------- /DotNetCRUD/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Information", 5 | "Microsoft.AspNetCore": "Warning" 6 | } 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /DotNetCRUD/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Information", 5 | "Microsoft.AspNetCore": "Warning" 6 | } 7 | }, 8 | "AllowedHosts": "*", 9 | "ConnectionStrings": { 10 | "Employeedb" : "Server=localhost;Database=testdb;User Id=postgres;Password=yohan123;" 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ASP.NET Core Web API with Dapper and PostgreSQL 2 | 3 | This sample explains that how to use Web API with Dapper and PostgreSQL in ASP.NET Core application. 4 | 5 | ## Prerequisites 6 | 7 | * Visual Studio 2022 8 | * Visual Studio Code 9 | 10 | ## How to run this application? 11 | 12 | * Checkout this project to a location in your disk. 13 | * Open the solution file using the Visual Studio 2022. 14 | * Restore the NuGet packages by rebuilding the solution. 15 | * Run the project. --------------------------------------------------------------------------------