Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions PayCalculator/EFCoreConsole/EFCoreConsole.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\PayCalculatorData\PayCalculatorData.csproj" />
</ItemGroup>

</Project>
72 changes: 72 additions & 0 deletions PayCalculator/EFCoreConsole/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using PayCalculatorData;
using PayCalculatorLibrary.Models;

EmployeeContext _context = new();

GetPermanentEmployees();
//AddPermanentEmployee();
//GetPermanentEmployees();
//QueryFilters();
//SortEmployees();
//DeleteEmployee();
UpdateEmployee();
GetPermanentEmployees();

void UpdateEmployee()
{
var employee = _context.PermanentEmployees.FirstOrDefault(x => x.Id == 1);

if (employee != null)
{
employee.Name = "William W";
_context.SaveChanges();
}
}

void DeleteEmployee()
{
var employee = _context.PermanentEmployees.Find(3);

if (employee != null)
{
_context.PermanentEmployees.Remove(employee);
_context.SaveChanges();
}
}

void SortEmployees()
{
var employeesByName = _context.PermanentEmployees.OrderBy(x => x.Name).ToList();
employeesByName.ForEach(x => Console.WriteLine(x.Name));
}

void QueryFilters()
{
var name = "Tom W";
var employees = _context.PermanentEmployees.Where(x => x.Name == name).ToList();
Console.WriteLine(employees);
}

void AddPermanentEmployee()
{
var employee = new PermanentEmployee()
{
Name = "Harry A",
Salary = 25000,
Bonus = 2000,
StartDate = new DateTime(2022, 8, 15)
};

_context.PermanentEmployees.Add(employee);
_context.SaveChanges();
}

void GetPermanentEmployees()
{
var employees = _context.PermanentEmployees.ToList();

foreach (var employee in employees)
{
Console.WriteLine(employee.Name);
}
}
5 changes: 5 additions & 0 deletions PayCalculator/EFCoreConsole/libman.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"version": "1.0",
"defaultProvider": "cdnjs",
"libraries": []
}
25 changes: 25 additions & 0 deletions PayCalculator/EFCoreMockTest/EFCoreMockTest.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="Moq.EntityFrameworkCore" Version="7.0.0.2" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.4.2" />
<PackageReference Include="NUnit.Analyzers" Version="3.6.1" />
<PackageReference Include="coverlet.collector" Version="3.2.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\PayCalculatorLibrary\PayCalculatorLibrary.csproj" />
</ItemGroup>

</Project>
67 changes: 67 additions & 0 deletions PayCalculator/EFCoreMockTest/EmployeeContextTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using Moq;
using Moq.EntityFrameworkCore;
using PayCalculatorLibrary.Models;
using PayCalculatorLibrary.Repositories;
using PayCalculatorLibrary.Services;

