diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..1ff0c42 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,63 @@ +############################################################################### +# Set default behavior to automatically normalize line endings. +############################################################################### +* text=auto + +############################################################################### +# Set default behavior for command prompt diff. +# +# This is need for earlier builds of msysgit that does not have it on by +# default for csharp files. +# Note: This is only used by command line +############################################################################### +#*.cs diff=csharp + +############################################################################### +# Set the merge driver for project and solution files +# +# Merging from the command prompt will add diff markers to the files if there +# are conflicts (Merging from VS is not affected by the settings below, in VS +# the diff markers are never inserted). Diff markers may cause the following +# file extensions to fail to load in VS. An alternative would be to treat +# these files as binary and thus will always conflict and require user +# intervention with every merge. To do so, just uncomment the entries below +############################################################################### +#*.sln merge=binary +#*.csproj merge=binary +#*.vbproj merge=binary +#*.vcxproj merge=binary +#*.vcproj merge=binary +#*.dbproj merge=binary +#*.fsproj merge=binary +#*.lsproj merge=binary +#*.wixproj merge=binary +#*.modelproj merge=binary +#*.sqlproj merge=binary +#*.wwaproj merge=binary + +############################################################################### +# behavior for image files +# +# image files are treated as binary by default. +############################################################################### +#*.jpg binary +#*.png binary +#*.gif binary + +############################################################################### +# diff behavior for common document formats +# +# Convert binary document formats to text before diffing them. This feature +# is only available from the command line. Turn it on by uncommenting the +# entries below. +############################################################################### +#*.doc diff=astextplain +#*.DOC diff=astextplain +#*.docx diff=astextplain +#*.DOCX diff=astextplain +#*.dot diff=astextplain +#*.DOT diff=astextplain +#*.pdf diff=astextplain +#*.PDF diff=astextplain +#*.rtf diff=astextplain +#*.RTF diff=astextplain diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c4efe2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,261 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# User-specific files +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ + +# Visual Studio 2015 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUNIT +*.VisualState.xml +TestResult.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# DNX +project.lock.json +project.fragment.lock.json +artifacts/ + +*_i.c +*_p.c +*_i.h +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# JustCode is a .NET coding add-in +.JustCode + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# TODO: Comment the next line if you want to checkin your web deploy settings +# but database connection strings (with potential passwords) will be unencrypted +#*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# The packages folder can be ignored because of Package Restore +**/packages/* +# except build/, which is used as an MSBuild target. +!**/packages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/packages/repositories.config +# NuGet v3's project.json files produces more ignoreable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +node_modules/ +orleans.codegen.cs + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm + +# SQL Server files +*.mdf +*.ldf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# JetBrains Rider +.idea/ +*.sln.iml + +# CodeRush +.cr/ + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc \ No newline at end of file diff --git a/FullStackTesting/FullStackTesting.Web.Api/ClientApp b/FullStackTesting/FullStackTesting.Web.Api/ClientApp new file mode 160000 index 0000000..7c959a7 --- /dev/null +++ b/FullStackTesting/FullStackTesting.Web.Api/ClientApp @@ -0,0 +1 @@ +Subproject commit 7c959a77d43c90c5bf11d50807b30be13ea8d273 diff --git a/FullStackTesting/FullStackTesting.Web.Api/Controllers/EmployeeController.cs b/FullStackTesting/FullStackTesting.Web.Api/Controllers/EmployeeController.cs new file mode 100644 index 0000000..0ce8447 --- /dev/null +++ b/FullStackTesting/FullStackTesting.Web.Api/Controllers/EmployeeController.cs @@ -0,0 +1,73 @@ +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Http; +using System.Collections.Generic; +using FullStackTesting.Web.Api.Models; +using FullStackTesting.Web.Api.Extensions; +using FullStackTesting.Web.Api.Persistence; + +namespace FullStackTesting.Web.Api.Controllers +{ + [Route("api/[controller]/[action]")] + [ApiController] + public class EmployeeController : ControllerBase + { + private readonly IEmployeeRepository _employeeRepo; + + public EmployeeController(IEmployeeRepository employeeRepo) + { + _employeeRepo = employeeRepo; + } + + // GET api/Employee/GetAllEmployeesAsync + [HttpGet] + [ProducesResponseType(typeof(IEnumerable), StatusCodes.Status200OK)] + public async Task GetAllEmployeesAsync() + { + var employees = await _employeeRepo.GetAllAsync(); + return Ok(employees); + } + + // GET api/Employee/GetEmployeeByIdAsync?id=3 + [HttpGet] + [ProducesResponseType(typeof(Employee), StatusCodes.Status200OK)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + public async Task GetEmployeeByIdAsync(int id) + { + var employee = await _employeeRepo.GetByIdAsync(id); + if (employee == null) + return NotFound(); + + return Ok(employee); + } + + // POST api/Employee/AddEmployeeAsync + [HttpPost] + [ProducesResponseType(typeof(Employee), StatusCodes.Status201Created)] + [ProducesResponseType(StatusCodes.Status400BadRequest)] + public async Task AddEmployeeAsync(Employee employee) + { + if (!ModelState.IsValid) + return BadRequest(ModelState.GetErrorMessages()); + + var newEmployee = await _employeeRepo.AddAsync(employee); + + return CreatedAtAction(nameof(GetEmployeeByIdAsync), new { id = newEmployee.Id }, newEmployee); + } + + // DELETE api/Employee/DeleteEmployeeAsync?id=3 + [HttpDelete] + [ProducesResponseType(StatusCodes.Status204NoContent)] + [ProducesResponseType(StatusCodes.Status404NotFound)] + public async Task DeleteEmployeeAsync(int id) + { + var employee = await _employeeRepo.GetByIdAsync(id); + if (employee == null) + return NotFound(); + + await _employeeRepo.DeleteAsync(employee); + + return NoContent(); + } + } +} \ No newline at end of file diff --git a/FullStackTesting/FullStackTesting.Web.Api/Extensions/CollectionExtensions.cs b/FullStackTesting/FullStackTesting.Web.Api/Extensions/CollectionExtensions.cs new file mode 100644 index 0000000..e2bff60 --- /dev/null +++ b/FullStackTesting/FullStackTesting.Web.Api/Extensions/CollectionExtensions.cs @@ -0,0 +1,14 @@ +using System.Linq; +using System.Collections.Generic; + +namespace FullStackTesting.Web.Api.Extensions +{ + public static class CollectionExtensions + { + public static bool IsNullOrEmpty(this IEnumerable source) + => !source?.Any() ?? true; + + public static List ToListNullSafe(this IEnumerable source) + => source?.ToList() ?? new List(); + } +} diff --git a/FullStackTesting/FullStackTesting.Web.Api/Extensions/ModelStateExtensions.cs b/FullStackTesting/FullStackTesting.Web.Api/Extensions/ModelStateExtensions.cs new file mode 100644 index 0000000..c303bd4 --- /dev/null +++ b/FullStackTesting/FullStackTesting.Web.Api/Extensions/ModelStateExtensions.cs @@ -0,0 +1,14 @@ +using System.Linq; +using System.Collections.Generic; +using Microsoft.AspNetCore.Mvc.ModelBinding; + +namespace FullStackTesting.Web.Api.Extensions +{ + public static class ModelStateExtensions + { + public static List GetErrorMessages(this ModelStateDictionary dictionary) + => dictionary.SelectMany(m => m.Value.Errors) + .Select(m => m.ErrorMessage) + .ToListNullSafe(); + } +} diff --git a/FullStackTesting/FullStackTesting.Web.Api/Extensions/ServiceCollectionExtensions.cs b/FullStackTesting/FullStackTesting.Web.Api/Extensions/ServiceCollectionExtensions.cs new file mode 100644 index 0000000..de50349 --- /dev/null +++ b/FullStackTesting/FullStackTesting.Web.Api/Extensions/ServiceCollectionExtensions.cs @@ -0,0 +1,34 @@ +using Newtonsoft.Json; +using Microsoft.AspNetCore.Mvc; +using Newtonsoft.Json.Serialization; +using Microsoft.Extensions.DependencyInjection; + +namespace FullStackTesting.Web.Api.Extensions +{ + public static class ServiceCollectionExtensions + { + public static IServiceCollection AddCorsConfig(this IServiceCollection services, string policyName) + { + services.AddCors(options => + options.AddPolicy(policyName, + corsBuilder => corsBuilder + .AllowAnyOrigin() + .AllowAnyHeader() + .AllowAnyMethod())); + + return services; + } + + public static IServiceCollection AddMvcConfig(this IServiceCollection services, CompatibilityVersion aspNetCoreVersion) + { + services.AddMvc() + .SetCompatibilityVersion(aspNetCoreVersion) + .AddJsonOptions(options => { + options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore; + options.SerializerSettings.ContractResolver = new DefaultContractResolver(); + }); + + return services; + } + } +} diff --git a/FullStackTesting/FullStackTesting.Web.Api/FullStackTesting.Web.Api.csproj b/FullStackTesting/FullStackTesting.Web.Api/FullStackTesting.Web.Api.csproj new file mode 100644 index 0000000..7b22954 --- /dev/null +++ b/FullStackTesting/FullStackTesting.Web.Api/FullStackTesting.Web.Api.csproj @@ -0,0 +1,51 @@ + + + + netcoreapp2.2 + true + Latest + InProcess + false + ClientApp\ + $(DefaultItemExcludes);$(SpaRoot)node_modules\** + FullStackTesting.Web.Api + FullStackTesting.Web.Api + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + %(DistFiles.Identity) + PreserveNewest + + + + + diff --git a/FullStackTesting/FullStackTesting.Web.Api/Models/BaseEntity.cs b/FullStackTesting/FullStackTesting.Web.Api/Models/BaseEntity.cs new file mode 100644 index 0000000..e18d697 --- /dev/null +++ b/FullStackTesting/FullStackTesting.Web.Api/Models/BaseEntity.cs @@ -0,0 +1,16 @@ +using System; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace FullStackTesting.Web.Api.Models +{ + public abstract class BaseEntity + { + [DatabaseGenerated(DatabaseGeneratedOption.Identity)] + [Key] + public int Id { get; set; } + + public DateTime Created { get; set; } + public DateTime Modified { get; set; } + } +} \ No newline at end of file diff --git a/FullStackTesting/FullStackTesting.Web.Api/Models/Employee.cs b/FullStackTesting/FullStackTesting.Web.Api/Models/Employee.cs new file mode 100644 index 0000000..3cf6f9b --- /dev/null +++ b/FullStackTesting/FullStackTesting.Web.Api/Models/Employee.cs @@ -0,0 +1,16 @@ +using System.ComponentModel.DataAnnotations; + +namespace FullStackTesting.Web.Api.Models +{ + public class Employee : BaseEntity, IEmployee + { + [Required] + public string FirstName { get; set; } + + [Required] + public string LastName { get; set; } + + public string Department { get; set; } + public bool FullTime { get; set; } + } +} diff --git a/FullStackTesting/FullStackTesting.Web.Api/Models/IEmployee.cs b/FullStackTesting/FullStackTesting.Web.Api/Models/IEmployee.cs new file mode 100644 index 0000000..f4ee5a2 --- /dev/null +++ b/FullStackTesting/FullStackTesting.Web.Api/Models/IEmployee.cs @@ -0,0 +1,10 @@ +namespace FullStackTesting.Web.Api.Models +{ + public interface IEmployee + { + string FirstName { get; set; } + string LastName { get; set; } + string Department { get; set; } + bool FullTime { get; set; } + } +} diff --git a/FullStackTesting/FullStackTesting.Web.Api/Models/ISpecification.cs b/FullStackTesting/FullStackTesting.Web.Api/Models/ISpecification.cs new file mode 100644 index 0000000..cebb568 --- /dev/null +++ b/FullStackTesting/FullStackTesting.Web.Api/Models/ISpecification.cs @@ -0,0 +1,13 @@ +using System; +using System.Linq.Expressions; +using System.Collections.Generic; + +namespace FullStackTesting.Web.Api.Models +{ + public interface ISpecification + { + List IncludeStrings { get; } + Expression> Criteria { get; } + List>> Includes { get; } + } +} diff --git a/FullStackTesting/FullStackTesting.Web.Api/Models/Specification.cs b/FullStackTesting/FullStackTesting.Web.Api/Models/Specification.cs new file mode 100644 index 0000000..7f584d7 --- /dev/null +++ b/FullStackTesting/FullStackTesting.Web.Api/Models/Specification.cs @@ -0,0 +1,13 @@ +using System; +using System.Linq.Expressions; +using System.Collections.Generic; + +namespace FullStackTesting.Web.Api.Models +{ + public class Specification : ISpecification + { + public List IncludeStrings { get; } + public Expression> Criteria { get; } + public List>> Includes { get; } + } +} diff --git a/FullStackTesting/FullStackTesting.Web.Api/Pages/Error.cshtml b/FullStackTesting/FullStackTesting.Web.Api/Pages/Error.cshtml new file mode 100644 index 0000000..6f92b95 --- /dev/null +++ b/FullStackTesting/FullStackTesting.Web.Api/Pages/Error.cshtml @@ -0,0 +1,26 @@ +@page +@model ErrorModel +@{ + ViewData["Title"] = "Error"; +} + +

