├── .gitignore ├── LICENSE ├── MyApp.Api ├── MyApp.Api.sln ├── MyApp.Api │ ├── Controllers │ │ ├── EmployeesController.cs │ │ └── ExternalVendorController.cs │ ├── DependencyInjection.cs │ ├── MyApp.Api.csproj │ ├── MyApp.Api.http │ ├── Program.cs │ ├── Properties │ │ └── launchSettings.json │ ├── appsettings.Development.json │ └── appsettings.json ├── MyApp.Application │ ├── Commands │ │ ├── AddEmployeeCommand.cs │ │ ├── DeleteEmployeeCommand.cs │ │ └── UpdateEmployeeCommand.cs │ ├── DependencyInjection.cs │ ├── Events │ │ ├── SendEmailEventHandler.cs │ │ ├── StartMembershipEventHandler.cs │ │ └── UserCreatedEvent.cs │ ├── MyApp.Application.csproj │ └── Queries │ │ ├── GetAllEmployeesQuery.cs │ │ ├── GetCoinkdeskDataQuery.cs │ │ ├── GetEmployeeByIdQuery.cs │ │ └── GetJokeQuery.cs ├── MyApp.Core │ ├── DependencyInjection.cs │ ├── Entities │ │ └── EmployeeEntity.cs │ ├── Interfaces │ │ ├── IEmployeeRepository.cs │ │ └── IExternalVendorRepository.cs │ ├── Models │ │ ├── CoindeskData.cs │ │ └── JokeModel.cs │ ├── MyApp.Core.csproj │ └── Options │ │ └── ConnectionStringOptions.cs └── MyApp.Infrastructure │ ├── Data │ └── AppDbContext.cs │ ├── DependencyInjection.cs │ ├── Migrations │ ├── 20240710143212_dbinit.Designer.cs │ ├── 20240710143212_dbinit.cs │ └── AppDbContextModelSnapshot.cs │ ├── MyApp.Infrastructure.csproj │ ├── Repositories │ ├── EmployeeRepository.cs │ └── ExternalVendorRepository.cs │ └── Services │ ├── CoindeskHttpClientService.cs │ ├── ICoindeskHttpClientService.cs │ ├── IJokeHttpClientService.cs │ └── JokeHttpClientService.cs └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/main/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Mono auto generated files 17 | mono_crash.* 18 | 19 | # Build results 20 | [Dd]ebug/ 21 | [Dd]ebugPublic/ 22 | [Rr]elease/ 23 | [Rr]eleases/ 24 | x64/ 25 | x86/ 26 | [Ww][Ii][Nn]32/ 27 | [Aa][Rr][Mm]/ 28 | [Aa][Rr][Mm]64/ 29 | bld/ 30 | [Bb]in/ 31 | [Oo]bj/ 32 | [Ll]og/ 33 | [Ll]ogs/ 34 | 35 | # Visual Studio 2015/2017 cache/options directory 36 | .vs/ 37 | # Uncomment if you have tasks that create the project's static files in wwwroot 38 | #wwwroot/ 39 | 40 | # Visual Studio 2017 auto generated files 41 | Generated\ Files/ 42 | 43 | # MSTest test Results 44 | [Tt]est[Rr]esult*/ 45 | [Bb]uild[Ll]og.* 46 | 47 | # NUnit 48 | *.VisualState.xml 49 | TestResult.xml 50 | nunit-*.xml 51 | 52 | # Build Results of an ATL Project 53 | [Dd]ebugPS/ 54 | [Rr]eleasePS/ 55 | dlldata.c 56 | 57 | # Benchmark Results 58 | BenchmarkDotNet.Artifacts/ 59 | 60 | # .NET Core 61 | project.lock.json 62 | project.fragment.lock.json 63 | artifacts/ 64 | 65 | # ASP.NET Scaffolding 66 | ScaffoldingReadMe.txt 67 | 68 | # StyleCop 69 | StyleCopReport.xml 70 | 71 | # Files built by Visual Studio 72 | *_i.c 73 | *_p.c 74 | *_h.h 75 | *.ilk 76 | *.meta 77 | *.obj 78 | *.iobj 79 | *.pch 80 | *.pdb 81 | *.ipdb 82 | *.pgc 83 | *.pgd 84 | *.rsp 85 | *.sbr 86 | *.tlb 87 | *.tli 88 | *.tlh 89 | *.tmp 90 | *.tmp_proj 91 | *_wpftmp.csproj 92 | *.log 93 | *.tlog 94 | *.vspscc 95 | *.vssscc 96 | .builds 97 | *.pidb 98 | *.svclog 99 | *.scc 100 | 101 | # Chutzpah Test files 102 | _Chutzpah* 103 | 104 | # Visual C++ cache files 105 | ipch/ 106 | *.aps 107 | *.ncb 108 | *.opendb 109 | *.opensdf 110 | *.sdf 111 | *.cachefile 112 | *.VC.db 113 | *.VC.VC.opendb 114 | 115 | # Visual Studio profiler 116 | *.psess 117 | *.vsp 118 | *.vspx 119 | *.sap 120 | 121 | # Visual Studio Trace Files 122 | *.e2e 123 | 124 | # TFS 2012 Local Workspace 125 | $tf/ 126 | 127 | # Guidance Automation Toolkit 128 | *.gpState 129 | 130 | # ReSharper is a .NET coding add-in 131 | _ReSharper*/ 132 | *.[Rr]e[Ss]harper 133 | *.DotSettings.user 134 | 135 | # TeamCity is a build add-in 136 | _TeamCity* 137 | 138 | # DotCover is a Code Coverage Tool 139 | *.dotCover 140 | 141 | # AxoCover is a Code Coverage Tool 142 | .axoCover/* 143 | !.axoCover/settings.json 144 | 145 | # Coverlet is a free, cross platform Code Coverage Tool 146 | coverage*.json 147 | coverage*.xml 148 | coverage*.info 149 | 150 | # Visual Studio code coverage results 151 | *.coverage 152 | *.coveragexml 153 | 154 | # NCrunch 155 | _NCrunch_* 156 | .*crunch*.local.xml 157 | nCrunchTemp_* 158 | 159 | # MightyMoose 160 | *.mm.* 161 | AutoTest.Net/ 162 | 163 | # Web workbench (sass) 164 | .sass-cache/ 165 | 166 | # Installshield output folder 167 | [Ee]xpress/ 168 | 169 | # DocProject is a documentation generator add-in 170 | DocProject/buildhelp/ 171 | DocProject/Help/*.HxT 172 | DocProject/Help/*.HxC 173 | DocProject/Help/*.hhc 174 | DocProject/Help/*.hhk 175 | DocProject/Help/*.hhp 176 | DocProject/Help/Html2 177 | DocProject/Help/html 178 | 179 | # Click-Once directory 180 | publish/ 181 | 182 | # Publish Web Output 183 | *.[Pp]ublish.xml 184 | *.azurePubxml 185 | # Note: Comment the next line if you want to checkin your web deploy settings, 186 | # but database connection strings (with potential passwords) will be unencrypted 187 | *.pubxml 188 | *.publishproj 189 | 190 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 191 | # checkin your Azure Web App publish settings, but sensitive information contained 192 | # in these scripts will be unencrypted 193 | PublishScripts/ 194 | 195 | # NuGet Packages 196 | *.nupkg 197 | # NuGet Symbol Packages 198 | *.snupkg 199 | # The packages folder can be ignored because of Package Restore 200 | **/[Pp]ackages/* 201 | # except build/, which is used as an MSBuild target. 202 | !**/[Pp]ackages/build/ 203 | # Uncomment if necessary however generally it will be regenerated when needed 204 | #!**/[Pp]ackages/repositories.config 205 | # NuGet v3's project.json files produces more ignorable files 206 | *.nuget.props 207 | *.nuget.targets 208 | 209 | # Microsoft Azure Build Output 210 | csx/ 211 | *.build.csdef 212 | 213 | # Microsoft Azure Emulator 214 | ecf/ 215 | rcf/ 216 | 217 | # Windows Store app package directories and files 218 | AppPackages/ 219 | BundleArtifacts/ 220 | Package.StoreAssociation.xml 221 | _pkginfo.txt 222 | *.appx 223 | *.appxbundle 224 | *.appxupload 225 | 226 | # Visual Studio cache files 227 | # files ending in .cache can be ignored 228 | *.[Cc]ache 229 | # but keep track of directories ending in .cache 230 | !?*.[Cc]ache/ 231 | 232 | # Others 233 | ClientBin/ 234 | ~$* 235 | *~ 236 | *.dbmdl 237 | *.dbproj.schemaview 238 | *.jfm 239 | *.pfx 240 | *.publishsettings 241 | orleans.codegen.cs 242 | 243 | # Including strong name files can present a security risk 244 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 245 | #*.snk 246 | 247 | # Since there are multiple workflows, uncomment next line to ignore bower_components 248 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 249 | #bower_components/ 250 | 251 | # RIA/Silverlight projects 252 | Generated_Code/ 253 | 254 | # Backup & report files from converting an old project file 255 | # to a newer Visual Studio version. Backup files are not needed, 256 | # because we have git ;-) 257 | _UpgradeReport_Files/ 258 | Backup*/ 259 | UpgradeLog*.XML 260 | UpgradeLog*.htm 261 | ServiceFabricBackup/ 262 | *.rptproj.bak 263 | 264 | # SQL Server files 265 | *.mdf 266 | *.ldf 267 | *.ndf 268 | 269 | # Business Intelligence projects 270 | *.rdl.data 271 | *.bim.layout 272 | *.bim_*.settings 273 | *.rptproj.rsuser 274 | *- [Bb]ackup.rdl 275 | *- [Bb]ackup ([0-9]).rdl 276 | *- [Bb]ackup ([0-9][0-9]).rdl 277 | 278 | # Microsoft Fakes 279 | FakesAssemblies/ 280 | 281 | # GhostDoc plugin setting file 282 | *.GhostDoc.xml 283 | 284 | # Node.js Tools for Visual Studio 285 | .ntvs_analysis.dat 286 | node_modules/ 287 | 288 | # Visual Studio 6 build log 289 | *.plg 290 | 291 | # Visual Studio 6 workspace options file 292 | *.opt 293 | 294 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 295 | *.vbw 296 | 297 | # Visual Studio 6 auto-generated project file (contains which files were open etc.) 298 | *.vbp 299 | 300 | # Visual Studio 6 workspace and project file (working project files containing files to include in project) 301 | *.dsw 302 | *.dsp 303 | 304 | # Visual Studio 6 technical files 305 | *.ncb 306 | *.aps 307 | 308 | # Visual Studio LightSwitch build output 309 | **/*.HTMLClient/GeneratedArtifacts 310 | **/*.DesktopClient/GeneratedArtifacts 311 | **/*.DesktopClient/ModelManifest.xml 312 | **/*.Server/GeneratedArtifacts 313 | **/*.Server/ModelManifest.xml 314 | _Pvt_Extensions 315 | 316 | # Paket dependency manager 317 | .paket/paket.exe 318 | paket-files/ 319 | 320 | # FAKE - F# Make 321 | .fake/ 322 | 323 | # CodeRush personal settings 324 | .cr/personal 325 | 326 | # Python Tools for Visual Studio (PTVS) 327 | __pycache__/ 328 | *.pyc 329 | 330 | # Cake - Uncomment if you are using it 331 | # tools/** 332 | # !tools/packages.config 333 | 334 | # Tabs Studio 335 | *.tss 336 | 337 | # Telerik's JustMock configuration file 338 | *.jmconfig 339 | 340 | # BizTalk build output 341 | *.btp.cs 342 | *.btm.cs 343 | *.odx.cs 344 | *.xsd.cs 345 | 346 | # OpenCover UI analysis results 347 | OpenCover/ 348 | 349 | # Azure Stream Analytics local run output 350 | ASALocalRun/ 351 | 352 | # MSBuild Binary and Structured Log 353 | *.binlog 354 | 355 | # NVidia Nsight GPU debugger configuration file 356 | *.nvuser 357 | 358 | # MFractors (Xamarin productivity tool) working folder 359 | .mfractor/ 360 | 361 | # Local History for Visual Studio 362 | .localhistory/ 363 | 364 | # Visual Studio History (VSHistory) files 365 | .vshistory/ 366 | 367 | # BeatPulse healthcheck temp database 368 | healthchecksdb 369 | 370 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 371 | MigrationBackup/ 372 | 373 | # Ionide (cross platform F# VS Code tools) working folder 374 | .ionide/ 375 | 376 | # Fody - auto-generated XML schema 377 | FodyWeavers.xsd 378 | 379 | # VS Code files for those working on multiple tools 380 | .vscode/* 381 | !.vscode/settings.json 382 | !.vscode/tasks.json 383 | !.vscode/launch.json 384 | !.vscode/extensions.json 385 | *.code-workspace 386 | 387 | # Local History for Visual Studio Code 388 | .history/ 389 | 390 | # Windows Installer files from build outputs 391 | *.cab 392 | *.msi 393 | *.msix 394 | *.msm 395 | *.msp 396 | 397 | # JetBrains Rider 398 | *.sln.iml 399 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 Nitish Kaushik 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /MyApp.Api/MyApp.Api.sln: -------------------------------------------------------------------------------- 1 | Microsoft Visual Studio Solution File, Format Version 12.00 2 | # Visual Studio Version 17 3 | VisualStudioVersion = 17.11.35017.193 4 | MinimumVisualStudioVersion = 10.0.40219.1 5 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MyApp.Api", "MyApp.Api\MyApp.Api.csproj", "{55F55C0B-20AB-4580-95DB-C16E0458225A}" 6 | EndProject 7 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MyApp.Application", "MyApp.Application\MyApp.Application.csproj", "{E9768FD4-AFC7-4DB3-B2D2-0E17A1ADC1E6}" 8 | EndProject 9 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MyApp.Core", "MyApp.Core\MyApp.Core.csproj", "{70659097-65F4-4014-967B-FE68A65D3619}" 10 | EndProject 11 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MyApp.Infrastructure", "MyApp.Infrastructure\MyApp.Infrastructure.csproj", "{99578FB1-93C2-4CD6-AB33-5E1FA7A11672}" 12 | EndProject 13 | Global 14 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 15 | Debug|Any CPU = Debug|Any CPU 16 | Release|Any CPU = Release|Any CPU 17 | EndGlobalSection 18 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 19 | {55F55C0B-20AB-4580-95DB-C16E0458225A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 20 | {55F55C0B-20AB-4580-95DB-C16E0458225A}.Debug|Any CPU.Build.0 = Debug|Any CPU 21 | {55F55C0B-20AB-4580-95DB-C16E0458225A}.Release|Any CPU.ActiveCfg = Release|Any CPU 22 | {55F55C0B-20AB-4580-95DB-C16E0458225A}.Release|Any CPU.Build.0 = Release|Any CPU 23 | {E9768FD4-AFC7-4DB3-B2D2-0E17A1ADC1E6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 24 | {E9768FD4-AFC7-4DB3-B2D2-0E17A1ADC1E6}.Debug|Any CPU.Build.0 = Debug|Any CPU 25 | {E9768FD4-AFC7-4DB3-B2D2-0E17A1ADC1E6}.Release|Any CPU.ActiveCfg = Release|Any CPU 26 | {E9768FD4-AFC7-4DB3-B2D2-0E17A1ADC1E6}.Release|Any CPU.Build.0 = Release|Any CPU 27 | {70659097-65F4-4014-967B-FE68A65D3619}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 28 | {70659097-65F4-4014-967B-FE68A65D3619}.Debug|Any CPU.Build.0 = Debug|Any CPU 29 | {70659097-65F4-4014-967B-FE68A65D3619}.Release|Any CPU.ActiveCfg = Release|Any CPU 30 | {70659097-65F4-4014-967B-FE68A65D3619}.Release|Any CPU.Build.0 = Release|Any CPU 31 | {99578FB1-93C2-4CD6-AB33-5E1FA7A11672}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 32 | {99578FB1-93C2-4CD6-AB33-5E1FA7A11672}.Debug|Any CPU.Build.0 = Debug|Any CPU 33 | {99578FB1-93C2-4CD6-AB33-5E1FA7A11672}.Release|Any CPU.ActiveCfg = Release|Any CPU 34 | {99578FB1-93C2-4CD6-AB33-5E1FA7A11672}.Release|Any CPU.Build.0 = Release|Any CPU 35 | EndGlobalSection 36 | GlobalSection(SolutionProperties) = preSolution 37 | HideSolutionNode = FALSE 38 | EndGlobalSection 39 | GlobalSection(ExtensibilityGlobals) = postSolution 40 | SolutionGuid = {C47B85F4-F88E-4394-ABB7-747A993D7312} 41 | EndGlobalSection 42 | EndGlobal 43 | -------------------------------------------------------------------------------- /MyApp.Api/MyApp.Api/Controllers/EmployeesController.cs: -------------------------------------------------------------------------------- 1 | using MediatR; 2 | using Microsoft.AspNetCore.Http; 3 | using Microsoft.AspNetCore.Mvc; 4 | using MyApp.Application.Commands; 5 | using MyApp.Application.Queries; 6 | using MyApp.Core.Entities; 7 | 8 | namespace MyApp.Api.Controllers 9 | { 10 | [Route("api/[controller]")] 11 | [ApiController] 12 | public class EmployeesController(ISender sender) : ControllerBase 13 | { 14 | [HttpPost("")] 15 | public async Task AddEmployeeAsync([FromBody] EmployeeEntity employee) 16 | { 17 | var result = await sender.Send(new AddEmployeeCommand(employee)); 18 | return Ok(result); 19 | } 20 | 21 | [HttpGet("")] 22 | public async Task GetAllEmployeesAsync() 23 | { 24 | var result = await sender.Send(new GetAllEmployeesQuery()); 25 | return Ok(result); 26 | } 27 | 28 | [HttpGet("{employeeId}")] 29 | public async Task GetEmployeeByIdAsync([FromRoute] Guid employeeId) 30 | { 31 | var result = await sender.Send(new GetEmployeeByIdQuery(employeeId)); 32 | return Ok(result); 33 | } 34 | 35 | [HttpPut("{employeeId}")] 36 | public async Task UpdateEmployeeAsync([FromRoute] Guid employeeId, [FromBody] EmployeeEntity employee) 37 | { 38 | var result = await sender.Send(new UpdateEmployeeCommand(employeeId, employee)); 39 | return Ok(result); 40 | } 41 | 42 | [HttpDelete("{employeeId}")] 43 | public async Task DeleteEmployeeAsync([FromRoute] Guid employeeId) 44 | { 45 | var result = await sender.Send(new DeleteEmployeeCommand(employeeId)); 46 | return Ok(result); 47 | } 48 | } 49 | 50 | 51 | } 52 | -------------------------------------------------------------------------------- /MyApp.Api/MyApp.Api/Controllers/ExternalVendorController.cs: -------------------------------------------------------------------------------- 1 | using MediatR; 2 | using Microsoft.AspNetCore.Http; 3 | using Microsoft.AspNetCore.Mvc; 4 | using MyApp.Application.Commands; 5 | using MyApp.Application.Queries; 6 | using MyApp.Core.Entities; 7 | 8 | namespace MyApp.Api.Controllers 9 | { 10 | [Route("api/[controller]")] 11 | [ApiController] 12 | public class ExternalVendorController(ISender sender) : ControllerBase 13 | { 14 | [HttpGet("")] 15 | public async Task GetCoindeskData() 16 | { 17 | var result = await sender.Send(new GetCoinkdeskDataQuery()); 18 | return Ok(result); 19 | } 20 | 21 | [HttpGet("joke")] 22 | public async Task GetJoke() 23 | { 24 | var result = await sender.Send(new GetJokeQuery()); 25 | return Ok(result); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /MyApp.Api/MyApp.Api/DependencyInjection.cs: -------------------------------------------------------------------------------- 1 | using MyApp.Application; 2 | using MyApp.Infrastructure; 3 | using MyApp.Core; 4 | 5 | namespace MyApp.Api 6 | { 7 | public static class DependencyInjection 8 | { 9 | public static IServiceCollection AddAppDI(this IServiceCollection services, IConfiguration configuration) 10 | { 11 | services.AddApplicationDI() 12 | .AddInfrastructureDI() 13 | .AddCoreDI(configuration); 14 | 15 | return services; 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /MyApp.Api/MyApp.Api/MyApp.Api.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net8.0 5 | enable 6 | enable 7 | 8 | 9 | 10 | 11 | all 12 | runtime; build; native; contentfiles; analyzers; buildtransitive 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /MyApp.Api/MyApp.Api/MyApp.Api.http: -------------------------------------------------------------------------------- 1 | @HostAddress = https://localhost:7199 2 | 3 | ### 4 | 5 | 6 | 7 | ### 8 | 9 | GET {{HostAddress}}/api/employees/employees 10 | 11 | ### 12 | 13 | GET {{HostAddress}}/api/employees/employees/{employeeId} 14 | 15 | ### 16 | -------------------------------------------------------------------------------- /MyApp.Api/MyApp.Api/Program.cs: -------------------------------------------------------------------------------- 1 | using MyApp.Api; 2 | 3 | var builder = WebApplication.CreateBuilder(args); 4 | 5 | // Add services to the container. 6 | 7 | builder.Services.AddControllers(); 8 | // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle 9 | builder.Services.AddEndpointsApiExplorer(); 10 | builder.Services.AddSwaggerGen(); 11 | 12 | //builder.Services.Configure(builder.Configuration.GetSection(ConnectionStringOptions.SectionName)); 13 | 14 | builder.Services.AddAppDI(builder.Configuration); 15 | 16 | var app = builder.Build(); 17 | 18 | // Configure the HTTP request pipeline. 19 | if (app.Environment.IsDevelopment()) 20 | { 21 | app.UseSwagger(); 22 | app.UseSwaggerUI(); 23 | } 24 | 25 | app.UseHttpsRedirection(); 26 | 27 | app.UseAuthorization(); 28 | 29 | app.MapControllers(); 30 | 31 | app.Run(); 32 | -------------------------------------------------------------------------------- /MyApp.Api/MyApp.Api/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json.schemastore.org/launchsettings.json", 3 | "iisSettings": { 4 | "windowsAuthentication": false, 5 | "anonymousAuthentication": true, 6 | "iisExpress": { 7 | "applicationUrl": "http://localhost:52728", 8 | "sslPort": 44345 9 | } 10 | }, 11 | "profiles": { 12 | "http": { 13 | "commandName": "Project", 14 | "dotnetRunMessages": true, 15 | "launchBrowser": true, 16 | "launchUrl": "swagger", 17 | "applicationUrl": "http://localhost:5112", 18 | "environmentVariables": { 19 | "ASPNETCORE_ENVIRONMENT": "Development" 20 | } 21 | }, 22 | "https": { 23 | "commandName": "Project", 24 | "dotnetRunMessages": true, 25 | "launchBrowser": true, 26 | "launchUrl": "swagger", 27 | "applicationUrl": "https://localhost:7199;http://localhost:5112", 28 | "environmentVariables": { 29 | "ASPNETCORE_ENVIRONMENT": "Development" 30 | } 31 | }, 32 | "IIS Express": { 33 | "commandName": "IISExpress", 34 | "launchBrowser": true, 35 | "launchUrl": "swagger", 36 | "environmentVariables": { 37 | "ASPNETCORE_ENVIRONMENT": "Development" 38 | } 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /MyApp.Api/MyApp.Api/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Information", 5 | "Microsoft.AspNetCore": "Warning" 6 | } 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /MyApp.Api/MyApp.Api/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Information", 5 | "Microsoft.AspNetCore": "Warning" 6 | } 7 | }, 8 | "AllowedHosts": "*", 9 | "ConnectionStrings": { 10 | "DefaultConnection": "Server=.;Database=TestAPIDb;Trusted_Connection=True;TrustServerCertificate=true;MultipleActiveResultSets=true" 11 | 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /MyApp.Api/MyApp.Application/Commands/AddEmployeeCommand.cs: -------------------------------------------------------------------------------- 1 | using MediatR; 2 | using MyApp.Application.Events; 3 | using MyApp.Core.Entities; 4 | using MyApp.Core.Interfaces; 5 | 6 | namespace MyApp.Application.Commands 7 | { 8 | public record AddEmployeeCommand(EmployeeEntity Employee) : IRequest; 9 | 10 | 11 | public class AddEmployeeCommandHandler(IEmployeeRepository employeeRepository, IPublisher mediator) 12 | : IRequestHandler 13 | { 14 | public async Task Handle(AddEmployeeCommand request, CancellationToken cancellationToken) 15 | { 16 | var user = await employeeRepository.AddEmployeeAsync(request.Employee); 17 | await mediator.Publish(new UserCreatedEvent(user.Id)); 18 | return user; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /MyApp.Api/MyApp.Application/Commands/DeleteEmployeeCommand.cs: -------------------------------------------------------------------------------- 1 | using MediatR; 2 | using MyApp.Core.Interfaces; 3 | 4 | namespace MyApp.Application.Commands 5 | { 6 | public record DeleteEmployeeCommand(Guid EmployeeId) : IRequest; 7 | 8 | internal class DeleteEmployeeCommandHandler(IEmployeeRepository employeeRepository) 9 | : IRequestHandler 10 | { 11 | public async Task Handle(DeleteEmployeeCommand request, CancellationToken cancellationToken) 12 | { 13 | return await employeeRepository.DeleteEmployeeAsync(request.EmployeeId); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /MyApp.Api/MyApp.Application/Commands/UpdateEmployeeCommand.cs: -------------------------------------------------------------------------------- 1 | using MediatR; 2 | using MyApp.Core.Entities; 3 | using MyApp.Core.Interfaces; 4 | 5 | namespace MyApp.Application.Commands 6 | { 7 | public record UpdateEmployeeCommand(Guid EmployeeId, EmployeeEntity Employee) 8 | : IRequest; 9 | public class UpdateEmployeeCommandHandler(IEmployeeRepository employeeRepository) 10 | : IRequestHandler 11 | { 12 | public async Task Handle(UpdateEmployeeCommand request, CancellationToken cancellationToken) 13 | { 14 | return await employeeRepository.UpdateEmployeeAsync(request.EmployeeId, request.Employee); 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /MyApp.Api/MyApp.Application/DependencyInjection.cs: -------------------------------------------------------------------------------- 1 | using MediatR.NotificationPublishers; 2 | using Microsoft.Extensions.DependencyInjection; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | 9 | namespace MyApp.Application 10 | { 11 | public static class DependencyInjection 12 | { 13 | public static IServiceCollection AddApplicationDI(this IServiceCollection services) 14 | { 15 | services.AddMediatR(cfg => 16 | { 17 | cfg.RegisterServicesFromAssembly(typeof(DependencyInjection).Assembly); 18 | cfg.NotificationPublisher = new TaskWhenAllPublisher(); 19 | }); 20 | 21 | return services; 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /MyApp.Api/MyApp.Application/Events/SendEmailEventHandler.cs: -------------------------------------------------------------------------------- 1 | using MediatR; 2 | using Microsoft.Extensions.Logging; 3 | 4 | namespace MyApp.Application.Events; 5 | 6 | public class SendEmailEventHandler 7 | (ILogger logger) 8 | : INotificationHandler 9 | { 10 | public async Task Handle(UserCreatedEvent notification, CancellationToken cancellationToken) 11 | { 12 | logger.LogInformation("User created: Send mail start for {UserId}", notification.UserId); 13 | 14 | await Task.Delay(2000, cancellationToken); 15 | 16 | logger.LogInformation("User created: Send mail done for {UserId}", notification.UserId); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /MyApp.Api/MyApp.Application/Events/StartMembershipEventHandler.cs: -------------------------------------------------------------------------------- 1 | using MediatR; 2 | using Microsoft.Extensions.Logging; 3 | 4 | namespace MyApp.Application.Events; 5 | 6 | 7 | public class StartMembershipEventHandler 8 | (ILogger logger) 9 | : INotificationHandler 10 | { 11 | public async Task Handle(UserCreatedEvent notification, CancellationToken cancellationToken) 12 | { 13 | logger.LogInformation("User created: membership start {UserId}", notification.UserId); 14 | 15 | await Task.Delay(2000, cancellationToken); 16 | 17 | logger.LogInformation("User created: membership {UserId}", notification.UserId); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /MyApp.Api/MyApp.Application/Events/UserCreatedEvent.cs: -------------------------------------------------------------------------------- 1 | using MediatR; 2 | 3 | namespace MyApp.Application.Events; 4 | 5 | public record UserCreatedEvent(Guid UserId) : INotification; 6 | -------------------------------------------------------------------------------- /MyApp.Api/MyApp.Application/MyApp.Application.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | net8.0 5 | enable 6 | enable 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /MyApp.Api/MyApp.Application/Queries/GetAllEmployeesQuery.cs: -------------------------------------------------------------------------------- 1 | using MediatR; 2 | using MyApp.Core.Entities; 3 | using MyApp.Core.Interfaces; 4 | 5 | namespace MyApp.Application.Queries 6 | { 7 | public record GetAllEmployeesQuery() : IRequest>; 8 | public class GetAllEmployeesQueryHandler(IEmployeeRepository employeeRepository) 9 | : IRequestHandler> 10 | { 11 | public async Task> Handle(GetAllEmployeesQuery request, CancellationToken cancellationToken) 12 | { 13 | return await employeeRepository.GetEmployees(); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /MyApp.Api/MyApp.Application/Queries/GetCoinkdeskDataQuery.cs: -------------------------------------------------------------------------------- 1 | using MediatR; 2 | using MyApp.Core.Interfaces; 3 | using MyApp.Core.Models; 4 | 5 | namespace MyApp.Application.Queries 6 | { 7 | public record GetCoinkdeskDataQuery() : IRequest; 8 | public class GetCoinkdeskDataQueryHandler(IExternalVendorRepository externalVendorRepository) 9 | : IRequestHandler 10 | { 11 | public async Task Handle(GetCoinkdeskDataQuery request, CancellationToken cancellationToken) 12 | { 13 | return await externalVendorRepository.GetData(); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /MyApp.Api/MyApp.Application/Queries/GetEmployeeByIdQuery.cs: -------------------------------------------------------------------------------- 1 | using MediatR; 2 | using MyApp.Core.Entities; 3 | using MyApp.Core.Interfaces; 4 | using System; 5 | using System.Collections.Generic; 6 | using System.Linq; 7 | using System.Text; 8 | using System.Threading.Tasks; 9 | 10 | namespace MyApp.Application.Queries 11 | { 12 | public record GetEmployeeByIdQuery(Guid EmployeeId) : IRequest; 13 | 14 | public class GetEmployeeByIdQueryHandler(IEmployeeRepository employeeRepository) 15 | : IRequestHandler 16 | { 17 | public async Task Handle(GetEmployeeByIdQuery request, CancellationToken cancellationToken) 18 | { 19 | return await employeeRepository.GetEmployeeByIdAsync(request.EmployeeId); 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /MyApp.Api/MyApp.Application/Queries/GetJokeQuery.cs: -------------------------------------------------------------------------------- 1 | using MediatR; 2 | using MyApp.Core.Interfaces; 3 | using MyApp.Core.Models; 4 | 5 | namespace MyApp.Application.Queries 6 | { 7 | public record GetJokeQuery() : IRequest; 8 | public class GetJokeQueryHandler(IExternalVendorRepository externalVendorRepository) 9 | : IRequestHandler 10 | { 11 | public async Task Handle(GetJokeQuery request, CancellationToken cancellationToken) 12 | { 13 | return await externalVendorRepository.GetJoke(); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /MyApp.Api/MyApp.Core/DependencyInjection.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.Extensions.Configuration; 2 | using Microsoft.Extensions.DependencyInjection; 3 | using MyApp.Core.Options; 4 | 5 | namespace MyApp.Core 6 | { 7 | public static class DependencyInjection 8 | { 9 | public static IServiceCollection AddCoreDI(this IServiceCollection services, IConfiguration configuration) 10 | { 11 | services.Configure(configuration.GetSection(ConnectionStringOptions.SectionName)); 12 | 13 | return services; 14 | } 15 | } 16 | } 17 | 18 | 19 | -------------------------------------------------------------------------------- /MyApp.Api/MyApp.Core/Entities/EmployeeEntity.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace MyApp.Core.Entities 8 | { 9 | public class EmployeeEntity 10 | { 11 | public Guid Id { get; set; } 12 | public string Name { get; set; } = null!; 13 | public string Email { get; set; } = null!; 14 | public string Phone { get; set; } = null!; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /MyApp.Api/MyApp.Core/Interfaces/IEmployeeRepository.cs: -------------------------------------------------------------------------------- 1 | using MyApp.Core.Entities; 2 | 3 | namespace MyApp.Core.Interfaces 4 | { 5 | public interface IEmployeeRepository 6 | { 7 | Task> GetEmployees(); 8 | Task GetEmployeeByIdAsync(Guid id); 9 | Task AddEmployeeAsync(EmployeeEntity entity); 10 | Task UpdateEmployeeAsync(Guid employeeId, EmployeeEntity entity); 11 | Task DeleteEmployeeAsync(Guid employeeId); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /MyApp.Api/MyApp.Core/Interfaces/IExternalVendorRepository.cs: -------------------------------------------------------------------------------- 1 | 2 | using MyApp.Core.Models; 3 | 4 | namespace MyApp.Core.Interfaces; 5 | public interface IExternalVendorRepository 6 | { 7 | Task GetData(); 8 | Task GetJoke(); 9 | } -------------------------------------------------------------------------------- /MyApp.Api/MyApp.Core/Models/CoindeskData.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace MyApp.Core.Models 8 | { 9 | public class CoindeskData 10 | { 11 | public Time Time { get; set; } 12 | public string Disclaimer { get; set; } 13 | public string ChartName { get; set; } 14 | public Bpi Bpi { get; set; } 15 | } 16 | 17 | public class Time 18 | { 19 | public string Updated { get; set; } 20 | public DateTime UpdatedISO { get; set; } 21 | public string Updateduk { get; set; } 22 | } 23 | 24 | public class Bpi 25 | { 26 | public USD USD { get; set; } 27 | public GBP GBP { get; set; } 28 | public EUR EUR { get; set; } 29 | } 30 | 31 | public class USD 32 | { 33 | public string Code { get; set; } 34 | public string Symbol { get; set; } 35 | public string Rate { get; set; } 36 | public string Description { get; set; } 37 | public float RateFloat { get; set; } 38 | } 39 | 40 | public class GBP 41 | { 42 | public string Code { get; set; } 43 | public string Symbol { get; set; } 44 | public string Rate { get; set; } 45 | public string Description { get; set; } 46 | public float RateFloat { get; set; } 47 | } 48 | 49 | public class EUR 50 | { 51 | public string Code { get; set; } 52 | public string Symbol { get; set; } 53 | public string Rate { get; set; } 54 | public string Description { get; set; } 55 | public float RateFloat { get; set; } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /MyApp.Api/MyApp.Core/Models/JokeModel.cs: -------------------------------------------------------------------------------- 1 | namespace MyApp.Core.Models; 2 | 3 | public record JokeModel(string Type, string Setup, string Punchline, int Id); -------------------------------------------------------------------------------- /MyApp.Api/MyApp.Core/MyApp.Core.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | net8.0 5 | enable 6 | enable 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /MyApp.Api/MyApp.Core/Options/ConnectionStringOptions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace MyApp.Core.Options 8 | { 9 | public class ConnectionStringOptions 10 | { 11 | public const string SectionName = "ConnectionStrings"; 12 | public string DefaultConnection { get; set; } = null!; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /MyApp.Api/MyApp.Infrastructure/Data/AppDbContext.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore; 2 | using MyApp.Core.Entities; 3 | 4 | namespace MyApp.Infrastructure.Data 5 | { 6 | public class AppDbContext(DbContextOptions options) : DbContext(options) 7 | { 8 | public DbSet Employees { get; set; } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /MyApp.Api/MyApp.Infrastructure/DependencyInjection.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore; 2 | using Microsoft.Extensions.DependencyInjection; 3 | using Microsoft.Extensions.Options; 4 | using MyApp.Core.Interfaces; 5 | using MyApp.Core.Options; 6 | using MyApp.Infrastructure.Data; 7 | using MyApp.Infrastructure.Repositories; 8 | using MyApp.Infrastructure.Services; 9 | 10 | namespace MyApp.Infrastructure 11 | { 12 | public static class DependencyInjection 13 | { 14 | public static IServiceCollection AddInfrastructureDI(this IServiceCollection services) 15 | { 16 | services.AddDbContext((provider, options) => 17 | { 18 | options.UseSqlServer(provider.GetRequiredService>().Value.DefaultConnection); 19 | }); 20 | 21 | services.AddScoped(); 22 | services.AddScoped(); 23 | 24 | services.AddHttpClient(option => 25 | { 26 | option.BaseAddress = new Uri("https://api.coindesk.com/v1/"); 27 | }); 28 | 29 | services.AddHttpClient(option => 30 | { 31 | option.BaseAddress = new Uri("https://official-joke-api.appspot.com/"); 32 | }); 33 | 34 | return services; 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /MyApp.Api/MyApp.Infrastructure/Migrations/20240710143212_dbinit.Designer.cs: -------------------------------------------------------------------------------- 1 | // 2 | using System; 3 | using Microsoft.EntityFrameworkCore; 4 | using Microsoft.EntityFrameworkCore.Infrastructure; 5 | using Microsoft.EntityFrameworkCore.Metadata; 6 | using Microsoft.EntityFrameworkCore.Migrations; 7 | using Microsoft.EntityFrameworkCore.Storage.ValueConversion; 8 | using MyApp.Infrastructure.Data; 9 | 10 | #nullable disable 11 | 12 | namespace MyApp.Infrastructure.Migrations 13 | { 14 | [DbContext(typeof(AppDbContext))] 15 | [Migration("20240710143212_dbinit")] 16 | partial class dbinit 17 | { 18 | /// 19 | protected override void BuildTargetModel(ModelBuilder modelBuilder) 20 | { 21 | #pragma warning disable 612, 618 22 | modelBuilder 23 | .HasAnnotation("ProductVersion", "8.0.7") 24 | .HasAnnotation("Relational:MaxIdentifierLength", 128); 25 | 26 | SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); 27 | 28 | modelBuilder.Entity("MyApp.Core.Entities.EmployeeEntity", b => 29 | { 30 | b.Property("Id") 31 | .ValueGeneratedOnAdd() 32 | .HasColumnType("uniqueidentifier"); 33 | 34 | b.Property("Email") 35 | .IsRequired() 36 | .HasColumnType("nvarchar(max)"); 37 | 38 | b.Property("Name") 39 | .IsRequired() 40 | .HasColumnType("nvarchar(max)"); 41 | 42 | b.Property("Phone") 43 | .IsRequired() 44 | .HasColumnType("nvarchar(max)"); 45 | 46 | b.HasKey("Id"); 47 | 48 | b.ToTable("Employees"); 49 | }); 50 | #pragma warning restore 612, 618 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /MyApp.Api/MyApp.Infrastructure/Migrations/20240710143212_dbinit.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Microsoft.EntityFrameworkCore.Migrations; 3 | 4 | #nullable disable 5 | 6 | namespace MyApp.Infrastructure.Migrations 7 | { 8 | /// 9 | public partial class dbinit : Migration 10 | { 11 | /// 12 | protected override void Up(MigrationBuilder migrationBuilder) 13 | { 14 | migrationBuilder.CreateTable( 15 | name: "Employees", 16 | columns: table => new 17 | { 18 | Id = table.Column(type: "uniqueidentifier", nullable: false), 19 | Name = table.Column(type: "nvarchar(max)", nullable: false), 20 | Email = table.Column(type: "nvarchar(max)", nullable: false), 21 | Phone = table.Column(type: "nvarchar(max)", nullable: false) 22 | }, 23 | constraints: table => 24 | { 25 | table.PrimaryKey("PK_Employees", x => x.Id); 26 | }); 27 | } 28 | 29 | /// 30 | protected override void Down(MigrationBuilder migrationBuilder) 31 | { 32 | migrationBuilder.DropTable( 33 | name: "Employees"); 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /MyApp.Api/MyApp.Infrastructure/Migrations/AppDbContextModelSnapshot.cs: -------------------------------------------------------------------------------- 1 | // 2 | using System; 3 | using Microsoft.EntityFrameworkCore; 4 | using Microsoft.EntityFrameworkCore.Infrastructure; 5 | using Microsoft.EntityFrameworkCore.Metadata; 6 | using Microsoft.EntityFrameworkCore.Storage.ValueConversion; 7 | using MyApp.Infrastructure.Data; 8 | 9 | #nullable disable 10 | 11 | namespace MyApp.Infrastructure.Migrations 12 | { 13 | [DbContext(typeof(AppDbContext))] 14 | partial class AppDbContextModelSnapshot : ModelSnapshot 15 | { 16 | protected override void BuildModel(ModelBuilder modelBuilder) 17 | { 18 | #pragma warning disable 612, 618 19 | modelBuilder 20 | .HasAnnotation("ProductVersion", "8.0.7") 21 | .HasAnnotation("Relational:MaxIdentifierLength", 128); 22 | 23 | SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder); 24 | 25 | modelBuilder.Entity("MyApp.Core.Entities.EmployeeEntity", b => 26 | { 27 | b.Property("Id") 28 | .ValueGeneratedOnAdd() 29 | .HasColumnType("uniqueidentifier"); 30 | 31 | b.Property("Email") 32 | .IsRequired() 33 | .HasColumnType("nvarchar(max)"); 34 | 35 | b.Property("Name") 36 | .IsRequired() 37 | .HasColumnType("nvarchar(max)"); 38 | 39 | b.Property("Phone") 40 | .IsRequired() 41 | .HasColumnType("nvarchar(max)"); 42 | 43 | b.HasKey("Id"); 44 | 45 | b.ToTable("Employees"); 46 | }); 47 | #pragma warning restore 612, 618 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /MyApp.Api/MyApp.Infrastructure/MyApp.Infrastructure.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | net8.0 5 | enable 6 | enable 7 | 8 | 9 | 10 | 11 | 12 | 13 | all 14 | runtime; build; native; contentfiles; analyzers; buildtransitive 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /MyApp.Api/MyApp.Infrastructure/Repositories/EmployeeRepository.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore; 2 | using MyApp.Core.Entities; 3 | using MyApp.Core.Interfaces; 4 | using MyApp.Infrastructure.Data; 5 | using System; 6 | using System.Collections.Generic; 7 | using System.Linq; 8 | using System.Text; 9 | using System.Threading.Tasks; 10 | 11 | namespace MyApp.Infrastructure.Repositories 12 | { 13 | public class EmployeeRepository(AppDbContext dbContext) : IEmployeeRepository 14 | { 15 | public async Task> GetEmployees() 16 | { 17 | return await dbContext.Employees.ToListAsync(); 18 | } 19 | 20 | public async Task GetEmployeeByIdAsync(Guid id) 21 | { 22 | return await dbContext.Employees.FirstOrDefaultAsync(x => x.Id == id); 23 | } 24 | 25 | public async Task AddEmployeeAsync(EmployeeEntity entity) 26 | { 27 | entity.Id = Guid.NewGuid(); 28 | dbContext.Employees.Add(entity); 29 | 30 | await dbContext.SaveChangesAsync(); 31 | 32 | return entity; 33 | } 34 | 35 | public async Task UpdateEmployeeAsync(Guid employeeId, EmployeeEntity entity) 36 | { 37 | var employee = await dbContext.Employees.FirstOrDefaultAsync(x => x.Id == employeeId); 38 | 39 | if (employee is not null) 40 | { 41 | employee.Name = entity.Name; 42 | employee.Email = entity.Email; 43 | employee.Phone = entity.Phone; 44 | 45 | await dbContext.SaveChangesAsync(); 46 | 47 | return employee; 48 | } 49 | 50 | return entity; 51 | } 52 | 53 | public async Task DeleteEmployeeAsync(Guid employeeId) 54 | { 55 | var employee = await dbContext.Employees.FirstOrDefaultAsync(x => x.Id == employeeId); 56 | 57 | if (employee is not null) 58 | { 59 | dbContext.Employees.Remove(employee); 60 | 61 | return await dbContext.SaveChangesAsync() > 0; 62 | } 63 | 64 | return false; 65 | } 66 | } 67 | } 68 | 69 | -------------------------------------------------------------------------------- /MyApp.Api/MyApp.Infrastructure/Repositories/ExternalVendorRepository.cs: -------------------------------------------------------------------------------- 1 | using MyApp.Core.Interfaces; 2 | using MyApp.Core.Models; 3 | using MyApp.Infrastructure.Services; 4 | using System; 5 | using System.Collections.Generic; 6 | using System.Linq; 7 | using System.Text; 8 | using System.Threading.Tasks; 9 | 10 | namespace MyApp.Infrastructure.Repositories 11 | { 12 | public class ExternalVendorRepository( 13 | ICoindeskHttpClientService coindeskHttpClientService, 14 | IJokeHttpClientService jokeHttpClientService) 15 | : IExternalVendorRepository 16 | { 17 | public async Task GetData() 18 | { 19 | return await coindeskHttpClientService.GetData(); 20 | } 21 | 22 | public async Task GetJoke() 23 | { 24 | return await jokeHttpClientService.GetData(); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /MyApp.Api/MyApp.Infrastructure/Services/CoindeskHttpClientService.cs: -------------------------------------------------------------------------------- 1 | using MyApp.Core.Models; 2 | using System.Net.Http.Json; 3 | 4 | namespace MyApp.Infrastructure.Services 5 | { 6 | public class CoindeskHttpClientService(HttpClient httpClient) : ICoindeskHttpClientService 7 | { 8 | public async Task GetData() 9 | { 10 | return await httpClient.GetFromJsonAsync("bpi/currentprice.json"); 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /MyApp.Api/MyApp.Infrastructure/Services/ICoindeskHttpClientService.cs: -------------------------------------------------------------------------------- 1 | using MyApp.Core.Models; 2 | 3 | namespace MyApp.Infrastructure.Services 4 | { 5 | public interface ICoindeskHttpClientService 6 | { 7 | Task GetData(); 8 | } 9 | } -------------------------------------------------------------------------------- /MyApp.Api/MyApp.Infrastructure/Services/IJokeHttpClientService.cs: -------------------------------------------------------------------------------- 1 | using MyApp.Core.Models; 2 | 3 | namespace MyApp.Infrastructure.Services 4 | { 5 | public interface IJokeHttpClientService 6 | { 7 | Task GetData(); 8 | } 9 | } -------------------------------------------------------------------------------- /MyApp.Api/MyApp.Infrastructure/Services/JokeHttpClientService.cs: -------------------------------------------------------------------------------- 1 | using MyApp.Core.Models; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Net.Http.Json; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | 9 | namespace MyApp.Infrastructure.Services 10 | { 11 | public class JokeHttpClientService(HttpClient httpClient) : IJokeHttpClientService 12 | { 13 | public async Task GetData() 14 | { 15 | return await httpClient.GetFromJsonAsync("random_joke"); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ASP.NET Core Web API Clean Architecture: CRUD Operations 🚀🚀 2 | 3 | Welcome! This guide will help you learn about implementing CRUD operations using ASP.NET Core Web API with a clean architecture approach. The following technologies are used: 4 | 5 | - **ASP.NET Core Web API (DotNet 8)** 6 | - **MediatR** 7 | - **Entity Framework Core** 8 | - **SQL Server** 9 | 10 | --- 11 | ## 👨‍💻 Instructor 12 | 13 | **[Nitish Kaushik](https://www.linkedin.com/in/meettonitish/) is a [Microsoft MVP](https://mvp.microsoft.com/en-US/MVP/profile/e20e687b-7976-eb11-a812-000d3a8dfe0d) in developer technologies and has rich experience in Microsoft Technologies.** 14 | 15 | You can find more content from Nitish on following YouTube channels: 16 | 17 | - **[WebGentle YouTube channel](https://www.youtube.com/@webgentle)** 18 | - **[Nitish Kaushik YouTube channel](https://www.youtube.com/@nitish.kaushik)** 19 | 20 | 21 | ## How to Learn and Implement Clean Architecture in ASP.NET Core Web API 22 | 23 | ### 👉 Lesson 1: Setting Up Clean Architecture from Scratch 24 | 25 | In this lesson, you will learn how to set up clean architecture from the ground up. We will cover the essential layers required and how to create references between them. 26 | 27 | 🎥 Video [How to Learn and Implement Clean Architecture in ASP.NET Core Web API](https://www.youtube.com/watch?v=sBAB_EKYPYs) 28 | 29 | 30 | ### 👉 Lesson 2: Implement the CRUD Operations in ASP.NET Core application 31 | 32 | In this lesson we will implement the follwing CRUD Operations 33 | - Get All records 34 | - Get single record by Id 35 | - Add new record 36 | - Update the record 37 | - Delete the record 38 | 39 | We will be using following technologies in these CRUD operations 40 | - Asp.Net Core Web API 41 | - Entity Framework Core 42 | - SQL Server 43 | - CQRS (MediatR) 44 | - Clean Architecture 45 | 46 | 🎥 Video [CRUD opeations in ASP.Net Core](https://www.youtube.com/watch?v=YjQwGKZKWgI) 47 | 48 | ### 👉 Lesson 3: Options Pattern (Get data from appsettings.json file) in ASP.NET Core application with Clean Architecture 49 | 50 | Options pattern is used to get the data (configuration) from appsettings.json and other configuration places with strongly binding. 51 | Options patern reduced the chances of typo mistake for the key name while getting the data from appsetings.json 52 | 53 | 🎥 Video [Options Pattern in Asp.Net Core in Class Library using Clean Architecture](https://www.youtube.com/watch?v=osSRgaBXkPY) 54 | 55 | ### 👉 Lesson 4: Use HttpClient Typed implementation to call the external API (endpoints) from ASP.NET Core application 56 | 57 | In an ASP.NET Core application, there are many instances where calling external endpoints is necessary. To achieve this, we can utilize the `HttpClient`. 58 | 59 | In this video, we will explore how to use `HttpClient` to make calls to external APIs, adhering to Clean Architecture principles. 60 | 61 | --------------------------------------------------------------------------------