namespace EFCoreMockTest
{
public class EmployeeContextTest
{
#nullable disable
private Mock<EmployeeContext> _mockEmployeeContext;

private PersistentPermanentEmployeeRepo _persistentPermEmployeeRepo;
private List<PermanentEmployee> _employees;
private IPermanentPayCalculator _permPayCalculator;
private ITimeCalculator _timeCalculator;
#nullable enable

[SetUp]
public void Setup()
{
_employees = new()
{
new PermanentEmployee() { Id = 1, Name = "William" },
new PermanentEmployee() { Id = 2, Name = "Amir" }
};

_mockEmployeeContext = new();
_persistentPermEmployeeRepo = new(_mockEmployeeContext.Object, _permPayCalculator, _timeCalculator);
}

[Test]
public void Test_PermanentEmployee_GetAll()
{
// Arrange
_mockEmployeeContext.Setup(x => x.PermanentEmployees).ReturnsDbSet(_employees);

// Act
var employeeList = _persistentPermEmployeeRepo.GetAll();

// Assert
Assert.Multiple(() =>
{
Assert.That(employeeList, Is.Not.Null);
Assert.That(employeeList, Has.Exactly(2).Items);
});
}

[TestCase(2)]
public void Test_PermanentEmployee_GetEmployee(int id)
{
// Arrange
_mockEmployeeContext.Setup(x => x.PermanentEmployees.Find(id)).Returns(_employees[1]);

// Act
var employee = _persistentPermEmployeeRepo.GetEmployee(id);

// Assert
Assert.Multiple(() =>
{
Assert.That(employee, Is.Not.Null);
Assert.That(employee.Name, Is.EqualTo("Amir"));
});
}
}
}
1 change: 1 addition & 0 deletions PayCalculator/EFCoreMockTest/Usings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using NUnit.Framework;
25 changes: 23 additions & 2 deletions PayCalculator/PayCalculator.sln
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,24 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PayCalculatorAPI", "PayCalc
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PayCalculatorAPITest", "PayCalculatorAPITest\PayCalculatorAPITest.csproj", "{010DA0D4-A215-43B9-8DB1-F4CE26CDB00B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PayCalculatorMVC", "PayCalculatorMVC\PayCalculatorMVC.csproj", "{D5269444-163C-4442-948B-DAC9FAC65863}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PayCalculatorMVC", "PayCalculatorMVC\PayCalculatorMVC.csproj", "{D5269444-163C-4442-948B-DAC9FAC65863}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PayCalculatorMVCTest", "PayCalculatorMVCTest\PayCalculatorMVCTest.csproj", "{7EBC2E62-02AA-4FF8-B368-9FAAA2D75D74}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PayCalculatorMVCTest", "PayCalculatorMVCTest\PayCalculatorMVCTest.csproj", "{7EBC2E62-02AA-4FF8-B368-9FAAA2D75D74}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{BE04616B-1013-4492-BBBF-BBF99B1A9F0F}"
ProjectSection(SolutionItems) = preProject
log4net.config = log4net.config
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PayCalculatorData", "PayCalculatorData\PayCalculatorData.csproj", "{F01AF298-D595-4240-BED4-3625A3D06D49}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EFCoreConsole", "EFCoreConsole\EFCoreConsole.csproj", "{F90F0A79-A1ED-4BE4-A1FC-A707D2621373}"
ProjectSection(ProjectDependencies) = postProject
{F01AF298-D595-4240-BED4-3625A3D06D49} = {F01AF298-D595-4240-BED4-3625A3D06D49}
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EFCoreMockTest", "EFCoreMockTest\EFCoreMockTest.csproj", "{1D9A5942-B277-4ECE-B163-B056E0424CEC}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -56,6 +65,18 @@ Global
{7EBC2E62-02AA-4FF8-B368-9FAAA2D75D74}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7EBC2E62-02AA-4FF8-B368-9FAAA2D75D74}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7EBC2E62-02AA-4FF8-B368-9FAAA2D75D74}.Release|Any CPU.Build.0 = Release|Any CPU
{F01AF298-D595-4240-BED4-3625A3D06D49}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F01AF298-D595-4240-BED4-3625A3D06D49}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F01AF298-D595-4240-BED4-3625A3D06D49}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F01AF298-D595-4240-BED4-3625A3D06D49}.Release|Any CPU.Build.0 = Release|Any CPU
{F90F0A79-A1ED-4BE4-A1FC-A707D2621373}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F90F0A79-A1ED-4BE4-A1FC-A707D2621373}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F90F0A79-A1ED-4BE4-A1FC-A707D2621373}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F90F0A79-A1ED-4BE4-A1FC-A707D2621373}.Release|Any CPU.Build.0 = Release|Any CPU
{1D9A5942-B277-4ECE-B163-B056E0424CEC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1D9A5942-B277-4ECE-B163-B056E0424CEC}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1D9A5942-B277-4ECE-B163-B056E0424CEC}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1D9A5942-B277-4ECE-B163-B056E0424CEC}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public IActionResult Get()
{
employee.HoursWorked = _timeCalculator.HoursWorked(employee.StartDate, DateTime.Now);
employee.TotalAnnualPay = Math.Round(_permPayCalculator.TotalAnnualPay(employee.Salary.Value, employee.Bonus.Value), 2);
employee.HourlyRate = Math.Round(_permPayCalculator.HourlyRate(employee.Salary.Value, employee.HoursWorked.Value), 2);
employee.HourlyRate = Math.Round(_permPayCalculator.HourlyRate(employee.Salary.Value, employee.HoursWorked), 2);
}

