Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Teste Backend #84

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
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
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[Bb]in/
[Oo]bj/

*.received.txt
[Oo]ut/
.vs/
msbuild.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace TheatricalPlayersRefactoringKata.Api.Controllers
{
[Route("api/invoice")]
[ApiController]
public class InvoiceController : ControllerBase
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using Microsoft.AspNetCore.Mvc;
using System.Net;
using TheatricalPlayersRefactoringKata.Application.Services.Statement;

namespace TheatricalPlayersRefactoringKata.Api.Controllers
{
[Route("api/statement")]
[Produces("application/json")]
[ApiController]
public class StatementController : ControllerBase
{
private readonly IServiceProvider _serviceProvider;

public StatementController(
IServiceProvider serviceProvider
)
{
_serviceProvider = serviceProvider;
}

[HttpPost("generate/txt")]
[ProducesResponseType(StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<IActionResult> GenereteStatementTxt([FromBody] Domain.Entities.Invoice invoice)
{
var strategy = _serviceProvider.GetService<TextStatementGenerator>();
var result = await new StatementService(strategy).Execute(invoice);

return Ok(result);
}

[HttpPost("generate/xml")]
[ProducesResponseType(StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<IActionResult> GenereteStatementXml([FromBody] Domain.Entities.Invoice invoice)
{
var strategy = _serviceProvider.GetService<XmlStatementGenerator>();
var result = await new StatementService(strategy).Execute(invoice);

return Ok(result);
}
}
}
54 changes: 54 additions & 0 deletions TheatricalPlayersRefactoringKata.Api/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using Microsoft.OpenApi.Models;
using Microsoft.AspNetCore.Http.Json;
using TheatricalPlayersRefactoringKata.Application.Converters;
using TheatricalPlayersRefactoringKata.Application.Services.Statement;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddTransient<TextStatementGenerator>();
builder.Services.AddTransient<XmlStatementGenerator>();

builder.Services.Configure<JsonOptions>(options =>
{
options.SerializerOptions.Converters.Add(new GenderConverter());
});

builder.Services.AddControllers();

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new OpenApiInfo
{
Version = "v1",
Title = "Teste Backend",
Description = "Essa aplica��o � usada por uma companhia de teatro para gerar extratos impressos a partir das faturas de seus clientes.",
Contact = new OpenApiContact
{
Name = "Link projeto",
Url = new Uri("https://github.com/aikodigital/teste-backend-v3")
}
});
});

var app = builder.Build();


if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI(options =>
{
options.SwaggerEndpoint("/swagger/v1/swagger.json", "v1");
options.RoutePrefix = string.Empty;
});
}

app.UseHttpsRedirection();

app.UseAuthorization();

app.MapControllers();

app.Run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:7211",
"sslPort": 44310
}
},
"profiles": {
"TheatricalPlayersRefactoringKata.Api": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7018;http://localhost:5104",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

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

<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>

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

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Controller_SelectedScaffolderID>ApiControllerEmptyScaffolder</Controller_SelectedScaffolderID>
<Controller_SelectedScaffolderCategoryPath>root/Common/Api</Controller_SelectedScaffolderCategoryPath>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
9 changes: 9 additions & 0 deletions TheatricalPlayersRefactoringKata.Api/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
Othello: $456.00 (40 seats)
Henry V: $705.40 (20 seats)
King John: $931.60 (39 seats)
Henry V: $705.40 (20 seats)
Amount owed is $3,995.40
Richard III: $803.60 (20 seats)
Amount owed is $4,093.60
You earned 56 credits
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
<Seats>39</Seats>
</Item>
<Item>
<AmountOwed>705.4</AmountOwed>
<AmountOwed>803.6</AmountOwed>
<EarnedCredits>0</EarnedCredits>
<Seats>20</Seats>
</Item>
</Items>
<AmountOwed>3995.4</AmountOwed>
<AmountOwed>4093.6</AmountOwed>
<EarnedCredits>56</EarnedCredits>
</Statement>
92 changes: 59 additions & 33 deletions TheatricalPlayersRefactoringKata.Tests/StatementPrinterTests.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
using System;
using Xunit;
using System.Collections.Generic;
using ApprovalTests;
using ApprovalTests.Reporters;
using Xunit;
using TheatricalPlayersRefactoringKata.Legacy;
using TheatricalPlayersRefactoringKata.Domain.Entities;
using TheatricalPlayersRefactoringKata.Domain.Entities.Gender;
using TheatricalPlayersRefactoringKata.Application.Services.Statement;
using System.Text.Json;
using System.Threading.Tasks;

namespace TheatricalPlayersRefactoringKata.Tests;

Expand All @@ -12,54 +17,75 @@ public class StatementPrinterTests
[UseReporter(typeof(DiffReporter))]
public void TestStatementExampleLegacy()
{
var plays = new Dictionary<string, Play>();
plays.Add("hamlet", new Play("Hamlet", 4024, "tragedy"));
plays.Add("as-like", new Play("As You Like It", 2670, "comedy"));
plays.Add("othello", new Play("Othello", 3560, "tragedy"));
var plays = new Dictionary<string, Play>
{
{ "hamlet", new Play("Hamlet", 4024, "tragedy") },
{ "as-like", new Play("As You Like It", 2670, "comedy") },
{ "othello", new Play("Othello", 3560, "tragedy") }
};

Invoice invoice = new Invoice(
Legacy.Invoice invoice = new(
"BigCo",
new List<Performance>
new List<Legacy.Performance>
{
new Performance("hamlet", 55),
new Performance("as-like", 35),
new Performance("othello", 40),
new("hamlet", 55),
new ("as-like", 35),
new ("othello", 40),
}
);

StatementPrinter statementPrinter = new StatementPrinter();
var statementPrinter = new StatementPrinter();
var result = statementPrinter.Print(invoice, plays);

Approvals.Verify(result);
}

[Fact]
[UseReporter(typeof(DiffReporter))]
public void TestTextStatementExample()
public async Task TestTextStatementExample()
{
var plays = new Dictionary<string, Play>();
plays.Add("hamlet", new Play("Hamlet", 4024, "tragedy"));
plays.Add("as-like", new Play("As You Like It", 2670, "comedy"));
plays.Add("othello", new Play("Othello", 3560, "tragedy"));
plays.Add("henry-v", new Play("Henry V", 3227, "history"));
plays.Add("john", new Play("King John", 2648, "history"));
plays.Add("richard-iii", new Play("Richard III", 3718, "history"));
var plays = new Dictionary<string, Domain.Entities.Performance>
{
{ "hamlet", new Domain.Entities.Performance("Hamlet", 4024, 55, new Tragedy()) },
{ "as-like", new Domain.Entities.Performance("As You Like It", 2670, 35, new Comedy()) },
{ "othello", new Domain.Entities.Performance("Othello", 3560, 40, new Tragedy()) },
{ "henry-v", new Domain.Entities.Performance("Henry V", 3227, 20, new History(new Tragedy(), new Comedy())) },
{ "john", new Domain.Entities.Performance("King John", 2648, 39, new History(new Tragedy(), new Comedy())) },
{ "richard-iii", new Domain.Entities.Performance("Richard III", 3718, 20, new History(new Tragedy(), new Comedy())) }
};

Invoice invoice = new Invoice(
"BigCo",
new List<Performance>
{
new Performance("hamlet", 55),
new Performance("as-like", 35),
new Performance("othello", 40),
new Performance("henry-v", 20),
new Performance("john", 39),
new Performance("henry-v", 20)
}
Domain.Entities.Invoice invoice = new(
new Customer("BigCo"),
plays
);

StatementPrinter statementPrinter = new StatementPrinter();
var result = statementPrinter.Print(invoice, plays);
var statement = new StatementService(new TextStatementGenerator());
var result = await statement.Execute(invoice);

Approvals.Verify(result);
}

[Fact]
[UseReporter(typeof(DiffReporter))]
public async Task TestXmlStatementExample()
{
var plays = new Dictionary<string, Domain.Entities.Performance>
{
{ "hamlet", new Domain.Entities.Performance("Hamlet", 4024, 55, new Tragedy()) },
{ "as-like", new Domain.Entities.Performance("As You Like It", 2670, 35, new Comedy()) },
{ "othello", new Domain.Entities.Performance("Othello", 3560, 40, new Tragedy()) },
{ "henry-v", new Domain.Entities.Performance("Henry V", 3227, 20, new History(new Tragedy(), new Comedy())) },
{ "john", new Domain.Entities.Performance("King John", 2648, 39, new History(new Tragedy(), new Comedy())) },
{ "richard-iii", new Domain.Entities.Performance("Richard III", 3718, 20, new History(new Tragedy(), new Comedy())) }
};

Domain.Entities.Invoice invoice = new(
new Customer("BigCo"),
plays
);

var statement = new StatementService(new XmlStatementGenerator());
var result = await statement.Execute(invoice);

Approvals.Verify(result);
}
Expand Down
23 changes: 16 additions & 7 deletions TheatricalPlayersRefactoringKata.sln
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30114.105
# Visual Studio Version 17
VisualStudioVersion = 17.11.35222.181
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TheatricalPlayersRefactoringKata", "TheatricalPlayersRefactoringKata\TheatricalPlayersRefactoringKata.csproj", "{6D24051E-3E14-4B98-A8B3-DFDA7E4ACD7B}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TheatricalPlayersRefactoringKata", "TheatricalPlayersRefactoringKata\TheatricalPlayersRefactoringKata.csproj", "{6D24051E-3E14-4B98-A8B3-DFDA7E4ACD7B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TheatricalPlayersRefactoringKata.Tests", "TheatricalPlayersRefactoringKata.Tests\TheatricalPlayersRefactoringKata.Tests.csproj", "{2AFBF20D-3D97-46EC-B506-8B7C82598951}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TheatricalPlayersRefactoringKata.Tests", "TheatricalPlayersRefactoringKata.Tests\TheatricalPlayersRefactoringKata.Tests.csproj", "{2AFBF20D-3D97-46EC-B506-8B7C82598951}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TheatricalPlayersRefactoringKata.Api", "TheatricalPlayersRefactoringKata.Api\TheatricalPlayersRefactoringKata.Api.csproj", "{5C5A2AF5-1440-4D24-8DE0-BF32F8C96B9B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{6D24051E-3E14-4B98-A8B3-DFDA7E4ACD7B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6D24051E-3E14-4B98-A8B3-DFDA7E4ACD7B}.Debug|Any CPU.Build.0 = Debug|Any CPU
Expand All @@ -24,5 +23,15 @@ Global
{2AFBF20D-3D97-46EC-B506-8B7C82598951}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2AFBF20D-3D97-46EC-B506-8B7C82598951}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2AFBF20D-3D97-46EC-B506-8B7C82598951}.Release|Any CPU.Build.0 = Release|Any CPU
{5C5A2AF5-1440-4D24-8DE0-BF32F8C96B9B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5C5A2AF5-1440-4D24-8DE0-BF32F8C96B9B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5C5A2AF5-1440-4D24-8DE0-BF32F8C96B9B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5C5A2AF5-1440-4D24-8DE0-BF32F8C96B9B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {E35E5723-F5D2-4D12-B932-6A06E027098F}
EndGlobalSection
EndGlobal
Loading