├── .dockerignore ├── .gitignore ├── Clean-Onion_Architecture.sln ├── assets └── cleanarch.png ├── docker-compose.dcproj ├── docker-compose.yml ├── readme.md └── src ├── Core ├── Application │ ├── Application.csproj │ ├── ApplicationRegistration.cs │ ├── Behaviors │ │ └── CachingBehavior.cs │ ├── Constants │ │ └── Messages.cs │ ├── Dtos │ │ ├── RoleDTO.cs │ │ ├── TokenDTO.cs │ │ └── UserDTO.cs │ ├── Exceptions │ │ └── ApiException.cs │ ├── Features │ │ ├── Roles │ │ │ ├── Commands │ │ │ │ ├── CreateRoleCommand.cs │ │ │ │ ├── RemoveRoleCommand.cs │ │ │ │ └── UpdateRoleCommand.cs │ │ │ ├── Queries │ │ │ │ ├── GetAllRolesQuery.cs │ │ │ │ ├── GetRoleByIdQuery.cs │ │ │ │ └── GetRolesByUserIdQuery.cs │ │ │ └── Validators │ │ │ │ └── RoleValidator.cs │ │ └── Users │ │ │ ├── Commands │ │ │ ├── ChangeEmailCommand.cs │ │ │ ├── ChangePasswordCommand.cs │ │ │ ├── ConfirmEmailCommand.cs │ │ │ ├── CreateTokenByRefreshTokenCommand.cs │ │ │ ├── ForgetPasswordCommand.cs │ │ │ ├── LoginCommand.cs │ │ │ ├── RegisterCommand.cs │ │ │ ├── RemoveUserCommand.cs │ │ │ ├── ResetPasswordCommand.cs │ │ │ ├── UpdateUserCommand.cs │ │ │ └── UpdateUserRoleCommand.cs │ │ │ ├── Queries │ │ │ ├── GetAllUsersWithRolesQuery.cs │ │ │ └── GetAuthenticatedUserWithRolesQuery.cs │ │ │ └── Validators │ │ │ ├── ChangeEmailValidator.cs │ │ │ ├── ChangePasswordValidator.cs │ │ │ ├── ForgetPasswordValidator.cs │ │ │ ├── LoginValidator.cs │ │ │ ├── RefreshTokenValidator.cs │ │ │ ├── RegisterValidator.cs │ │ │ ├── UpdateUserRoleValidator.cs │ │ │ └── UpdateUserValidator.cs │ ├── Helpers │ │ └── PasswordHelper.cs │ ├── Interfaces │ │ ├── ICacheable.cs │ │ ├── Repositories │ │ │ ├── IRefreshTokenRepository.cs │ │ │ ├── IRepository.cs │ │ │ ├── IRoleRepository.cs │ │ │ ├── IUnitOfWork.cs │ │ │ └── IUserRepository.cs │ │ └── Services │ │ │ ├── IEasyCacheService.cs │ │ │ ├── IEmailService.cs │ │ │ └── ITokenService.cs │ ├── Mappings │ │ └── Automapper.cs │ └── Wrappers │ │ ├── Abstract │ │ ├── IDataResponse.cs │ │ ├── IErrorResponse.cs │ │ ├── IPagedDataResponse.cs │ │ ├── IResponse.cs │ │ └── ISuccessResponse.cs │ │ └── Concrete │ │ ├── DataResponse.cs │ │ ├── ErrorResponse.cs │ │ ├── PagedDataResponse.cs │ │ ├── Response.cs │ │ └── SuccessResponse.cs └── Domain │ ├── Domain.csproj │ └── Entities │ ├── Base │ └── BaseEntity.cs │ ├── RefreshToken.cs │ ├── Role.cs │ ├── User.cs │ └── UserRole.cs ├── Infrastructure ├── Infrastructure │ ├── Infrastructure.csproj │ ├── InfrastructureRegistration.cs │ ├── Services │ │ ├── EasyCacheService.cs │ │ ├── EmailService.cs │ │ └── TokenService.cs │ └── Settings │ │ ├── CacheSettings.cs │ │ ├── EmailSettings.cs │ │ └── JWTSettings.cs └── Persistence │ ├── Context │ └── CAContext.cs │ ├── EntityConfigurations │ ├── RoleConfiguration.cs │ ├── UserConfiguration.cs │ └── UserRoleConfiguration.cs │ ├── Migrations │ ├── 20220527113337_CreateDatabase.Designer.cs │ ├── 20220527113337_CreateDatabase.cs │ └── CAContextModelSnapshot.cs │ ├── Persistence.csproj │ ├── PersistenceRegistration.cs │ └── Repositories │ ├── EfEntityRepository.cs │ ├── RefreshTokenRepository.cs │ ├── RoleRepository.cs │ ├── UnitOfWork.cs │ └── UserRepository.cs └── Presentation └── WebAPI ├── Controllers ├── AuthController.cs ├── BaseController.cs ├── RolesController.cs └── UsersController.cs ├── Dockerfile ├── Infrastructure ├── Extensions │ ├── AuthRegistration.cs │ └── ExceptionMiddlewareExtension.cs ├── Filters │ └── ValidationFilter.cs └── Middleware │ └── ExceptionMiddleware.cs ├── Program.cs ├── Properties └── launchSettings.json ├── WebAPI.csproj ├── appsettings.Development.json └── appsettings.json /.dockerignore: -------------------------------------------------------------------------------- 1 | **/.classpath 2 | **/.dockerignore 3 | **/.env 4 | **/.git 5 | **/.gitignore 6 | **/.project 7 | **/.settings 8 | **/.toolstarget 9 | **/.vs 10 | **/.vscode 11 | **/*.*proj.user 12 | **/*.dbmdl 13 | **/*.jfm 14 | **/azds.yaml 15 | **/bin 16 | **/charts 17 | **/docker-compose* 18 | **/Dockerfile* 19 | **/node_modules 20 | **/npm-debug.log 21 | **/obj 22 | **/secrets.dev.yaml 23 | **/values.dev.yaml 24 | LICENSE 25 | 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 -------------------------------------------------------------------------------- /Clean-Onion_Architecture.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 17 4 | VisualStudioVersion = 17.2.32505.173 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{ECD7A81E-BCD2-4927-BF9C-75381E20A612}" 7 | EndProject 8 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Core", "Core", "{7372E137-8445-4318-B5BE-F41FC69B1E29}" 9 | EndProject 10 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Application", "src\Core\Application\Application.csproj", "{B3F8A539-6801-4741-B706-908E138FE448}" 11 | EndProject 12 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Domain", "src\Core\Domain\Domain.csproj", "{7C4D67AA-0090-4A8F-814F-864F18433252}" 13 | EndProject 14 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Infrastructure", "Infrastructure", "{01B14A20-4B4B-4EDF-86CC-54D22C76DE5C}" 15 | EndProject 16 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Persistence", "src\Infrastructure\Persistence\Persistence.csproj", "{CAA907CD-C661-4E00-A7DD-8BD4CBA8F961}" 17 | EndProject 18 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Presentation", "Presentation", "{D6B846AD-B7A3-4511-A177-DFF307B3B481}" 19 | EndProject 20 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebAPI", "src\Presentation\WebAPI\WebAPI.csproj", "{5D865ECB-BB7E-44B3-BAF5-D0C8D218A1D7}" 21 | EndProject 22 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Infrastructure", "src\Infrastructure\Infrastructure\Infrastructure.csproj", "{453DD5D1-4366-4CB2-ADCE-4DBCED50A9E6}" 23 | EndProject 24 | Project("{E53339B2-1760-4266-BCC7-CA923CBCF16C}") = "docker-compose", "docker-compose.dcproj", "{D75BA8DF-9B1B-4F83-A2A3-B4256E2C0BCA}" 25 | EndProject 26 | Global 27 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 28 | Debug|Any CPU = Debug|Any CPU 29 | Release|Any CPU = Release|Any CPU 30 | EndGlobalSection 31 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 32 | {B3F8A539-6801-4741-B706-908E138FE448}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 33 | {B3F8A539-6801-4741-B706-908E138FE448}.Debug|Any CPU.Build.0 = Debug|Any CPU 34 | {B3F8A539-6801-4741-B706-908E138FE448}.Release|Any CPU.ActiveCfg = Release|Any CPU 35 | {B3F8A539-6801-4741-B706-908E138FE448}.Release|Any CPU.Build.0 = Release|Any CPU 36 | {7C4D67AA-0090-4A8F-814F-864F18433252}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 37 | {7C4D67AA-0090-4A8F-814F-864F18433252}.Debug|Any CPU.Build.0 = Debug|Any CPU 38 | {7C4D67AA-0090-4A8F-814F-864F18433252}.Release|Any CPU.ActiveCfg = Release|Any CPU 39 | {7C4D67AA-0090-4A8F-814F-864F18433252}.Release|Any CPU.Build.0 = Release|Any CPU 40 | {CAA907CD-C661-4E00-A7DD-8BD4CBA8F961}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 41 | {CAA907CD-C661-4E00-A7DD-8BD4CBA8F961}.Debug|Any CPU.Build.0 = Debug|Any CPU 42 | {CAA907CD-C661-4E00-A7DD-8BD4CBA8F961}.Release|Any CPU.ActiveCfg = Release|Any CPU 43 | {CAA907CD-C661-4E00-A7DD-8BD4CBA8F961}.Release|Any CPU.Build.0 = Release|Any CPU 44 | {5D865ECB-BB7E-44B3-BAF5-D0C8D218A1D7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 45 | {5D865ECB-BB7E-44B3-BAF5-D0C8D218A1D7}.Debug|Any CPU.Build.0 = Debug|Any CPU 46 | {5D865ECB-BB7E-44B3-BAF5-D0C8D218A1D7}.Release|Any CPU.ActiveCfg = Release|Any CPU 47 | {5D865ECB-BB7E-44B3-BAF5-D0C8D218A1D7}.Release|Any CPU.Build.0 = Release|Any CPU 48 | {453DD5D1-4366-4CB2-ADCE-4DBCED50A9E6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 49 | {453DD5D1-4366-4CB2-ADCE-4DBCED50A9E6}.Debug|Any CPU.Build.0 = Debug|Any CPU 50 | {453DD5D1-4366-4CB2-ADCE-4DBCED50A9E6}.Release|Any CPU.ActiveCfg = Release|Any CPU 51 | {453DD5D1-4366-4CB2-ADCE-4DBCED50A9E6}.Release|Any CPU.Build.0 = Release|Any CPU 52 | {D75BA8DF-9B1B-4F83-A2A3-B4256E2C0BCA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 53 | {D75BA8DF-9B1B-4F83-A2A3-B4256E2C0BCA}.Debug|Any CPU.Build.0 = Debug|Any CPU 54 | {D75BA8DF-9B1B-4F83-A2A3-B4256E2C0BCA}.Release|Any CPU.ActiveCfg = Release|Any CPU 55 | {D75BA8DF-9B1B-4F83-A2A3-B4256E2C0BCA}.Release|Any CPU.Build.0 = Release|Any CPU 56 | EndGlobalSection 57 | GlobalSection(SolutionProperties) = preSolution 58 | HideSolutionNode = FALSE 59 | EndGlobalSection 60 | GlobalSection(NestedProjects) = preSolution 61 | {7372E137-8445-4318-B5BE-F41FC69B1E29} = {ECD7A81E-BCD2-4927-BF9C-75381E20A612} 62 | {B3F8A539-6801-4741-B706-908E138FE448} = {7372E137-8445-4318-B5BE-F41FC69B1E29} 63 | {7C4D67AA-0090-4A8F-814F-864F18433252} = {7372E137-8445-4318-B5BE-F41FC69B1E29} 64 | {01B14A20-4B4B-4EDF-86CC-54D22C76DE5C} = {ECD7A81E-BCD2-4927-BF9C-75381E20A612} 65 | {CAA907CD-C661-4E00-A7DD-8BD4CBA8F961} = {01B14A20-4B4B-4EDF-86CC-54D22C76DE5C} 66 | {D6B846AD-B7A3-4511-A177-DFF307B3B481} = {ECD7A81E-BCD2-4927-BF9C-75381E20A612} 67 | {5D865ECB-BB7E-44B3-BAF5-D0C8D218A1D7} = {D6B846AD-B7A3-4511-A177-DFF307B3B481} 68 | {453DD5D1-4366-4CB2-ADCE-4DBCED50A9E6} = {01B14A20-4B4B-4EDF-86CC-54D22C76DE5C} 69 | EndGlobalSection 70 | GlobalSection(ExtensibilityGlobals) = postSolution 71 | SolutionGuid = {711CC539-1283-4A92-84A9-B87F849FB05E} 72 | EndGlobalSection 73 | EndGlobal 74 | -------------------------------------------------------------------------------- /assets/cleanarch.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/YALCINCAN/NET6_Clean-OnionArchitectureAPI/212ffb9a634346146a58c472af61bbe62a9dc8e6/assets/cleanarch.png -------------------------------------------------------------------------------- /docker-compose.dcproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 2.1 5 | Linux 6 | d75ba8df-9b1b-4f83-a2a3-b4256e2c0bca 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3.8' 2 | 3 | services: 4 | rediscache: 5 | image: redis 6 | container_name: rediscache 7 | restart: always 8 | ports: 9 | - "6379:6379" 10 | volumes: 11 | - redis_volume:/data 12 | cleanarchitecturedb: 13 | image: postgres 14 | container_name: cleanarchitecturedb 15 | restart : always 16 | ports: 17 | - 5432:5432 18 | environment: 19 | - "POSTGRES_PASSWORD=1283" 20 | volumes: 21 | - postgres_volume:/var/lib/postgresql/data 22 | webapi: 23 | image: webapi 24 | restart: always 25 | build: 26 | context: . 27 | dockerfile: src/Presentation/WebAPI/Dockerfile 28 | ports: 29 | - "5010:80" 30 | environment: 31 | - ASPNETCORE_ENVIRONMENT=Development 32 | - "ConnectionStrings:PostgreSqlConnection=User ID=postgres; Password=1283; Server=cleanarchitecturedb; Port=5432;Database=CleanArchitecture;Integrated Security=true;" 33 | - "ConnectionStrings:SeriLogConnection=User ID=postgres; Password=1283; Server=cleanarchitecturedb; Port=5432;Database=CleanArchitecture;Integrated Security=true;" 34 | - "CacheSettings:RedisURL=rediscache" 35 | - "CacheSettings:Port=6379" 36 | - "CacheSettings:PreferRedis=true" 37 | 38 | 39 | volumes: 40 | redis_volume: 41 | postgres_volume: 42 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # NET6 Clean-Onion Architecture Project 2 | # New features added [v2 branch](https://github.com/YALCINCAN/NET6_Clean-OnionArchitectureAPI/tree/v2) / [v3 branch](https://github.com/YALCINCAN/NET6_Clean-OnionArchitectureAPI/tree/v3) 3 | # New features added 4 | CleanArchitecture 5 | 6 | ## Features 7 | - .NET6 8 | - Entity Framework Core – Code First 9 | - Repository Pattern 10 | - UnitOfWork Pattern 11 | - CQRS Pattern 12 | - Mediatr 13 | - CQRS Pipeline Caching (Redis,InMemory configure from appsettings.json file) 14 | - Response Wrappers 15 | - Fluent Validation 16 | - Validation Filter 17 | - Serilog Logging 18 | - Automapper 19 | - Docker 20 | - JWT Authentication,Refresh Token 21 | - Complete User Management (Register / Forgot Password / Confirmation Mail) Without Identity 22 | - Role Based Authorization 23 | - Database Seeding 24 | - Custom Exception Handling Middleware 25 | 26 | 27 | ## How To Start .Net API 28 | 29 | For api, you must edit the appsettings.json file email settings eg. 30 | 31 | Docker support added you can start project with docker, first you must look docker compose yaml file rediscache settings eg. and write 32 | 33 | ```sh 34 | docker compose -f "docker-compose.yml" up -d --build 35 | ``` 36 | When the project is up, the migrations run automatically 37 | 38 | After a database will be created. 39 | 40 | Default Admin Account : 41 | 42 | ```sh 43 | Username : admin 44 | Password : 159357456qW 45 | ``` 46 | 47 | 48 | 49 | 50 | -------------------------------------------------------------------------------- /src/Core/Application/Application.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | net6.0 5 | enable 6 | disable 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /src/Core/Application/ApplicationRegistration.cs: -------------------------------------------------------------------------------- 1 | using Application.Behaviors; 2 | using FluentValidation; 3 | using MediatR; 4 | using Microsoft.Extensions.DependencyInjection; 5 | using System.Reflection; 6 | 7 | 8 | namespace Application 9 | { 10 | public static class ApplicationRegistration 11 | { 12 | public static void AddApplicationServices(this IServiceCollection services) 13 | { 14 | var assm = Assembly.GetExecutingAssembly(); 15 | services.AddAutoMapper(assm); 16 | services.AddMediatR(assm); 17 | services.AddTransient(typeof(IPipelineBehavior<,>), typeof(CachingBehavior<,>)); 18 | services.AddValidatorsFromAssembly(assm); 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /src/Core/Application/Behaviors/CachingBehavior.cs: -------------------------------------------------------------------------------- 1 | using Application.Interfaces; 2 | using Application.Interfaces.Services; 3 | using MediatR; 4 | using Microsoft.Extensions.Logging; 5 | 6 | namespace Application.Behaviors 7 | { 8 | public class CachingBehavior : IPipelineBehavior where TRequest : IRequest 9 | { 10 | private readonly IEasyCacheService _easyCacheService; 11 | private readonly ILogger> _logger; 12 | 13 | public CachingBehavior(IEasyCacheService easyCacheService, ILogger> logger) 14 | { 15 | _easyCacheService = easyCacheService; 16 | _logger = logger; 17 | } 18 | 19 | public async Task Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate next) 20 | { 21 | if (request is ICacheable cacheableQuery) 22 | { 23 | TResponse response; 24 | if (cacheableQuery.BypassCache) return await next(); 25 | async Task GetResponseAndAddToCache() 26 | { 27 | response = await next(); 28 | 29 | await _easyCacheService.SetAsync((string)cacheableQuery.CacheKey, response); 30 | return response; 31 | } 32 | var cachedResponse = await _easyCacheService.GetAsync(cacheableQuery.CacheKey, typeof(TResponse)); 33 | if (cachedResponse != null) 34 | { 35 | response = (TResponse)cachedResponse; 36 | _logger.LogInformation($"Fetched from Cache -> '{cacheableQuery.CacheKey}'."); 37 | } 38 | else 39 | { 40 | response = await GetResponseAndAddToCache(); 41 | _logger.LogInformation($"Added to Cache -> '{cacheableQuery.CacheKey}'."); 42 | } 43 | return response; 44 | } 45 | else 46 | { 47 | return await next(); 48 | } 49 | } 50 | } 51 | } -------------------------------------------------------------------------------- /src/Core/Application/Constants/Messages.cs: -------------------------------------------------------------------------------- 1 | namespace Application.Constants 2 | { 3 | public static class Messages 4 | { 5 | public static string AddedSuccesfully => "Added Successfully"; 6 | public static string UpdatedSuccessfully => "Updated Successfully"; 7 | public static string DeletedSuccessfully => "Deleted Successfully"; 8 | public static string NotFound => "Entity not found"; 9 | 10 | public static string UserNameOrPasswordIsIncorrect => "Username or password is incorrect"; 11 | public static string ConfirmYourEmail => "Please confirm your email"; 12 | public static string EmailIsAlreadyExist => "Email is already exist"; 13 | public static string UsernameIsAlreadyExist => "Username is already exist"; 14 | public static string PasswordDontMatchWithConfirmation => "Password doesn't match its confirmation"; 15 | public static string RegisterSuccessfully => "Register successfuly please look at your mail box for account confirmation."; 16 | public static string UserNotFound => "User not found"; 17 | public static string TokenOrUserNotFound => "Token or User Not Found"; 18 | public static string RefreshTokenNotFound => "Refresh Token Not Found"; 19 | public static string AlreadyEmailConfirmed => "Already your email confirmed"; 20 | public static string SuccessfullyEmailConfirmed => "Email confirmed successfully.You can login now"; 21 | public static string RefreshTokenExpired => "Refresh Token Expired"; 22 | public static string RoleNameAlreadyExist => "Role Name Already Exist"; 23 | public static string UserRolesUpdatedSuccessfully => "User Roles Updated Successfully"; 24 | public static string PasswordChangedSuccessfully => "Password changed successfully"; 25 | public static string CurrentPasswordIsFalse => "Current Password is false"; 26 | public static string IfEmailTrue => "If your email address is entered correctly, you will receive a link to reset your password."; 27 | public static string PasswordSuccessfullyReset => "Your password has been successfully reset.Your new password has been sent to your email address.We recommend that you change your password."; 28 | public static string ResetPasswordCodeInvalid => "Your Reset Password Code is invalid"; 29 | public static string EmailSuccessfullyChangedConfirmYourEmail => "Email Successfully Changed.Please confirm your email"; 30 | } 31 | } -------------------------------------------------------------------------------- /src/Core/Application/Dtos/RoleDTO.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 Application.Dtos 8 | { 9 | public class RoleDTO 10 | { 11 | public Guid Id { get; set; } 12 | public string Name { get; set; } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/Core/Application/Dtos/TokenDTO.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 Application.Dtos 8 | { 9 | public class TokenDTO 10 | { 11 | public string AccessToken { get; set; } 12 | public DateTime AccessTokenExpiration { get; set; } 13 | public string RefreshToken { get; set; } 14 | public DateTime RefreshTokenExpiration { get; set; } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/Core/Application/Dtos/UserDTO.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 Application.Dtos 8 | { 9 | public class UserDTO 10 | { 11 | public Guid Id { get; set; } 12 | public string UserName { get; set; } 13 | public string FirstName { get; set; } 14 | public string LastName { get; set; } 15 | public string Email { get; set; } 16 | public List Roles { get; set; } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/Core/Application/Exceptions/ApiException.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Runtime.Serialization; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace Application.Exceptions 9 | { 10 | [Serializable] 11 | public class ApiException : Exception 12 | { 13 | public int StatusCode { get; } 14 | public List Errors { get; private set; } = new List(); 15 | 16 | public ApiException(int statuscode, List errors) 17 | { 18 | StatusCode = statuscode; 19 | Errors = errors; 20 | } 21 | public ApiException(int statuscode, string error) 22 | { 23 | StatusCode = statuscode; 24 | Errors.Add(error); 25 | } 26 | public ApiException(SerializationInfo info, StreamingContext context) : base(info, context) { } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Core/Application/Features/Roles/Commands/CreateRoleCommand.cs: -------------------------------------------------------------------------------- 1 | using Application.Constants; 2 | using Application.Dtos; 3 | using Application.Exceptions; 4 | using Application.Interfaces.Repositories; 5 | using Application.Interfaces.Services; 6 | using Application.Wrappers.Abstract; 7 | using Application.Wrappers.Concrete; 8 | using AutoMapper; 9 | using Domain.Entities; 10 | using MediatR; 11 | 12 | namespace Application.Features.Roles.Commands 13 | { 14 | public class CreateRoleCommand : IRequest 15 | { 16 | public string Name { get; set; } 17 | 18 | public class CreateRoleCommandHandler : IRequestHandler 19 | { 20 | private readonly IRoleRepository _roleRepository; 21 | private readonly IUnitOfWork _unitOfWork; 22 | private readonly IMapper _mapper; 23 | private readonly IEasyCacheService _easyCacheService; 24 | 25 | public CreateRoleCommandHandler(IRoleRepository roleRepository, IUnitOfWork unitOfWork, IEasyCacheService easyCacheService, IMapper mapper) 26 | { 27 | _roleRepository = roleRepository; 28 | _unitOfWork = unitOfWork; 29 | _easyCacheService = easyCacheService; 30 | _mapper = mapper; 31 | } 32 | 33 | public async Task Handle(CreateRoleCommand request, CancellationToken cancellationToken) 34 | { 35 | var existrole = await _roleRepository.GetAsync(x => x.Name == request.Name); 36 | if (existrole != null) 37 | { 38 | throw new ApiException(400, Messages.RoleNameAlreadyExist); 39 | } 40 | var role = await _roleRepository.AddAsync(new Role() 41 | { 42 | Name = request.Name 43 | }); 44 | await _unitOfWork.SaveChangesAsync(); 45 | await _easyCacheService.RemoveByPrefixAsync("GetAuthenticatedUserWithRoles"); 46 | var mappedrole = _mapper.Map(role); 47 | return new DataResponse(mappedrole, 200, Messages.AddedSuccesfully); 48 | } 49 | } 50 | } 51 | } -------------------------------------------------------------------------------- /src/Core/Application/Features/Roles/Commands/RemoveRoleCommand.cs: -------------------------------------------------------------------------------- 1 | using Application.Constants; 2 | using Application.Exceptions; 3 | using Application.Interfaces.Repositories; 4 | using Application.Interfaces.Services; 5 | using Application.Wrappers.Abstract; 6 | using Application.Wrappers.Concrete; 7 | using MediatR; 8 | 9 | namespace Application.Features.Roles.Commands 10 | { 11 | public class RemoveRoleCommand : IRequest 12 | { 13 | public Guid Id { get; set; } 14 | 15 | public RemoveRoleCommand(Guid id) 16 | { 17 | Id = id; 18 | } 19 | 20 | public class RemoveRoleCommandHandler : IRequestHandler 21 | { 22 | private readonly IRoleRepository _roleRepository; 23 | private readonly IUnitOfWork _unitOfWork; 24 | private readonly IEasyCacheService _easyCacheService; 25 | 26 | public RemoveRoleCommandHandler(IRoleRepository roleRepository, IUnitOfWork unitOfWork, IEasyCacheService easyCacheService) 27 | { 28 | _roleRepository = roleRepository; 29 | _unitOfWork = unitOfWork; 30 | _easyCacheService = easyCacheService; 31 | } 32 | 33 | public async Task Handle(RemoveRoleCommand request, CancellationToken cancellationToken) 34 | { 35 | var exisrole = await _roleRepository.GetByIdAsync(request.Id); 36 | if (exisrole == null) 37 | { 38 | throw new ApiException(404, Messages.NotFound); 39 | } 40 | _roleRepository.Remove(exisrole); 41 | await _unitOfWork.SaveChangesAsync(); 42 | await _easyCacheService.RemoveByPrefixAsync("GetAuthenticatedUserWithRoles"); 43 | return new SuccessResponse(200, Messages.DeletedSuccessfully); 44 | } 45 | } 46 | } 47 | } -------------------------------------------------------------------------------- /src/Core/Application/Features/Roles/Commands/UpdateRoleCommand.cs: -------------------------------------------------------------------------------- 1 | using Application.Constants; 2 | using Application.Exceptions; 3 | using Application.Interfaces.Repositories; 4 | using Application.Interfaces.Services; 5 | using Application.Wrappers.Abstract; 6 | using Application.Wrappers.Concrete; 7 | using AutoMapper; 8 | using MediatR; 9 | 10 | namespace Application.Features.Roles.Commands 11 | { 12 | public class UpdateRoleCommand : IRequest 13 | { 14 | public Guid Id { get; set; } 15 | public string Name { get; set; } 16 | 17 | public class UpdateRoleCommandHandler : IRequestHandler 18 | { 19 | private readonly IRoleRepository _roleRepository; 20 | private readonly IUnitOfWork _unitOfWork; 21 | private readonly IMapper _mapper; 22 | private readonly IEasyCacheService _easyCacheService; 23 | 24 | public UpdateRoleCommandHandler(IRoleRepository roleRepository, IUnitOfWork unitOfWork, IMapper mapper, IEasyCacheService easyCacheService) 25 | { 26 | _roleRepository = roleRepository; 27 | _unitOfWork = unitOfWork; 28 | _mapper = mapper; 29 | _easyCacheService = easyCacheService; 30 | } 31 | 32 | 33 | public async Task Handle(UpdateRoleCommand request, CancellationToken cancellationToken) 34 | { 35 | var existrole = await _roleRepository.GetByIdAsync(request.Id); 36 | if (existrole == null) 37 | { 38 | throw new ApiException(404, Messages.NotFound); 39 | } 40 | var roleName = await _roleRepository.GetAsync(x => x.Name == request.Name); 41 | if (roleName != null && existrole.Name != request.Name) 42 | { 43 | throw new ApiException(400, Messages.RoleNameAlreadyExist); 44 | } 45 | _mapper.Map(request, existrole); 46 | await _unitOfWork.SaveChangesAsync(); 47 | await _easyCacheService.RemoveByPrefixAsync("GetAuthenticatedUserWithRoles"); 48 | return new SuccessResponse(200, Messages.UpdatedSuccessfully); 49 | } 50 | } 51 | } 52 | } -------------------------------------------------------------------------------- /src/Core/Application/Features/Roles/Queries/GetAllRolesQuery.cs: -------------------------------------------------------------------------------- 1 | using Application.Dtos; 2 | using Application.Interfaces; 3 | using Application.Interfaces.Repositories; 4 | using Application.Wrappers.Abstract; 5 | using Application.Wrappers.Concrete; 6 | using AutoMapper; 7 | using MediatR; 8 | 9 | namespace Application.Features.Roles.Queries 10 | { 11 | public class GetAllRolesQuery : IRequest 12 | { 13 | public class GetAllRolesQueryHandler : IRequestHandler 14 | { 15 | private readonly IRoleRepository _roleRepository; 16 | private readonly IMapper _mapper; 17 | 18 | public GetAllRolesQueryHandler(IRoleRepository roleRepository, IMapper mapper) 19 | { 20 | _roleRepository = roleRepository; 21 | _mapper = mapper; 22 | } 23 | 24 | public async Task Handle(GetAllRolesQuery request, CancellationToken cancellationToken) 25 | { 26 | var roles = await _roleRepository.GetAllAsync(); 27 | var mappedroles = _mapper.Map>(roles); 28 | return new DataResponse>(mappedroles, 200); 29 | } 30 | } 31 | } 32 | } -------------------------------------------------------------------------------- /src/Core/Application/Features/Roles/Queries/GetRoleByIdQuery.cs: -------------------------------------------------------------------------------- 1 | using Application.Constants; 2 | using Application.Dtos; 3 | using Application.Exceptions; 4 | using Application.Interfaces; 5 | using Application.Interfaces.Repositories; 6 | using Application.Wrappers.Abstract; 7 | using Application.Wrappers.Concrete; 8 | using AutoMapper; 9 | using MediatR; 10 | 11 | namespace Application.Features.Roles.Queries 12 | { 13 | public class GetRoleByIdQuery : IRequest> 14 | { 15 | public Guid Id { get; set; } 16 | 17 | public GetRoleByIdQuery(Guid id) 18 | { 19 | Id = id; 20 | } 21 | 22 | public class GetRoleByIdQueryHandler : IRequestHandler> 23 | { 24 | private readonly IRoleRepository _roleRepository; 25 | private readonly IMapper _mapper; 26 | 27 | public GetRoleByIdQueryHandler(IRoleRepository roleRepository, IMapper mapper) 28 | { 29 | _roleRepository = roleRepository; 30 | _mapper = mapper; 31 | } 32 | 33 | public async Task> Handle(GetRoleByIdQuery request, CancellationToken cancellationToken) 34 | { 35 | var role = await _roleRepository.GetByIdAsync(request.Id); 36 | if (role == null) 37 | { 38 | throw new ApiException(404, Messages.NotFound); 39 | } 40 | var mappedrole = _mapper.Map(role); 41 | return new DataResponse(mappedrole, 200); 42 | } 43 | } 44 | } 45 | } -------------------------------------------------------------------------------- /src/Core/Application/Features/Roles/Queries/GetRolesByUserIdQuery.cs: -------------------------------------------------------------------------------- 1 | using Application.Dtos; 2 | using Application.Interfaces; 3 | using Application.Interfaces.Repositories; 4 | using Application.Wrappers.Abstract; 5 | using Application.Wrappers.Concrete; 6 | using MediatR; 7 | using System; 8 | using System.Collections.Generic; 9 | using System.Linq; 10 | using System.Text; 11 | using System.Threading.Tasks; 12 | 13 | namespace Application.Features.Roles.Queries 14 | { 15 | public class GetRolesByUserIdQuery : IRequest>> 16 | { 17 | public Guid UserId { get; set; } 18 | 19 | public GetRolesByUserIdQuery(Guid userid) 20 | { 21 | UserId = userid; 22 | } 23 | 24 | public class GetRolesByUserIdQueryHandler : IRequestHandler>> 25 | { 26 | 27 | private readonly IRoleRepository _roleRepository; 28 | 29 | public GetRolesByUserIdQueryHandler(IRoleRepository roleRepository) 30 | { 31 | _roleRepository = roleRepository; 32 | } 33 | 34 | public async Task>> Handle(GetRolesByUserIdQuery request, CancellationToken cancellationToken) 35 | { 36 | var roles = await _roleRepository.GetRolesByUserIdAsync(request.UserId); 37 | return new DataResponse>(roles, 200); 38 | } 39 | } 40 | 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /src/Core/Application/Features/Roles/Validators/RoleValidator.cs: -------------------------------------------------------------------------------- 1 | using Application.Features.Roles.Commands; 2 | using FluentValidation; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | 9 | namespace Application.Features.Roles.Validators 10 | { 11 | public class CreateRoleValidator : AbstractValidator 12 | { 13 | public CreateRoleValidator() 14 | { 15 | RuleFor(x => x.Name).NotEmpty().WithMessage("Role name is required"); 16 | } 17 | } 18 | 19 | public class UpdateRoleValidator : AbstractValidator 20 | { 21 | public UpdateRoleValidator() 22 | { 23 | RuleFor(x => x.Id).NotEmpty().WithMessage("Role ID is required"); 24 | RuleFor(x => x.Name).NotEmpty().WithMessage("Role name is required"); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/Core/Application/Features/Users/Commands/ChangeEmailCommand.cs: -------------------------------------------------------------------------------- 1 | using Application.Constants; 2 | using Application.Exceptions; 3 | using Application.Helpers; 4 | using Application.Interfaces.Repositories; 5 | using Application.Interfaces.Services; 6 | using Application.Wrappers.Abstract; 7 | using Application.Wrappers.Concrete; 8 | using MediatR; 9 | using System.Text.Json.Serialization; 10 | 11 | namespace Application.Features.Users.Commands 12 | { 13 | public class ChangeEmailCommand : IRequest 14 | { 15 | [JsonIgnore] 16 | public Guid UserId { get; set; } 17 | 18 | public string Email { get; set; } 19 | 20 | public ChangeEmailCommand(Guid userid, string email) 21 | { 22 | UserId = userid; 23 | Email = email; 24 | } 25 | 26 | public class ChangeEmailCommandHandler : IRequestHandler 27 | { 28 | private readonly IUserRepository _userRepository; 29 | private readonly IUnitOfWork _unitOfWork; 30 | private readonly IEmailService _emailService; 31 | private readonly IEasyCacheService _easyCacheService; 32 | 33 | public ChangeEmailCommandHandler(IUserRepository userRepository, IUnitOfWork unitOfWork, IEmailService emailService, IEasyCacheService easyCacheService) 34 | { 35 | _userRepository = userRepository; 36 | _unitOfWork = unitOfWork; 37 | _emailService = emailService; 38 | _easyCacheService = easyCacheService; 39 | } 40 | 41 | public async Task Handle(ChangeEmailCommand request, CancellationToken cancellationToken) 42 | { 43 | var user = await _userRepository.GetByIdAsync(request.UserId); 44 | if (user == null) 45 | { 46 | throw new ApiException(404, Messages.UserNotFound); 47 | } 48 | var email = await _userRepository.GetAsync(x => x.Email == request.Email); 49 | if (email != null && user.Email != request.Email) 50 | { 51 | throw new ApiException(400, Messages.EmailIsAlreadyExist); 52 | } 53 | user.Email = request.Email; 54 | user.EmailConfirmed = false; 55 | user.EmailConfirmedCode = null; 56 | user.EmailConfirmationCode = PasswordHelper.GenerateRandomString(20); 57 | await _unitOfWork.SaveChangesAsync(); 58 | string link = "http://localhost:5010/api/users/confirmemail/" + user.EmailConfirmationCode; 59 | await _emailService.ConfirmationMailAsync(link, request.Email); 60 | await _easyCacheService.RemoveByPrefixAsync("GetAuthenticatedUserWithRoles"); 61 | return new SuccessResponse(200, Messages.EmailSuccessfullyChangedConfirmYourEmail); 62 | } 63 | } 64 | } 65 | } -------------------------------------------------------------------------------- /src/Core/Application/Features/Users/Commands/ChangePasswordCommand.cs: -------------------------------------------------------------------------------- 1 | using Application.Constants; 2 | using Application.Exceptions; 3 | using Application.Helpers; 4 | using Application.Interfaces.Repositories; 5 | using Application.Wrappers.Abstract; 6 | using Application.Wrappers.Concrete; 7 | using MediatR; 8 | using System; 9 | using System.Collections.Generic; 10 | using System.Linq; 11 | using System.Text; 12 | using System.Text.Json.Serialization; 13 | using System.Threading.Tasks; 14 | 15 | namespace Application.Features.Users.Commands 16 | { 17 | public class ChangePasswordCommand : IRequest 18 | { 19 | [JsonIgnore] 20 | public Guid UserId { get; set; } 21 | public string CurrentPassword { get; set; } 22 | public string NewPassword { get; set; } 23 | public string ConfirmPassword { get; set; } 24 | 25 | 26 | public class ChangePasswordCommandHandler : IRequestHandler 27 | { 28 | private readonly IUserRepository _userRepository; 29 | private readonly IUnitOfWork _unitOfWork; 30 | 31 | public ChangePasswordCommandHandler(IUserRepository userRepository, IUnitOfWork unitOfWork) 32 | { 33 | _userRepository = userRepository; 34 | _unitOfWork = unitOfWork; 35 | } 36 | 37 | public async Task Handle(ChangePasswordCommand request, CancellationToken cancellationToken) 38 | { 39 | var user = await _userRepository.GetByIdAsync(request.UserId); 40 | if (user == null) 41 | { 42 | throw new ApiException(404, Messages.UserNotFound); 43 | } 44 | if (request.NewPassword != request.ConfirmPassword) 45 | { 46 | throw new ApiException(400, Messages.PasswordDontMatchWithConfirmation); 47 | } 48 | if (!PasswordHelper.VerifyHash(request.CurrentPassword, user.PasswordHash, user.PasswordSalt)) 49 | { 50 | throw new ApiException(400, Messages.CurrentPasswordIsFalse); 51 | } 52 | 53 | var (passwordHash, passwordSalt) = PasswordHelper.CreateHash(request.NewPassword); 54 | user.PasswordHash = passwordHash; 55 | user.PasswordSalt = passwordSalt; 56 | await _unitOfWork.SaveChangesAsync(); 57 | return new SuccessResponse(200, Messages.PasswordChangedSuccessfully); 58 | } 59 | } 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/Core/Application/Features/Users/Commands/ConfirmEmailCommand.cs: -------------------------------------------------------------------------------- 1 | using Application.Constants; 2 | using Application.Exceptions; 3 | using Application.Interfaces.Repositories; 4 | using Application.Wrappers.Abstract; 5 | using Application.Wrappers.Concrete; 6 | using MediatR; 7 | 8 | namespace Application.Features.Users.Commands 9 | { 10 | public class ConfirmEmailCommand : IRequest 11 | { 12 | public string EmailConfirmationCode { get; set; } 13 | 14 | public ConfirmEmailCommand(string code) 15 | { 16 | EmailConfirmationCode = code; 17 | } 18 | public class ConfirmEmailCommandHandler : IRequestHandler 19 | { 20 | private readonly IUserRepository _userRepository; 21 | private readonly IUnitOfWork _unitOfWork; 22 | 23 | public ConfirmEmailCommandHandler(IUserRepository userRepository, IUnitOfWork unitOfWork) 24 | { 25 | _userRepository = userRepository; 26 | _unitOfWork = unitOfWork; 27 | } 28 | 29 | public async Task Handle(ConfirmEmailCommand request, CancellationToken cancellationToken) 30 | { 31 | var user = await _userRepository.GetAsync(x => x.EmailConfirmationCode == request.EmailConfirmationCode || x.EmailConfirmedCode == request.EmailConfirmationCode); 32 | if (user == null) 33 | { 34 | throw new ApiException(404, Messages.UserNotFound); 35 | } 36 | if (user.EmailConfirmed) 37 | { 38 | throw new ApiException(400, Messages.AlreadyEmailConfirmed); 39 | } 40 | user.EmailConfirmed = true; 41 | user.EmailConfirmationCode = null; 42 | user.EmailConfirmedCode = request.EmailConfirmationCode; 43 | await _unitOfWork.SaveChangesAsync(); 44 | return new SuccessResponse(200, Messages.SuccessfullyEmailConfirmed); 45 | } 46 | } 47 | } 48 | } -------------------------------------------------------------------------------- /src/Core/Application/Features/Users/Commands/CreateTokenByRefreshTokenCommand.cs: -------------------------------------------------------------------------------- 1 | using Application.Constants; 2 | using Application.Dtos; 3 | using Application.Exceptions; 4 | using Application.Interfaces.Repositories; 5 | using Application.Interfaces.Services; 6 | using Application.Wrappers.Abstract; 7 | using Application.Wrappers.Concrete; 8 | using MediatR; 9 | 10 | namespace Application.Features.Users.Commands 11 | { 12 | public class CreateTokenByRefreshTokenCommand : IRequest 13 | { 14 | public string RefreshToken { get; set; } 15 | 16 | public class CreateTokenByRefreshTokenHandler : IRequestHandler 17 | { 18 | private readonly IUserRepository _userRepository; 19 | private readonly IRoleRepository _roleRepository; 20 | private readonly IRefreshTokenRepository _refreshTokenRepository; 21 | private readonly ITokenService _tokenService; 22 | private readonly IUnitOfWork _unitOfWork; 23 | private readonly IEasyCacheService _easyCacheService; 24 | 25 | public CreateTokenByRefreshTokenHandler(IUserRepository userRepository, IRefreshTokenRepository refreshTokenRepository, IUnitOfWork unitOfWork, ITokenService tokenService, IRoleRepository roleRepository, IEasyCacheService easyCacheService) 26 | { 27 | _userRepository = userRepository; 28 | _unitOfWork = unitOfWork; 29 | _tokenService = tokenService; 30 | _refreshTokenRepository = refreshTokenRepository; 31 | _roleRepository = roleRepository; 32 | _easyCacheService = easyCacheService; 33 | } 34 | 35 | public async Task Handle(CreateTokenByRefreshTokenCommand request, CancellationToken cancellationToken) 36 | { 37 | var existRefreshToken = await _refreshTokenRepository.GetAsync(x => x.Code == request.RefreshToken); 38 | if (existRefreshToken == null) 39 | { 40 | throw new ApiException(404, Messages.RefreshTokenNotFound); 41 | } 42 | var user = await _userRepository.GetByIdAsync(existRefreshToken.UserId); 43 | if (user == null) 44 | { 45 | throw new ApiException(404, Messages.UserNotFound); 46 | } 47 | if (existRefreshToken.Expiration < DateTime.Now) 48 | { 49 | throw new ApiException(404, Messages.RefreshTokenExpired); 50 | } 51 | var roles = await _roleRepository.GetAllAsync(x => x.UserRoles.Any(y => y.UserId == user.Id)); 52 | List roleNames = new List(); 53 | foreach (var role in roles) 54 | { 55 | roleNames.Add(role.Name); 56 | } 57 | var tokendto = _tokenService.CreateToken(user, roleNames); 58 | existRefreshToken.Code = tokendto.RefreshToken; 59 | existRefreshToken.Expiration = tokendto.RefreshTokenExpiration; 60 | await _unitOfWork.SaveChangesAsync(); 61 | await _easyCacheService.RemoveByPrefixAsync("GetAuthenticatedUserWithRoles"); 62 | return new DataResponse(tokendto, 200); 63 | } 64 | } 65 | } 66 | } -------------------------------------------------------------------------------- /src/Core/Application/Features/Users/Commands/ForgetPasswordCommand.cs: -------------------------------------------------------------------------------- 1 | using Application.Constants; 2 | using Application.Exceptions; 3 | using Application.Helpers; 4 | using Application.Interfaces.Repositories; 5 | using Application.Interfaces.Services; 6 | using Application.Wrappers.Abstract; 7 | using Application.Wrappers.Concrete; 8 | using MediatR; 9 | using System; 10 | using System.Collections.Generic; 11 | using System.Linq; 12 | using System.Text; 13 | using System.Threading.Tasks; 14 | 15 | namespace Application.Features.Users.Commands 16 | { 17 | public class ForgetPasswordCommand : IRequest 18 | { 19 | public string Email { get; set; } 20 | 21 | public class ForgetPasswordCommandHandler : IRequestHandler 22 | { 23 | private readonly IUserRepository _userRepository; 24 | private readonly IUnitOfWork _unitOfWork; 25 | private readonly IEmailService _emailService; 26 | public ForgetPasswordCommandHandler(IUserRepository userRepository, IUnitOfWork unitOfWork, IEmailService emailService) 27 | { 28 | _userRepository = userRepository; 29 | _unitOfWork = unitOfWork; 30 | _emailService = emailService; 31 | } 32 | public async Task Handle(ForgetPasswordCommand request, CancellationToken cancellationToken) 33 | { 34 | var user = await _userRepository.GetAsync(x => x.Email == request.Email); 35 | if (user == null) 36 | { 37 | throw new ApiException(404, Messages.UserNotFound); 38 | } 39 | user.ResetPasswordCode = PasswordHelper.GenerateRandomString(20); 40 | await _unitOfWork.SaveChangesAsync(); 41 | string link = "http://localhost:5010/api/users/resetpassword/" + user.ResetPasswordCode + "/" + user.Email; 42 | await _emailService.ForgetPasswordMailAsync(link, user.Email); 43 | return new SuccessResponse(200, Messages.IfEmailTrue); 44 | } 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/Core/Application/Features/Users/Commands/LoginCommand.cs: -------------------------------------------------------------------------------- 1 | using Application.Constants; 2 | using Application.Dtos; 3 | using Application.Exceptions; 4 | using Application.Helpers; 5 | using Application.Interfaces.Repositories; 6 | using Application.Interfaces.Services; 7 | using Application.Wrappers.Abstract; 8 | using Application.Wrappers.Concrete; 9 | using Domain.Entities; 10 | using MediatR; 11 | using Serilog; 12 | 13 | namespace Application.Features.Users.Commands 14 | { 15 | public class LoginCommand : IRequest 16 | { 17 | public string UserName { get; set; } 18 | public string Password { get; set; } 19 | 20 | public class LoginCommandHandler : IRequestHandler 21 | { 22 | private readonly IUserRepository _userRepository; 23 | private readonly IRoleRepository _roleRepository; 24 | private readonly ITokenService _tokenService; 25 | private readonly IRefreshTokenRepository _refreshTokenRepository; 26 | private readonly IUnitOfWork _unitOfWork; 27 | 28 | public LoginCommandHandler(IUserRepository userRepository, IRoleRepository roleRepository, IUnitOfWork unitOfWork, ITokenService tokenService, IRefreshTokenRepository refreshTokenRepository) 29 | { 30 | _userRepository = userRepository; 31 | _roleRepository = roleRepository; 32 | _tokenService = tokenService; 33 | _refreshTokenRepository = refreshTokenRepository; 34 | _unitOfWork = unitOfWork; 35 | } 36 | 37 | public async Task Handle(LoginCommand request, CancellationToken cancellationToken) 38 | { 39 | var user = await _userRepository.GetAsync(x => x.UserName == request.UserName, noTracking: true); 40 | if (user == null) 41 | { 42 | throw new ApiException(404, Messages.UserNotFound); 43 | } 44 | if (!user.EmailConfirmed) 45 | { 46 | throw new ApiException(400, Messages.ConfirmYourEmail); 47 | } 48 | if (!PasswordHelper.VerifyHash(request.Password, user.PasswordHash, user.PasswordSalt)) 49 | { 50 | throw new ApiException(400, Messages.UserNameOrPasswordIsIncorrect); 51 | } 52 | var roles = await _roleRepository.GetAllAsync(x => x.UserRoles.Any(y => y.UserId == user.Id)); 53 | List roleNames = new List(); 54 | foreach (var role in roles) 55 | { 56 | roleNames.Add(role.Name); 57 | } 58 | var tokendto = _tokenService.CreateToken(user, roleNames); 59 | var refreshToken = await _refreshTokenRepository.GetAsync(x => x.UserId == user.Id); 60 | if (refreshToken == null) 61 | { 62 | await _refreshTokenRepository.AddAsync(new RefreshToken { UserId = user.Id, Code = tokendto.RefreshToken, Expiration = tokendto.RefreshTokenExpiration }); 63 | await _unitOfWork.SaveChangesAsync(); 64 | } 65 | else 66 | { 67 | refreshToken.Code = tokendto.RefreshToken; 68 | refreshToken.Expiration = tokendto.RefreshTokenExpiration; 69 | await _unitOfWork.SaveChangesAsync(); 70 | } 71 | return new DataResponse(tokendto, 200); 72 | } 73 | } 74 | } 75 | } -------------------------------------------------------------------------------- /src/Core/Application/Features/Users/Commands/RegisterCommand.cs: -------------------------------------------------------------------------------- 1 | using Application.Constants; 2 | using Application.Exceptions; 3 | using Application.Helpers; 4 | using Application.Interfaces.Repositories; 5 | using Application.Interfaces.Services; 6 | using Application.Wrappers.Abstract; 7 | using Application.Wrappers.Concrete; 8 | using AutoMapper; 9 | using Domain.Entities; 10 | using MediatR; 11 | 12 | namespace Application.Features.Users.Commands 13 | { 14 | public class RegisterCommand : IRequest 15 | { 16 | public string UserName { get; set; } 17 | public string FirstName { get; set; } 18 | public string LastName { get; set; } 19 | public string Email { get; set; } 20 | public string Password { get; set; } 21 | public string ConfirmPassword { get; set; } 22 | 23 | public class RegisterCommandHandler : IRequestHandler 24 | { 25 | private readonly IUserRepository _userRepository; 26 | private readonly IUnitOfWork _unitOfWork; 27 | private readonly IEmailService _emailService; 28 | private readonly IMapper _mapper; 29 | 30 | public RegisterCommandHandler(IUserRepository userRepository, IUnitOfWork unitOfWork, IEmailService emailService, IMapper mapper) 31 | { 32 | _userRepository = userRepository; 33 | _unitOfWork = unitOfWork; 34 | _emailService = emailService; 35 | _mapper = mapper; 36 | } 37 | 38 | public async Task Handle(RegisterCommand request, CancellationToken cancellationToken) 39 | { 40 | // Example transaction usage 41 | // using var transaction = _unitOfWork.BeginTransaction(); 42 | //try 43 | //{ 44 | var existuser = await _userRepository.GetAsync(x => x.UserName == request.UserName || x.Email == request.Email, noTracking: true); 45 | if (existuser?.UserName == request.UserName) 46 | throw new ApiException(400, Messages.UsernameIsAlreadyExist); 47 | 48 | if (existuser?.Email == request.Email) 49 | throw new ApiException(400, Messages.EmailIsAlreadyExist); 50 | 51 | 52 | var (passwordHash, passwordSalt) = PasswordHelper.CreateHash(request.Password); 53 | var user = _mapper.Map(request); 54 | user.PasswordHash = passwordHash; 55 | user.PasswordSalt = passwordSalt; 56 | user.EmailConfirmationCode = PasswordHelper.GenerateRandomString(20); 57 | await _userRepository.AddAsync(user); 58 | await _unitOfWork.SaveChangesAsync(); 59 | //transaction.Commit(); 60 | 61 | //string link = "http://localhost:8080/confirmemail/" + user.EmailConfirmationCode; if u use spa you must use this link example 62 | string link = "http://localhost:5010/api/users/confirmemail/" + user.EmailConfirmationCode; 63 | await _emailService.ConfirmationMailAsync(link, request.Email); 64 | return new SuccessResponse(200, Messages.RegisterSuccessfully); 65 | //} 66 | //catch (Exception ex) 67 | //{ 68 | // transaction.Rollback(); 69 | // // Logging ... 70 | //} 71 | } 72 | } 73 | } 74 | } -------------------------------------------------------------------------------- /src/Core/Application/Features/Users/Commands/RemoveUserCommand.cs: -------------------------------------------------------------------------------- 1 | using Application.Constants; 2 | using Application.Exceptions; 3 | using Application.Interfaces.Repositories; 4 | using Application.Interfaces.Services; 5 | using Application.Wrappers.Abstract; 6 | using Application.Wrappers.Concrete; 7 | using MediatR; 8 | using System; 9 | using System.Collections.Generic; 10 | using System.Linq; 11 | using System.Text; 12 | using System.Threading.Tasks; 13 | 14 | namespace Application.Features.Users.Commands 15 | { 16 | public class RemoveUserCommand : IRequest 17 | { 18 | public Guid UserId { get; set; } 19 | 20 | public RemoveUserCommand(Guid userid) 21 | { 22 | UserId = userid; 23 | } 24 | 25 | public class RemoveUserCommandHandler : IRequestHandler 26 | { 27 | private readonly IUserRepository _userRepository; 28 | private readonly IUnitOfWork _unitOfWork; 29 | private readonly IEasyCacheService _easyCacheService; 30 | 31 | public RemoveUserCommandHandler(IUserRepository userRepository, IUnitOfWork unitOfWork, IEasyCacheService easyCacheService) 32 | { 33 | _userRepository = userRepository; 34 | _unitOfWork = unitOfWork; 35 | _easyCacheService = easyCacheService; 36 | } 37 | 38 | public async Task Handle(RemoveUserCommand request, CancellationToken cancellationToken) 39 | { 40 | var user = await _userRepository.GetByIdAsync(request.UserId); 41 | if (user == null) 42 | { 43 | throw new ApiException(404, Messages.UserNotFound); 44 | } 45 | _userRepository.Remove(user); 46 | await _unitOfWork.SaveChangesAsync(); 47 | await _easyCacheService.RemoveByPrefixAsync("GetAuthenticatedUserWithRoles"); 48 | return new SuccessResponse(200, Messages.DeletedSuccessfully); 49 | } 50 | } 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/Core/Application/Features/Users/Commands/ResetPasswordCommand.cs: -------------------------------------------------------------------------------- 1 | using Application.Constants; 2 | using Application.Exceptions; 3 | using Application.Helpers; 4 | using Application.Interfaces.Repositories; 5 | using Application.Interfaces.Services; 6 | using Application.Wrappers.Abstract; 7 | using Application.Wrappers.Concrete; 8 | using MediatR; 9 | using System; 10 | using System.Collections.Generic; 11 | using System.Linq; 12 | using System.Text; 13 | using System.Threading.Tasks; 14 | 15 | namespace Application.Features.Users.Commands 16 | { 17 | public class ResetPasswordCommand : IRequest 18 | { 19 | public string ResetPasswordCode { get; set; } 20 | public string Email { get; set; } 21 | 22 | public ResetPasswordCommand(string code, string email) 23 | { 24 | ResetPasswordCode = code; 25 | Email = email; 26 | } 27 | 28 | public class ResetPasswordCommandHandler : IRequestHandler 29 | { 30 | private readonly IUserRepository _userRepository; 31 | private readonly IUnitOfWork _unitOfWork; 32 | private readonly IEmailService _emailService; 33 | 34 | public ResetPasswordCommandHandler(IUserRepository userRepository, IUnitOfWork unitOfWork, IEmailService emailService) 35 | { 36 | _userRepository = userRepository; 37 | _unitOfWork = unitOfWork; 38 | _emailService = emailService; 39 | } 40 | 41 | public async Task Handle(ResetPasswordCommand request, CancellationToken cancellationToken) 42 | { 43 | var user = await _userRepository.GetAsync(x => x.Email == request.Email); 44 | if (user == null) 45 | { 46 | throw new ApiException(404, Messages.UserNotFound); 47 | } 48 | var controlcode = await _userRepository.GetAsync(x => x.ResetPasswordCode == request.ResetPasswordCode); 49 | if (controlcode == null) 50 | { 51 | throw new ApiException(400, Messages.ResetPasswordCodeInvalid); 52 | } 53 | 54 | var newPassword = PasswordHelper.GenerateRandomString(8); 55 | var (passwordHash, passwordSalt) = PasswordHelper.CreateHash(newPassword); 56 | user.PasswordHash = passwordHash; 57 | user.PasswordSalt = passwordSalt; 58 | user.ResetPasswordCode = null; 59 | await _unitOfWork.SaveChangesAsync(); 60 | await _emailService.SendNewPasswordAsync(newPassword, user.Email); 61 | return new SuccessResponse(200, Messages.PasswordSuccessfullyReset); 62 | } 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/Core/Application/Features/Users/Commands/UpdateUserCommand.cs: -------------------------------------------------------------------------------- 1 | using Application.Constants; 2 | using Application.Exceptions; 3 | using Application.Interfaces.Repositories; 4 | using Application.Interfaces.Services; 5 | using Application.Wrappers.Abstract; 6 | using Application.Wrappers.Concrete; 7 | using AutoMapper; 8 | using MediatR; 9 | using System.Text.Json.Serialization; 10 | 11 | namespace Application.Features.Users.Commands 12 | { 13 | public class UpdateUserCommand : IRequest 14 | { 15 | [JsonIgnore] 16 | public Guid UserId { get; set; } 17 | public string UserName { get; set; } 18 | public string FirstName { get; set; } 19 | public string LastName { get; set; } 20 | 21 | public class UpdateUserCommandHandler : IRequestHandler 22 | { 23 | private readonly IUserRepository _userRepository; 24 | private readonly IUnitOfWork _unitOfWork; 25 | private readonly IMapper _mapper; 26 | private readonly IEasyCacheService _easyCacheService; 27 | 28 | public UpdateUserCommandHandler(IUserRepository userRepository, IUnitOfWork unitOfWork, IMapper mapper, IEasyCacheService easyCacheService) 29 | { 30 | _userRepository = userRepository; 31 | _unitOfWork = unitOfWork; 32 | _mapper = mapper; 33 | _easyCacheService = easyCacheService; 34 | } 35 | 36 | public async Task Handle(UpdateUserCommand request, CancellationToken cancellationToken) 37 | { 38 | var user = await _userRepository.GetByIdAsync(request.UserId); 39 | if (user == null) 40 | { 41 | throw new ApiException(404, Messages.UserNotFound); 42 | } 43 | var username = await _userRepository.GetAsync(x => x.UserName == request.UserName); 44 | if (username != null && user.UserName != request.UserName) 45 | { 46 | throw new ApiException(400, Messages.UsernameIsAlreadyExist); 47 | } 48 | _mapper.Map(request, user); 49 | await _unitOfWork.SaveChangesAsync(); 50 | await _easyCacheService.RemoveByPrefixAsync("GetAuthenticatedUserWithRoles"); 51 | return new SuccessResponse(200, Messages.UpdatedSuccessfully); 52 | } 53 | } 54 | } 55 | } -------------------------------------------------------------------------------- /src/Core/Application/Features/Users/Commands/UpdateUserRoleCommand.cs: -------------------------------------------------------------------------------- 1 | using Application.Constants; 2 | using Application.Exceptions; 3 | using Application.Interfaces.Repositories; 4 | using Application.Interfaces.Services; 5 | using Application.Wrappers.Abstract; 6 | using Application.Wrappers.Concrete; 7 | using Domain.Entities; 8 | using MediatR; 9 | using System; 10 | using System.Collections.Generic; 11 | using System.Linq; 12 | using System.Text; 13 | using System.Threading.Tasks; 14 | 15 | namespace Application.Features.Users.Commands 16 | { 17 | public class UpdateUserRoleCommand : IRequest 18 | { 19 | public Guid UserId { get; set; } 20 | public Guid[] RoleIds { get; set; } 21 | 22 | 23 | public class UpdateUserRoleCommandHandler : IRequestHandler 24 | { 25 | private readonly IUserRepository _userRepository; 26 | private readonly IUnitOfWork _unitOfWork; 27 | private readonly IEasyCacheService _easyCacheService; 28 | 29 | public UpdateUserRoleCommandHandler(IUserRepository userRepository, IUnitOfWork unitOfWork, IEasyCacheService easyCacheService) 30 | { 31 | _userRepository = userRepository; 32 | _unitOfWork = unitOfWork; 33 | _easyCacheService = easyCacheService; 34 | } 35 | 36 | public async Task Handle(UpdateUserRoleCommand request, CancellationToken cancellationToken) 37 | { 38 | var user = await _userRepository.GetUserRolesByUserIdAsync(request.UserId); 39 | if (user == null) 40 | { 41 | throw new ApiException(404, Messages.UserNotFound); 42 | } 43 | if (request.RoleIds != null) 44 | { 45 | user.UserRoles = request.RoleIds.Select(roleid => new UserRole 46 | { 47 | RoleId = roleid 48 | }).ToList(); 49 | } 50 | else 51 | { 52 | user.UserRoles = new List(); 53 | } 54 | await _unitOfWork.SaveChangesAsync(); 55 | await _easyCacheService.RemoveByPrefixAsync("GetAuthenticatedUserWithRoles"); 56 | return new SuccessResponse(200, Messages.UserRolesUpdatedSuccessfully); 57 | } 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/Core/Application/Features/Users/Queries/GetAllUsersWithRolesQuery.cs: -------------------------------------------------------------------------------- 1 | using Application.Dtos; 2 | using Application.Interfaces.Repositories; 3 | using Application.Wrappers.Concrete; 4 | using MediatR; 5 | using Microsoft.Extensions.Logging; 6 | 7 | namespace Application.Features.Users.Queries 8 | { 9 | public class GetAllUsersWithRolesQuery : IRequest>> 10 | { 11 | public class GetAllUsersWithRolesQueryHandler : IRequestHandler>> 12 | { 13 | private readonly IUserRepository _userRepository; 14 | private readonly ILogger _logger; 15 | 16 | public GetAllUsersWithRolesQueryHandler(IUserRepository userRepository, ILogger logger) 17 | { 18 | _userRepository = userRepository; 19 | _logger = logger; 20 | } 21 | 22 | public async Task>> Handle(GetAllUsersWithRolesQuery request, CancellationToken cancellationToken) 23 | { 24 | var userswithroles = await _userRepository.GetAllUsersWithRolesAsync(); 25 | _logger.LogInformation("GetAllUserWithRoles = {@GetAllUserWithRoles}",userswithroles); 26 | return new DataResponse>(userswithroles, 200); 27 | } 28 | } 29 | } 30 | } -------------------------------------------------------------------------------- /src/Core/Application/Features/Users/Queries/GetAuthenticatedUserWithRolesQuery.cs: -------------------------------------------------------------------------------- 1 | using Application.Constants; 2 | using Application.Dtos; 3 | using Application.Exceptions; 4 | using Application.Interfaces; 5 | using Application.Interfaces.Repositories; 6 | using Application.Wrappers.Concrete; 7 | using MediatR; 8 | 9 | namespace Application.Features.Users.Queries 10 | { 11 | public class GetAuthenticatedUserWithRolesQuery : IRequest>, ICacheable 12 | { 13 | public Guid UserId { get; set; } 14 | 15 | public bool BypassCache => false; 16 | 17 | public string CacheKey => $"GetAuthenticatedUserWithRoles-{UserId}"; 18 | 19 | public GetAuthenticatedUserWithRolesQuery(Guid userId) 20 | { 21 | UserId = userId; 22 | } 23 | 24 | public class GetAuthenticatedUserWithRolesQueryHandler : IRequestHandler> 25 | { 26 | private readonly IUserRepository _userRepository; 27 | 28 | public GetAuthenticatedUserWithRolesQueryHandler(IUserRepository userRepository) 29 | { 30 | _userRepository = userRepository; 31 | } 32 | 33 | public async Task> Handle(GetAuthenticatedUserWithRolesQuery request, CancellationToken cancellationToken) 34 | { 35 | var user = await _userRepository.GetByIdAsync(request.UserId); 36 | if (user == null) 37 | { 38 | throw new ApiException(404, Messages.UserNotFound); 39 | } 40 | var userwithroles = await _userRepository.GetUserWithRolesAsync(request.UserId); 41 | return new DataResponse(userwithroles, 200); 42 | } 43 | } 44 | } 45 | } -------------------------------------------------------------------------------- /src/Core/Application/Features/Users/Validators/ChangeEmailValidator.cs: -------------------------------------------------------------------------------- 1 | using Application.Features.Users.Commands; 2 | using FluentValidation; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | 9 | namespace Application.Features.Users.Validators 10 | { 11 | public class ChangeEmailValidator : AbstractValidator 12 | { 13 | public ChangeEmailValidator() 14 | { 15 | RuleFor(x => x.Email).NotEmpty().WithMessage("Email is required"); 16 | RuleFor(x => x.Email).EmailAddress().WithMessage("Email is not valid"); 17 | 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/Core/Application/Features/Users/Validators/ChangePasswordValidator.cs: -------------------------------------------------------------------------------- 1 | using Application.Features.Users.Commands; 2 | using FluentValidation; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | 9 | namespace Application.Features.Users.Validators 10 | { 11 | public class ChangePasswordValidator : AbstractValidator 12 | { 13 | public ChangePasswordValidator() 14 | { 15 | RuleFor(x => x.CurrentPassword).NotEmpty().WithMessage("Current password is required"); 16 | RuleFor(x => x.NewPassword).NotEmpty().WithMessage("New Password is required") 17 | .MinimumLength(8).WithMessage("Your password length must be at least 8.") 18 | .MaximumLength(16).WithMessage("Your password length must not exceed 16.") 19 | .Matches(@"[A-Z]+").WithMessage("Your password must contain at least one uppercase letter.") 20 | .Matches(@"[a-z]+").WithMessage("Your password must contain at least one lowercase letter.") 21 | .Matches(@"[0-9]+").WithMessage("Your password must contain at least one number."); 22 | RuleFor(x => x.ConfirmPassword).NotEmpty().WithMessage("Confirm password is required").Equal(x => x.NewPassword).WithMessage("New Password and ConfirmPassword must match"); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/Core/Application/Features/Users/Validators/ForgetPasswordValidator.cs: -------------------------------------------------------------------------------- 1 | using Application.Features.Users.Commands; 2 | using FluentValidation; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | 9 | namespace Application.Features.Users.Validators 10 | { 11 | public class ForgetPasswordValidator : AbstractValidator 12 | { 13 | public ForgetPasswordValidator() 14 | { 15 | RuleFor(x => x.Email).NotEmpty().EmailAddress(); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/Core/Application/Features/Users/Validators/LoginValidator.cs: -------------------------------------------------------------------------------- 1 | using Application.Features.Users.Commands; 2 | using FluentValidation; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | 9 | namespace Application.Features.Users.Validators 10 | { 11 | public class LoginValidator : AbstractValidator 12 | { 13 | public LoginValidator() 14 | { 15 | RuleFor(x => x.UserName).NotEmpty().WithMessage("UserName is required"); 16 | RuleFor(x => x.Password).NotEmpty().WithMessage("Password is required"); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/Core/Application/Features/Users/Validators/RefreshTokenValidator.cs: -------------------------------------------------------------------------------- 1 | using Application.Features.Users.Commands; 2 | using FluentValidation; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | 9 | namespace Application.Features.Users.Validators 10 | { 11 | public class RefreshTokenValidator : AbstractValidator 12 | { 13 | public RefreshTokenValidator() 14 | { 15 | RuleFor(x => x.RefreshToken).NotEmpty().WithMessage("Refresh token is required"); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/Core/Application/Features/Users/Validators/RegisterValidator.cs: -------------------------------------------------------------------------------- 1 | using Application.Features.Users.Commands; 2 | using FluentValidation; 3 | 4 | namespace Application.Features.Users.Validators 5 | { 6 | public class RegisterValidator : AbstractValidator 7 | { 8 | public RegisterValidator() 9 | { 10 | RuleFor(x => x.UserName).NotEmpty().WithMessage("UserName is required"); 11 | RuleFor(x => x.FirstName).NotEmpty().WithMessage("FirstName is required"); 12 | RuleFor(x => x.LastName).NotEmpty().WithMessage("LastName is required"); 13 | RuleFor(x => x.Email).NotEmpty().WithMessage("Email is required"); 14 | RuleFor(x => x.Email).EmailAddress().WithMessage("Email is not valid"); 15 | RuleFor(x => x.Password).NotEmpty().WithMessage("Password is required") 16 | .MinimumLength(8).WithMessage("Your password length must be at least 8.") 17 | .MaximumLength(16).WithMessage("Your password length must not exceed 16.") 18 | .Matches(@"[A-Z]+").WithMessage("Your password must contain at least one uppercase letter.") 19 | .Matches(@"[a-z]+").WithMessage("Your password must contain at least one lowercase letter.") 20 | .Matches(@"[0-9]+").WithMessage("Your password must contain at least one number."); 21 | RuleFor(x => x.ConfirmPassword).NotEmpty().WithMessage("Confirm password is required").Equal(x => x.Password).WithMessage("Password and ConfirmPassword must match"); 22 | } 23 | } 24 | } -------------------------------------------------------------------------------- /src/Core/Application/Features/Users/Validators/UpdateUserRoleValidator.cs: -------------------------------------------------------------------------------- 1 | using Application.Features.Users.Commands; 2 | using FluentValidation; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | 9 | namespace Application.Features.Users.Validators 10 | { 11 | public class UpdateUserRoleValidator : AbstractValidator 12 | { 13 | public UpdateUserRoleValidator() 14 | { 15 | RuleFor(x => x.UserId).NotEmpty().WithMessage("UserId is required"); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/Core/Application/Features/Users/Validators/UpdateUserValidator.cs: -------------------------------------------------------------------------------- 1 | using Application.Features.Users.Commands; 2 | using FluentValidation; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | 9 | namespace Application.Features.Users.Validators 10 | { 11 | public class UpdateUserValidator : AbstractValidator 12 | { 13 | public UpdateUserValidator() 14 | { 15 | RuleFor(x => x.FirstName).NotEmpty().WithMessage("First name is required"); 16 | RuleFor(x => x.LastName).NotEmpty().WithMessage("Last name is required"); 17 | RuleFor(x => x.UserName).NotEmpty().WithMessage("User name is required"); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/Core/Application/Helpers/PasswordHelper.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Security.Cryptography; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace Application.Helpers 9 | { 10 | public static class PasswordHelper 11 | { 12 | public static (byte[] passwordHash, byte[] passwordSalt) CreateHash(string password) 13 | { 14 | using var hmac = new HMACSHA512(); 15 | return ( 16 | passwordHash: hmac.ComputeHash(Encoding.UTF8.GetBytes(password)), 17 | passwordSalt: hmac.Key); 18 | } 19 | 20 | public static bool VerifyHash(string password, byte[] hash, byte[] salt) 21 | { 22 | using var hmac = new HMACSHA512(salt); 23 | var computedHash = hmac.ComputeHash(Encoding.UTF8.GetBytes(password)); 24 | for (var i = 0; i < computedHash.Length; i++) 25 | if (computedHash[i] != hash[i]) 26 | return false; 27 | 28 | return true; 29 | } 30 | 31 | public static string GenerateRandomString(int length,bool createGuid = true) 32 | { 33 | var randomstring = new string(Enumerable 34 | .Repeat("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", length) 35 | .Select(x => 36 | { 37 | var cryptoResult = new byte[4]; 38 | using (var cryptoProvider = RandomNumberGenerator.Create()) 39 | cryptoProvider.GetBytes(cryptoResult); 40 | return x[new Random(BitConverter.ToInt32(cryptoResult, 0)).Next(x.Length)]; 41 | }) 42 | .ToArray()); 43 | if (createGuid) 44 | return randomstring + Guid.NewGuid().ToString().Replace("-", ""); 45 | else 46 | return randomstring; 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/Core/Application/Interfaces/ICacheable.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 Application.Interfaces 8 | { 9 | public interface ICacheable 10 | { 11 | bool BypassCache { get; } 12 | string CacheKey { get; } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/Core/Application/Interfaces/Repositories/IRefreshTokenRepository.cs: -------------------------------------------------------------------------------- 1 | using Domain.Entities; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace Application.Interfaces.Repositories 9 | { 10 | public interface IRefreshTokenRepository : IRepository 11 | { 12 | 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/Core/Application/Interfaces/Repositories/IRepository.cs: -------------------------------------------------------------------------------- 1 | using Domain.Entities.Base; 2 | using System.Linq.Expressions; 3 | 4 | namespace Application.Interfaces.Repositories 5 | { 6 | public interface IRepository where TEntity : BaseEntity 7 | { 8 | TEntity Add(TEntity entity); 9 | 10 | Task AddAsync(TEntity entity); 11 | 12 | IEnumerable AddRange(IEnumerable entities); 13 | 14 | Task> AddRangeAsync(IEnumerable entities); 15 | 16 | void Update(TEntity entity); 17 | 18 | void UpdateRange(IEnumerable entities); 19 | 20 | void Remove(TEntity entity); 21 | 22 | void RemoveRange(IEnumerable entities); 23 | 24 | TEntity GetById(TKey id); 25 | 26 | Task GetByIdAsync(TKey id); 27 | 28 | IEnumerable GetAll(Expression> expression = null, bool noTracking = true); 29 | 30 | Task> GetAllAsync(Expression> expression = null, bool noTracking = true); 31 | 32 | TEntity Get(Expression> expression, bool noTracking = false); 33 | 34 | Task GetAsync(Expression> expression, bool noTracking = false); 35 | 36 | IQueryable Queryable(); 37 | 38 | Task GetCountAsync(Expression> expression = null); 39 | 40 | int GetCount(Expression> expression = null); 41 | } 42 | } -------------------------------------------------------------------------------- /src/Core/Application/Interfaces/Repositories/IRoleRepository.cs: -------------------------------------------------------------------------------- 1 | using Application.Dtos; 2 | using Domain.Entities; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | 9 | namespace Application.Interfaces.Repositories 10 | { 11 | public interface IRoleRepository : IRepository 12 | { 13 | Task> GetRolesByUserIdAsync(Guid userId); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/Core/Application/Interfaces/Repositories/IUnitOfWork.cs: -------------------------------------------------------------------------------- 1 | using System.Data; 2 | 3 | namespace Application.Interfaces.Repositories 4 | { 5 | public interface IUnitOfWork 6 | { 7 | Task SaveChangesAsync(); 8 | void SaveChanges(); 9 | 10 | IDbTransaction BeginTransaction(); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/Core/Application/Interfaces/Repositories/IUserRepository.cs: -------------------------------------------------------------------------------- 1 | using Application.Dtos; 2 | using Domain.Entities; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | 9 | namespace Application.Interfaces.Repositories 10 | { 11 | public interface IUserRepository : IRepository 12 | { 13 | Task GetUserWithRolesAsync(Guid userid); 14 | Task> GetAllUsersWithRolesAsync(); 15 | Task GetUserRolesByUserIdAsync(Guid userid); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/Core/Application/Interfaces/Services/IEasyCacheService.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 Application.Interfaces.Services 8 | { 9 | public interface IEasyCacheService 10 | { 11 | Task GetAsync(string key,Type cachedData); 12 | Task SetAsync(string key, T value,TimeSpan? expiration = null); 13 | Task RemoveAsync(string key); 14 | Task RemoveByPrefixAsync(string prefix); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/Core/Application/Interfaces/Services/IEmailService.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 Application.Interfaces.Services 8 | { 9 | public interface IEmailService 10 | { 11 | Task ConfirmationMailAsync(string link, string email); 12 | Task ForgetPasswordMailAsync(string link, string email); 13 | 14 | Task SendNewPasswordAsync(string password, string email); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/Core/Application/Interfaces/Services/ITokenService.cs: -------------------------------------------------------------------------------- 1 | using Application.Dtos; 2 | using Domain.Entities; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | 9 | namespace Application.Interfaces.Services 10 | { 11 | public interface ITokenService 12 | { 13 | TokenDTO CreateToken(User user,List roles); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/Core/Application/Mappings/Automapper.cs: -------------------------------------------------------------------------------- 1 | using Application.Dtos; 2 | using Application.Features.Roles.Commands; 3 | using Application.Features.Users.Commands; 4 | using AutoMapper; 5 | using Domain.Entities; 6 | using System; 7 | using System.Collections.Generic; 8 | using System.Linq; 9 | using System.Text; 10 | using System.Threading.Tasks; 11 | 12 | namespace Application.Mappings 13 | { 14 | public class Automapper : Profile 15 | { 16 | public Automapper() 17 | { 18 | CreateMap().ReverseMap(); 19 | CreateMap().ReverseMap(); 20 | CreateMap().ReverseMap(); 21 | CreateMap().ReverseMap(); 22 | CreateMap().ReverseMap(); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/Core/Application/Wrappers/Abstract/IDataResponse.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 Application.Wrappers.Abstract 8 | { 9 | public interface IDataResponse : IResponse 10 | { 11 | T Data { get; } 12 | string Message { get; } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/Core/Application/Wrappers/Abstract/IErrorResponse.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 Application.Wrappers.Abstract 8 | { 9 | public interface IErrorResponse : IResponse 10 | { 11 | List Errors { get; } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/Core/Application/Wrappers/Abstract/IPagedDataResponse.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 Application.Wrappers.Abstract 8 | { 9 | interface IPagedDataResponse : IResponse 10 | { 11 | int TotalItems { get; } 12 | T Data { get; } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/Core/Application/Wrappers/Abstract/IResponse.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 Application.Wrappers.Abstract 8 | { 9 | public interface IResponse 10 | { 11 | bool Success { get; } 12 | int StatusCode { get; } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/Core/Application/Wrappers/Abstract/ISuccessResponse.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 Application.Wrappers.Abstract 8 | { 9 | public interface ISuccessResponse : IResponse 10 | { 11 | string Message { get; } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/Core/Application/Wrappers/Concrete/DataResponse.cs: -------------------------------------------------------------------------------- 1 | using Application.Wrappers.Abstract; 2 | using JsonConstructorAttribute = Newtonsoft.Json.JsonConstructorAttribute; 3 | 4 | namespace Application.Wrappers.Concrete 5 | { 6 | public class DataResponse : IDataResponse 7 | { 8 | public bool Success { get; } = true; 9 | public T Data { get; } 10 | 11 | public int StatusCode { get; } 12 | public string Message { get; set; } 13 | 14 | [JsonConstructor] 15 | public DataResponse(T data, int statuscode) 16 | { 17 | Data = data; 18 | StatusCode = statuscode; 19 | } 20 | 21 | public DataResponse(T data, int statuscode, string message) 22 | { 23 | Data = data; 24 | StatusCode = statuscode; 25 | Message = message; 26 | } 27 | } 28 | } -------------------------------------------------------------------------------- /src/Core/Application/Wrappers/Concrete/ErrorResponse.cs: -------------------------------------------------------------------------------- 1 | using Application.Wrappers.Abstract; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace Application.Wrappers.Concrete 9 | { 10 | public class ErrorResponse : IErrorResponse 11 | { 12 | public bool Success { get; } = false; 13 | public int StatusCode { get; } 14 | public List Errors { get; private set; } = new List(); 15 | 16 | public ErrorResponse(int statuscode, List errors) 17 | { 18 | StatusCode = statuscode; 19 | Errors = errors; 20 | } 21 | 22 | public ErrorResponse(int statuscode, string error) 23 | { 24 | StatusCode = statuscode; 25 | Errors.Add(error); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Core/Application/Wrappers/Concrete/PagedDataResponse.cs: -------------------------------------------------------------------------------- 1 | using Application.Wrappers.Abstract; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace Application.Wrappers.Concrete 9 | { 10 | public class PagedDataResponse : IPagedDataResponse 11 | { 12 | public bool Success { get; } = true; 13 | public int TotalItems { get; } 14 | 15 | public T Data { get; } 16 | 17 | public int StatusCode { get; } 18 | 19 | public PagedDataResponse(T data, int statuscode, int totalitems) 20 | { 21 | Data = data; 22 | StatusCode = statuscode; 23 | TotalItems = totalitems; 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/Core/Application/Wrappers/Concrete/Response.cs: -------------------------------------------------------------------------------- 1 | using Application.Wrappers.Abstract; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace Application.Wrappers.Concrete 9 | { 10 | public class Response : IResponse 11 | { 12 | public bool Success { get; } 13 | 14 | public int StatusCode { get; } 15 | 16 | public Response(bool success, int statuscode) 17 | { 18 | Success = success; 19 | StatusCode = statuscode; 20 | } 21 | 22 | public Response(bool success) 23 | { 24 | Success = success; 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/Core/Application/Wrappers/Concrete/SuccessResponse.cs: -------------------------------------------------------------------------------- 1 | using Application.Wrappers.Abstract; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace Application.Wrappers.Concrete 9 | { 10 | public class SuccessResponse : ISuccessResponse 11 | { 12 | public bool Success { get; } = true; 13 | public string Message { get; } 14 | public int StatusCode { get; } 15 | 16 | 17 | public SuccessResponse() 18 | { 19 | 20 | } 21 | 22 | public SuccessResponse(int statuscode, string message) 23 | { 24 | StatusCode = statuscode; 25 | Message = message; 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/Core/Domain/Domain.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net6.0 5 | enable 6 | disable 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /src/Core/Domain/Entities/Base/BaseEntity.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 Domain.Entities.Base 8 | { 9 | public class BaseEntity 10 | { 11 | public TKey Id { get; set; } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/Core/Domain/Entities/RefreshToken.cs: -------------------------------------------------------------------------------- 1 | using Domain.Entities.Base; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace Domain.Entities 9 | { 10 | public class RefreshToken:BaseEntity 11 | { 12 | public Guid UserId { get; set; } 13 | public string Code { get; set; } 14 | public DateTime Expiration { get; set; } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/Core/Domain/Entities/Role.cs: -------------------------------------------------------------------------------- 1 | using Domain.Entities.Base; 2 | 3 | namespace Domain.Entities 4 | { 5 | public class Role : BaseEntity 6 | { 7 | public string Name { get; set; } 8 | public virtual ICollection UserRoles { get; set; } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /src/Core/Domain/Entities/User.cs: -------------------------------------------------------------------------------- 1 | using Domain.Entities.Base; 2 | 3 | namespace Domain.Entities 4 | { 5 | public class User : BaseEntity 6 | { 7 | public string UserName { get; set; } 8 | public string FirstName { get; set; } 9 | public string LastName { get; set; } 10 | public string Email { get; set; } 11 | public byte[] PasswordHash { get; set; } 12 | public byte[] PasswordSalt { get; set; } 13 | public bool EmailConfirmed { get; set; } = false; 14 | public virtual ICollection UserRoles { get; set; } 15 | public string EmailConfirmationCode { get; set; } 16 | public string EmailConfirmedCode { get; set; } 17 | public string ResetPasswordCode { get; set; } 18 | } 19 | } -------------------------------------------------------------------------------- /src/Core/Domain/Entities/UserRole.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 Domain.Entities 8 | { 9 | public class UserRole 10 | { 11 | public Guid UserId { get; set; } 12 | public Guid RoleId { get; set; } 13 | public virtual User User { get; set; } 14 | public virtual Role Role { get; set; } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/Infrastructure/Infrastructure/Infrastructure.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | net6.0 5 | enable 6 | disable 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /src/Infrastructure/Infrastructure/InfrastructureRegistration.cs: -------------------------------------------------------------------------------- 1 | using Application.Interfaces.Services; 2 | using EasyCaching.Core.Configurations; 3 | using Infrastructure.Services; 4 | using Infrastructure.Settings; 5 | using Microsoft.Extensions.Configuration; 6 | using Microsoft.Extensions.DependencyInjection; 7 | 8 | namespace Infrastructure 9 | { 10 | public static class InfrastructureRegistration 11 | { 12 | public static void AddInfrastructureServices(this IServiceCollection services,IConfiguration configuration) 13 | { 14 | services.AddScoped(); 15 | services.AddScoped(); 16 | services.Configure(configuration.GetSection("JWTSettings")); 17 | services.Configure(configuration.GetSection("EmailSettings")); 18 | services.Configure(configuration.GetSection("CacheSettings")); 19 | 20 | var cacheSettings = configuration.GetSection("CacheSettings").Get(); 21 | 22 | if (cacheSettings.PreferRedis) 23 | { 24 | services.AddEasyCaching(option => 25 | { 26 | option.WithJson(); 27 | option.UseRedis(config => 28 | { 29 | config.DBConfig.Endpoints.Add(new ServerEndPoint(cacheSettings.RedisURL, cacheSettings.RedisPort)); 30 | }, "json"); 31 | }); 32 | } 33 | else 34 | { 35 | services.AddEasyCaching(option => 36 | { 37 | option.UseInMemory(); 38 | }); 39 | } 40 | services.AddTransient(); 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/Infrastructure/Infrastructure/Services/EasyCacheService.cs: -------------------------------------------------------------------------------- 1 | using Application.Interfaces.Services; 2 | using EasyCaching.Core; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | 9 | namespace Infrastructure.Services 10 | { 11 | public class EasyCacheService : IEasyCacheService 12 | { 13 | private readonly IEasyCachingProvider _easyCachingProvider; 14 | 15 | public EasyCacheService(IEasyCachingProvider easyCachingProvider) 16 | { 17 | _easyCachingProvider = easyCachingProvider; 18 | } 19 | 20 | public async Task GetAsync(string key, Type cachedData) 21 | { 22 | return await _easyCachingProvider.GetAsync(key, cachedData); 23 | } 24 | 25 | public async Task RemoveAsync(string key) 26 | { 27 | await _easyCachingProvider.RemoveAsync(key); 28 | } 29 | 30 | public async Task RemoveByPrefixAsync(string prefix) 31 | { 32 | await _easyCachingProvider.RemoveByPrefixAsync(prefix); 33 | } 34 | 35 | public async Task SetAsync(string key, T value, TimeSpan? expiration = null) 36 | { 37 | if (expiration.HasValue) 38 | { 39 | await _easyCachingProvider.SetAsync(key, value, expiration.Value); 40 | } 41 | else 42 | { 43 | await _easyCachingProvider.SetAsync(key, value, TimeSpan.FromMinutes(10)); 44 | } 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/Infrastructure/Infrastructure/Services/EmailService.cs: -------------------------------------------------------------------------------- 1 | using Application.Interfaces.Services; 2 | using Microsoft.Extensions.Options; 3 | using Infrastructure.Settings; 4 | using System; 5 | using System.Collections.Generic; 6 | using System.Linq; 7 | using System.Net; 8 | using System.Net.Mail; 9 | using System.Text; 10 | using System.Threading.Tasks; 11 | 12 | namespace Infrastructure.Services 13 | { 14 | public class EmailService : IEmailService 15 | { 16 | private readonly EmailSettings _emailSettings; 17 | public EmailService(IOptions options) 18 | { 19 | _emailSettings = options.Value; 20 | } 21 | public async Task ConfirmationMailAsync(string link, string email) 22 | { 23 | var client = new SmtpClient(_emailSettings.Host, _emailSettings.Port) 24 | { 25 | Credentials = new NetworkCredential(_emailSettings.Email, _emailSettings.Password), 26 | EnableSsl = _emailSettings.EnableSSL 27 | }; 28 | var subject = $"www.CleanArchitecture.com || Confirm Email"; 29 | var body = "

