Initial
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
namespace TaxCalculator.Api.Controllers
|
||||
{
|
||||
using Domain;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using TaxCalculator.Models;
|
||||
|
||||
[Route("api/[controller]")]
|
||||
[ApiController]
|
||||
public class SalaryController : ControllerBase
|
||||
{
|
||||
private readonly ISalaryService _salaryService;
|
||||
|
||||
public SalaryController(ISalaryService salaryService)
|
||||
{
|
||||
_salaryService = salaryService;
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<SalaryDetails> Post([FromBody] int grossAnnualSalary)
|
||||
{
|
||||
var salaryDetails = await _salaryService.CalculateSalaryDetails(grossAnnualSalary);
|
||||
|
||||
return salaryDetails;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.
|
||||
|
||||
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
|
||||
USER app
|
||||
WORKDIR /app
|
||||
EXPOSE 8080
|
||||
EXPOSE 8081
|
||||
|
||||
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
|
||||
ARG BUILD_CONFIGURATION=Release
|
||||
WORKDIR /src
|
||||
COPY ["TaxCalculator.Api/TaxCalculator.Api.csproj", "TaxCalculator.Api/"]
|
||||
RUN dotnet restore "./TaxCalculator.Api/./TaxCalculator.Api.csproj"
|
||||
COPY . .
|
||||
WORKDIR "/src/TaxCalculator.Api"
|
||||
RUN dotnet build "./TaxCalculator.Api.csproj" -c $BUILD_CONFIGURATION -o /app/build
|
||||
|
||||
FROM build AS publish
|
||||
ARG BUILD_CONFIGURATION=Release
|
||||
RUN dotnet publish "./TaxCalculator.Api.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
|
||||
|
||||
FROM base AS final
|
||||
WORKDIR /app
|
||||
COPY --from=publish /app/publish .
|
||||
ENTRYPOINT ["dotnet", "TaxCalculator.Api.dll"]
|
||||
@@ -0,0 +1,52 @@
|
||||
|
||||
namespace TaxCalculator.Api
|
||||
{
|
||||
using DAL;
|
||||
using TaxCalculator.Api.Services;
|
||||
using TaxCalculator.Models;
|
||||
|
||||
public class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Add services to the container.
|
||||
builder.Services.AddControllers();
|
||||
|
||||
// Register your services and repositories here
|
||||
builder.Services.AddScoped<ITaxBandRepository, TaxBandRepository>();
|
||||
builder.Services.AddScoped<ITaxCalculatorService, TaxCalculatorService>();
|
||||
builder.Services.AddScoped<ISalaryService, SalaryService>();
|
||||
|
||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI();
|
||||
}
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
// Configure CORS with the variable
|
||||
app.UseCors(builder =>
|
||||
{
|
||||
builder.AllowAnyOrigin()
|
||||
.AllowAnyMethod()
|
||||
.AllowAnyHeader();
|
||||
});
|
||||
|
||||
app.UseAuthorization();
|
||||
|
||||
app.MapControllers();
|
||||
|
||||
app.Run();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
{
|
||||
"profiles": {
|
||||
"http": {
|
||||
"commandName": "Project",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
},
|
||||
"dotnetRunMessages": true,
|
||||
"applicationUrl": "http://localhost:5020"
|
||||
},
|
||||
"https": {
|
||||
"commandName": "Project",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
},
|
||||
"dotnetRunMessages": true,
|
||||
"applicationUrl": "https://localhost:7122;http://localhost:5020"
|
||||
},
|
||||
"IIS Express": {
|
||||
"commandName": "IISExpress",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"Docker": {
|
||||
"commandName": "Docker",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/swagger",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_HTTPS_PORTS": "8081",
|
||||
"ASPNETCORE_HTTP_PORTS": "8080"
|
||||
},
|
||||
"publishAllPorts": true,
|
||||
"useSSL": true
|
||||
}
|
||||
},
|
||||
"$schema": "http://json.schemastore.org/launchsettings.json",
|
||||
"iisSettings": {
|
||||
"windowsAuthentication": false,
|
||||
"anonymousAuthentication": true,
|
||||
"iisExpress": {
|
||||
"applicationUrl": "http://localhost:55237",
|
||||
"sslPort": 44352
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace TaxCalculator.Models
|
||||
{
|
||||
using Domain;
|
||||
|
||||
public interface ISalaryService
|
||||
{
|
||||
Task<SalaryDetails> CalculateSalaryDetails(int grossAnnualSalary);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace TaxCalculator.Api.Services
|
||||
{
|
||||
public interface ITaxCalculatorService
|
||||
{
|
||||
Task<decimal> CalculateTax(int annualSalary);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
namespace TaxCalculator.Api.Services
|
||||
{
|
||||
using Domain;
|
||||
using TaxCalculator.Models;
|
||||
|
||||
public class SalaryService : ISalaryService
|
||||
{
|
||||
private readonly ITaxCalculatorService _taxCalculator;
|
||||
|
||||
public SalaryService(ITaxCalculatorService taxCalculator)
|
||||
{
|
||||
_taxCalculator = taxCalculator ?? throw new ArgumentNullException(nameof(taxCalculator));
|
||||
}
|
||||
|
||||
public async Task<SalaryDetails> CalculateSalaryDetails(int grossAnnualSalary)
|
||||
{
|
||||
if (grossAnnualSalary < 0)
|
||||
{
|
||||
throw new ArgumentException("Gross annual salary must be non-negative.");
|
||||
}
|
||||
|
||||
// Calculate tax once
|
||||
decimal annualTax = await _taxCalculator.CalculateTax(grossAnnualSalary);
|
||||
|
||||
decimal netAnnualSalary = grossAnnualSalary - annualTax;
|
||||
decimal grossMonthlySalary = (decimal)grossAnnualSalary / 12;
|
||||
decimal netMonthlySalary = netAnnualSalary / 12;
|
||||
decimal monthlyTaxPaid = annualTax / 12;
|
||||
|
||||
return new SalaryDetails
|
||||
{
|
||||
GrossAnnualSalary = grossAnnualSalary,
|
||||
GrossMonthlySalary = Math.Round(grossMonthlySalary, 2),
|
||||
NetAnnualSalary = netAnnualSalary,
|
||||
NetMonthlySalary = Math.Round(netMonthlySalary, 2),
|
||||
AnnualTaxPaid = annualTax,
|
||||
MonthlyTaxPaid = Math.Round(monthlyTaxPaid, 2)
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
namespace TaxCalculator.Api.Services
|
||||
{
|
||||
using DAL;
|
||||
|
||||
public class TaxCalculatorService : ITaxCalculatorService
|
||||
{
|
||||
private readonly ITaxBandRepository _taxBandRepository;
|
||||
|
||||
public TaxCalculatorService(ITaxBandRepository taxBandRepository)
|
||||
{
|
||||
_taxBandRepository = taxBandRepository ?? throw new ArgumentNullException(nameof(taxBandRepository));
|
||||
}
|
||||
|
||||
public async Task<decimal> CalculateTax(int annualSalary)
|
||||
{
|
||||
if (annualSalary < 0)
|
||||
{
|
||||
throw new ArgumentException("Gross annual salary must be non-negative.");
|
||||
}
|
||||
|
||||
decimal taxPaid = 0;
|
||||
|
||||
foreach (var band in await _taxBandRepository.GetTaxBands())
|
||||
{
|
||||
if (annualSalary <= band.LowerLimit)
|
||||
continue;
|
||||
|
||||
decimal taxableAmountInBand = band.UpperLimit.HasValue
|
||||
? Math.Min(annualSalary, band.UpperLimit.Value) - band.LowerLimit
|
||||
: annualSalary - band.LowerLimit;
|
||||
|
||||
decimal taxInBand = taxableAmountInBand * (band.TaxRate / 100m);
|
||||
taxPaid += taxInBand;
|
||||
|
||||
if (!band.UpperLimit.HasValue || annualSalary <= band.UpperLimit.Value)
|
||||
break;
|
||||
}
|
||||
|
||||
return taxPaid;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<InvariantGlobalization>true</InvariantGlobalization>
|
||||
<UserSecretsId>da1c3abf-bae6-42a2-9318-1ee6c4dd7e64</UserSecretsId>
|
||||
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.Cors" Version="2.2.0" />
|
||||
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.5" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Models\" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\DAL\DAL.csproj" />
|
||||
<ProjectReference Include="..\Domain\Domain.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,6 @@
|
||||
@TaxCalculator.Api_HostAddress = http://localhost:5020
|
||||
|
||||
GET {{TaxCalculator.Api_HostAddress}}/weatherforecast/
|
||||
Accept: application/json
|
||||
|
||||
###
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
Reference in New Issue
Block a user