Error.

+

An error occurred while processing your request.

+ +@if (Model.ShowRequestId) +{ +

+ Request ID: @Model.RequestId +

+} + +

Development Mode

+

+ Swapping to the Development environment displays detailed information about the error that occurred. +

+

+ The Development environment shouldn't be enabled for deployed applications. + It can result in displaying sensitive information from exceptions to end users. + For local debugging, enable the Development environment by setting the ASPNETCORE_ENVIRONMENT environment variable to Development + and restarting the app. +

diff --git a/FullStackTesting/FullStackTesting.Web.Api/Pages/Error.cshtml.cs b/FullStackTesting/FullStackTesting.Web.Api/Pages/Error.cshtml.cs new file mode 100644 index 0000000..efffd19 --- /dev/null +++ b/FullStackTesting/FullStackTesting.Web.Api/Pages/Error.cshtml.cs @@ -0,0 +1,23 @@ +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.RazorPages; + +namespace FullStackTesting.Web.Api.Pages +{ + [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)] + public class ErrorModel : PageModel + { + public string RequestId { get; set; } + + public bool ShowRequestId => !string.IsNullOrEmpty(RequestId); + + public void OnGet() + { + RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier; + } + } +} diff --git a/FullStackTesting/FullStackTesting.Web.Api/Pages/_ViewImports.cshtml b/FullStackTesting/FullStackTesting.Web.Api/Pages/_ViewImports.cshtml new file mode 100644 index 0000000..ca6886a --- /dev/null +++ b/FullStackTesting/FullStackTesting.Web.Api/Pages/_ViewImports.cshtml @@ -0,0 +1,3 @@ +@using FullStackTesting.Web.Api +@namespace FullStackTesting.Web.Api.Pages +@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers diff --git a/FullStackTesting/FullStackTesting.Web.Api/Persistence/Contexts/AppDbContext.cs b/FullStackTesting/FullStackTesting.Web.Api/Persistence/Contexts/AppDbContext.cs new file mode 100644 index 0000000..91cc22a --- /dev/null +++ b/FullStackTesting/FullStackTesting.Web.Api/Persistence/Contexts/AppDbContext.cs @@ -0,0 +1,38 @@ +using System; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; +using FullStackTesting.Web.Api.Models; + +namespace FullStackTesting.Web.Api.Persistence +{ + public class AppDbContext : DbContext + { + public AppDbContext(DbContextOptions options) : base(options) { } + + public DbSet Employees { get; set; } + + public override int SaveChanges() + { + AddDbGeneratedInfo(); + return base.SaveChanges(); + } + + public async Task SaveChangesAsync() + { + AddDbGeneratedInfo(); + return await base.SaveChangesAsync(); + } + + private void AddDbGeneratedInfo() + { + foreach (var entry in ChangeTracker.Entries().Where(x => x.Entity is BaseEntity && (x.State.Equals(EntityState.Added) || x.State.Equals(EntityState.Modified)))) + { + if (entry.State.Equals(EntityState.Added)) + ((BaseEntity)entry.Entity).Created = DateTime.UtcNow; + + ((BaseEntity)entry.Entity).Modified = DateTime.UtcNow; + } + } + } +} diff --git a/FullStackTesting/FullStackTesting.Web.Api/Persistence/Contexts/SeedData.cs b/FullStackTesting/FullStackTesting.Web.Api/Persistence/Contexts/SeedData.cs new file mode 100644 index 0000000..86fdb39 --- /dev/null +++ b/FullStackTesting/FullStackTesting.Web.Api/Persistence/Contexts/SeedData.cs @@ -0,0 +1,17 @@ +using FullStackTesting.Web.Api.Models; + +namespace FullStackTesting.Web.Api.Persistence +{ + public static class SeedData + { + public static void LoadTestData(AppDbContext dbContext) + { + dbContext.Employees.Add(new Employee { Id = 1, FirstName = "Matt", LastName = "Areddia", Department = "Information Technology", FullTime = true }); + dbContext.Employees.Add(new Employee { Id = 2, FirstName = "Jane", LastName = "Doe", Department = "Accounting", FullTime = true }); + dbContext.Employees.Add(new Employee { Id = 3, FirstName = "Bob", LastName = "Smith", Department = "Human Resources", FullTime = true }); + dbContext.Employees.Add(new Employee { Id = 4, FirstName = "Debbie", LastName = "Test", Department = "Information Technology", FullTime = true }); + dbContext.Employees.Add(new Employee { Id = 5, FirstName = "Jeremy", LastName = "Wu", Department = "Claims", FullTime = false }); + dbContext.SaveChanges(); + } + } +} \ No newline at end of file diff --git a/FullStackTesting/FullStackTesting.Web.Api/Persistence/Repositories/EFRepository.cs b/FullStackTesting/FullStackTesting.Web.Api/Persistence/Repositories/EFRepository.cs new file mode 100644 index 0000000..f30253c --- /dev/null +++ b/FullStackTesting/FullStackTesting.Web.Api/Persistence/Repositories/EFRepository.cs @@ -0,0 +1,67 @@ +using System.Linq; +using System.Threading.Tasks; +using System.Collections.Generic; +using Microsoft.EntityFrameworkCore; +using FullStackTesting.Web.Api.Models; + +namespace FullStackTesting.Web.Api.Persistence +{ + public abstract class EFRepository : IEFepository where T : BaseEntity + { + protected readonly AppDbContext _appDbContext; + + protected EFRepository(AppDbContext appDbContext) + { + _appDbContext = appDbContext; + } + + public async Task> GetAllAsync() + { + return await _appDbContext.Set().ToListAsync(); + } + + public virtual async Task GetByIdAsync(int id) + { + return await _appDbContext.Set().FindAsync(id); + } + + public async Task GetSingleBySpecAsync(ISpecification spec) + { + var result = await ListAsync(spec); + return result.FirstOrDefault(); + } + + public async Task> ListAsync(ISpecification spec) + { + // fetch a Queryable that includes all expression-based includes + var queryableResultWithIncludes = spec.Includes + .Aggregate(_appDbContext.Set().AsQueryable(), (current, include) => current.Include(include)); + + // modify the IQueryable to include any string-based include statements + var secondaryResult = spec.IncludeStrings + .Aggregate(queryableResultWithIncludes, (current, include) => current.Include(include)); + + // return the result of the query using the specification's criteria expression + return await secondaryResult.Where(spec.Criteria).ToListAsync(); + } + + public async Task AddAsync(T entity) + { + _appDbContext.Set().Add(entity); + await _appDbContext.SaveChangesAsync(); + return entity; + } + + public async Task DeleteAsync(T entity) + { + _appDbContext.Set().Remove(entity); + await _appDbContext.SaveChangesAsync(); + } + + public async Task UpdateAsync(T entity) + { + _appDbContext.Entry(entity).State = EntityState.Modified; + await _appDbContext.SaveChangesAsync(); + } + } +} \ No newline at end of file diff --git a/FullStackTesting/FullStackTesting.Web.Api/Persistence/Repositories/EmployeeRepository.cs b/FullStackTesting/FullStackTesting.Web.Api/Persistence/Repositories/EmployeeRepository.cs new file mode 100644 index 0000000..3060aea --- /dev/null +++ b/FullStackTesting/FullStackTesting.Web.Api/Persistence/Repositories/EmployeeRepository.cs @@ -0,0 +1,9 @@ +using FullStackTesting.Web.Api.Models; + +namespace FullStackTesting.Web.Api.Persistence +{ + public sealed class EmployeeRepository : EFRepository, IEmployeeRepository + { + public EmployeeRepository(AppDbContext appDbContext) : base(appDbContext){ } + } +} \ No newline at end of file diff --git a/FullStackTesting/FullStackTesting.Web.Api/Persistence/Repositories/IEFepository.cs b/FullStackTesting/FullStackTesting.Web.Api/Persistence/Repositories/IEFepository.cs new file mode 100644 index 0000000..a442736 --- /dev/null +++ b/FullStackTesting/FullStackTesting.Web.Api/Persistence/Repositories/IEFepository.cs @@ -0,0 +1,17 @@ +using System.Threading.Tasks; +using System.Collections.Generic; +using FullStackTesting.Web.Api.Models; + +namespace FullStackTesting.Web.Api.Persistence +{ + public interface IEFepository where T : BaseEntity + { + Task AddAsync(T entity); + Task UpdateAsync(T entity); + Task DeleteAsync(T entity); + Task> GetAllAsync(); + Task GetByIdAsync(int id); + Task> ListAsync(ISpecification spec); + Task GetSingleBySpecAsync(ISpecification spec); + } +} \ No newline at end of file diff --git a/FullStackTesting/FullStackTesting.Web.Api/Persistence/Repositories/IEmployeeRepository.cs b/FullStackTesting/FullStackTesting.Web.Api/Persistence/Repositories/IEmployeeRepository.cs new file mode 100644 index 0000000..18cba77 --- /dev/null +++ b/FullStackTesting/FullStackTesting.Web.Api/Persistence/Repositories/IEmployeeRepository.cs @@ -0,0 +1,6 @@ +using FullStackTesting.Web.Api.Models; + +namespace FullStackTesting.Web.Api.Persistence +{ + public interface IEmployeeRepository : IEFepository { } +} diff --git a/FullStackTesting/FullStackTesting.Web.Api/Program.cs b/FullStackTesting/FullStackTesting.Web.Api/Program.cs new file mode 100644 index 0000000..1ccdf94 --- /dev/null +++ b/FullStackTesting/FullStackTesting.Web.Api/Program.cs @@ -0,0 +1,44 @@ +using System; +using Microsoft.AspNetCore; +using Microsoft.Extensions.Logging; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using FullStackTesting.Web.Api.Persistence; +using Microsoft.Extensions.DependencyInjection; + +namespace FullStackTesting.Web.Api +{ + public class Program + { + public static void Main(string[] args) + { + var host = CreateWebHostBuilder(args).Build(); + + using (var scope = host.Services.CreateScope()) + { + var scopedServices = scope.ServiceProvider; + var appDb = scopedServices.GetRequiredService(); + var logger = scopedServices.GetRequiredService>(); + + // Ensure the database is created. + appDb.Database.EnsureCreated(); + + try + { + // Add testing data for memoryDB + SeedData.LoadTestData(appDb); + } + catch (Exception ex) + { + logger.LogError(ex, $"An error occurred seeding the database with test data. Error: {ex?.Message}"); + } + } + + host.Run(); + } + + public static IWebHostBuilder CreateWebHostBuilder(string[] args) + => WebHost.CreateDefaultBuilder(args) + .UseStartup(); + } +} diff --git a/FullStackTesting/FullStackTesting.Web.Api/Properties/launchSettings.json b/FullStackTesting/FullStackTesting.Web.Api/Properties/launchSettings.json new file mode 100644 index 0000000..d50f5f0 --- /dev/null +++ b/FullStackTesting/FullStackTesting.Web.Api/Properties/launchSettings.json @@ -0,0 +1,30 @@ +{ + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:55276", + "sslPort": 0 + //"sslPort": 44346 + } + }, + "profiles": { + "IIS Express": { + "commandName": "IISExpress", + //"launchBrowser": true, + "launchBrowser": false, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "FullStackTesting.Web.Api": { + "commandName": "Project", + //"launchBrowser": true, + "launchBrowser": false, + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + }, + "applicationUrl": "https://localhost:5001;http://localhost:5000" + } + } +} \ No newline at end of file diff --git a/FullStackTesting/FullStackTesting.Web.Api/Startup.cs b/FullStackTesting/FullStackTesting.Web.Api/Startup.cs new file mode 100644 index 0000000..e838a8b --- /dev/null +++ b/FullStackTesting/FullStackTesting.Web.Api/Startup.cs @@ -0,0 +1,84 @@ +using System; +using VueCliMiddleware; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Configuration; +using FullStackTesting.Web.Api.Extensions; +using FullStackTesting.Web.Api.Persistence; +using Microsoft.Extensions.DependencyInjection; + +namespace FullStackTesting.Web.Api +{ + public class Startup + { + private readonly string _spaSourcePath; + private readonly string _corsPolicyName; + + public Startup(IConfiguration configuration) + { + Configuration = configuration; + _spaSourcePath = Configuration.GetValue("SPA:SourcePath"); + _corsPolicyName = Configuration.GetValue("CORS:PolicyName"); + } + + public IConfiguration Configuration { get; } + + public void ConfigureServices(IServiceCollection services) + { + // Register the in-memory db (Data is seeded in Main method of the Program.cs now) + services.AddDbContext(context => context.UseInMemoryDatabase("EmployeeMemoryDB")); + + // Registered a scoped EmployeeRepository service (DI into EmployeeController) + services.AddScoped(); + + // Register CORS and Mvc + services.AddCorsConfig(_corsPolicyName) + .AddMvcConfig(CompatibilityVersion.Version_2_2); + + // In production, the Vue files will be served from this directory + services.AddSpaStaticFiles(configuration => configuration.RootPath = $"{_spaSourcePath}/dist"); + } + + public void Configure(IApplicationBuilder app, IHostingEnvironment env) + { + if (env.IsDevelopment()) + { + app.UseDeveloperExceptionPage(); + } + else + { + app.UseExceptionHandler("/Error"); + app.UseHttpsRedirection(); + app.UseHsts(); + } + + app.UseCors(_corsPolicyName); + app.UseStaticFiles(); + app.UseSpaStaticFiles(); + + app.UseMvc(routes => + { + routes.MapRoute( + name: "default", + template: "{controller}/{action=Index}/{id?}"); + }); + + app.UseSpa(spa => + { + spa.Options.SourcePath = _spaSourcePath; + + if (env.IsDevelopment()) + { + // Option 1: Run npm process with client app (VueCli - pretty buggy, likely should stick with second option and launch client independently) + // spa.Options.StartupTimeout = new TimeSpan(days: 0, hours: 0, minutes: 1, seconds: 30); + // spa.UseVueCli(npmScript: "serve", port: 8080); + + // Option 2: Serve ClientApp independently and proxy requests from ClientApp (baseUri using Vue app port): + spa.UseProxyToSpaDevelopmentServer("http://localhost:8080"); + } + }); + } + } +} diff --git a/FullStackTesting/FullStackTesting.Web.Api/appsettings.Development.json b/FullStackTesting/FullStackTesting.Web.Api/appsettings.Development.json new file mode 100644 index 0000000..e203e94 --- /dev/null +++ b/FullStackTesting/FullStackTesting.Web.Api/appsettings.Development.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Debug", + "System": "Information", + "Microsoft": "Information" + } + } +} diff --git a/FullStackTesting/FullStackTesting.Web.Api/appsettings.json b/FullStackTesting/FullStackTesting.Web.Api/appsettings.json new file mode 100644 index 0000000..109870d --- /dev/null +++ b/FullStackTesting/FullStackTesting.Web.Api/appsettings.json @@ -0,0 +1,16 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Debug", + "System": "Information", + "Microsoft": "Information" + } + }, + "CORS": { + "PolicyName": "AllowAll" + }, + "SPA": { + "SourcePath": "ClientApp" + }, + "AllowedHosts": "*" +} diff --git a/FullStackTesting/tests/FullStackTesting.Web.Api.IntegrationTests/Controllers/EmployeeControllerTests.cs b/FullStackTesting/tests/FullStackTesting.Web.Api.IntegrationTests/Controllers/EmployeeControllerTests.cs new file mode 100644 index 0000000..cbc951e --- /dev/null +++ b/FullStackTesting/tests/FullStackTesting.Web.Api.IntegrationTests/Controllers/EmployeeControllerTests.cs @@ -0,0 +1,109 @@ +using Xunit; +using System.Text; +using System.Net.Http; +using Newtonsoft.Json; +using System.Threading.Tasks; +using System.Collections.Generic; +using FullStackTesting.Web.Api.Models; + +namespace FullStackTesting.Web.Api.IntegrationTests.Controllers +{ + public class EmployeeControllerTests : IClassFixture> + { + private readonly HttpClient _client; + + public EmployeeControllerTests(CustomWebApplicationFactory factory) + { + _client = factory.CreateClient(); + } + + [Fact] + public async Task CanGetAllEmployeesAsync() + { + // The endpoint or route of the controller action + var httpResponse = await _client.GetAsync("/api/Employee/GetAllEmployeesAsync"); + + // Must be successful + httpResponse.EnsureSuccessStatusCode(); + + // Deserialize and examine results + var stringResponse = await httpResponse.Content.ReadAsStringAsync(); + var employees = JsonConvert.DeserializeObject>(stringResponse); + + Assert.Contains(employees, e => e.FirstName.Equals("Matt") && e.LastName.Equals("Areddia")); + Assert.Contains(employees, e => e.FirstName.Equals("Jeremy") && e.LastName.Equals("Wu")); + } + + [Fact] + public async Task CanGetEmployeeByIdAsync() + { + // The endpoint or route of the controller action + const int targetId = 5; + var httpResponse = await _client.GetAsync($"/api/Employee/GetEmployeeByIdAsync?id={targetId}"); + + // Must be successful + httpResponse.EnsureSuccessStatusCode(); + + // Deserialize and examine results + var stringResponse = await httpResponse.Content.ReadAsStringAsync(); + var employee = JsonConvert.DeserializeObject(stringResponse); + + Assert.Equal(targetId, employee.Id); + Assert.Equal("Jeremy", employee.FirstName); + Assert.Equal("Wu", employee.LastName); + } + + [Fact] + public async Task CanDeleteEmployeeAsync() + { + // The endpoint or route of the controller action (DeleteEmployeeAsync) + const int targetId = 2; + var httpDeleteResponse = await _client.DeleteAsync($"/api/Employee/DeleteEmployeeAsync?id={targetId}"); + + // Must be successful + httpDeleteResponse.EnsureSuccessStatusCode(); + + // The endpoint or route of the controller action (GetAllEmployeesAsync) + var httpResponse = await _client.GetAsync("/api/Employee/GetAllEmployeesAsync"); + + // Must be successful + httpResponse.EnsureSuccessStatusCode(); + + // Deserialize and examine results (should no longer contain record having Id = 1, FirstName = 'Matt', LastName = 'Areddia') + var stringResponse = await httpResponse.Content.ReadAsStringAsync(); + var employees = JsonConvert.DeserializeObject>(stringResponse); + + Assert.DoesNotContain(employees, e => e.Id.Equals(targetId)); + Assert.DoesNotContain(employees, e => e.FirstName.Equals("Jane") && e.LastName.Equals("Doe")); + } + + [Fact] + public async Task CanAddEmployeeAsync() + { + // New Employee record to be posted in content + var addEmployee = new Employee { + Id = 7, + FirstName = "TestFirstName", + LastName = "TestLastName", + Department = "Claims", + FullTime = false + }; + + // The endpoint or route of the controller action (AddEmployeeAsync) with StringContent comprised of the employee to add + var addEmployeeStringContent = new StringContent(JsonConvert.SerializeObject(addEmployee), Encoding.UTF8, "application/json"); + var httpResponse = await _client.PostAsync("/api/Employee/AddEmployeeAsync", addEmployeeStringContent); + + // Must be successful + httpResponse.EnsureSuccessStatusCode(); + + // Deserialize and examine results (compare employee object returned in response to that passed in initial request - should match) + var stringResponse = await httpResponse.Content.ReadAsStringAsync(); + var employee = JsonConvert.DeserializeObject(stringResponse); + + Assert.Equal(addEmployee.Id, employee.Id); + Assert.Equal(addEmployee.FirstName, employee.FirstName); + Assert.Equal(addEmployee.LastName, employee.LastName); + } + } +} + diff --git a/FullStackTesting/tests/FullStackTesting.Web.Api.IntegrationTests/CustomWebApplicationFactory.cs b/FullStackTesting/tests/FullStackTesting.Web.Api.IntegrationTests/CustomWebApplicationFactory.cs new file mode 100644 index 0000000..b806842 --- /dev/null +++ b/FullStackTesting/tests/FullStackTesting.Web.Api.IntegrationTests/CustomWebApplicationFactory.cs @@ -0,0 +1,52 @@ +using System; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.Logging; +using Microsoft.EntityFrameworkCore; +using Microsoft.AspNetCore.Mvc.Testing; +using FullStackTesting.Web.Api.Persistence; +using Microsoft.Extensions.DependencyInjection; + +namespace FullStackTesting.Web.Api.IntegrationTests +{ + public class CustomWebApplicationFactory : WebApplicationFactory where TStartup : class + { + protected override void ConfigureWebHost(IWebHostBuilder builder) + { + builder.ConfigureServices(services => + { + // Create a new service provider. + var serviceProvider = new ServiceCollection() + .AddEntityFrameworkInMemoryDatabase() + .BuildServiceProvider(); + + // Add a database context (AppDbContext) using an in-memory database for testing. + services.AddDbContext(options => + { + options.UseInMemoryDatabase("EmployeeMemoryDB"); + options.UseInternalServiceProvider(serviceProvider); + }); + + // Create a scope (with the built service provider) to obtain a reference to the database contexts + using (var scope = services.BuildServiceProvider().CreateScope()) + { + var scopedServices = scope.ServiceProvider; + var appDb = scopedServices.GetRequiredService(); + var logger = scopedServices.GetRequiredService>>(); + + // Ensure the database is created. + appDb.Database.EnsureCreated(); + + try + { + // Add testing data for memoryDB + SeedData.LoadTestData(appDb); + } + catch (Exception ex) + { + logger.LogError(ex, $"An error occurred seeding the database with test data. Error: {ex?.Message}"); + } + } + }); + } + } +} diff --git a/FullStackTesting/tests/FullStackTesting.Web.Api.IntegrationTests/FullStackTesting.Web.Api.IntegrationTests.csproj b/FullStackTesting/tests/FullStackTesting.Web.Api.IntegrationTests/FullStackTesting.Web.Api.IntegrationTests.csproj new file mode 100644 index 0000000..7131b4f --- /dev/null +++ b/FullStackTesting/tests/FullStackTesting.Web.Api.IntegrationTests/FullStackTesting.Web.Api.IntegrationTests.csproj @@ -0,0 +1,28 @@ + + + + netcoreapp2.2 + + false + + FullStackTesting.Web.Api.IntegrationTests + + FullStackTesting.Web.Api.IntegrationTests + + + + + + + + + all + runtime; build; native; contentfiles; analyzers + + + + + + + + diff --git a/sln.sln b/sln.sln new file mode 100644 index 0000000..50f714e --- /dev/null +++ b/sln.sln @@ -0,0 +1,37 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.28307.489 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FullStackTesting.Web.Api", "FullStackTesting\FullStackTesting.Web.Api\FullStackTesting.Web.Api.csproj", "{5AEDBD9A-FC90-4636-8940-563F3812AE0C}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FullStackTesting.Web.Api.IntegrationTests", "FullStackTesting\tests\FullStackTesting.Web.Api.IntegrationTests\FullStackTesting.Web.Api.IntegrationTests.csproj", "{7EF6A873-8783-4551-B2C7-896B38D9F5BF}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "FullStackTesting", "FullStackTesting", "{C89CE22C-EF7D-468B-B060-3D95547795D0}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {5AEDBD9A-FC90-4636-8940-563F3812AE0C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5AEDBD9A-FC90-4636-8940-563F3812AE0C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5AEDBD9A-FC90-4636-8940-563F3812AE0C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5AEDBD9A-FC90-4636-8940-563F3812AE0C}.Release|Any CPU.Build.0 = Release|Any CPU + {7EF6A873-8783-4551-B2C7-896B38D9F5BF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7EF6A873-8783-4551-B2C7-896B38D9F5BF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7EF6A873-8783-4551-B2C7-896B38D9F5BF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7EF6A873-8783-4551-B2C7-896B38D9F5BF}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {5AEDBD9A-FC90-4636-8940-563F3812AE0C} = {C89CE22C-EF7D-468B-B060-3D95547795D0} + {7EF6A873-8783-4551-B2C7-896B38D9F5BF} = {C89CE22C-EF7D-468B-B060-3D95547795D0} + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {012152CE-FCE4-4671-9A14-C07F14A14160} + EndGlobalSection +EndGlobal