_log4net.Info("Permanent " + LogMessages.ReturnedAllEmployees.Insert(24, Convert.ToString(employeeCount)));
Expand All @@ -64,7 +64,7 @@ public IActionResult GetEmployee(int id)

employee.HoursWorked = _timeCalculator.DaysWorked(employee.StartDate, DateTime.Now, daysInAWeek);
employee.TotalAnnualPay = _permPayCalculator.TotalAnnualPay(employee.Salary.Value, employee.Bonus.Value);
employee.HourlyRate = _permPayCalculator.HourlyRate(employee.Salary.Value, employee.HoursWorked.Value);
employee.HourlyRate = _permPayCalculator.HourlyRate(employee.Salary.Value, employee.HoursWorked);
_log4net.Info("Permanent " + LogMessages.EmployeeRead.Insert(17, stringID));
return Ok(employee);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ public void Setup()
{
Name = EmployeeName,
DayRate = EmployeeDayRate,
WeeksWorked = EmployeeWeeksWorked
};

_mockRepository = new();
Expand Down
17 changes: 17 additions & 0 deletions PayCalculator/PayCalculatorData/PayCalculatorData.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\PayCalculatorLibrary\PayCalculatorLibrary.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.5" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@
<PackageReference Include="Microsoft.Extensions.Logging.Log4Net.AspNetCore" Version="6.1.0" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.5" />
</ItemGroup>

</Project>
25 changes: 25 additions & 0 deletions PayCalculator/PayCalculatorLibrary/Repositories/EmployeeContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Microsoft.EntityFrameworkCore;
using PayCalculatorLibrary.Models;

namespace PayCalculatorLibrary.Repositories
{
public class EmployeeContext : DbContext
{
public EmployeeContext() { }

public EmployeeContext(DbContextOptions options) : base(options) { }

public virtual DbSet<PermanentEmployee> PermanentEmployees { get; set; }
public virtual DbSet<TemporaryEmployee> TemporaryEmployees { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
optionsBuilder.UseSqlServer(
"Data Source = (localdb)\\MSSQLLocalDB; Initial Catalog = EmployeeDatabase"
);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using PayCalculatorLibrary.Models;
using PayCalculatorLibrary.Services;

namespace PayCalculatorLibrary.Repositories
{
public class PersistentPermanentEmployeeRepo : IEmployeeRepository<PermanentEmployee>
{
private readonly EmployeeContext _context;
private readonly IPermanentPayCalculator _payCalculaor;
private readonly ITimeCalculator _timeCalculator;

public PersistentPermanentEmployeeRepo(EmployeeContext context, IPermanentPayCalculator payCalculator, ITimeCalculator timeCalculator)
{
_context = context;
_payCalculaor = payCalculator;
_timeCalculator = timeCalculator;
}

public PermanentEmployee Create(PermanentEmployee employee)
{
employee.Contract = ContractType.Permanent;
employee.HoursWorked = _timeCalculator.HoursWorked(employee.StartDate, DateTime.Now);
employee.TotalAnnualPay = _payCalculaor.TotalAnnualPay(employee.Salary.Value, employee.Bonus.Value);
employee.HourlyRate = _payCalculaor.HourlyRate(employee.Salary.Value, employee.HoursWorked);
_context.PermanentEmployees.Add(employee);
_context.SaveChanges();
return employee;
}

public bool Delete(int id)
{
var employee = _context.PermanentEmployees.Find(id);

if (employee != null)
{
_context.PermanentEmployees.Remove(employee);
_context.SaveChanges();
return true;
}

return false;
}

public IEnumerable<PermanentEmployee> GetAll()
{
return _context.PermanentEmployees.ToList();
}

public PermanentEmployee? GetEmployee(int id)
{
var employee = _context.PermanentEmployees.Find(id);
return employee;
}

public PermanentEmployee? Update(PermanentEmployee updated)
{
var existing = _context.PermanentEmployees.FirstOrDefault(x => x.Id == updated.Id);

if (existing != null)
{
existing.Name = updated.Name;
existing.Salary = updated.Salary;
existing.Bonus = updated.Bonus;
existing.StartDate = updated.StartDate;
_context.SaveChanges();
return updated;
}

return null;
}
}
}
Loading