Please click this link for confirm email


"; 30 | body += $"Confirmation Link"; 31 | await client.SendMailAsync( 32 | new MailMessage(_emailSettings.Email, email, subject, body) { IsBodyHtml = true } 33 | ); 34 | } 35 | 36 | public async Task ForgetPasswordMailAsync(string link, string email) 37 | { 38 | var client = new SmtpClient(_emailSettings.Host, _emailSettings.Port) 39 | { 40 | Credentials = new NetworkCredential(_emailSettings.Email, _emailSettings.Password), 41 | EnableSsl = _emailSettings.EnableSSL 42 | }; 43 | var subject = $"www.CleanArchitecture.com || Reset Password"; 44 | var body = "

Please click this link for reset password


"; 45 | body += $"reset password link"; 46 | await client.SendMailAsync( 47 | new MailMessage(_emailSettings.Email, email, subject, body) { IsBodyHtml = true } 48 | ); 49 | } 50 | 51 | public async Task SendNewPasswordAsync(string password, string email) 52 | { 53 | var client = new SmtpClient(_emailSettings.Host, _emailSettings.Port) 54 | { 55 | Credentials = new NetworkCredential(_emailSettings.Email, _emailSettings.Password), 56 | EnableSsl = _emailSettings.EnableSSL 57 | }; 58 | var subject = $"www.CleanArchitecture.com || New Password"; 59 | var body = "

