├── .gitignore ├── LICENSE ├── README.md ├── end └── CompanyEmployees │ ├── CompanyEmployees.sln │ ├── CompanyEmployees │ ├── CompanyEmployees.csproj │ ├── Controllers │ │ └── CompaniesController.cs │ ├── Extensions │ │ └── ServiceExtensions.cs │ ├── MappingProfile.cs │ ├── Migrations │ │ ├── 20190927181200_DataBaseCreation.Designer.cs │ │ ├── 20190927181200_DataBaseCreation.cs │ │ ├── 20190927182016_InitialData.Designer.cs │ │ ├── 20190927182016_InitialData.cs │ │ └── RepositoryContextModelSnapshot.cs │ ├── Program.cs │ ├── Properties │ │ └── launchSettings.json │ ├── Startup.cs │ ├── appsettings.Development.json │ ├── appsettings.json │ └── nlog.config │ ├── Contracts │ ├── Contracts.csproj │ ├── ICompanyRepository.cs │ └── ILoggerManager.cs │ ├── Entities │ ├── Configuration │ │ └── CompanyConfiguration.cs │ ├── DataTransferObjects │ │ ├── CompanyDto.cs │ │ └── CompanyForCreationDto.cs │ ├── Entities.csproj │ ├── Models │ │ └── Company.cs │ └── RepositoryContext.cs │ ├── LoggerService │ ├── LoggerManager.cs │ └── LoggerService.csproj │ └── Repository │ ├── CompanyRepository.cs │ └── Repository.csproj └── start └── CompanyEmployees ├── CompanyEmployees.sln ├── CompanyEmployees ├── CompanyEmployees.csproj ├── Controllers │ └── CompaniesController.cs ├── Extensions │ └── ServiceExtensions.cs ├── MappingProfile.cs ├── Migrations │ ├── 20190927181200_DataBaseCreation.Designer.cs │ ├── 20190927181200_DataBaseCreation.cs │ ├── 20190927182016_InitialData.Designer.cs │ ├── 20190927182016_InitialData.cs │ └── RepositoryContextModelSnapshot.cs ├── Program.cs ├── Properties │ └── launchSettings.json ├── Startup.cs ├── appsettings.Development.json ├── appsettings.json └── nlog.config ├── Contracts ├── Contracts.csproj ├── ICompanyRepository.cs └── ILoggerManager.cs ├── Entities ├── Configuration │ └── CompanyConfiguration.cs ├── DataTransferObjects │ ├── CompanyDto.cs │ └── CompanyForCreationDto.cs ├── Entities.csproj ├── Models │ └── Company.cs └── RepositoryContext.cs ├── LoggerService ├── LoggerManager.cs └── LoggerService.csproj └── Repository ├── CompanyRepository.cs └── Repository.csproj /.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/master/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 | [Aa][Rr][Mm]/ 27 | [Aa][Rr][Mm]64/ 28 | bld/ 29 | [Bb]in/ 30 | [Oo]bj/ 31 | [Ll]og/ 32 | [Ll]ogs/ 33 | 34 | # Visual Studio 2015/2017 cache/options directory 35 | .vs/ 36 | # Uncomment if you have tasks that create the project's static files in wwwroot 37 | #wwwroot/ 38 | 39 | # Visual Studio 2017 auto generated files 40 | Generated\ Files/ 41 | 42 | # MSTest test Results 43 | [Tt]est[Rr]esult*/ 44 | [Bb]uild[Ll]og.* 45 | 46 | # NUnit 47 | *.VisualState.xml 48 | TestResult.xml 49 | nunit-*.xml 50 | 51 | # Build Results of an ATL Project 52 | [Dd]ebugPS/ 53 | [Rr]eleasePS/ 54 | dlldata.c 55 | 56 | # Benchmark Results 57 | BenchmarkDotNet.Artifacts/ 58 | 59 | # .NET Core 60 | project.lock.json 61 | project.fragment.lock.json 62 | artifacts/ 63 | 64 | # StyleCop 65 | StyleCopReport.xml 66 | 67 | # Files built by Visual Studio 68 | *_i.c 69 | *_p.c 70 | *_h.h 71 | *.ilk 72 | *.meta 73 | *.obj 74 | *.iobj 75 | *.pch 76 | *.pdb 77 | *.ipdb 78 | *.pgc 79 | *.pgd 80 | *.rsp 81 | *.sbr 82 | *.tlb 83 | *.tli 84 | *.tlh 85 | *.tmp 86 | *.tmp_proj 87 | *_wpftmp.csproj 88 | *.log 89 | *.vspscc 90 | *.vssscc 91 | .builds 92 | *.pidb 93 | *.svclog 94 | *.scc 95 | 96 | # Chutzpah Test files 97 | _Chutzpah* 98 | 99 | # Visual C++ cache files 100 | ipch/ 101 | *.aps 102 | *.ncb 103 | *.opendb 104 | *.opensdf 105 | *.sdf 106 | *.cachefile 107 | *.VC.db 108 | *.VC.VC.opendb 109 | 110 | # Visual Studio profiler 111 | *.psess 112 | *.vsp 113 | *.vspx 114 | *.sap 115 | 116 | # Visual Studio Trace Files 117 | *.e2e 118 | 119 | # TFS 2012 Local Workspace 120 | $tf/ 121 | 122 | # Guidance Automation Toolkit 123 | *.gpState 124 | 125 | # ReSharper is a .NET coding add-in 126 | _ReSharper*/ 127 | *.[Rr]e[Ss]harper 128 | *.DotSettings.user 129 | 130 | # TeamCity is a build add-in 131 | _TeamCity* 132 | 133 | # DotCover is a Code Coverage Tool 134 | *.dotCover 135 | 136 | # AxoCover is a Code Coverage Tool 137 | .axoCover/* 138 | !.axoCover/settings.json 139 | 140 | # Visual Studio code coverage results 141 | *.coverage 142 | *.coveragexml 143 | 144 | # NCrunch 145 | _NCrunch_* 146 | .*crunch*.local.xml 147 | nCrunchTemp_* 148 | 149 | # MightyMoose 150 | *.mm.* 151 | AutoTest.Net/ 152 | 153 | # Web workbench (sass) 154 | .sass-cache/ 155 | 156 | # Installshield output folder 157 | [Ee]xpress/ 158 | 159 | # DocProject is a documentation generator add-in 160 | DocProject/buildhelp/ 161 | DocProject/Help/*.HxT 162 | DocProject/Help/*.HxC 163 | DocProject/Help/*.hhc 164 | DocProject/Help/*.hhk 165 | DocProject/Help/*.hhp 166 | DocProject/Help/Html2 167 | DocProject/Help/html 168 | 169 | # Click-Once directory 170 | publish/ 171 | 172 | # Publish Web Output 173 | *.[Pp]ublish.xml 174 | *.azurePubxml 175 | # Note: Comment the next line if you want to checkin your web deploy settings, 176 | # but database connection strings (with potential passwords) will be unencrypted 177 | *.pubxml 178 | *.publishproj 179 | 180 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 181 | # checkin your Azure Web App publish settings, but sensitive information contained 182 | # in these scripts will be unencrypted 183 | PublishScripts/ 184 | 185 | # NuGet Packages 186 | *.nupkg 187 | # NuGet Symbol Packages 188 | *.snupkg 189 | # The packages folder can be ignored because of Package Restore 190 | **/[Pp]ackages/* 191 | # except build/, which is used as an MSBuild target. 192 | !**/[Pp]ackages/build/ 193 | # Uncomment if necessary however generally it will be regenerated when needed 194 | #!**/[Pp]ackages/repositories.config 195 | # NuGet v3's project.json files produces more ignorable files 196 | *.nuget.props 197 | *.nuget.targets 198 | 199 | # Microsoft Azure Build Output 200 | csx/ 201 | *.build.csdef 202 | 203 | # Microsoft Azure Emulator 204 | ecf/ 205 | rcf/ 206 | 207 | # Windows Store app package directories and files 208 | AppPackages/ 209 | BundleArtifacts/ 210 | Package.StoreAssociation.xml 211 | _pkginfo.txt 212 | *.appx 213 | *.appxbundle 214 | *.appxupload 215 | 216 | # Visual Studio cache files 217 | # files ending in .cache can be ignored 218 | *.[Cc]ache 219 | # but keep track of directories ending in .cache 220 | !?*.[Cc]ache/ 221 | 222 | # Others 223 | ClientBin/ 224 | ~$* 225 | *~ 226 | *.dbmdl 227 | *.dbproj.schemaview 228 | *.jfm 229 | *.pfx 230 | *.publishsettings 231 | orleans.codegen.cs 232 | 233 | # Including strong name files can present a security risk 234 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 235 | #*.snk 236 | 237 | # Since there are multiple workflows, uncomment next line to ignore bower_components 238 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 239 | #bower_components/ 240 | 241 | # RIA/Silverlight projects 242 | Generated_Code/ 243 | 244 | # Backup & report files from converting an old project file 245 | # to a newer Visual Studio version. Backup files are not needed, 246 | # because we have git ;-) 247 | _UpgradeReport_Files/ 248 | Backup*/ 249 | UpgradeLog*.XML 250 | UpgradeLog*.htm 251 | ServiceFabricBackup/ 252 | *.rptproj.bak 253 | 254 | # SQL Server files 255 | *.mdf 256 | *.ldf 257 | *.ndf 258 | 259 | # Business Intelligence projects 260 | *.rdl.data 261 | *.bim.layout 262 | *.bim_*.settings 263 | *.rptproj.rsuser 264 | *- [Bb]ackup.rdl 265 | *- [Bb]ackup ([0-9]).rdl 266 | *- [Bb]ackup ([0-9][0-9]).rdl 267 | 268 | # Microsoft Fakes 269 | FakesAssemblies/ 270 | 271 | # GhostDoc plugin setting file 272 | *.GhostDoc.xml 273 | 274 | # Node.js Tools for Visual Studio 275 | .ntvs_analysis.dat 276 | node_modules/ 277 | 278 | # Visual Studio 6 build log 279 | *.plg 280 | 281 | # Visual Studio 6 workspace options file 282 | *.opt 283 | 284 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 285 | *.vbw 286 | 287 | # Visual Studio LightSwitch build output 288 | **/*.HTMLClient/GeneratedArtifacts 289 | **/*.DesktopClient/GeneratedArtifacts 290 | **/*.DesktopClient/ModelManifest.xml 291 | **/*.Server/GeneratedArtifacts 292 | **/*.Server/ModelManifest.xml 293 | _Pvt_Extensions 294 | 295 | # Paket dependency manager 296 | .paket/paket.exe 297 | paket-files/ 298 | 299 | # FAKE - F# Make 300 | .fake/ 301 | 302 | # CodeRush personal settings 303 | .cr/personal 304 | 305 | # Python Tools for Visual Studio (PTVS) 306 | __pycache__/ 307 | *.pyc 308 | 309 | # Cake - Uncomment if you are using it 310 | # tools/** 311 | # !tools/packages.config 312 | 313 | # Tabs Studio 314 | *.tss 315 | 316 | # Telerik's JustMock configuration file 317 | *.jmconfig 318 | 319 | # BizTalk build output 320 | *.btp.cs 321 | *.btm.cs 322 | *.odx.cs 323 | *.xsd.cs 324 | 325 | # OpenCover UI analysis results 326 | OpenCover/ 327 | 328 | # Azure Stream Analytics local run output 329 | ASALocalRun/ 330 | 331 | # MSBuild Binary and Structured Log 332 | *.binlog 333 | 334 | # NVidia Nsight GPU debugger configuration file 335 | *.nvuser 336 | 337 | # MFractors (Xamarin productivity tool) working folder 338 | .mfractor/ 339 | 340 | # Local History for Visual Studio 341 | .localhistory/ 342 | 343 | # BeatPulse healthcheck temp database 344 | healthchecksdb 345 | 346 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 347 | MigrationBackup/ 348 | 349 | # Ionide (cross platform F# VS Code tools) working folder 350 | .ionide/ 351 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Code Maze 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Asynchronous Programming with Async and Await in ASP.NET Core 2 | ## https://code-maze.com/asynchronous-programming-with-async-and-await-in-asp-net-core 3 | This repo contains the source code for the "Asynchronous Programming with Async and Await in ASP.NET Core" article on Code Maze 4 | -------------------------------------------------------------------------------- /end/CompanyEmployees/CompanyEmployees.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.29318.209 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CompanyEmployees", "CompanyEmployees\CompanyEmployees.csproj", "{8980D1CF-20AB-488F-A809-A297D21DAE9A}" 7 | EndProject 8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Contracts", "Contracts\Contracts.csproj", "{D573853A-C4FE-4F59-A6C2-5BD50D481551}" 9 | EndProject 10 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LoggerService", "LoggerService\LoggerService.csproj", "{CAEEE4DD-EC8B-4DA5-B8A6-894B0FC799E3}" 11 | EndProject 12 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Entities", "Entities\Entities.csproj", "{48986B5B-FFB7-44D7-9A7E-76BC389EA83B}" 13 | EndProject 14 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Repository", "Repository\Repository.csproj", "{80FA5BD0-F17B-4F58-925D-EC359B951EB8}" 15 | EndProject 16 | Global 17 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 18 | Debug|Any CPU = Debug|Any CPU 19 | Release|Any CPU = Release|Any CPU 20 | EndGlobalSection 21 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 22 | {8980D1CF-20AB-488F-A809-A297D21DAE9A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 23 | {8980D1CF-20AB-488F-A809-A297D21DAE9A}.Debug|Any CPU.Build.0 = Debug|Any CPU 24 | {8980D1CF-20AB-488F-A809-A297D21DAE9A}.Release|Any CPU.ActiveCfg = Release|Any CPU 25 | {8980D1CF-20AB-488F-A809-A297D21DAE9A}.Release|Any CPU.Build.0 = Release|Any CPU 26 | {D573853A-C4FE-4F59-A6C2-5BD50D481551}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 27 | {D573853A-C4FE-4F59-A6C2-5BD50D481551}.Debug|Any CPU.Build.0 = Debug|Any CPU 28 | {D573853A-C4FE-4F59-A6C2-5BD50D481551}.Release|Any CPU.ActiveCfg = Release|Any CPU 29 | {D573853A-C4FE-4F59-A6C2-5BD50D481551}.Release|Any CPU.Build.0 = Release|Any CPU 30 | {CAEEE4DD-EC8B-4DA5-B8A6-894B0FC799E3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 31 | {CAEEE4DD-EC8B-4DA5-B8A6-894B0FC799E3}.Debug|Any CPU.Build.0 = Debug|Any CPU 32 | {CAEEE4DD-EC8B-4DA5-B8A6-894B0FC799E3}.Release|Any CPU.ActiveCfg = Release|Any CPU 33 | {CAEEE4DD-EC8B-4DA5-B8A6-894B0FC799E3}.Release|Any CPU.Build.0 = Release|Any CPU 34 | {48986B5B-FFB7-44D7-9A7E-76BC389EA83B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 35 | {48986B5B-FFB7-44D7-9A7E-76BC389EA83B}.Debug|Any CPU.Build.0 = Debug|Any CPU 36 | {48986B5B-FFB7-44D7-9A7E-76BC389EA83B}.Release|Any CPU.ActiveCfg = Release|Any CPU 37 | {48986B5B-FFB7-44D7-9A7E-76BC389EA83B}.Release|Any CPU.Build.0 = Release|Any CPU 38 | {80FA5BD0-F17B-4F58-925D-EC359B951EB8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 39 | {80FA5BD0-F17B-4F58-925D-EC359B951EB8}.Debug|Any CPU.Build.0 = Debug|Any CPU 40 | {80FA5BD0-F17B-4F58-925D-EC359B951EB8}.Release|Any CPU.ActiveCfg = Release|Any CPU 41 | {80FA5BD0-F17B-4F58-925D-EC359B951EB8}.Release|Any CPU.Build.0 = Release|Any CPU 42 | EndGlobalSection 43 | GlobalSection(SolutionProperties) = preSolution 44 | HideSolutionNode = FALSE 45 | EndGlobalSection 46 | GlobalSection(ExtensibilityGlobals) = postSolution 47 | SolutionGuid = {7E2E5905-0DED-4CBE-804E-1A7A08496593} 48 | EndGlobalSection 49 | EndGlobal 50 | -------------------------------------------------------------------------------- /end/CompanyEmployees/CompanyEmployees/CompanyEmployees.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net5.0 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | all 14 | runtime; build; native; contentfiles; analyzers; buildtransitive 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /end/CompanyEmployees/CompanyEmployees/Controllers/CompaniesController.cs: -------------------------------------------------------------------------------- 1 | using AutoMapper; 2 | using Contracts; 3 | using Entities.DataTransferObjects; 4 | using Entities.Models; 5 | using Microsoft.AspNetCore.Mvc; 6 | using System; 7 | using System.Collections.Generic; 8 | using System.Linq; 9 | using System.Threading.Tasks; 10 | 11 | namespace CompanyEmployees.Controllers 12 | { 13 | [Route("api/companies")] 14 | [ApiController] 15 | public class CompaniesController : ControllerBase 16 | { 17 | private readonly ICompanyRepository _repository; 18 | private readonly ILoggerManager _logger; 19 | private readonly IMapper _mapper; 20 | 21 | public CompaniesController(ICompanyRepository repository, ILoggerManager logger, IMapper mapper) 22 | { 23 | _repository = repository; 24 | _logger = logger; 25 | _mapper = mapper; 26 | } 27 | 28 | [HttpGet] 29 | public async Task GetCompanies() 30 | { 31 | try 32 | { 33 | var companies = await _repository.GetAllCompanies(); 34 | 35 | var companiesDto = _mapper.Map>(companies); 36 | 37 | _logger.LogInfo("All companies fetched from the database"); 38 | 39 | return Ok(companiesDto); 40 | } 41 | catch (Exception ex) 42 | { 43 | _logger.LogError($"Exception occurred with a message: {ex.Message}"); 44 | return StatusCode(500, ex.Message); 45 | } 46 | } 47 | 48 | [HttpGet("{id}", Name = "CompanyById")] 49 | public async Task GetCompany(Guid id) 50 | { 51 | var company = await _repository.GetCompany(id); 52 | if (company == null) 53 | { 54 | _logger.LogInfo($"Company with id: {id} doesn't exist in the database."); 55 | return NotFound(); 56 | } 57 | else 58 | { 59 | var companyDto = _mapper.Map(company); 60 | return Ok(companyDto); 61 | } 62 | } 63 | 64 | [HttpPost] 65 | public async Task CreateCompany([FromBody] CompanyForCreationDto company) 66 | { 67 | if (company == null) 68 | { 69 | _logger.LogError("CompanyForCreationDto object sent from client is null."); 70 | return BadRequest("CompanyForCreationDto object is null"); 71 | } 72 | 73 | var companyEntity = _mapper.Map(company); 74 | 75 | await _repository.CreateCompany(companyEntity); 76 | 77 | var companyToReturn = _mapper.Map(companyEntity); 78 | 79 | return CreatedAtRoute("CompanyById", new { id = companyToReturn.Id }, companyToReturn); 80 | } 81 | } 82 | } -------------------------------------------------------------------------------- /end/CompanyEmployees/CompanyEmployees/Extensions/ServiceExtensions.cs: -------------------------------------------------------------------------------- 1 | using Contracts; 2 | using Entities; 3 | using LoggerService; 4 | using Microsoft.AspNetCore.Builder; 5 | using Microsoft.EntityFrameworkCore; 6 | using Microsoft.Extensions.Configuration; 7 | using Microsoft.Extensions.DependencyInjection; 8 | using Repository; 9 | 10 | namespace CompanyEmployees.Extensions 11 | { 12 | public static class ServiceExtensions 13 | { 14 | public static void ConfigureLoggerService(this IServiceCollection services) => 15 | services.AddScoped(); 16 | 17 | public static void ConfigureSqlContext(this IServiceCollection services, IConfiguration configuration) => 18 | services.AddDbContext(opts => 19 | opts.UseSqlServer(configuration.GetConnectionString("sqlConnection"), b => b.MigrationsAssembly("CompanyEmployees"))); 20 | 21 | public static void ConfigureRepository(this IServiceCollection services) => 22 | services.AddScoped(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /end/CompanyEmployees/CompanyEmployees/MappingProfile.cs: -------------------------------------------------------------------------------- 1 | using AutoMapper; 2 | using Entities.DataTransferObjects; 3 | using Entities.Models; 4 | 5 | namespace CompanyEmployees 6 | { 7 | public class MappingProfile : Profile 8 | { 9 | public MappingProfile() 10 | { 11 | CreateMap() 12 | .ForMember(c => c.FullAddress, 13 | opt => opt.MapFrom(x => string.Join(' ', x.Address, x.Country))); 14 | 15 | CreateMap(); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /end/CompanyEmployees/CompanyEmployees/Migrations/20190927181200_DataBaseCreation.Designer.cs: -------------------------------------------------------------------------------- 1 | // 2 | using System; 3 | using Entities; 4 | using Microsoft.EntityFrameworkCore; 5 | using Microsoft.EntityFrameworkCore.Infrastructure; 6 | using Microsoft.EntityFrameworkCore.Metadata; 7 | using Microsoft.EntityFrameworkCore.Migrations; 8 | using Microsoft.EntityFrameworkCore.Storage.ValueConversion; 9 | 10 | namespace CompanyEmployees.Migrations 11 | { 12 | [DbContext(typeof(RepositoryContext))] 13 | [Migration("20190927181200_DataBaseCreation")] 14 | partial class DataBaseCreation 15 | { 16 | protected override void BuildTargetModel(ModelBuilder modelBuilder) 17 | { 18 | #pragma warning disable 612, 618 19 | modelBuilder 20 | .HasAnnotation("ProductVersion", "3.0.0") 21 | .HasAnnotation("Relational:MaxIdentifierLength", 128) 22 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 23 | 24 | modelBuilder.Entity("Entities.Models.Company", b => 25 | { 26 | b.Property("Id") 27 | .ValueGeneratedOnAdd() 28 | .HasColumnName("CompanyId") 29 | .HasColumnType("uniqueidentifier"); 30 | 31 | b.Property("Address") 32 | .IsRequired() 33 | .HasColumnType("nvarchar(60)") 34 | .HasMaxLength(60); 35 | 36 | b.Property("Country") 37 | .HasColumnType("nvarchar(max)"); 38 | 39 | b.Property("Name") 40 | .IsRequired() 41 | .HasColumnType("nvarchar(60)") 42 | .HasMaxLength(60); 43 | 44 | b.HasKey("Id"); 45 | 46 | b.ToTable("Companies"); 47 | }); 48 | 49 | modelBuilder.Entity("Entities.Models.Employee", b => 50 | { 51 | b.Property("Id") 52 | .ValueGeneratedOnAdd() 53 | .HasColumnName("EmployeeId") 54 | .HasColumnType("uniqueidentifier"); 55 | 56 | b.Property("Age") 57 | .HasColumnType("int"); 58 | 59 | b.Property("CompanyId") 60 | .HasColumnType("uniqueidentifier"); 61 | 62 | b.Property("Name") 63 | .IsRequired() 64 | .HasColumnType("nvarchar(30)") 65 | .HasMaxLength(30); 66 | 67 | b.Property("Position") 68 | .IsRequired() 69 | .HasColumnType("nvarchar(20)") 70 | .HasMaxLength(20); 71 | 72 | b.HasKey("Id"); 73 | 74 | b.HasIndex("CompanyId"); 75 | 76 | b.ToTable("Employees"); 77 | }); 78 | 79 | modelBuilder.Entity("Entities.Models.Employee", b => 80 | { 81 | b.HasOne("Entities.Models.Company", "Company") 82 | .WithMany("Employees") 83 | .HasForeignKey("CompanyId") 84 | .OnDelete(DeleteBehavior.Cascade) 85 | .IsRequired(); 86 | }); 87 | #pragma warning restore 612, 618 88 | } 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /end/CompanyEmployees/CompanyEmployees/Migrations/20190927181200_DataBaseCreation.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Microsoft.EntityFrameworkCore.Migrations; 3 | 4 | namespace CompanyEmployees.Migrations 5 | { 6 | public partial class DataBaseCreation : Migration 7 | { 8 | protected override void Up(MigrationBuilder migrationBuilder) 9 | { 10 | migrationBuilder.CreateTable( 11 | name: "Companies", 12 | columns: table => new 13 | { 14 | CompanyId = table.Column(nullable: false), 15 | Name = table.Column(maxLength: 60, nullable: false), 16 | Address = table.Column(maxLength: 60, nullable: false), 17 | Country = table.Column(nullable: true) 18 | }, 19 | constraints: table => 20 | { 21 | table.PrimaryKey("PK_Companies", x => x.CompanyId); 22 | }); 23 | } 24 | 25 | protected override void Down(MigrationBuilder migrationBuilder) 26 | { 27 | migrationBuilder.DropTable( 28 | name: "Companies"); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /end/CompanyEmployees/CompanyEmployees/Migrations/20190927182016_InitialData.Designer.cs: -------------------------------------------------------------------------------- 1 | // 2 | using System; 3 | using Entities; 4 | using Microsoft.EntityFrameworkCore; 5 | using Microsoft.EntityFrameworkCore.Infrastructure; 6 | using Microsoft.EntityFrameworkCore.Metadata; 7 | using Microsoft.EntityFrameworkCore.Migrations; 8 | using Microsoft.EntityFrameworkCore.Storage.ValueConversion; 9 | 10 | namespace CompanyEmployees.Migrations 11 | { 12 | [DbContext(typeof(RepositoryContext))] 13 | [Migration("20190927182016_InitialData")] 14 | partial class InitialData 15 | { 16 | protected override void BuildTargetModel(ModelBuilder modelBuilder) 17 | { 18 | #pragma warning disable 612, 618 19 | modelBuilder 20 | .HasAnnotation("ProductVersion", "3.0.0") 21 | .HasAnnotation("Relational:MaxIdentifierLength", 128) 22 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 23 | 24 | modelBuilder.Entity("Entities.Models.Company", b => 25 | { 26 | b.Property("Id") 27 | .ValueGeneratedOnAdd() 28 | .HasColumnName("CompanyId") 29 | .HasColumnType("uniqueidentifier"); 30 | 31 | b.Property("Address") 32 | .IsRequired() 33 | .HasColumnType("nvarchar(60)") 34 | .HasMaxLength(60); 35 | 36 | b.Property("Country") 37 | .HasColumnType("nvarchar(max)"); 38 | 39 | b.Property("Name") 40 | .IsRequired() 41 | .HasColumnType("nvarchar(60)") 42 | .HasMaxLength(60); 43 | 44 | b.HasKey("Id"); 45 | 46 | b.ToTable("Companies"); 47 | 48 | b.HasData( 49 | new 50 | { 51 | Id = new Guid("c9d4c053-49b6-410c-bc78-2d54a9991870"), 52 | Address = "583 Wall Dr. Gwynn Oak, MD 21207", 53 | Country = "USA", 54 | Name = "IT_Solutions Ltd" 55 | }, 56 | new 57 | { 58 | Id = new Guid("3d490a70-94ce-4d15-9494-5248280c2ce3"), 59 | Address = "312 Forest Avenue, BF 923", 60 | Country = "USA", 61 | Name = "Admin_Solutions Ltd" 62 | }); 63 | }); 64 | 65 | modelBuilder.Entity("Entities.Models.Employee", b => 66 | { 67 | b.Property("Id") 68 | .ValueGeneratedOnAdd() 69 | .HasColumnName("EmployeeId") 70 | .HasColumnType("uniqueidentifier"); 71 | 72 | b.Property("Age") 73 | .HasColumnType("int"); 74 | 75 | b.Property("CompanyId") 76 | .HasColumnType("uniqueidentifier"); 77 | 78 | b.Property("Name") 79 | .IsRequired() 80 | .HasColumnType("nvarchar(30)") 81 | .HasMaxLength(30); 82 | 83 | b.Property("Position") 84 | .IsRequired() 85 | .HasColumnType("nvarchar(20)") 86 | .HasMaxLength(20); 87 | 88 | b.HasKey("Id"); 89 | 90 | b.HasIndex("CompanyId"); 91 | 92 | b.ToTable("Employees"); 93 | 94 | b.HasData( 95 | new 96 | { 97 | Id = new Guid("80abbca8-664d-4b20-b5de-024705497d4a"), 98 | Age = 26, 99 | CompanyId = new Guid("c9d4c053-49b6-410c-bc78-2d54a9991870"), 100 | Name = "Sam Raiden", 101 | Position = "Software developer" 102 | }, 103 | new 104 | { 105 | Id = new Guid("86dba8c0-d178-41e7-938c-ed49778fb52a"), 106 | Age = 30, 107 | CompanyId = new Guid("c9d4c053-49b6-410c-bc78-2d54a9991870"), 108 | Name = "Jana McLeaf", 109 | Position = "Software developer" 110 | }, 111 | new 112 | { 113 | Id = new Guid("021ca3c1-0deb-4afd-ae94-2159a8479811"), 114 | Age = 35, 115 | CompanyId = new Guid("3d490a70-94ce-4d15-9494-5248280c2ce3"), 116 | Name = "Kane Miller", 117 | Position = "Administrator" 118 | }); 119 | }); 120 | 121 | modelBuilder.Entity("Entities.Models.Employee", b => 122 | { 123 | b.HasOne("Entities.Models.Company", "Company") 124 | .WithMany("Employees") 125 | .HasForeignKey("CompanyId") 126 | .OnDelete(DeleteBehavior.Cascade) 127 | .IsRequired(); 128 | }); 129 | #pragma warning restore 612, 618 130 | } 131 | } 132 | } 133 | -------------------------------------------------------------------------------- /end/CompanyEmployees/CompanyEmployees/Migrations/20190927182016_InitialData.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Microsoft.EntityFrameworkCore.Migrations; 3 | 4 | namespace CompanyEmployees.Migrations 5 | { 6 | public partial class InitialData : Migration 7 | { 8 | protected override void Up(MigrationBuilder migrationBuilder) 9 | { 10 | migrationBuilder.InsertData( 11 | table: "Companies", 12 | columns: new[] { "CompanyId", "Address", "Country", "Name" }, 13 | values: new object[] { new Guid("c9d4c053-49b6-410c-bc78-2d54a9991870"), "583 Wall Dr. Gwynn Oak, MD 21207", "USA", "IT_Solutions Ltd" }); 14 | 15 | migrationBuilder.InsertData( 16 | table: "Companies", 17 | columns: new[] { "CompanyId", "Address", "Country", "Name" }, 18 | values: new object[] { new Guid("3d490a70-94ce-4d15-9494-5248280c2ce3"), "312 Forest Avenue, BF 923", "USA", "Admin_Solutions Ltd" }); 19 | } 20 | 21 | protected override void Down(MigrationBuilder migrationBuilder) 22 | { 23 | migrationBuilder.DeleteData( 24 | table: "Companies", 25 | keyColumn: "CompanyId", 26 | keyValue: new Guid("3d490a70-94ce-4d15-9494-5248280c2ce3")); 27 | 28 | migrationBuilder.DeleteData( 29 | table: "Companies", 30 | keyColumn: "CompanyId", 31 | keyValue: new Guid("c9d4c053-49b6-410c-bc78-2d54a9991870")); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /end/CompanyEmployees/CompanyEmployees/Migrations/RepositoryContextModelSnapshot.cs: -------------------------------------------------------------------------------- 1 | // 2 | using System; 3 | using Entities; 4 | using Microsoft.EntityFrameworkCore; 5 | using Microsoft.EntityFrameworkCore.Infrastructure; 6 | using Microsoft.EntityFrameworkCore.Metadata; 7 | using Microsoft.EntityFrameworkCore.Storage.ValueConversion; 8 | 9 | namespace CompanyEmployees.Migrations 10 | { 11 | [DbContext(typeof(RepositoryContext))] 12 | partial class RepositoryContextModelSnapshot : ModelSnapshot 13 | { 14 | protected override void BuildModel(ModelBuilder modelBuilder) 15 | { 16 | #pragma warning disable 612, 618 17 | modelBuilder 18 | .HasAnnotation("ProductVersion", "3.0.0") 19 | .HasAnnotation("Relational:MaxIdentifierLength", 128) 20 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 21 | 22 | modelBuilder.Entity("Entities.Models.Company", b => 23 | { 24 | b.Property("Id") 25 | .ValueGeneratedOnAdd() 26 | .HasColumnName("CompanyId") 27 | .HasColumnType("uniqueidentifier"); 28 | 29 | b.Property("Address") 30 | .IsRequired() 31 | .HasColumnType("nvarchar(60)") 32 | .HasMaxLength(60); 33 | 34 | b.Property("Country") 35 | .HasColumnType("nvarchar(max)"); 36 | 37 | b.Property("Name") 38 | .IsRequired() 39 | .HasColumnType("nvarchar(60)") 40 | .HasMaxLength(60); 41 | 42 | b.HasKey("Id"); 43 | 44 | b.ToTable("Companies"); 45 | 46 | b.HasData( 47 | new 48 | { 49 | Id = new Guid("c9d4c053-49b6-410c-bc78-2d54a9991870"), 50 | Address = "583 Wall Dr. Gwynn Oak, MD 21207", 51 | Country = "USA", 52 | Name = "IT_Solutions Ltd" 53 | }, 54 | new 55 | { 56 | Id = new Guid("3d490a70-94ce-4d15-9494-5248280c2ce3"), 57 | Address = "312 Forest Avenue, BF 923", 58 | Country = "USA", 59 | Name = "Admin_Solutions Ltd" 60 | }); 61 | }); 62 | 63 | modelBuilder.Entity("Entities.Models.Employee", b => 64 | { 65 | b.Property("Id") 66 | .ValueGeneratedOnAdd() 67 | .HasColumnName("EmployeeId") 68 | .HasColumnType("uniqueidentifier"); 69 | 70 | b.Property("Age") 71 | .HasColumnType("int"); 72 | 73 | b.Property("CompanyId") 74 | .HasColumnType("uniqueidentifier"); 75 | 76 | b.Property("Name") 77 | .IsRequired() 78 | .HasColumnType("nvarchar(30)") 79 | .HasMaxLength(30); 80 | 81 | b.Property("Position") 82 | .IsRequired() 83 | .HasColumnType("nvarchar(20)") 84 | .HasMaxLength(20); 85 | 86 | b.HasKey("Id"); 87 | 88 | b.HasIndex("CompanyId"); 89 | 90 | b.ToTable("Employees"); 91 | 92 | b.HasData( 93 | new 94 | { 95 | Id = new Guid("80abbca8-664d-4b20-b5de-024705497d4a"), 96 | Age = 26, 97 | CompanyId = new Guid("c9d4c053-49b6-410c-bc78-2d54a9991870"), 98 | Name = "Sam Raiden", 99 | Position = "Software developer" 100 | }, 101 | new 102 | { 103 | Id = new Guid("86dba8c0-d178-41e7-938c-ed49778fb52a"), 104 | Age = 30, 105 | CompanyId = new Guid("c9d4c053-49b6-410c-bc78-2d54a9991870"), 106 | Name = "Jana McLeaf", 107 | Position = "Software developer" 108 | }, 109 | new 110 | { 111 | Id = new Guid("021ca3c1-0deb-4afd-ae94-2159a8479811"), 112 | Age = 35, 113 | CompanyId = new Guid("3d490a70-94ce-4d15-9494-5248280c2ce3"), 114 | Name = "Kane Miller", 115 | Position = "Administrator" 116 | }); 117 | }); 118 | 119 | modelBuilder.Entity("Entities.Models.Employee", b => 120 | { 121 | b.HasOne("Entities.Models.Company", "Company") 122 | .WithMany("Employees") 123 | .HasForeignKey("CompanyId") 124 | .OnDelete(DeleteBehavior.Cascade) 125 | .IsRequired(); 126 | }); 127 | #pragma warning restore 612, 618 128 | } 129 | } 130 | } 131 | -------------------------------------------------------------------------------- /end/CompanyEmployees/CompanyEmployees/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | using Microsoft.AspNetCore.Hosting; 6 | using Microsoft.Extensions.Configuration; 7 | using Microsoft.Extensions.Hosting; 8 | using Microsoft.Extensions.Logging; 9 | 10 | namespace CompanyEmployees 11 | { 12 | public class Program 13 | { 14 | public static void Main(string[] args) 15 | { 16 | CreateHostBuilder(args).Build().Run(); 17 | } 18 | 19 | public static IHostBuilder CreateHostBuilder(string[] args) => 20 | Host.CreateDefaultBuilder(args) 21 | .ConfigureWebHostDefaults(webBuilder => 22 | { 23 | webBuilder.UseStartup(); 24 | }); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /end/CompanyEmployees/CompanyEmployees/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "profiles": { 3 | "CompanyEmployees": { 4 | "commandName": "Project", 5 | "launchBrowser": false, 6 | "launchUrl": "weatherforecast", 7 | "applicationUrl": "https://localhost:5001;http://localhost:5000", 8 | "environmentVariables": { 9 | "ASPNETCORE_ENVIRONMENT": "Development" 10 | } 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /end/CompanyEmployees/CompanyEmployees/Startup.cs: -------------------------------------------------------------------------------- 1 | using CompanyEmployees.Extensions; 2 | using Contracts; 3 | using Microsoft.AspNetCore.Builder; 4 | using Microsoft.AspNetCore.Hosting; 5 | using Microsoft.Extensions.Configuration; 6 | using Microsoft.Extensions.DependencyInjection; 7 | using Microsoft.Extensions.Hosting; 8 | using NLog; 9 | using System.IO; 10 | 11 | namespace CompanyEmployees 12 | { 13 | public class Startup 14 | { 15 | public Startup(IConfiguration configuration) 16 | { 17 | LogManager.LoadConfiguration(string.Concat(Directory.GetCurrentDirectory(), "/nlog.config")); 18 | Configuration = configuration; 19 | } 20 | 21 | public IConfiguration Configuration { get; } 22 | 23 | // This method gets called by the runtime. Use this method to add services to the container. 24 | public void ConfigureServices(IServiceCollection services) 25 | { 26 | services.ConfigureLoggerService(); 27 | services.ConfigureSqlContext(Configuration); 28 | services.ConfigureRepository(); 29 | services.AddAutoMapper(typeof(Startup)); 30 | 31 | services.AddControllers(); 32 | } 33 | 34 | // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 35 | public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerManager logger) 36 | { 37 | if (env.IsDevelopment()) 38 | { 39 | app.UseDeveloperExceptionPage(); 40 | } 41 | else 42 | { 43 | app.UseHsts(); 44 | } 45 | 46 | app.UseHttpsRedirection(); 47 | app.UseStaticFiles(); 48 | 49 | app.UseRouting(); 50 | 51 | app.UseAuthorization(); 52 | 53 | app.UseEndpoints(endpoints => 54 | { 55 | endpoints.MapControllers(); 56 | }); 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /end/CompanyEmployees/CompanyEmployees/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Debug", 5 | "System": "Information", 6 | "Microsoft": "Information" 7 | } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /end/CompanyEmployees/CompanyEmployees/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Information", 5 | "Microsoft": "Warning", 6 | "Microsoft.Hosting.Lifetime": "Information" 7 | } 8 | }, 9 | "ConnectionStrings": { 10 | "sqlConnection": "server=.; database=CompanyAsyncAwaitExample; Integrated Security=true" 11 | }, 12 | "AllowedHosts": "*" 13 | } 14 | -------------------------------------------------------------------------------- /end/CompanyEmployees/CompanyEmployees/nlog.config: -------------------------------------------------------------------------------- 1 |  2 | 7 | 8 | 9 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /end/CompanyEmployees/Contracts/Contracts.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net5.0 5 | 6 | 7 | 8 | 9 | all 10 | runtime; build; native; contentfiles; analyzers; buildtransitive 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /end/CompanyEmployees/Contracts/ICompanyRepository.cs: -------------------------------------------------------------------------------- 1 | using Entities.Models; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Threading.Tasks; 5 | 6 | namespace Contracts 7 | { 8 | public interface ICompanyRepository 9 | { 10 | Task> GetAllCompanies(); 11 | Task GetCompany(Guid companyId); 12 | Task CreateCompany(Company company); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /end/CompanyEmployees/Contracts/ILoggerManager.cs: -------------------------------------------------------------------------------- 1 | namespace Contracts 2 | { 3 | public interface ILoggerManager 4 | { 5 | void LogInfo(string message); 6 | void LogWarn(string message); 7 | void LogDebug(string message); 8 | void LogError(string message); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /end/CompanyEmployees/Entities/Configuration/CompanyConfiguration.cs: -------------------------------------------------------------------------------- 1 | using Entities.Models; 2 | using Microsoft.EntityFrameworkCore; 3 | using Microsoft.EntityFrameworkCore.Metadata.Builders; 4 | using System; 5 | 6 | namespace Entities.Configuration 7 | { 8 | public class CompanyConfiguration : IEntityTypeConfiguration 9 | { 10 | public void Configure(EntityTypeBuilder builder) 11 | { 12 | builder.HasData 13 | ( 14 | new Company 15 | { 16 | Id = new Guid("c9d4c053-49b6-410c-bc78-2d54a9991870"), 17 | Name = "IT_Solutions Ltd", 18 | Address = "583 Wall Dr. Gwynn Oak, MD 21207", 19 | Country = "USA" 20 | }, 21 | new Company 22 | { 23 | Id = new Guid("3d490a70-94ce-4d15-9494-5248280c2ce3"), 24 | Name = "Admin_Solutions Ltd", 25 | Address = "312 Forest Avenue, BF 923", 26 | Country = "USA" 27 | } 28 | ); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /end/CompanyEmployees/Entities/DataTransferObjects/CompanyDto.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Entities.DataTransferObjects 6 | { 7 | public class CompanyDto 8 | { 9 | public Guid Id { get; set; } 10 | public string Name { get; set; } 11 | public string FullAddress { get; set; } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /end/CompanyEmployees/Entities/DataTransferObjects/CompanyForCreationDto.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Entities.DataTransferObjects 6 | { 7 | public class CompanyForCreationDto 8 | { 9 | public string Name { get; set; } 10 | public string Address { get; set; } 11 | public string Country { get; set; } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /end/CompanyEmployees/Entities/Entities.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net5.0 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /end/CompanyEmployees/Entities/Models/Company.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.ComponentModel.DataAnnotations; 4 | using System.ComponentModel.DataAnnotations.Schema; 5 | 6 | namespace Entities.Models 7 | { 8 | public class Company 9 | { 10 | [Column("CompanyId")] 11 | public Guid Id { get; set; } 12 | 13 | [Required(ErrorMessage = "Company name is a required field.")] 14 | [MaxLength(60, ErrorMessage = "Maximum length for the Name is 60 characters.")] 15 | public string Name { get; set; } 16 | 17 | [Required(ErrorMessage = "Company address is a required field.")] 18 | [MaxLength(60, ErrorMessage = "Maximum length for the Address is 60 characters.")] 19 | public string Address { get; set; } 20 | 21 | public string Country { get; set; } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /end/CompanyEmployees/Entities/RepositoryContext.cs: -------------------------------------------------------------------------------- 1 | using Entities.Configuration; 2 | using Entities.Models; 3 | using Microsoft.EntityFrameworkCore; 4 | 5 | namespace Entities 6 | { 7 | public class RepositoryContext : DbContext 8 | { 9 | public RepositoryContext(DbContextOptions options) 10 | : base(options) 11 | { 12 | } 13 | 14 | protected override void OnModelCreating(ModelBuilder modelBuilder) 15 | { 16 | modelBuilder.ApplyConfiguration(new CompanyConfiguration()); 17 | } 18 | 19 | public DbSet Companies { get; set; } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /end/CompanyEmployees/LoggerService/LoggerManager.cs: -------------------------------------------------------------------------------- 1 | using Contracts; 2 | using NLog; 3 | 4 | namespace LoggerService 5 | { 6 | public class LoggerManager : ILoggerManager 7 | { 8 | private static ILogger logger = LogManager.GetCurrentClassLogger(); 9 | 10 | public LoggerManager() 11 | { 12 | } 13 | 14 | public void LogDebug(string message) 15 | { 16 | logger.Debug(message); 17 | } 18 | 19 | public void LogError(string message) 20 | { 21 | logger.Error(message); 22 | } 23 | 24 | public void LogInfo(string message) 25 | { 26 | logger.Info(message); 27 | } 28 | 29 | public void LogWarn(string message) 30 | { 31 | logger.Warn(message); 32 | } 33 | 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /end/CompanyEmployees/LoggerService/LoggerService.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net5.0 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /end/CompanyEmployees/Repository/CompanyRepository.cs: -------------------------------------------------------------------------------- 1 | using Contracts; 2 | using Entities; 3 | using Entities.Models; 4 | using Microsoft.EntityFrameworkCore; 5 | using System; 6 | using System.Collections.Generic; 7 | using System.Linq; 8 | using System.Threading.Tasks; 9 | 10 | namespace Repository 11 | { 12 | public class CompanyRepository : ICompanyRepository 13 | { 14 | private readonly RepositoryContext _repoContext; 15 | 16 | public CompanyRepository(RepositoryContext repoContext) 17 | { 18 | _repoContext = repoContext; 19 | } 20 | 21 | public async Task> GetAllCompanies() 22 | { 23 | //throw new Exception("Custom exception for testing purposes"); 24 | return await _repoContext.Companies 25 | .OrderBy(c => c.Name) 26 | .ToListAsync(); 27 | } 28 | 29 | public async Task GetCompany(Guid companyId) => 30 | await _repoContext.Companies 31 | .SingleOrDefaultAsync(c => c.Id.Equals(companyId)); 32 | 33 | public async Task CreateCompany(Company company) 34 | { 35 | _repoContext.Add(company); 36 | await _repoContext.SaveChangesAsync(); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /end/CompanyEmployees/Repository/Repository.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net5.0 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /start/CompanyEmployees/CompanyEmployees.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.29318.209 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CompanyEmployees", "CompanyEmployees\CompanyEmployees.csproj", "{8980D1CF-20AB-488F-A809-A297D21DAE9A}" 7 | EndProject 8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Contracts", "Contracts\Contracts.csproj", "{D573853A-C4FE-4F59-A6C2-5BD50D481551}" 9 | EndProject 10 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LoggerService", "LoggerService\LoggerService.csproj", "{CAEEE4DD-EC8B-4DA5-B8A6-894B0FC799E3}" 11 | EndProject 12 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Entities", "Entities\Entities.csproj", "{48986B5B-FFB7-44D7-9A7E-76BC389EA83B}" 13 | EndProject 14 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Repository", "Repository\Repository.csproj", "{80FA5BD0-F17B-4F58-925D-EC359B951EB8}" 15 | EndProject 16 | Global 17 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 18 | Debug|Any CPU = Debug|Any CPU 19 | Release|Any CPU = Release|Any CPU 20 | EndGlobalSection 21 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 22 | {8980D1CF-20AB-488F-A809-A297D21DAE9A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 23 | {8980D1CF-20AB-488F-A809-A297D21DAE9A}.Debug|Any CPU.Build.0 = Debug|Any CPU 24 | {8980D1CF-20AB-488F-A809-A297D21DAE9A}.Release|Any CPU.ActiveCfg = Release|Any CPU 25 | {8980D1CF-20AB-488F-A809-A297D21DAE9A}.Release|Any CPU.Build.0 = Release|Any CPU 26 | {D573853A-C4FE-4F59-A6C2-5BD50D481551}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 27 | {D573853A-C4FE-4F59-A6C2-5BD50D481551}.Debug|Any CPU.Build.0 = Debug|Any CPU 28 | {D573853A-C4FE-4F59-A6C2-5BD50D481551}.Release|Any CPU.ActiveCfg = Release|Any CPU 29 | {D573853A-C4FE-4F59-A6C2-5BD50D481551}.Release|Any CPU.Build.0 = Release|Any CPU 30 | {CAEEE4DD-EC8B-4DA5-B8A6-894B0FC799E3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 31 | {CAEEE4DD-EC8B-4DA5-B8A6-894B0FC799E3}.Debug|Any CPU.Build.0 = Debug|Any CPU 32 | {CAEEE4DD-EC8B-4DA5-B8A6-894B0FC799E3}.Release|Any CPU.ActiveCfg = Release|Any CPU 33 | {CAEEE4DD-EC8B-4DA5-B8A6-894B0FC799E3}.Release|Any CPU.Build.0 = Release|Any CPU 34 | {48986B5B-FFB7-44D7-9A7E-76BC389EA83B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 35 | {48986B5B-FFB7-44D7-9A7E-76BC389EA83B}.Debug|Any CPU.Build.0 = Debug|Any CPU 36 | {48986B5B-FFB7-44D7-9A7E-76BC389EA83B}.Release|Any CPU.ActiveCfg = Release|Any CPU 37 | {48986B5B-FFB7-44D7-9A7E-76BC389EA83B}.Release|Any CPU.Build.0 = Release|Any CPU 38 | {80FA5BD0-F17B-4F58-925D-EC359B951EB8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 39 | {80FA5BD0-F17B-4F58-925D-EC359B951EB8}.Debug|Any CPU.Build.0 = Debug|Any CPU 40 | {80FA5BD0-F17B-4F58-925D-EC359B951EB8}.Release|Any CPU.ActiveCfg = Release|Any CPU 41 | {80FA5BD0-F17B-4F58-925D-EC359B951EB8}.Release|Any CPU.Build.0 = Release|Any CPU 42 | EndGlobalSection 43 | GlobalSection(SolutionProperties) = preSolution 44 | HideSolutionNode = FALSE 45 | EndGlobalSection 46 | GlobalSection(ExtensibilityGlobals) = postSolution 47 | SolutionGuid = {7E2E5905-0DED-4CBE-804E-1A7A08496593} 48 | EndGlobalSection 49 | EndGlobal 50 | -------------------------------------------------------------------------------- /start/CompanyEmployees/CompanyEmployees/CompanyEmployees.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net5.0 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | all 14 | runtime; build; native; contentfiles; analyzers; buildtransitive 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /start/CompanyEmployees/CompanyEmployees/Controllers/CompaniesController.cs: -------------------------------------------------------------------------------- 1 | using AutoMapper; 2 | using Contracts; 3 | using Entities.DataTransferObjects; 4 | using Entities.Models; 5 | using Microsoft.AspNetCore.Mvc; 6 | using System; 7 | using System.Collections.Generic; 8 | using System.Linq; 9 | 10 | namespace CompanyEmployees.Controllers 11 | { 12 | [Route("api/companies")] 13 | [ApiController] 14 | public class CompaniesController : ControllerBase 15 | { 16 | private readonly ICompanyRepository _repository; 17 | private readonly ILoggerManager _logger; 18 | private readonly IMapper _mapper; 19 | 20 | public CompaniesController(ICompanyRepository repository, ILoggerManager logger, IMapper mapper) 21 | { 22 | _repository = repository; 23 | _logger = logger; 24 | _mapper = mapper; 25 | } 26 | 27 | [HttpGet] 28 | public IActionResult GetCompanies() 29 | { 30 | var companies = _repository.GetAllCompanies(); 31 | 32 | var companiesDto = _mapper.Map>(companies); 33 | 34 | _logger.LogInfo("All companies fetched from the database"); 35 | 36 | return Ok(companiesDto); 37 | } 38 | 39 | [HttpGet("{id}", Name = "CompanyById")] 40 | public IActionResult GetCompany(Guid id) 41 | { 42 | var company = _repository.GetCompany(id); 43 | if (company == null) 44 | { 45 | _logger.LogInfo($"Company with id: {id} doesn't exist in the database."); 46 | return NotFound(); 47 | } 48 | else 49 | { 50 | var companyDto = _mapper.Map(company); 51 | return Ok(companyDto); 52 | } 53 | } 54 | 55 | [HttpPost] 56 | public IActionResult CreateCompany([FromBody] CompanyForCreationDto company) 57 | { 58 | if (company == null) 59 | { 60 | _logger.LogError("CompanyForCreationDto object sent from client is null."); 61 | return BadRequest("CompanyForCreationDto object is null"); 62 | } 63 | 64 | var companyEntity = _mapper.Map(company); 65 | 66 | _repository.CreateCompany(companyEntity); 67 | 68 | var companyToReturn = _mapper.Map(companyEntity); 69 | 70 | return CreatedAtRoute("CompanyById", new { id = companyToReturn.Id }, companyToReturn); 71 | } 72 | } 73 | } -------------------------------------------------------------------------------- /start/CompanyEmployees/CompanyEmployees/Extensions/ServiceExtensions.cs: -------------------------------------------------------------------------------- 1 | using Contracts; 2 | using Entities; 3 | using LoggerService; 4 | using Microsoft.AspNetCore.Builder; 5 | using Microsoft.EntityFrameworkCore; 6 | using Microsoft.Extensions.Configuration; 7 | using Microsoft.Extensions.DependencyInjection; 8 | using Repository; 9 | 10 | namespace CompanyEmployees.Extensions 11 | { 12 | public static class ServiceExtensions 13 | { 14 | public static void ConfigureLoggerService(this IServiceCollection services) => 15 | services.AddScoped(); 16 | 17 | public static void ConfigureSqlContext(this IServiceCollection services, IConfiguration configuration) => 18 | services.AddDbContext(opts => 19 | opts.UseSqlServer(configuration.GetConnectionString("sqlConnection"), b => b.MigrationsAssembly("CompanyEmployees"))); 20 | 21 | public static void ConfigureRepository(this IServiceCollection services) => 22 | services.AddScoped(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /start/CompanyEmployees/CompanyEmployees/MappingProfile.cs: -------------------------------------------------------------------------------- 1 | using AutoMapper; 2 | using Entities.DataTransferObjects; 3 | using Entities.Models; 4 | 5 | namespace CompanyEmployees 6 | { 7 | public class MappingProfile : Profile 8 | { 9 | public MappingProfile() 10 | { 11 | CreateMap() 12 | .ForMember(c => c.FullAddress, 13 | opt => opt.MapFrom(x => string.Join(' ', x.Address, x.Country))); 14 | 15 | CreateMap(); 16 | } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /start/CompanyEmployees/CompanyEmployees/Migrations/20190927181200_DataBaseCreation.Designer.cs: -------------------------------------------------------------------------------- 1 | // 2 | using System; 3 | using Entities; 4 | using Microsoft.EntityFrameworkCore; 5 | using Microsoft.EntityFrameworkCore.Infrastructure; 6 | using Microsoft.EntityFrameworkCore.Metadata; 7 | using Microsoft.EntityFrameworkCore.Migrations; 8 | using Microsoft.EntityFrameworkCore.Storage.ValueConversion; 9 | 10 | namespace CompanyEmployees.Migrations 11 | { 12 | [DbContext(typeof(RepositoryContext))] 13 | [Migration("20190927181200_DataBaseCreation")] 14 | partial class DataBaseCreation 15 | { 16 | protected override void BuildTargetModel(ModelBuilder modelBuilder) 17 | { 18 | #pragma warning disable 612, 618 19 | modelBuilder 20 | .HasAnnotation("ProductVersion", "3.0.0") 21 | .HasAnnotation("Relational:MaxIdentifierLength", 128) 22 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 23 | 24 | modelBuilder.Entity("Entities.Models.Company", b => 25 | { 26 | b.Property("Id") 27 | .ValueGeneratedOnAdd() 28 | .HasColumnName("CompanyId") 29 | .HasColumnType("uniqueidentifier"); 30 | 31 | b.Property("Address") 32 | .IsRequired() 33 | .HasColumnType("nvarchar(60)") 34 | .HasMaxLength(60); 35 | 36 | b.Property("Country") 37 | .HasColumnType("nvarchar(max)"); 38 | 39 | b.Property("Name") 40 | .IsRequired() 41 | .HasColumnType("nvarchar(60)") 42 | .HasMaxLength(60); 43 | 44 | b.HasKey("Id"); 45 | 46 | b.ToTable("Companies"); 47 | }); 48 | 49 | modelBuilder.Entity("Entities.Models.Employee", b => 50 | { 51 | b.Property("Id") 52 | .ValueGeneratedOnAdd() 53 | .HasColumnName("EmployeeId") 54 | .HasColumnType("uniqueidentifier"); 55 | 56 | b.Property("Age") 57 | .HasColumnType("int"); 58 | 59 | b.Property("CompanyId") 60 | .HasColumnType("uniqueidentifier"); 61 | 62 | b.Property("Name") 63 | .IsRequired() 64 | .HasColumnType("nvarchar(30)") 65 | .HasMaxLength(30); 66 | 67 | b.Property("Position") 68 | .IsRequired() 69 | .HasColumnType("nvarchar(20)") 70 | .HasMaxLength(20); 71 | 72 | b.HasKey("Id"); 73 | 74 | b.HasIndex("CompanyId"); 75 | 76 | b.ToTable("Employees"); 77 | }); 78 | 79 | modelBuilder.Entity("Entities.Models.Employee", b => 80 | { 81 | b.HasOne("Entities.Models.Company", "Company") 82 | .WithMany("Employees") 83 | .HasForeignKey("CompanyId") 84 | .OnDelete(DeleteBehavior.Cascade) 85 | .IsRequired(); 86 | }); 87 | #pragma warning restore 612, 618 88 | } 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /start/CompanyEmployees/CompanyEmployees/Migrations/20190927181200_DataBaseCreation.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Microsoft.EntityFrameworkCore.Migrations; 3 | 4 | namespace CompanyEmployees.Migrations 5 | { 6 | public partial class DataBaseCreation : Migration 7 | { 8 | protected override void Up(MigrationBuilder migrationBuilder) 9 | { 10 | migrationBuilder.CreateTable( 11 | name: "Companies", 12 | columns: table => new 13 | { 14 | CompanyId = table.Column(nullable: false), 15 | Name = table.Column(maxLength: 60, nullable: false), 16 | Address = table.Column(maxLength: 60, nullable: false), 17 | Country = table.Column(nullable: true) 18 | }, 19 | constraints: table => 20 | { 21 | table.PrimaryKey("PK_Companies", x => x.CompanyId); 22 | }); 23 | } 24 | 25 | protected override void Down(MigrationBuilder migrationBuilder) 26 | { 27 | migrationBuilder.DropTable( 28 | name: "Companies"); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /start/CompanyEmployees/CompanyEmployees/Migrations/20190927182016_InitialData.Designer.cs: -------------------------------------------------------------------------------- 1 | // 2 | using System; 3 | using Entities; 4 | using Microsoft.EntityFrameworkCore; 5 | using Microsoft.EntityFrameworkCore.Infrastructure; 6 | using Microsoft.EntityFrameworkCore.Metadata; 7 | using Microsoft.EntityFrameworkCore.Migrations; 8 | using Microsoft.EntityFrameworkCore.Storage.ValueConversion; 9 | 10 | namespace CompanyEmployees.Migrations 11 | { 12 | [DbContext(typeof(RepositoryContext))] 13 | [Migration("20190927182016_InitialData")] 14 | partial class InitialData 15 | { 16 | protected override void BuildTargetModel(ModelBuilder modelBuilder) 17 | { 18 | #pragma warning disable 612, 618 19 | modelBuilder 20 | .HasAnnotation("ProductVersion", "3.0.0") 21 | .HasAnnotation("Relational:MaxIdentifierLength", 128) 22 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 23 | 24 | modelBuilder.Entity("Entities.Models.Company", b => 25 | { 26 | b.Property("Id") 27 | .ValueGeneratedOnAdd() 28 | .HasColumnName("CompanyId") 29 | .HasColumnType("uniqueidentifier"); 30 | 31 | b.Property("Address") 32 | .IsRequired() 33 | .HasColumnType("nvarchar(60)") 34 | .HasMaxLength(60); 35 | 36 | b.Property("Country") 37 | .HasColumnType("nvarchar(max)"); 38 | 39 | b.Property("Name") 40 | .IsRequired() 41 | .HasColumnType("nvarchar(60)") 42 | .HasMaxLength(60); 43 | 44 | b.HasKey("Id"); 45 | 46 | b.ToTable("Companies"); 47 | 48 | b.HasData( 49 | new 50 | { 51 | Id = new Guid("c9d4c053-49b6-410c-bc78-2d54a9991870"), 52 | Address = "583 Wall Dr. Gwynn Oak, MD 21207", 53 | Country = "USA", 54 | Name = "IT_Solutions Ltd" 55 | }, 56 | new 57 | { 58 | Id = new Guid("3d490a70-94ce-4d15-9494-5248280c2ce3"), 59 | Address = "312 Forest Avenue, BF 923", 60 | Country = "USA", 61 | Name = "Admin_Solutions Ltd" 62 | }); 63 | }); 64 | 65 | modelBuilder.Entity("Entities.Models.Employee", b => 66 | { 67 | b.Property("Id") 68 | .ValueGeneratedOnAdd() 69 | .HasColumnName("EmployeeId") 70 | .HasColumnType("uniqueidentifier"); 71 | 72 | b.Property("Age") 73 | .HasColumnType("int"); 74 | 75 | b.Property("CompanyId") 76 | .HasColumnType("uniqueidentifier"); 77 | 78 | b.Property("Name") 79 | .IsRequired() 80 | .HasColumnType("nvarchar(30)") 81 | .HasMaxLength(30); 82 | 83 | b.Property("Position") 84 | .IsRequired() 85 | .HasColumnType("nvarchar(20)") 86 | .HasMaxLength(20); 87 | 88 | b.HasKey("Id"); 89 | 90 | b.HasIndex("CompanyId"); 91 | 92 | b.ToTable("Employees"); 93 | 94 | b.HasData( 95 | new 96 | { 97 | Id = new Guid("80abbca8-664d-4b20-b5de-024705497d4a"), 98 | Age = 26, 99 | CompanyId = new Guid("c9d4c053-49b6-410c-bc78-2d54a9991870"), 100 | Name = "Sam Raiden", 101 | Position = "Software developer" 102 | }, 103 | new 104 | { 105 | Id = new Guid("86dba8c0-d178-41e7-938c-ed49778fb52a"), 106 | Age = 30, 107 | CompanyId = new Guid("c9d4c053-49b6-410c-bc78-2d54a9991870"), 108 | Name = "Jana McLeaf", 109 | Position = "Software developer" 110 | }, 111 | new 112 | { 113 | Id = new Guid("021ca3c1-0deb-4afd-ae94-2159a8479811"), 114 | Age = 35, 115 | CompanyId = new Guid("3d490a70-94ce-4d15-9494-5248280c2ce3"), 116 | Name = "Kane Miller", 117 | Position = "Administrator" 118 | }); 119 | }); 120 | 121 | modelBuilder.Entity("Entities.Models.Employee", b => 122 | { 123 | b.HasOne("Entities.Models.Company", "Company") 124 | .WithMany("Employees") 125 | .HasForeignKey("CompanyId") 126 | .OnDelete(DeleteBehavior.Cascade) 127 | .IsRequired(); 128 | }); 129 | #pragma warning restore 612, 618 130 | } 131 | } 132 | } 133 | -------------------------------------------------------------------------------- /start/CompanyEmployees/CompanyEmployees/Migrations/20190927182016_InitialData.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Microsoft.EntityFrameworkCore.Migrations; 3 | 4 | namespace CompanyEmployees.Migrations 5 | { 6 | public partial class InitialData : Migration 7 | { 8 | protected override void Up(MigrationBuilder migrationBuilder) 9 | { 10 | migrationBuilder.InsertData( 11 | table: "Companies", 12 | columns: new[] { "CompanyId", "Address", "Country", "Name" }, 13 | values: new object[] { new Guid("c9d4c053-49b6-410c-bc78-2d54a9991870"), "583 Wall Dr. Gwynn Oak, MD 21207", "USA", "IT_Solutions Ltd" }); 14 | 15 | migrationBuilder.InsertData( 16 | table: "Companies", 17 | columns: new[] { "CompanyId", "Address", "Country", "Name" }, 18 | values: new object[] { new Guid("3d490a70-94ce-4d15-9494-5248280c2ce3"), "312 Forest Avenue, BF 923", "USA", "Admin_Solutions Ltd" }); 19 | } 20 | 21 | protected override void Down(MigrationBuilder migrationBuilder) 22 | { 23 | migrationBuilder.DeleteData( 24 | table: "Companies", 25 | keyColumn: "CompanyId", 26 | keyValue: new Guid("3d490a70-94ce-4d15-9494-5248280c2ce3")); 27 | 28 | migrationBuilder.DeleteData( 29 | table: "Companies", 30 | keyColumn: "CompanyId", 31 | keyValue: new Guid("c9d4c053-49b6-410c-bc78-2d54a9991870")); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /start/CompanyEmployees/CompanyEmployees/Migrations/RepositoryContextModelSnapshot.cs: -------------------------------------------------------------------------------- 1 | // 2 | using System; 3 | using Entities; 4 | using Microsoft.EntityFrameworkCore; 5 | using Microsoft.EntityFrameworkCore.Infrastructure; 6 | using Microsoft.EntityFrameworkCore.Metadata; 7 | using Microsoft.EntityFrameworkCore.Storage.ValueConversion; 8 | 9 | namespace CompanyEmployees.Migrations 10 | { 11 | [DbContext(typeof(RepositoryContext))] 12 | partial class RepositoryContextModelSnapshot : ModelSnapshot 13 | { 14 | protected override void BuildModel(ModelBuilder modelBuilder) 15 | { 16 | #pragma warning disable 612, 618 17 | modelBuilder 18 | .HasAnnotation("ProductVersion", "3.0.0") 19 | .HasAnnotation("Relational:MaxIdentifierLength", 128) 20 | .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn); 21 | 22 | modelBuilder.Entity("Entities.Models.Company", b => 23 | { 24 | b.Property("Id") 25 | .ValueGeneratedOnAdd() 26 | .HasColumnName("CompanyId") 27 | .HasColumnType("uniqueidentifier"); 28 | 29 | b.Property("Address") 30 | .IsRequired() 31 | .HasColumnType("nvarchar(60)") 32 | .HasMaxLength(60); 33 | 34 | b.Property("Country") 35 | .HasColumnType("nvarchar(max)"); 36 | 37 | b.Property("Name") 38 | .IsRequired() 39 | .HasColumnType("nvarchar(60)") 40 | .HasMaxLength(60); 41 | 42 | b.HasKey("Id"); 43 | 44 | b.ToTable("Companies"); 45 | 46 | b.HasData( 47 | new 48 | { 49 | Id = new Guid("c9d4c053-49b6-410c-bc78-2d54a9991870"), 50 | Address = "583 Wall Dr. Gwynn Oak, MD 21207", 51 | Country = "USA", 52 | Name = "IT_Solutions Ltd" 53 | }, 54 | new 55 | { 56 | Id = new Guid("3d490a70-94ce-4d15-9494-5248280c2ce3"), 57 | Address = "312 Forest Avenue, BF 923", 58 | Country = "USA", 59 | Name = "Admin_Solutions Ltd" 60 | }); 61 | }); 62 | 63 | modelBuilder.Entity("Entities.Models.Employee", b => 64 | { 65 | b.Property("Id") 66 | .ValueGeneratedOnAdd() 67 | .HasColumnName("EmployeeId") 68 | .HasColumnType("uniqueidentifier"); 69 | 70 | b.Property("Age") 71 | .HasColumnType("int"); 72 | 73 | b.Property("CompanyId") 74 | .HasColumnType("uniqueidentifier"); 75 | 76 | b.Property("Name") 77 | .IsRequired() 78 | .HasColumnType("nvarchar(30)") 79 | .HasMaxLength(30); 80 | 81 | b.Property("Position") 82 | .IsRequired() 83 | .HasColumnType("nvarchar(20)") 84 | .HasMaxLength(20); 85 | 86 | b.HasKey("Id"); 87 | 88 | b.HasIndex("CompanyId"); 89 | 90 | b.ToTable("Employees"); 91 | 92 | b.HasData( 93 | new 94 | { 95 | Id = new Guid("80abbca8-664d-4b20-b5de-024705497d4a"), 96 | Age = 26, 97 | CompanyId = new Guid("c9d4c053-49b6-410c-bc78-2d54a9991870"), 98 | Name = "Sam Raiden", 99 | Position = "Software developer" 100 | }, 101 | new 102 | { 103 | Id = new Guid("86dba8c0-d178-41e7-938c-ed49778fb52a"), 104 | Age = 30, 105 | CompanyId = new Guid("c9d4c053-49b6-410c-bc78-2d54a9991870"), 106 | Name = "Jana McLeaf", 107 | Position = "Software developer" 108 | }, 109 | new 110 | { 111 | Id = new Guid("021ca3c1-0deb-4afd-ae94-2159a8479811"), 112 | Age = 35, 113 | CompanyId = new Guid("3d490a70-94ce-4d15-9494-5248280c2ce3"), 114 | Name = "Kane Miller", 115 | Position = "Administrator" 116 | }); 117 | }); 118 | 119 | modelBuilder.Entity("Entities.Models.Employee", b => 120 | { 121 | b.HasOne("Entities.Models.Company", "Company") 122 | .WithMany("Employees") 123 | .HasForeignKey("CompanyId") 124 | .OnDelete(DeleteBehavior.Cascade) 125 | .IsRequired(); 126 | }); 127 | #pragma warning restore 612, 618 128 | } 129 | } 130 | } 131 | -------------------------------------------------------------------------------- /start/CompanyEmployees/CompanyEmployees/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | using Microsoft.AspNetCore.Hosting; 6 | using Microsoft.Extensions.Configuration; 7 | using Microsoft.Extensions.Hosting; 8 | using Microsoft.Extensions.Logging; 9 | 10 | namespace CompanyEmployees 11 | { 12 | public class Program 13 | { 14 | public static void Main(string[] args) 15 | { 16 | CreateHostBuilder(args).Build().Run(); 17 | } 18 | 19 | public static IHostBuilder CreateHostBuilder(string[] args) => 20 | Host.CreateDefaultBuilder(args) 21 | .ConfigureWebHostDefaults(webBuilder => 22 | { 23 | webBuilder.UseStartup(); 24 | }); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /start/CompanyEmployees/CompanyEmployees/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "profiles": { 3 | "CompanyEmployees": { 4 | "commandName": "Project", 5 | "launchBrowser": false, 6 | "launchUrl": "weatherforecast", 7 | "applicationUrl": "https://localhost:5001;http://localhost:5000", 8 | "environmentVariables": { 9 | "ASPNETCORE_ENVIRONMENT": "Development" 10 | } 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /start/CompanyEmployees/CompanyEmployees/Startup.cs: -------------------------------------------------------------------------------- 1 | using CompanyEmployees.Extensions; 2 | using Contracts; 3 | using Microsoft.AspNetCore.Builder; 4 | using Microsoft.AspNetCore.Hosting; 5 | using Microsoft.Extensions.Configuration; 6 | using Microsoft.Extensions.DependencyInjection; 7 | using Microsoft.Extensions.Hosting; 8 | using NLog; 9 | using System.IO; 10 | 11 | namespace CompanyEmployees 12 | { 13 | public class Startup 14 | { 15 | public Startup(IConfiguration configuration) 16 | { 17 | LogManager.LoadConfiguration(string.Concat(Directory.GetCurrentDirectory(), "/nlog.config")); 18 | Configuration = configuration; 19 | } 20 | 21 | public IConfiguration Configuration { get; } 22 | 23 | // This method gets called by the runtime. Use this method to add services to the container. 24 | public void ConfigureServices(IServiceCollection services) 25 | { 26 | services.ConfigureLoggerService(); 27 | services.ConfigureSqlContext(Configuration); 28 | services.ConfigureRepository(); 29 | services.AddAutoMapper(typeof(Startup)); 30 | 31 | services.AddControllers(); 32 | } 33 | 34 | // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 35 | public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerManager logger) 36 | { 37 | if (env.IsDevelopment()) 38 | { 39 | app.UseDeveloperExceptionPage(); 40 | } 41 | else 42 | { 43 | app.UseHsts(); 44 | } 45 | 46 | app.UseHttpsRedirection(); 47 | app.UseStaticFiles(); 48 | 49 | app.UseRouting(); 50 | 51 | app.UseAuthorization(); 52 | 53 | app.UseEndpoints(endpoints => 54 | { 55 | endpoints.MapControllers(); 56 | }); 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /start/CompanyEmployees/CompanyEmployees/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Debug", 5 | "System": "Information", 6 | "Microsoft": "Information" 7 | } 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /start/CompanyEmployees/CompanyEmployees/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Information", 5 | "Microsoft": "Warning", 6 | "Microsoft.Hosting.Lifetime": "Information" 7 | } 8 | }, 9 | "ConnectionStrings": { 10 | "sqlConnection": "server=.; database=CompanyAsyncAwaitExample; Integrated Security=true" 11 | }, 12 | "AllowedHosts": "*" 13 | } 14 | -------------------------------------------------------------------------------- /start/CompanyEmployees/CompanyEmployees/nlog.config: -------------------------------------------------------------------------------- 1 |  2 | 7 | 8 | 9 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /start/CompanyEmployees/Contracts/Contracts.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net5.0 5 | 6 | 7 | 8 | 9 | all 10 | runtime; build; native; contentfiles; analyzers; buildtransitive 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /start/CompanyEmployees/Contracts/ICompanyRepository.cs: -------------------------------------------------------------------------------- 1 | using Entities.Models; 2 | using System; 3 | using System.Collections.Generic; 4 | 5 | namespace Contracts 6 | { 7 | public interface ICompanyRepository 8 | { 9 | IEnumerable GetAllCompanies(); 10 | Company GetCompany(Guid companyId); 11 | void CreateCompany(Company company); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /start/CompanyEmployees/Contracts/ILoggerManager.cs: -------------------------------------------------------------------------------- 1 | namespace Contracts 2 | { 3 | public interface ILoggerManager 4 | { 5 | void LogInfo(string message); 6 | void LogWarn(string message); 7 | void LogDebug(string message); 8 | void LogError(string message); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /start/CompanyEmployees/Entities/Configuration/CompanyConfiguration.cs: -------------------------------------------------------------------------------- 1 | using Entities.Models; 2 | using Microsoft.EntityFrameworkCore; 3 | using Microsoft.EntityFrameworkCore.Metadata.Builders; 4 | using System; 5 | 6 | namespace Entities.Configuration 7 | { 8 | public class CompanyConfiguration : IEntityTypeConfiguration 9 | { 10 | public void Configure(EntityTypeBuilder builder) 11 | { 12 | builder.HasData 13 | ( 14 | new Company 15 | { 16 | Id = new Guid("c9d4c053-49b6-410c-bc78-2d54a9991870"), 17 | Name = "IT_Solutions Ltd", 18 | Address = "583 Wall Dr. Gwynn Oak, MD 21207", 19 | Country = "USA" 20 | }, 21 | new Company 22 | { 23 | Id = new Guid("3d490a70-94ce-4d15-9494-5248280c2ce3"), 24 | Name = "Admin_Solutions Ltd", 25 | Address = "312 Forest Avenue, BF 923", 26 | Country = "USA" 27 | } 28 | ); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /start/CompanyEmployees/Entities/DataTransferObjects/CompanyDto.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Entities.DataTransferObjects 6 | { 7 | public class CompanyDto 8 | { 9 | public Guid Id { get; set; } 10 | public string Name { get; set; } 11 | public string FullAddress { get; set; } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /start/CompanyEmployees/Entities/DataTransferObjects/CompanyForCreationDto.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace Entities.DataTransferObjects 6 | { 7 | public class CompanyForCreationDto 8 | { 9 | public string Name { get; set; } 10 | public string Address { get; set; } 11 | public string Country { get; set; } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /start/CompanyEmployees/Entities/Entities.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net5.0 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /start/CompanyEmployees/Entities/Models/Company.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.ComponentModel.DataAnnotations; 4 | using System.ComponentModel.DataAnnotations.Schema; 5 | 6 | namespace Entities.Models 7 | { 8 | public class Company 9 | { 10 | [Column("CompanyId")] 11 | public Guid Id { get; set; } 12 | 13 | [Required(ErrorMessage = "Company name is a required field.")] 14 | [MaxLength(60, ErrorMessage = "Maximum length for the Name is 60 characters.")] 15 | public string Name { get; set; } 16 | 17 | [Required(ErrorMessage = "Company address is a required field.")] 18 | [MaxLength(60, ErrorMessage = "Maximum length for the Address is 60 characters.")] 19 | public string Address { get; set; } 20 | 21 | public string Country { get; set; } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /start/CompanyEmployees/Entities/RepositoryContext.cs: -------------------------------------------------------------------------------- 1 | using Entities.Configuration; 2 | using Entities.Models; 3 | using Microsoft.EntityFrameworkCore; 4 | 5 | namespace Entities 6 | { 7 | public class RepositoryContext : DbContext 8 | { 9 | public RepositoryContext(DbContextOptions options) 10 | : base(options) 11 | { 12 | } 13 | 14 | protected override void OnModelCreating(ModelBuilder modelBuilder) 15 | { 16 | modelBuilder.ApplyConfiguration(new CompanyConfiguration()); 17 | } 18 | 19 | public DbSet Companies { get; set; } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /start/CompanyEmployees/LoggerService/LoggerManager.cs: -------------------------------------------------------------------------------- 1 | using Contracts; 2 | using NLog; 3 | 4 | namespace LoggerService 5 | { 6 | public class LoggerManager : ILoggerManager 7 | { 8 | private static ILogger logger = LogManager.GetCurrentClassLogger(); 9 | 10 | public LoggerManager() 11 | { 12 | } 13 | 14 | public void LogDebug(string message) 15 | { 16 | logger.Debug(message); 17 | } 18 | 19 | public void LogError(string message) 20 | { 21 | logger.Error(message); 22 | } 23 | 24 | public void LogInfo(string message) 25 | { 26 | logger.Info(message); 27 | } 28 | 29 | public void LogWarn(string message) 30 | { 31 | logger.Warn(message); 32 | } 33 | 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /start/CompanyEmployees/LoggerService/LoggerService.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net5.0 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /start/CompanyEmployees/Repository/CompanyRepository.cs: -------------------------------------------------------------------------------- 1 | using Contracts; 2 | using Entities; 3 | using Entities.Models; 4 | using System; 5 | using System.Collections.Generic; 6 | using System.Linq; 7 | 8 | namespace Repository 9 | { 10 | public class CompanyRepository : ICompanyRepository 11 | { 12 | private readonly RepositoryContext _repoContext; 13 | 14 | public CompanyRepository(RepositoryContext repoContext) 15 | { 16 | _repoContext = repoContext; 17 | } 18 | 19 | public IEnumerable GetAllCompanies() => 20 | _repoContext.Companies 21 | .OrderBy(c => c.Name) 22 | .ToList(); 23 | 24 | public Company GetCompany(Guid companyId) => 25 | _repoContext.Companies 26 | .SingleOrDefault(c => c.Id.Equals(companyId)); 27 | 28 | public void CreateCompany(Company company) 29 | { 30 | _repoContext.Add(company); 31 | _repoContext.SaveChanges(); 32 | } 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /start/CompanyEmployees/Repository/Repository.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net5.0 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | --------------------------------------------------------------------------------