Your New Password


"; 60 | body += $"

Password : {password}

"; 61 | await client.SendMailAsync( 62 | new MailMessage(_emailSettings.Email, email, subject, body) { IsBodyHtml = true } 63 | ); 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/Infrastructure/Infrastructure/Services/TokenService.cs: -------------------------------------------------------------------------------- 1 | using Application.Dtos; 2 | using Application.Interfaces.Services; 3 | using Domain.Entities; 4 | using Microsoft.Extensions.Options; 5 | using Microsoft.IdentityModel.Tokens; 6 | using Infrastructure.Settings; 7 | using System.IdentityModel.Tokens.Jwt; 8 | using System.Security.Claims; 9 | using System.Security.Cryptography; 10 | using System.Text; 11 | 12 | namespace Infrastructure.Services 13 | { 14 | public class TokenService : ITokenService 15 | { 16 | private readonly JWTSettings _jwtSettings; 17 | 18 | public TokenService(IOptions jwtSettings) 19 | { 20 | _jwtSettings = jwtSettings.Value; 21 | } 22 | 23 | public TokenDTO CreateToken(User user, List roles) 24 | { 25 | var accessTokenExpiration = DateTime.Now.AddMinutes(_jwtSettings.AccessTokenExpiration); 26 | var refreshTokenExpiration = DateTime.Now.AddMinutes(_jwtSettings.RefreshTokenExpiration); 27 | var securityKey = Encoding.ASCII.GetBytes(_jwtSettings.SecurityKey); 28 | var signingCredentials = new SigningCredentials(new SymmetricSecurityKey(securityKey), 29 | SecurityAlgorithms.HmacSha256Signature); 30 | 31 | JwtSecurityToken jwtSecurityToken = new JwtSecurityToken( 32 | issuer: _jwtSettings.Issuer, 33 | audience: _jwtSettings.Audience[0], 34 | expires: accessTokenExpiration, 35 | notBefore: DateTime.Now, 36 | claims: GetClaims(user, _jwtSettings.Audience, roles), 37 | signingCredentials: signingCredentials); 38 | 39 | var handler = new JwtSecurityTokenHandler(); 40 | 41 | var token = handler.WriteToken(jwtSecurityToken); 42 | 43 | var tokenDto = new TokenDTO 44 | { 45 | AccessToken = token, 46 | RefreshToken = CreateRefreshToken(), 47 | AccessTokenExpiration = accessTokenExpiration, 48 | RefreshTokenExpiration = refreshTokenExpiration 49 | }; 50 | 51 | return tokenDto; 52 | } 53 | 54 | private IEnumerable GetClaims(User user, List audiences, List roles) 55 | { 56 | var claims = new List 57 | { 58 | new Claim(ClaimTypes.Email,user.Email), 59 | new Claim(ClaimTypes.Name,user.UserName), 60 | new Claim(ClaimTypes.NameIdentifier,user.Id.ToString()), 61 | new Claim(JwtRegisteredClaimNames.Jti,Guid.NewGuid().ToString()) 62 | }; 63 | foreach (var role in roles) 64 | { 65 | claims.Add(new Claim(ClaimTypes.Role, role)); 66 | } 67 | claims.AddRange(audiences.Select(x => new Claim(JwtRegisteredClaimNames.Aud, x))); 68 | return claims; 69 | } 70 | 71 | private string CreateRefreshToken() 72 | { 73 | var numberByte = new byte[32]; 74 | using var rnd = RandomNumberGenerator.Create(); 75 | rnd.GetBytes(numberByte); 76 | return Convert.ToBase64String(numberByte); 77 | } 78 | } 79 | } -------------------------------------------------------------------------------- /src/Infrastructure/Infrastructure/Settings/CacheSettings.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 Infrastructure.Settings 8 | { 9 | public class CacheSettings 10 | { 11 | public bool PreferRedis { get; set; } 12 | public string RedisURL { get; set; } 13 | public int RedisPort { get; set; } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /src/Infrastructure/Infrastructure/Settings/EmailSettings.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 Infrastructure.Settings 8 | { 9 | public class EmailSettings 10 | { 11 | public string Email { get; set; } 12 | public string Password { get; set; } 13 | public string Host { get; set; } 14 | public int Port { get; set; } 15 | public bool EnableSSL { get; set; } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/Infrastructure/Infrastructure/Settings/JWTSettings.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 Infrastructure.Settings 8 | { 9 | public class JWTSettings 10 | { 11 | public List Audience { get; set; } 12 | public string Issuer { get; set; } 13 | public int AccessTokenExpiration { get; set; } 14 | public int RefreshTokenExpiration { get; set; } 15 | public string SecurityKey { get; set; } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/Infrastructure/Persistence/Context/CAContext.cs: -------------------------------------------------------------------------------- 1 | using Domain.Entities; 2 | using Microsoft.EntityFrameworkCore; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | 9 | namespace Persistence.Context 10 | { 11 | public class CAContext : DbContext 12 | { 13 | public CAContext(DbContextOptions options) : base(options) 14 | { 15 | 16 | } 17 | 18 | protected override void OnModelCreating(ModelBuilder modelBuilder) 19 | { 20 | modelBuilder.ApplyConfigurationsFromAssembly(GetType().Assembly); 21 | base.OnModelCreating(modelBuilder); 22 | } 23 | 24 | 25 | public DbSet Users { get; set; } 26 | public DbSet Roles { get; set; } 27 | public DbSet UserRoles { get; set; } 28 | public DbSet RefreshTokens { get; set; } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /src/Infrastructure/Persistence/EntityConfigurations/RoleConfiguration.cs: -------------------------------------------------------------------------------- 1 | using Domain.Entities; 2 | using Microsoft.EntityFrameworkCore; 3 | using Microsoft.EntityFrameworkCore.Metadata.Builders; 4 | using System; 5 | using System.Collections.Generic; 6 | using System.Linq; 7 | using System.Text; 8 | using System.Threading.Tasks; 9 | 10 | namespace Persistence.EntityConfigurations 11 | { 12 | public class RoleConfiguration : IEntityTypeConfiguration 13 | { 14 | public void Configure(EntityTypeBuilder builder) 15 | { 16 | const string AdminRoleId = "43db034a-98cc-42ee-8fff-c57016484f4d"; 17 | builder.Property(r => r.Name).IsRequired().HasMaxLength(16); 18 | builder.HasData( 19 | new Role 20 | { 21 | Id = Guid.Parse(AdminRoleId), 22 | Name = "Admin" 23 | } 24 | ); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/Infrastructure/Persistence/EntityConfigurations/UserConfiguration.cs: -------------------------------------------------------------------------------- 1 | using Application.Helpers; 2 | using Application.Interfaces.Services; 3 | using Domain.Entities; 4 | using Microsoft.EntityFrameworkCore; 5 | using Microsoft.EntityFrameworkCore.Metadata.Builders; 6 | 7 | namespace Persistence.EntityConfigurations 8 | { 9 | public class UserConfiguration : IEntityTypeConfiguration 10 | { 11 | public void Configure(EntityTypeBuilder builder) 12 | { 13 | const string DefaultAdminUserId = "6e5d8fa8-fa96-419f-9c07-3e05b96b087e"; 14 | 15 | builder.Property(u => u.UserName) 16 | .HasMaxLength(50) 17 | .IsRequired(); 18 | builder.Property(u => u.FirstName) 19 | .HasMaxLength(50) 20 | .IsRequired(); 21 | builder.Property(u => u.LastName) 22 | .HasMaxLength(50) 23 | .IsRequired(); 24 | builder.Property(u => u.Email) 25 | .IsRequired(); 26 | builder.Property(u => u.PasswordHash) 27 | .IsRequired(); 28 | builder.Property(u => u.PasswordSalt) 29 | .IsRequired(); 30 | 31 | var (passwordHash, passwordSalt) = PasswordHelper.CreateHash("159357456qW"); 32 | 33 | builder.HasData( 34 | new User 35 | { 36 | Id = Guid.Parse(DefaultAdminUserId), 37 | UserName = "admin", 38 | FirstName = "Default", 39 | LastName = "Admin", 40 | PasswordHash = passwordHash, 41 | EmailConfirmed=true, 42 | PasswordSalt = passwordSalt, 43 | Email = "defaultadmin@gmail.com" 44 | }); 45 | } 46 | } 47 | } -------------------------------------------------------------------------------- /src/Infrastructure/Persistence/EntityConfigurations/UserRoleConfiguration.cs: -------------------------------------------------------------------------------- 1 | using Domain.Entities; 2 | using Microsoft.EntityFrameworkCore; 3 | using Microsoft.EntityFrameworkCore.Metadata.Builders; 4 | using System; 5 | using System.Collections.Generic; 6 | using System.Linq; 7 | using System.Text; 8 | using System.Threading.Tasks; 9 | 10 | namespace Persistence.EntityConfigurations 11 | { 12 | public class UserRoleConfiguration : IEntityTypeConfiguration 13 | { 14 | public void Configure(EntityTypeBuilder builder) 15 | { 16 | const string AdminRoleId = "43db034a-98cc-42ee-8fff-c57016484f4d"; 17 | const string DefaultAdminUserId = "6e5d8fa8-fa96-419f-9c07-3e05b96b087e"; 18 | 19 | builder.HasKey(ur => new { ur.UserId, ur.RoleId }); 20 | 21 | builder.HasData( 22 | new UserRole 23 | { 24 | UserId = Guid.Parse(DefaultAdminUserId), 25 | RoleId = Guid.Parse(AdminRoleId) 26 | }); 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/Infrastructure/Persistence/Migrations/20220527113337_CreateDatabase.Designer.cs: -------------------------------------------------------------------------------- 1 | // 2 | using System; 3 | using Microsoft.EntityFrameworkCore; 4 | using Microsoft.EntityFrameworkCore.Infrastructure; 5 | using Microsoft.EntityFrameworkCore.Migrations; 6 | using Microsoft.EntityFrameworkCore.Storage.ValueConversion; 7 | using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; 8 | using Persistence.Context; 9 | 10 | #nullable disable 11 | 12 | namespace Persistence.Migrations 13 | { 14 | [DbContext(typeof(CAContext))] 15 | [Migration("20220527113337_CreateDatabase")] 16 | partial class CreateDatabase 17 | { 18 | protected override void BuildTargetModel(ModelBuilder modelBuilder) 19 | { 20 | #pragma warning disable 612, 618 21 | modelBuilder 22 | .HasAnnotation("ProductVersion", "6.0.5") 23 | .HasAnnotation("Relational:MaxIdentifierLength", 63); 24 | 25 | NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); 26 | 27 | modelBuilder.Entity("Domain.Entities.RefreshToken", b => 28 | { 29 | b.Property("Id") 30 | .ValueGeneratedOnAdd() 31 | .HasColumnType("integer"); 32 | 33 | NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); 34 | 35 | b.Property("Code") 36 | .HasColumnType("text"); 37 | 38 | b.Property("Expiration") 39 | .HasColumnType("timestamp with time zone"); 40 | 41 | b.Property("UserId") 42 | .HasColumnType("uuid"); 43 | 44 | b.HasKey("Id"); 45 | 46 | b.ToTable("RefreshTokens"); 47 | }); 48 | 49 | modelBuilder.Entity("Domain.Entities.Role", b => 50 | { 51 | b.Property("Id") 52 | .ValueGeneratedOnAdd() 53 | .HasColumnType("uuid"); 54 | 55 | b.Property("Name") 56 | .IsRequired() 57 | .HasMaxLength(16) 58 | .HasColumnType("character varying(16)"); 59 | 60 | b.HasKey("Id"); 61 | 62 | b.ToTable("Roles"); 63 | 64 | b.HasData( 65 | new 66 | { 67 | Id = new Guid("43db034a-98cc-42ee-8fff-c57016484f4d"), 68 | Name = "Admin" 69 | }); 70 | }); 71 | 72 | modelBuilder.Entity("Domain.Entities.User", b => 73 | { 74 | b.Property("Id") 75 | .ValueGeneratedOnAdd() 76 | .HasColumnType("uuid"); 77 | 78 | b.Property("Email") 79 | .IsRequired() 80 | .HasColumnType("text"); 81 | 82 | b.Property("EmailConfirmationCode") 83 | .HasColumnType("text"); 84 | 85 | b.Property("EmailConfirmed") 86 | .HasColumnType("boolean"); 87 | 88 | b.Property("EmailConfirmedCode") 89 | .HasColumnType("text"); 90 | 91 | b.Property("FirstName") 92 | .IsRequired() 93 | .HasMaxLength(50) 94 | .HasColumnType("character varying(50)"); 95 | 96 | b.Property("LastName") 97 | .IsRequired() 98 | .HasMaxLength(50) 99 | .HasColumnType("character varying(50)"); 100 | 101 | b.Property("PasswordHash") 102 | .IsRequired() 103 | .HasColumnType("bytea"); 104 | 105 | b.Property("PasswordSalt") 106 | .IsRequired() 107 | .HasColumnType("bytea"); 108 | 109 | b.Property("ResetPasswordCode") 110 | .HasColumnType("text"); 111 | 112 | b.Property("UserName") 113 | .IsRequired() 114 | .HasMaxLength(50) 115 | .HasColumnType("character varying(50)"); 116 | 117 | b.HasKey("Id"); 118 | 119 | b.ToTable("Users"); 120 | 121 | b.HasData( 122 | new 123 | { 124 | Id = new Guid("6e5d8fa8-fa96-419f-9c07-3e05b96b087e"), 125 | Email = "defaultadmin@gmail.com", 126 | EmailConfirmed = true, 127 | FirstName = "Default", 128 | LastName = "Admin", 129 | PasswordHash = new byte[] { 30, 26, 213, 8, 103, 48, 182, 118, 120, 144, 112, 216, 242, 40, 40, 207, 239, 96, 119, 49, 33, 171, 55, 121, 126, 118, 156, 120, 77, 215, 156, 72, 179, 105, 183, 56, 0, 241, 155, 101, 195, 120, 71, 19, 202, 24, 89, 175, 55, 34, 225, 108, 10, 165, 18, 204, 237, 134, 207, 84, 99, 154, 47, 187 }, 130 | PasswordSalt = new byte[] { 249, 241, 17, 68, 23, 112, 83, 99, 27, 23, 64, 13, 227, 155, 63, 133, 99, 58, 201, 76, 70, 85, 152, 9, 70, 66, 108, 245, 248, 18, 81, 251, 62, 224, 104, 49, 200, 148, 2, 170, 114, 145, 100, 43, 6, 123, 10, 84, 187, 121, 24, 246, 140, 224, 220, 192, 149, 138, 206, 19, 121, 210, 255, 199, 215, 59, 144, 106, 249, 124, 89, 200, 250, 38, 182, 73, 82, 22, 204, 110, 185, 148, 190, 61, 240, 51, 253, 98, 3, 178, 132, 83, 200, 211, 185, 10, 10, 240, 232, 227, 222, 171, 218, 46, 2, 185, 11, 241, 191, 195, 67, 159, 124, 120, 65, 168, 254, 123, 30, 84, 153, 105, 64, 114, 183, 158, 125, 252 }, 131 | UserName = "admin" 132 | }); 133 | }); 134 | 135 | modelBuilder.Entity("Domain.Entities.UserRole", b => 136 | { 137 | b.Property("UserId") 138 | .HasColumnType("uuid"); 139 | 140 | b.Property("RoleId") 141 | .HasColumnType("uuid"); 142 | 143 | b.HasKey("UserId", "RoleId"); 144 | 145 | b.HasIndex("RoleId"); 146 | 147 | b.ToTable("UserRoles"); 148 | 149 | b.HasData( 150 | new 151 | { 152 | UserId = new Guid("6e5d8fa8-fa96-419f-9c07-3e05b96b087e"), 153 | RoleId = new Guid("43db034a-98cc-42ee-8fff-c57016484f4d") 154 | }); 155 | }); 156 | 157 | modelBuilder.Entity("Domain.Entities.UserRole", b => 158 | { 159 | b.HasOne("Domain.Entities.Role", "Role") 160 | .WithMany("UserRoles") 161 | .HasForeignKey("RoleId") 162 | .OnDelete(DeleteBehavior.Cascade) 163 | .IsRequired(); 164 | 165 | b.HasOne("Domain.Entities.User", "User") 166 | .WithMany("UserRoles") 167 | .HasForeignKey("UserId") 168 | .OnDelete(DeleteBehavior.Cascade) 169 | .IsRequired(); 170 | 171 | b.Navigation("Role"); 172 | 173 | b.Navigation("User"); 174 | }); 175 | 176 | modelBuilder.Entity("Domain.Entities.Role", b => 177 | { 178 | b.Navigation("UserRoles"); 179 | }); 180 | 181 | modelBuilder.Entity("Domain.Entities.User", b => 182 | { 183 | b.Navigation("UserRoles"); 184 | }); 185 | #pragma warning restore 612, 618 186 | } 187 | } 188 | } 189 | -------------------------------------------------------------------------------- /src/Infrastructure/Persistence/Migrations/20220527113337_CreateDatabase.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Microsoft.EntityFrameworkCore.Migrations; 3 | using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; 4 | 5 | #nullable disable 6 | 7 | namespace Persistence.Migrations 8 | { 9 | public partial class CreateDatabase : Migration 10 | { 11 | protected override void Up(MigrationBuilder migrationBuilder) 12 | { 13 | migrationBuilder.CreateTable( 14 | name: "RefreshTokens", 15 | columns: table => new 16 | { 17 | Id = table.Column(type: "integer", nullable: false) 18 | .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), 19 | UserId = table.Column(type: "uuid", nullable: false), 20 | Code = table.Column(type: "text", nullable: true), 21 | Expiration = table.Column(type: "timestamp with time zone", nullable: false) 22 | }, 23 | constraints: table => 24 | { 25 | table.PrimaryKey("PK_RefreshTokens", x => x.Id); 26 | }); 27 | 28 | migrationBuilder.CreateTable( 29 | name: "Roles", 30 | columns: table => new 31 | { 32 | Id = table.Column(type: "uuid", nullable: false), 33 | Name = table.Column(type: "character varying(16)", maxLength: 16, nullable: false) 34 | }, 35 | constraints: table => 36 | { 37 | table.PrimaryKey("PK_Roles", x => x.Id); 38 | }); 39 | 40 | migrationBuilder.CreateTable( 41 | name: "Users", 42 | columns: table => new 43 | { 44 | Id = table.Column(type: "uuid", nullable: false), 45 | UserName = table.Column(type: "character varying(50)", maxLength: 50, nullable: false), 46 | FirstName = table.Column(type: "character varying(50)", maxLength: 50, nullable: false), 47 | LastName = table.Column(type: "character varying(50)", maxLength: 50, nullable: false), 48 | Email = table.Column(type: "text", nullable: false), 49 | PasswordHash = table.Column(type: "bytea", nullable: false), 50 | PasswordSalt = table.Column(type: "bytea", nullable: false), 51 | EmailConfirmed = table.Column(type: "boolean", nullable: false), 52 | EmailConfirmationCode = table.Column(type: "text", nullable: true), 53 | EmailConfirmedCode = table.Column(type: "text", nullable: true), 54 | ResetPasswordCode = table.Column(type: "text", nullable: true) 55 | }, 56 | constraints: table => 57 | { 58 | table.PrimaryKey("PK_Users", x => x.Id); 59 | }); 60 | 61 | migrationBuilder.CreateTable( 62 | name: "UserRoles", 63 | columns: table => new 64 | { 65 | UserId = table.Column(type: "uuid", nullable: false), 66 | RoleId = table.Column(type: "uuid", nullable: false) 67 | }, 68 | constraints: table => 69 | { 70 | table.PrimaryKey("PK_UserRoles", x => new { x.UserId, x.RoleId }); 71 | table.ForeignKey( 72 | name: "FK_UserRoles_Roles_RoleId", 73 | column: x => x.RoleId, 74 | principalTable: "Roles", 75 | principalColumn: "Id", 76 | onDelete: ReferentialAction.Cascade); 77 | table.ForeignKey( 78 | name: "FK_UserRoles_Users_UserId", 79 | column: x => x.UserId, 80 | principalTable: "Users", 81 | principalColumn: "Id", 82 | onDelete: ReferentialAction.Cascade); 83 | }); 84 | 85 | migrationBuilder.InsertData( 86 | table: "Roles", 87 | columns: new[] { "Id", "Name" }, 88 | values: new object[] { new Guid("43db034a-98cc-42ee-8fff-c57016484f4d"), "Admin" }); 89 | 90 | migrationBuilder.InsertData( 91 | table: "Users", 92 | columns: new[] { "Id", "Email", "EmailConfirmationCode", "EmailConfirmed", "EmailConfirmedCode", "FirstName", "LastName", "PasswordHash", "PasswordSalt", "ResetPasswordCode", "UserName" }, 93 | values: new object[] { new Guid("6e5d8fa8-fa96-419f-9c07-3e05b96b087e"), "defaultadmin@gmail.com", null, true, null, "Default", "Admin", new byte[] { 30, 26, 213, 8, 103, 48, 182, 118, 120, 144, 112, 216, 242, 40, 40, 207, 239, 96, 119, 49, 33, 171, 55, 121, 126, 118, 156, 120, 77, 215, 156, 72, 179, 105, 183, 56, 0, 241, 155, 101, 195, 120, 71, 19, 202, 24, 89, 175, 55, 34, 225, 108, 10, 165, 18, 204, 237, 134, 207, 84, 99, 154, 47, 187 }, new byte[] { 249, 241, 17, 68, 23, 112, 83, 99, 27, 23, 64, 13, 227, 155, 63, 133, 99, 58, 201, 76, 70, 85, 152, 9, 70, 66, 108, 245, 248, 18, 81, 251, 62, 224, 104, 49, 200, 148, 2, 170, 114, 145, 100, 43, 6, 123, 10, 84, 187, 121, 24, 246, 140, 224, 220, 192, 149, 138, 206, 19, 121, 210, 255, 199, 215, 59, 144, 106, 249, 124, 89, 200, 250, 38, 182, 73, 82, 22, 204, 110, 185, 148, 190, 61, 240, 51, 253, 98, 3, 178, 132, 83, 200, 211, 185, 10, 10, 240, 232, 227, 222, 171, 218, 46, 2, 185, 11, 241, 191, 195, 67, 159, 124, 120, 65, 168, 254, 123, 30, 84, 153, 105, 64, 114, 183, 158, 125, 252 }, null, "admin" }); 94 | 95 | migrationBuilder.InsertData( 96 | table: "UserRoles", 97 | columns: new[] { "RoleId", "UserId" }, 98 | values: new object[] { new Guid("43db034a-98cc-42ee-8fff-c57016484f4d"), new Guid("6e5d8fa8-fa96-419f-9c07-3e05b96b087e") }); 99 | 100 | migrationBuilder.CreateIndex( 101 | name: "IX_UserRoles_RoleId", 102 | table: "UserRoles", 103 | column: "RoleId"); 104 | } 105 | 106 | protected override void Down(MigrationBuilder migrationBuilder) 107 | { 108 | migrationBuilder.DropTable( 109 | name: "RefreshTokens"); 110 | 111 | migrationBuilder.DropTable( 112 | name: "UserRoles"); 113 | 114 | migrationBuilder.DropTable( 115 | name: "Roles"); 116 | 117 | migrationBuilder.DropTable( 118 | name: "Users"); 119 | } 120 | } 121 | } 122 | -------------------------------------------------------------------------------- /src/Infrastructure/Persistence/Migrations/CAContextModelSnapshot.cs: -------------------------------------------------------------------------------- 1 | // 2 | using System; 3 | using Microsoft.EntityFrameworkCore; 4 | using Microsoft.EntityFrameworkCore.Infrastructure; 5 | using Microsoft.EntityFrameworkCore.Storage.ValueConversion; 6 | using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; 7 | using Persistence.Context; 8 | 9 | #nullable disable 10 | 11 | namespace Persistence.Migrations 12 | { 13 | [DbContext(typeof(CAContext))] 14 | partial class CAContextModelSnapshot : ModelSnapshot 15 | { 16 | protected override void BuildModel(ModelBuilder modelBuilder) 17 | { 18 | #pragma warning disable 612, 618 19 | modelBuilder 20 | .HasAnnotation("ProductVersion", "6.0.5") 21 | .HasAnnotation("Relational:MaxIdentifierLength", 63); 22 | 23 | NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); 24 | 25 | modelBuilder.Entity("Domain.Entities.RefreshToken", b => 26 | { 27 | b.Property("Id") 28 | .ValueGeneratedOnAdd() 29 | .HasColumnType("integer"); 30 | 31 | NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); 32 | 33 | b.Property("Code") 34 | .HasColumnType("text"); 35 | 36 | b.Property("Expiration") 37 | .HasColumnType("timestamp with time zone"); 38 | 39 | b.Property("UserId") 40 | .HasColumnType("uuid"); 41 | 42 | b.HasKey("Id"); 43 | 44 | b.ToTable("RefreshTokens"); 45 | }); 46 | 47 | modelBuilder.Entity("Domain.Entities.Role", b => 48 | { 49 | b.Property("Id") 50 | .ValueGeneratedOnAdd() 51 | .HasColumnType("uuid"); 52 | 53 | b.Property("Name") 54 | .IsRequired() 55 | .HasMaxLength(16) 56 | .HasColumnType("character varying(16)"); 57 | 58 | b.HasKey("Id"); 59 | 60 | b.ToTable("Roles"); 61 | 62 | b.HasData( 63 | new 64 | { 65 | Id = new Guid("43db034a-98cc-42ee-8fff-c57016484f4d"), 66 | Name = "Admin" 67 | }); 68 | }); 69 | 70 | modelBuilder.Entity("Domain.Entities.User", b => 71 | { 72 | b.Property("Id") 73 | .ValueGeneratedOnAdd() 74 | .HasColumnType("uuid"); 75 | 76 | b.Property("Email") 77 | .IsRequired() 78 | .HasColumnType("text"); 79 | 80 | b.Property("EmailConfirmationCode") 81 | .HasColumnType("text"); 82 | 83 | b.Property("EmailConfirmed") 84 | .HasColumnType("boolean"); 85 | 86 | b.Property("EmailConfirmedCode") 87 | .HasColumnType("text"); 88 | 89 | b.Property("FirstName") 90 | .IsRequired() 91 | .HasMaxLength(50) 92 | .HasColumnType("character varying(50)"); 93 | 94 | b.Property("LastName") 95 | .IsRequired() 96 | .HasMaxLength(50) 97 | .HasColumnType("character varying(50)"); 98 | 99 | b.Property("PasswordHash") 100 | .IsRequired() 101 | .HasColumnType("bytea"); 102 | 103 | b.Property("PasswordSalt") 104 | .IsRequired() 105 | .HasColumnType("bytea"); 106 | 107 | b.Property("ResetPasswordCode") 108 | .HasColumnType("text"); 109 | 110 | b.Property("UserName") 111 | .IsRequired() 112 | .HasMaxLength(50) 113 | .HasColumnType("character varying(50)"); 114 | 115 | b.HasKey("Id"); 116 | 117 | b.ToTable("Users"); 118 | 119 | b.HasData( 120 | new 121 | { 122 | Id = new Guid("6e5d8fa8-fa96-419f-9c07-3e05b96b087e"), 123 | Email = "defaultadmin@gmail.com", 124 | EmailConfirmed = true, 125 | FirstName = "Default", 126 | LastName = "Admin", 127 | PasswordHash = new byte[] { 30, 26, 213, 8, 103, 48, 182, 118, 120, 144, 112, 216, 242, 40, 40, 207, 239, 96, 119, 49, 33, 171, 55, 121, 126, 118, 156, 120, 77, 215, 156, 72, 179, 105, 183, 56, 0, 241, 155, 101, 195, 120, 71, 19, 202, 24, 89, 175, 55, 34, 225, 108, 10, 165, 18, 204, 237, 134, 207, 84, 99, 154, 47, 187 }, 128 | PasswordSalt = new byte[] { 249, 241, 17, 68, 23, 112, 83, 99, 27, 23, 64, 13, 227, 155, 63, 133, 99, 58, 201, 76, 70, 85, 152, 9, 70, 66, 108, 245, 248, 18, 81, 251, 62, 224, 104, 49, 200, 148, 2, 170, 114, 145, 100, 43, 6, 123, 10, 84, 187, 121, 24, 246, 140, 224, 220, 192, 149, 138, 206, 19, 121, 210, 255, 199, 215, 59, 144, 106, 249, 124, 89, 200, 250, 38, 182, 73, 82, 22, 204, 110, 185, 148, 190, 61, 240, 51, 253, 98, 3, 178, 132, 83, 200, 211, 185, 10, 10, 240, 232, 227, 222, 171, 218, 46, 2, 185, 11, 241, 191, 195, 67, 159, 124, 120, 65, 168, 254, 123, 30, 84, 153, 105, 64, 114, 183, 158, 125, 252 }, 129 | UserName = "admin" 130 | }); 131 | }); 132 | 133 | modelBuilder.Entity("Domain.Entities.UserRole", b => 134 | { 135 | b.Property("UserId") 136 | .HasColumnType("uuid"); 137 | 138 | b.Property("RoleId") 139 | .HasColumnType("uuid"); 140 | 141 | b.HasKey("UserId", "RoleId"); 142 | 143 | b.HasIndex("RoleId"); 144 | 145 | b.ToTable("UserRoles"); 146 | 147 | b.HasData( 148 | new 149 | { 150 | UserId = new Guid("6e5d8fa8-fa96-419f-9c07-3e05b96b087e"), 151 | RoleId = new Guid("43db034a-98cc-42ee-8fff-c57016484f4d") 152 | }); 153 | }); 154 | 155 | modelBuilder.Entity("Domain.Entities.UserRole", b => 156 | { 157 | b.HasOne("Domain.Entities.Role", "Role") 158 | .WithMany("UserRoles") 159 | .HasForeignKey("RoleId") 160 | .OnDelete(DeleteBehavior.Cascade) 161 | .IsRequired(); 162 | 163 | b.HasOne("Domain.Entities.User", "User") 164 | .WithMany("UserRoles") 165 | .HasForeignKey("UserId") 166 | .OnDelete(DeleteBehavior.Cascade) 167 | .IsRequired(); 168 | 169 | b.Navigation("Role"); 170 | 171 | b.Navigation("User"); 172 | }); 173 | 174 | modelBuilder.Entity("Domain.Entities.Role", b => 175 | { 176 | b.Navigation("UserRoles"); 177 | }); 178 | 179 | modelBuilder.Entity("Domain.Entities.User", b => 180 | { 181 | b.Navigation("UserRoles"); 182 | }); 183 | #pragma warning restore 612, 618 184 | } 185 | } 186 | } 187 | -------------------------------------------------------------------------------- /src/Infrastructure/Persistence/Persistence.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | net6.0 5 | enable 6 | disable 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 | -------------------------------------------------------------------------------- /src/Infrastructure/Persistence/PersistenceRegistration.cs: -------------------------------------------------------------------------------- 1 | using Application.Interfaces.Repositories; 2 | using Application.Interfaces.Services; 3 | using Microsoft.EntityFrameworkCore; 4 | using Microsoft.Extensions.Configuration; 5 | using Microsoft.Extensions.DependencyInjection; 6 | using Persistence.Context; 7 | using Persistence.Repositories; 8 | 9 | 10 | namespace Persistence 11 | { 12 | public static class PersistenceRegistration 13 | { 14 | public static void AddPersistenceServices(this IServiceCollection services, IConfiguration configuration) 15 | { 16 | services.AddDbContext(options => 17 | { 18 | options.UseNpgsql(configuration.GetConnectionString("PostgreSqlConnection")); 19 | }); 20 | 21 | services.AddScoped(); 22 | services.AddScoped(); 23 | services.AddScoped(); 24 | services.AddScoped(); 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /src/Infrastructure/Persistence/Repositories/EfEntityRepository.cs: -------------------------------------------------------------------------------- 1 | using Application.Interfaces.Repositories; 2 | using Domain.Entities.Base; 3 | using Microsoft.EntityFrameworkCore; 4 | using System; 5 | using System.Collections.Generic; 6 | using System.Linq; 7 | using System.Linq.Expressions; 8 | using System.Text; 9 | using System.Threading.Tasks; 10 | 11 | namespace Persistence.Repositories 12 | { 13 | public class EfEntityRepository : IRepository where TEntity : BaseEntity where TContext : DbContext 14 | { 15 | 16 | protected readonly TContext _context; 17 | public EfEntityRepository(TContext context) 18 | { 19 | _context = context; 20 | } 21 | public TEntity Add(TEntity entity) 22 | { 23 | _context.Set().Add(entity); 24 | return entity; 25 | } 26 | 27 | public async Task AddAsync(TEntity entity) 28 | { 29 | await _context.Set().AddAsync(entity); 30 | return entity; 31 | } 32 | 33 | public IEnumerable AddRange(IEnumerable entities) 34 | { 35 | _context.Set().AddRange(entities); 36 | return entities; 37 | } 38 | 39 | public async Task> AddRangeAsync(IEnumerable entities) 40 | { 41 | await _context.Set().AddRangeAsync(entities); 42 | return entities; 43 | } 44 | 45 | public TEntity Get(Expression> expression, bool noTracking = false) 46 | { 47 | if (noTracking) 48 | { 49 | return _context.Set().AsNoTracking().FirstOrDefault(expression); 50 | } 51 | else 52 | { 53 | return _context.Set().FirstOrDefault(expression); 54 | } 55 | } 56 | 57 | public IEnumerable GetAll(Expression> expression = null, bool noTracking = true) 58 | { 59 | if (noTracking) 60 | { 61 | if (expression == null) 62 | { 63 | return _context.Set().AsNoTracking().ToList(); 64 | } 65 | else 66 | { 67 | return _context.Set().AsNoTracking().Where(expression).ToList(); 68 | } 69 | } 70 | else 71 | { 72 | if (expression == null) 73 | { 74 | return _context.Set().ToList(); 75 | } 76 | else 77 | { 78 | return _context.Set().Where(expression).ToList(); 79 | } 80 | } 81 | } 82 | 83 | public async Task> GetAllAsync(Expression> expression = null, bool noTracking = true) 84 | { 85 | if (noTracking) 86 | { 87 | if (expression == null) 88 | { 89 | return await _context.Set().AsNoTracking().ToListAsync(); 90 | } 91 | else 92 | { 93 | return await _context.Set().AsNoTracking().Where(expression).ToListAsync(); 94 | } 95 | } 96 | else 97 | { 98 | if (expression == null) 99 | { 100 | return await _context.Set().ToListAsync(); 101 | } 102 | else 103 | { 104 | return await _context.Set().Where(expression).ToListAsync(); 105 | } 106 | } 107 | } 108 | 109 | public async Task GetAsync(Expression> expression, bool noTracking = false) 110 | { 111 | if (noTracking) 112 | { 113 | return await _context.Set().AsNoTracking().FirstOrDefaultAsync(expression); 114 | } 115 | else 116 | { 117 | return await _context.Set().FirstOrDefaultAsync(expression); 118 | } 119 | } 120 | 121 | public TEntity GetById(TKey id) 122 | { 123 | return _context.Set().Find(id); 124 | } 125 | 126 | public async Task GetByIdAsync(TKey id) 127 | { 128 | return await _context.Set().FindAsync(id); 129 | } 130 | 131 | public int GetCount(Expression> expression = null) 132 | { 133 | if (expression == null) 134 | { 135 | return _context.Set().Count(); 136 | } 137 | else 138 | { 139 | return _context.Set().Count(expression); 140 | } 141 | } 142 | 143 | public async Task GetCountAsync(Expression> expression = null) 144 | { 145 | if (expression == null) 146 | { 147 | return await _context.Set().CountAsync(); 148 | } 149 | else 150 | { 151 | return await _context.Set().CountAsync(expression); 152 | } 153 | } 154 | 155 | public IQueryable Queryable() 156 | { 157 | return _context.Set().AsQueryable(); 158 | } 159 | 160 | public void Remove(TEntity entity) 161 | { 162 | _context.Set().Remove(entity); 163 | } 164 | 165 | public void RemoveRange(IEnumerable entities) 166 | { 167 | _context.Set().RemoveRange(entities); 168 | } 169 | 170 | public void Update(TEntity entity) 171 | { 172 | _context.Set().Update(entity); 173 | } 174 | 175 | public void UpdateRange(IEnumerable entities) 176 | { 177 | _context.Set().UpdateRange(entities); 178 | } 179 | } 180 | } 181 | -------------------------------------------------------------------------------- /src/Infrastructure/Persistence/Repositories/RefreshTokenRepository.cs: -------------------------------------------------------------------------------- 1 | using Application.Interfaces.Repositories; 2 | using Domain.Entities; 3 | using Persistence.Context; 4 | using System; 5 | using System.Collections.Generic; 6 | using System.Linq; 7 | using System.Text; 8 | using System.Threading.Tasks; 9 | 10 | namespace Persistence.Repositories 11 | { 12 | public class RefreshTokenRepository : EfEntityRepository, IRefreshTokenRepository 13 | { 14 | public RefreshTokenRepository(CAContext context) : base(context) 15 | { 16 | 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/Infrastructure/Persistence/Repositories/RoleRepository.cs: -------------------------------------------------------------------------------- 1 | using Application.Dtos; 2 | using Application.Interfaces.Repositories; 3 | using Domain.Entities; 4 | using Microsoft.EntityFrameworkCore; 5 | using Persistence.Context; 6 | 7 | namespace Persistence.Repositories 8 | { 9 | public class RoleRepository : EfEntityRepository, IRoleRepository 10 | { 11 | public RoleRepository(CAContext context) : base(context) 12 | { 13 | } 14 | 15 | public async Task> GetRolesByUserIdAsync(Guid userId) 16 | { 17 | return await _context.Roles.Where(r => r.UserRoles.Any(ur => ur.UserId == userId)).Select(role => new RoleDTO 18 | { 19 | Id = role.Id, 20 | Name = role.Name 21 | }).ToListAsync(); 22 | } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/Infrastructure/Persistence/Repositories/UnitOfWork.cs: -------------------------------------------------------------------------------- 1 | using Application.Interfaces.Repositories; 2 | using Microsoft.EntityFrameworkCore.Storage; 3 | using Persistence.Context; 4 | using System.Data; 5 | 6 | namespace Persistence.Repositories 7 | { 8 | public class UnitOfWork : IUnitOfWork 9 | { 10 | private readonly CAContext _context; 11 | 12 | public UnitOfWork(CAContext context) 13 | { 14 | _context = context; 15 | } 16 | 17 | public IDbTransaction BeginTransaction() 18 | { 19 | var transaction = _context.Database.BeginTransaction(); 20 | return transaction.GetDbTransaction(); 21 | } 22 | 23 | public void SaveChanges() 24 | { 25 | _context.SaveChanges(); 26 | } 27 | 28 | public async Task SaveChangesAsync() 29 | { 30 | await _context.SaveChangesAsync(); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/Infrastructure/Persistence/Repositories/UserRepository.cs: -------------------------------------------------------------------------------- 1 | using Application.Dtos; 2 | using Application.Interfaces.Repositories; 3 | using Domain.Entities; 4 | using Microsoft.EntityFrameworkCore; 5 | using Persistence.Context; 6 | using System; 7 | using System.Collections.Generic; 8 | using System.Linq; 9 | using System.Text; 10 | using System.Threading.Tasks; 11 | 12 | namespace Persistence.Repositories 13 | { 14 | public class UserRepository : EfEntityRepository, IUserRepository 15 | { 16 | public UserRepository(CAContext context) : base(context) 17 | { 18 | } 19 | 20 | public async Task> GetAllUsersWithRolesAsync() 21 | { 22 | return await _context.Users.AsNoTracking().Select(user => new UserDTO 23 | { 24 | Id = user.Id, 25 | UserName = user.UserName, 26 | FirstName = user.FirstName, 27 | LastName = user.LastName, 28 | Email = user.Email, 29 | Roles = user.UserRoles.Select(ur => ur.Role.Name).ToList() 30 | }).ToListAsync(); 31 | } 32 | 33 | public async Task GetUserRolesByUserIdAsync(Guid userid) 34 | { 35 | return await _context.Users.Include(u => u.UserRoles).SingleOrDefaultAsync(u => u.Id == userid); 36 | } 37 | 38 | public async Task GetUserWithRolesAsync(Guid userid) 39 | { 40 | return await _context.Users.AsNoTracking().Select(user => new UserDTO 41 | { 42 | Id = user.Id, 43 | UserName=user.UserName, 44 | FirstName = user.FirstName, 45 | LastName = user.LastName, 46 | Email = user.Email, 47 | Roles = user.UserRoles.Select(ur => ur.Role.Name).ToList() 48 | }).FirstOrDefaultAsync(user => user.Id == userid); 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/Presentation/WebAPI/Controllers/AuthController.cs: -------------------------------------------------------------------------------- 1 | using Application.Features.Users.Commands; 2 | using Application.Features.Users.Queries; 3 | using Application.Wrappers.Abstract; 4 | using MediatR; 5 | using Microsoft.AspNetCore.Authorization; 6 | using Microsoft.AspNetCore.Mvc; 7 | 8 | namespace WebAPI.Controllers 9 | { 10 | [Route("api/[controller]")] 11 | [ApiController] 12 | public class AuthController : BaseController 13 | { 14 | private readonly IMediator _mediator; 15 | 16 | public AuthController(IMediator mediator) 17 | { 18 | _mediator = mediator; 19 | } 20 | 21 | [HttpGet] 22 | [Authorize] 23 | public async Task GetAuthenticatedUserWithRolesAsync() 24 | { 25 | return await _mediator.Send(new GetAuthenticatedUserWithRolesQuery(UserId.Value)); 26 | } 27 | 28 | [HttpPost("login")] 29 | public async Task Login(LoginCommand command) 30 | { 31 | return await _mediator.Send(command); 32 | } 33 | 34 | [HttpPost("register")] 35 | public async Task Register(RegisterCommand command) 36 | { 37 | return await _mediator.Send(command); 38 | } 39 | 40 | [HttpPost("refreshtoken")] 41 | public async Task RefreshToken(CreateTokenByRefreshTokenCommand command) 42 | { 43 | return await _mediator.Send(command); 44 | } 45 | } 46 | } -------------------------------------------------------------------------------- /src/Presentation/WebAPI/Controllers/BaseController.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Http; 2 | using Microsoft.AspNetCore.Mvc; 3 | using System.Security.Claims; 4 | 5 | namespace WebAPI.Controllers 6 | { 7 | [Route("api/[controller]")] 8 | [ApiController] 9 | public class BaseController : ControllerBase 10 | { 11 | public Guid? UserId => new(HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/Presentation/WebAPI/Controllers/RolesController.cs: -------------------------------------------------------------------------------- 1 | using Application.Features.Roles.Commands; 2 | using Application.Features.Roles.Queries; 3 | using Application.Wrappers.Abstract; 4 | using MediatR; 5 | using Microsoft.AspNetCore.Authorization; 6 | using Microsoft.AspNetCore.Mvc; 7 | 8 | namespace WebAPI.Controllers 9 | { 10 | [Authorize(Roles = "Admin")] 11 | [Route("api/[controller]")] 12 | [ApiController] 13 | public class RolesController : BaseController 14 | { 15 | private readonly IMediator _mediator; 16 | 17 | public RolesController(IMediator mediator) 18 | { 19 | _mediator = mediator; 20 | } 21 | 22 | 23 | [HttpGet] 24 | public async Task GetRoles() 25 | { 26 | return await _mediator.Send(new GetAllRolesQuery()); 27 | } 28 | 29 | [HttpGet("{id}")] 30 | public async Task GetRole(Guid id) 31 | { 32 | return await _mediator.Send(new GetRoleByIdQuery(id)); 33 | } 34 | 35 | [HttpPost] 36 | public async Task CreateRole(CreateRoleCommand command) 37 | { 38 | return await _mediator.Send(command); 39 | } 40 | 41 | [HttpPut] 42 | public async Task UpdateRole(UpdateRoleCommand command) 43 | { 44 | return await _mediator.Send(command); 45 | } 46 | 47 | [HttpDelete("{id}")] 48 | public async Task RemoveRole(Guid id) 49 | { 50 | return await _mediator.Send(new RemoveRoleCommand(id)); 51 | } 52 | 53 | [HttpGet("getrolesbyuserid/{userid}")] 54 | public async Task GetRolesByUserId(Guid userid) 55 | { 56 | return await _mediator.Send(new GetRolesByUserIdQuery(userid)); 57 | } 58 | } 59 | } -------------------------------------------------------------------------------- /src/Presentation/WebAPI/Controllers/UsersController.cs: -------------------------------------------------------------------------------- 1 | using Application.Features.Users.Commands; 2 | using Application.Features.Users.Queries; 3 | using Application.Wrappers.Abstract; 4 | using MediatR; 5 | using Microsoft.AspNetCore.Authorization; 6 | using Microsoft.AspNetCore.Mvc; 7 | 8 | namespace WebAPI.Controllers 9 | { 10 | [Route("api/[controller]")] 11 | [ApiController] 12 | public class UsersController : BaseController 13 | { 14 | private readonly IMediator _mediator; 15 | 16 | public UsersController(IMediator mediator) 17 | { 18 | _mediator = mediator; 19 | } 20 | 21 | [Authorize(Roles ="Admin")] 22 | [HttpGet] 23 | public async Task GetAllUsersWithRoles() 24 | { 25 | return await _mediator.Send(new GetAllUsersWithRolesQuery()); 26 | } 27 | 28 | 29 | [HttpGet("confirmemail/{code}")] 30 | public async Task ConfirmEmail(string code) 31 | { 32 | return await _mediator.Send(new ConfirmEmailCommand(code)); 33 | } 34 | 35 | 36 | [Authorize(Roles = "Admin")] 37 | [HttpPut("updateuserrole")] 38 | public async Task UpdateUserRole(UpdateUserRoleCommand command) 39 | { 40 | return await _mediator.Send(command); 41 | } 42 | 43 | [Authorize] 44 | [HttpPut] 45 | public async Task UpdateUser(UpdateUserCommand command) 46 | { 47 | command.UserId = UserId.Value; 48 | return await _mediator.Send(command); 49 | } 50 | 51 | [Authorize] 52 | [HttpPost("changepassword")] 53 | public async Task ChangePassword (ChangePasswordCommand command) 54 | { 55 | command.UserId = UserId.Value; 56 | return await _mediator.Send(command); 57 | } 58 | 59 | [Authorize] 60 | [HttpPost("changeemail")] 61 | public async Task ChangeEmail(ChangeEmailCommand command) 62 | { 63 | command.UserId = UserId.Value; 64 | return await _mediator.Send(command); 65 | } 66 | 67 | [HttpPost("forgetpassword")] 68 | public async Task ForgetPassword(ForgetPasswordCommand command) 69 | { 70 | return await _mediator.Send(command); 71 | } 72 | 73 | [HttpGet("resetpassword/{code}/{email}")] 74 | public async Task ResetPassword(string code,string email) 75 | { 76 | return await _mediator.Send(new ResetPasswordCommand(code, email)); 77 | } 78 | 79 | [Authorize(Roles = "Admin")] 80 | [HttpDelete("{userid}")] 81 | public async Task DeleteUser(Guid userid) 82 | { 83 | return await _mediator.Send(new RemoveUserCommand(userid)); 84 | } 85 | 86 | } 87 | } -------------------------------------------------------------------------------- /src/Presentation/WebAPI/Dockerfile: -------------------------------------------------------------------------------- 1 | #See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging. 2 | 3 | FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base 4 | WORKDIR /app 5 | EXPOSE 80 6 | 7 | FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build 8 | WORKDIR /src 9 | COPY ["src/Presentation/WebAPI/WebAPI.csproj", "src/Presentation/WebAPI/"] 10 | COPY ["src/Infrastructure/Infrastructure/Infrastructure.csproj", "src/Infrastructure/Infrastructure/"] 11 | COPY ["src/Core/Application/Application.csproj", "src/Core/Application/"] 12 | COPY ["src/Core/Domain/Domain.csproj", "src/Core/Domain/"] 13 | COPY ["src/Infrastructure/Persistence/Persistence.csproj", "src/Infrastructure/Persistence/"] 14 | RUN dotnet restore "src/Presentation/WebAPI/WebAPI.csproj" 15 | COPY . . 16 | WORKDIR "/src/src/Presentation/WebAPI" 17 | RUN dotnet build "WebAPI.csproj" -c Release -o /app/build 18 | 19 | FROM build AS publish 20 | RUN dotnet publish "WebAPI.csproj" -c Release -o /app/publish 21 | 22 | FROM base AS final 23 | WORKDIR /app 24 | COPY --from=publish /app/publish . 25 | ENTRYPOINT ["dotnet", "WebAPI.dll"] -------------------------------------------------------------------------------- /src/Presentation/WebAPI/Infrastructure/Extensions/AuthRegistration.cs: -------------------------------------------------------------------------------- 1 | using Infrastructure.Settings; 2 | using Microsoft.AspNetCore.Authentication.JwtBearer; 3 | using Microsoft.IdentityModel.Tokens; 4 | using System.Text; 5 | 6 | namespace WebAPI.Infrastructure.Extensions 7 | { 8 | public static class AuthRegistration 9 | { 10 | public static void ConfigureAuth(this IServiceCollection services,IConfiguration configuration) 11 | { 12 | services.AddAuthentication(options => 13 | { 14 | options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; 15 | options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; 16 | }).AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, opts => 17 | { 18 | var jwtSettings = configuration.GetSection("JWTSettings").Get(); 19 | opts.TokenValidationParameters = new TokenValidationParameters() 20 | { 21 | ValidIssuer = jwtSettings.Issuer, 22 | ValidAudience = jwtSettings.Audience[0], 23 | IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtSettings.SecurityKey)), 24 | ValidateIssuerSigningKey = true, 25 | ValidateAudience = true, 26 | ValidateIssuer = true, 27 | ValidateLifetime = true, 28 | ClockSkew = TimeSpan.Zero 29 | }; 30 | }); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/Presentation/WebAPI/Infrastructure/Extensions/ExceptionMiddlewareExtension.cs: -------------------------------------------------------------------------------- 1 | using WebAPI.Infrastructure.Middleware; 2 | 3 | namespace WebAPI.Infrastructure.Extensions 4 | { 5 | public static class ExceptionMiddlewareExtension 6 | { 7 | public static void UseCustomExceptionMiddleware(this IApplicationBuilder app) 8 | { 9 | app.UseMiddleware(); 10 | } 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /src/Presentation/WebAPI/Infrastructure/Filters/ValidationFilter.cs: -------------------------------------------------------------------------------- 1 | using Application.Wrappers.Concrete; 2 | using Microsoft.AspNetCore.Mvc; 3 | using Microsoft.AspNetCore.Mvc.Filters; 4 | 5 | namespace WebAPI.Infrastructure.Filters 6 | { 7 | public class ValidationFilter : IAsyncActionFilter 8 | { 9 | public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) 10 | { 11 | if (!context.ModelState.IsValid) 12 | { 13 | var errors = context.ModelState.Values.Where(v => v.Errors.Count > 0) 14 | .SelectMany(v => v.Errors) 15 | .Select(v => v.ErrorMessage) 16 | .ToList(); 17 | 18 | var errorResponse = new ErrorResponse(400,errors); 19 | 20 | context.Result = new BadRequestObjectResult(errorResponse); 21 | return; 22 | } 23 | 24 | await next(); 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /src/Presentation/WebAPI/Infrastructure/Middleware/ExceptionMiddleware.cs: -------------------------------------------------------------------------------- 1 | using Application.Exceptions; 2 | using Application.Wrappers.Concrete; 3 | using Microsoft.AspNetCore.Http; 4 | using Newtonsoft.Json; 5 | using Newtonsoft.Json.Serialization; 6 | using System.Net; 7 | 8 | namespace WebAPI.Infrastructure.Middleware 9 | { 10 | public class ExceptionMiddleware 11 | { 12 | private RequestDelegate _next; 13 | private readonly ILogger _logger; 14 | 15 | public ExceptionMiddleware(RequestDelegate next, ILogger logger) 16 | { 17 | _next = next; 18 | _logger = logger; 19 | } 20 | 21 | public async Task InvokeAsync(HttpContext httpContext) 22 | { 23 | try 24 | { 25 | await _next(httpContext); 26 | } 27 | catch (Exception e) 28 | { 29 | await HandleExceptionAsync(httpContext, e); 30 | } 31 | } 32 | 33 | private Task HandleExceptionAsync(HttpContext httpContext, Exception e) 34 | { 35 | httpContext.Response.ContentType = "application/json"; 36 | httpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError; 37 | string message = "Internal Server Error"; 38 | if (e.InnerException is ApiException || e.GetType() == typeof(ApiException)) 39 | { 40 | var ex = e.InnerException != null ? (ApiException)e.InnerException : (ApiException)e; 41 | httpContext.Response.StatusCode = ex.StatusCode; 42 | var apierror = JsonConvert.SerializeObject(new ErrorResponse(ex.StatusCode, ex.Errors), new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }); 43 | return httpContext.Response.WriteAsync(apierror); 44 | } 45 | 46 | List exceptions = new List(); 47 | 48 | if (e.InnerException != null) 49 | { 50 | exceptions.Add(e.InnerException.ToString()); 51 | if (e.InnerException.Message != null) 52 | { 53 | exceptions.Add(e.InnerException.Message); 54 | } 55 | else if (e.InnerException.InnerException.Message != null) 56 | { 57 | exceptions.Add(e.InnerException.InnerException.Message); 58 | } 59 | } 60 | else if (e.Message != null) 61 | { 62 | exceptions.Add(e.Message); 63 | } 64 | var errorlogDetail = new 65 | { 66 | Errors = exceptions, 67 | }; 68 | _logger.LogError("Unknown error occurred {@Error}", errorlogDetail); 69 | var error = JsonConvert.SerializeObject(new ErrorResponse(httpContext.Response.StatusCode, message), new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() }); 70 | return httpContext.Response.WriteAsync(error); 71 | } 72 | } 73 | } -------------------------------------------------------------------------------- /src/Presentation/WebAPI/Program.cs: -------------------------------------------------------------------------------- 1 | using Application; 2 | using FluentValidation.AspNetCore; 3 | using Infrastructure; 4 | using Microsoft.AspNetCore.Mvc; 5 | using Microsoft.EntityFrameworkCore; 6 | using Microsoft.OpenApi.Models; 7 | using NpgsqlTypes; 8 | using Persistence; 9 | using Persistence.Context; 10 | using Serilog; 11 | using Serilog.Sinks.PostgreSQL; 12 | using WebAPI.Infrastructure.Extensions; 13 | using WebAPI.Infrastructure.Filters; 14 | 15 | var builder = WebApplication.CreateBuilder(args); 16 | 17 | // Add services to the container. 18 | 19 | builder.Services.AddControllers(options => 20 | { 21 | options.Filters.Add(typeof(ValidationFilter)); 22 | }).AddFluentValidation(); 23 | // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle 24 | builder.Services.AddEndpointsApiExplorer(); 25 | 26 | 27 | builder.Services.AddSwaggerGen(c => 28 | { 29 | c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme() 30 | { 31 | Name = "Authorization", 32 | Type = SecuritySchemeType.ApiKey, 33 | Scheme = "Bearer", 34 | BearerFormat = "JWT", 35 | In = ParameterLocation.Header, 36 | Description = "JWT Authorization header using the Bearer scheme. \r\n\r\n Enter 'Bearer' [space] and then your token in the text input below.\r\n\r\nExample: \"Bearer 1safsfsdfdfd\"", 37 | }); 38 | c.AddSecurityRequirement(new OpenApiSecurityRequirement 39 | { 40 | { 41 | new OpenApiSecurityScheme 42 | { 43 | Reference = new OpenApiReference 44 | { 45 | Type = ReferenceType.SecurityScheme, 46 | Id = "Bearer" 47 | } 48 | }, 49 | new string[] {} 50 | } 51 | }); 52 | }); 53 | builder.Services.ConfigureAuth(builder.Configuration); 54 | builder.Services.Configure(options => 55 | { 56 | options.SuppressModelStateInvalidFilter = true; 57 | }); 58 | builder.Services.AddApplicationServices(); 59 | builder.Services.AddInfrastructureServices(builder.Configuration); 60 | builder.Services.AddPersistenceServices(builder.Configuration); 61 | 62 | var Logger = new LoggerConfiguration() 63 | .ReadFrom.Configuration(builder.Configuration) 64 | .Enrich.FromLogContext() 65 | .CreateLogger(); 66 | 67 | builder.Logging.ClearProviders(); 68 | builder.Logging.AddSerilog(Logger); 69 | 70 | var app = builder.Build(); 71 | AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true); 72 | using (var scope = app.Services.CreateScope()) 73 | { 74 | var dataContext = scope.ServiceProvider.GetRequiredService(); 75 | dataContext.Database.Migrate(); 76 | } 77 | 78 | 79 | // Configure the HTTP request pipeline. 80 | if (app.Environment.IsDevelopment()) 81 | { 82 | app.UseSwagger(); 83 | app.UseSwaggerUI(); 84 | } 85 | app.UseCustomExceptionMiddleware(); 86 | app.UseAuthentication(); 87 | app.UseCors(builder => 88 | { 89 | builder 90 | .AllowAnyOrigin() 91 | .AllowAnyMethod() 92 | .AllowAnyHeader(); 93 | }); 94 | app.UseAuthorization(); 95 | 96 | app.MapControllers(); 97 | 98 | app.Run(); 99 | -------------------------------------------------------------------------------- /src/Presentation/WebAPI/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:3252", 8 | "sslPort": 0 9 | } 10 | }, 11 | "profiles": { 12 | "WebAPI": { 13 | "commandName": "Project", 14 | "launchUrl": "swagger", 15 | "environmentVariables": { 16 | "ASPNETCORE_ENVIRONMENT": "Development" 17 | }, 18 | "applicationUrl": "http://localhost:5010", 19 | "dotnetRunMessages": true 20 | }, 21 | "IIS Express": { 22 | "commandName": "IISExpress", 23 | "launchBrowser": true, 24 | "launchUrl": "swagger", 25 | "environmentVariables": { 26 | "ASPNETCORE_ENVIRONMENT": "Development" 27 | } 28 | }, 29 | "Docker": { 30 | "commandName": "Docker", 31 | "launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/swagger", 32 | "publishAllPorts": true 33 | } 34 | } 35 | } -------------------------------------------------------------------------------- /src/Presentation/WebAPI/WebAPI.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | net6.0 5 | disable 6 | enable 7 | Linux 8 | ..\..\.. 9 | ..\..\..\docker-compose.dcproj 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | all 23 | runtime; build; native; contentfiles; analyzers; buildtransitive 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /src/Presentation/WebAPI/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Information", 5 | "Microsoft.AspNetCore": "Warning" 6 | } 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /src/Presentation/WebAPI/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Serilog": { 3 | "Using": [ "Serilog.Sinks.Console", "Serilog.Sinks.File", "Serilog.Sinks.PostgreSQL.Configuration" ], 4 | "MinimumLevel": { 5 | "Default": "Information", 6 | "Override": { 7 | "Microsoft": "Warning", 8 | "System": "Warning" 9 | } 10 | }, 11 | "WriteTo": [ 12 | { 13 | "Name": "Console", 14 | "Args": { 15 | "outputTemplate": "{Timestamp:HH:mm:ss.fff zzz} [{Level}] {Message}{NewLine}{Exception}" 16 | } 17 | }, 18 | { 19 | "Name": "File", 20 | "Args": { 21 | "path": "Logs/Log.txt", 22 | "outputTemplate": "{Timestamp:HH:mm:ss.fff zzz} [{Level}] {Message}{NewLine}{Exception}", 23 | "rollingInterval": "Hour" 24 | } 25 | }, 26 | { 27 | "Name": "PostgreSQL", 28 | "Args": { 29 | "connectionString": "SeriLogConnection", 30 | "tableName": "Logs", 31 | "needAutoCreateTable": true 32 | } 33 | } 34 | ] 35 | }, 36 | "Columns": { 37 | "message": "RenderedMessageColumnWriter", 38 | "message_template": "MessageTemplateColumnWriter", 39 | "level": { 40 | "Name": "LevelColumnWriter", 41 | "Args": { 42 | "renderAsText": true, 43 | "dbType": "Varchar" 44 | } 45 | }, 46 | "raise_date": "TimestampColumnWriter", 47 | "exception": "ExceptionColumnWriter", 48 | "properties": "LogEventSerializedColumnWriter" 49 | }, 50 | "ConnectionStrings": { 51 | "PostgreSqlConnection": "User Id=postgres;Password=1283;Server=localhost;Port=5432;Database=CleanArchitecture;Integrated Security=true;", 52 | "SeriLogConnection": "User Id=postgres;Password=1283;Server=localhost;Port=5432;Database=CleanArchitecture;Integrated Security=true;" 53 | }, 54 | "JWTSettings": { 55 | "Audience": [ "www.CleanArchitecture.com" ], 56 | "Issuer": "www.CleanArchitecture.com", 57 | "AccessTokenExpiration": 120, 58 | "RefreshTokenExpiration": 600, 59 | "SecurityKey": "mysecuritykeyysdadsadsadadsadsadsadsadsadsadsadsadsadsadsa" 60 | }, 61 | "EmailSettings": { 62 | "Host": "smtp", 63 | "Port": 587, 64 | "EnableSSL": true, 65 | "Email": "youremail", 66 | "Password": "yourpassword" 67 | }, 68 | "CacheSettings": { 69 | "PreferRedis": false, 70 | "RedisURL": "localhost", 71 | "Port": 6379 72 | }, 73 | "AllowedHosts": "*" 74 | } 75 | --------------------------------------------------------------------------------