From 0c4882f1d4594a903ef418745dc12e5d2470a043 Mon Sep 17 00:00:00 2001 From: lucasvil4r Date: Mon, 16 Sep 2024 18:00:56 -0300 Subject: [PATCH 01/12] feature: modelando o projeto para arquitetura clean arch e criando as duas novas features --- .../Interfaces/IGenreCalculator.cs | 15 ++++ .../Interfaces/IStatementPrinter.cs | 9 +++ .../Services/Calculators/ComedyCalculator.cs | 26 +++++++ .../Services/Calculators/HistoryCalculator.cs | 22 ++++++ .../Services/Calculators/TragedyCalculator.cs | 28 +++++++ .../Factories/GenreCalculatorFactory.cs | 24 ++++++ .../Factories/StatementPrinterFactory.cs | 23 ++++++ .../Services/Printers/TextStatementPrinter.cs | 38 ++++++++++ .../Services/Printers/XmlStatementPrinter.cs | 53 +++++++++++++ ...lPlayersRefactoringKata.Application.csproj | 13 ++++ .../Entities}/Invoice.cs | 6 +- .../Entities}/Performance.cs | 6 +- .../Entities}/Play.cs | 11 +-- ...tricalPlayersRefactoringKata.Domain.csproj | 9 +++ ...ayersRefactoringKata.Infrastructure.csproj | 13 ++++ .../Controllers/WeatherForecastController.cs | 33 +++++++++ .../Program.cs | 25 +++++++ .../Properties/launchSettings.json | 41 ++++++++++ ...PlayersRefactoringKata.Presentation.csproj | 13 ++++ ...rsRefactoringKata.Presentation.csproj.user | 6 ++ ...alPlayersRefactoringKata.Presentation.http | 6 ++ .../WeatherForecast.cs | 13 ++++ .../appsettings.Development.json | 8 ++ .../appsettings.json | 9 +++ .../StatementPrinterTests.cs | 74 ++++++++++++++++++- ...atricalPlayersRefactoringKata.Tests.csproj | 4 +- TheatricalPlayersRefactoringKata.sln | 47 ++++++++---- .../Interfaces/IGenreCalculator.cs | 15 ++++ .../Interfaces/IStatementPrinter.cs | 14 ++++ .../Services/Calculators/ComedyCalculator.cs | 31 ++++++++ .../Services/Calculators/HistoryCalculator.cs | 28 +++++++ .../Services/Calculators/TragedyCalculator.cs | 28 +++++++ .../Factories/GenreCalculatorFactory.cs | 24 ++++++ .../Factories/StatementPrinterFactory.cs | 23 ++++++ .../Services/Printers/TextStatementPrinter.cs | 43 +++++++++++ .../Services/Printers/XmlStatementPrinter.cs | 53 +++++++++++++ .../Domain/Entities/Invoice.cs | 19 +++++ .../Domain/Entities/Performance.cs | 17 +++++ .../Domain/Entities/Play.cs | 19 +++++ .../StatementPrinter.cs | 52 ------------- .../TheatricalPlayersRefactoringKata.csproj | 9 ++- 41 files changed, 869 insertions(+), 81 deletions(-) create mode 100644 TheatricalPlayersRefactoringKata.Application/Interfaces/IGenreCalculator.cs create mode 100644 TheatricalPlayersRefactoringKata.Application/Interfaces/IStatementPrinter.cs create mode 100644 TheatricalPlayersRefactoringKata.Application/Services/Calculators/ComedyCalculator.cs create mode 100644 TheatricalPlayersRefactoringKata.Application/Services/Calculators/HistoryCalculator.cs create mode 100644 TheatricalPlayersRefactoringKata.Application/Services/Calculators/TragedyCalculator.cs create mode 100644 TheatricalPlayersRefactoringKata.Application/Services/Factories/GenreCalculatorFactory.cs create mode 100644 TheatricalPlayersRefactoringKata.Application/Services/Factories/StatementPrinterFactory.cs create mode 100644 TheatricalPlayersRefactoringKata.Application/Services/Printers/TextStatementPrinter.cs create mode 100644 TheatricalPlayersRefactoringKata.Application/Services/Printers/XmlStatementPrinter.cs create mode 100644 TheatricalPlayersRefactoringKata.Application/TheatricalPlayersRefactoringKata.Application.csproj rename {TheatricalPlayersRefactoringKata => TheatricalPlayersRefactoringKata.Domain/Entities}/Invoice.cs (75%) rename {TheatricalPlayersRefactoringKata => TheatricalPlayersRefactoringKata.Domain/Entities}/Performance.cs (71%) rename {TheatricalPlayersRefactoringKata => TheatricalPlayersRefactoringKata.Domain/Entities}/Play.cs (61%) create mode 100644 TheatricalPlayersRefactoringKata.Domain/TheatricalPlayersRefactoringKata.Domain.csproj create mode 100644 TheatricalPlayersRefactoringKata.Infrastructure/TheatricalPlayersRefactoringKata.Infrastructure.csproj create mode 100644 TheatricalPlayersRefactoringKata.Presentation/Controllers/WeatherForecastController.cs create mode 100644 TheatricalPlayersRefactoringKata.Presentation/Program.cs create mode 100644 TheatricalPlayersRefactoringKata.Presentation/Properties/launchSettings.json create mode 100644 TheatricalPlayersRefactoringKata.Presentation/TheatricalPlayersRefactoringKata.Presentation.csproj create mode 100644 TheatricalPlayersRefactoringKata.Presentation/TheatricalPlayersRefactoringKata.Presentation.csproj.user create mode 100644 TheatricalPlayersRefactoringKata.Presentation/TheatricalPlayersRefactoringKata.Presentation.http create mode 100644 TheatricalPlayersRefactoringKata.Presentation/WeatherForecast.cs create mode 100644 TheatricalPlayersRefactoringKata.Presentation/appsettings.Development.json create mode 100644 TheatricalPlayersRefactoringKata.Presentation/appsettings.json create mode 100644 TheatricalPlayersRefactoringKata/Application/Interfaces/IGenreCalculator.cs create mode 100644 TheatricalPlayersRefactoringKata/Application/Interfaces/IStatementPrinter.cs create mode 100644 TheatricalPlayersRefactoringKata/Application/Services/Calculators/ComedyCalculator.cs create mode 100644 TheatricalPlayersRefactoringKata/Application/Services/Calculators/HistoryCalculator.cs create mode 100644 TheatricalPlayersRefactoringKata/Application/Services/Calculators/TragedyCalculator.cs create mode 100644 TheatricalPlayersRefactoringKata/Application/Services/Factories/GenreCalculatorFactory.cs create mode 100644 TheatricalPlayersRefactoringKata/Application/Services/Factories/StatementPrinterFactory.cs create mode 100644 TheatricalPlayersRefactoringKata/Application/Services/Printers/TextStatementPrinter.cs create mode 100644 TheatricalPlayersRefactoringKata/Application/Services/Printers/XmlStatementPrinter.cs create mode 100644 TheatricalPlayersRefactoringKata/Domain/Entities/Invoice.cs create mode 100644 TheatricalPlayersRefactoringKata/Domain/Entities/Performance.cs create mode 100644 TheatricalPlayersRefactoringKata/Domain/Entities/Play.cs delete mode 100644 TheatricalPlayersRefactoringKata/StatementPrinter.cs diff --git a/TheatricalPlayersRefactoringKata.Application/Interfaces/IGenreCalculator.cs b/TheatricalPlayersRefactoringKata.Application/Interfaces/IGenreCalculator.cs new file mode 100644 index 00000000..c5289099 --- /dev/null +++ b/TheatricalPlayersRefactoringKata.Application/Interfaces/IGenreCalculator.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TheatricalPlayersRefactoringKata.Domain.Entities; + +namespace TheatricalPlayersRefactoringKata.Application.Interfaces +{ + public interface IGenreCalculator + { + decimal CalculateAmount(Performance perf, Play play); + int CalculateVolumeCredits(Performance perf); + } +} diff --git a/TheatricalPlayersRefactoringKata.Application/Interfaces/IStatementPrinter.cs b/TheatricalPlayersRefactoringKata.Application/Interfaces/IStatementPrinter.cs new file mode 100644 index 00000000..fa3b8635 --- /dev/null +++ b/TheatricalPlayersRefactoringKata.Application/Interfaces/IStatementPrinter.cs @@ -0,0 +1,9 @@ +using TheatricalPlayersRefactoringKata.Domain.Entities; + +namespace TheatricalPlayersRefactoringKata.Application.Interfaces +{ + public interface IStatementPrinter + { + string Print(Invoice invoice, Dictionary plays); + } +} diff --git a/TheatricalPlayersRefactoringKata.Application/Services/Calculators/ComedyCalculator.cs b/TheatricalPlayersRefactoringKata.Application/Services/Calculators/ComedyCalculator.cs new file mode 100644 index 00000000..536b98cc --- /dev/null +++ b/TheatricalPlayersRefactoringKata.Application/Services/Calculators/ComedyCalculator.cs @@ -0,0 +1,26 @@ +using TheatricalPlayersRefactoringKata.Application.Interfaces; +using TheatricalPlayersRefactoringKata.Domain.Entities; + +namespace TheatricalPlayersRefactoringKata.Application.Services.Calculators +{ + public class ComedyCalculator : IGenreCalculator + { + public decimal CalculateAmount(Performance perf, Play play) + { + decimal thisAmount = 30000; + if (perf.Audience > 20) + { + thisAmount += 10000 + 500 * (perf.Audience - 20); + } + thisAmount += 300 * perf.Audience; + return thisAmount; + } + + public int CalculateVolumeCredits(Performance perf) + { + int volumeCredits = Math.Max(perf.Audience - 30, 0); + volumeCredits += (int)Math.Floor((decimal)perf.Audience / 5); // Extra credits for comedy + return volumeCredits; + } + } +} diff --git a/TheatricalPlayersRefactoringKata.Application/Services/Calculators/HistoryCalculator.cs b/TheatricalPlayersRefactoringKata.Application/Services/Calculators/HistoryCalculator.cs new file mode 100644 index 00000000..8b023dd9 --- /dev/null +++ b/TheatricalPlayersRefactoringKata.Application/Services/Calculators/HistoryCalculator.cs @@ -0,0 +1,22 @@ +using System; + +namespace TheatricalPlayersRefactoringKata.Application.Services.Calculators +{ + public class HistoryCalculator : IGenreCalculator + { + public decimal CalculateAmount(Performance perf, Play play) + { + decimal thisAmount = 50000; // Base amount for history plays + if (perf.Audience > 25) + { + thisAmount += 1500 * (perf.Audience - 25); + } + return thisAmount; + } + + public int CalculateVolumeCredits(Performance perf) + { + return Math.Max(perf.Audience - 25, 0); // Different credit rule for history + } + } +} diff --git a/TheatricalPlayersRefactoringKata.Application/Services/Calculators/TragedyCalculator.cs b/TheatricalPlayersRefactoringKata.Application/Services/Calculators/TragedyCalculator.cs new file mode 100644 index 00000000..174e48c3 --- /dev/null +++ b/TheatricalPlayersRefactoringKata.Application/Services/Calculators/TragedyCalculator.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TheatricalPlayersRefactoringKata.Application.Interfaces; +using TheatricalPlayersRefactoringKata.Domain.Entities; + +namespace TheatricalPlayersRefactoringKata.Application.Services.Calculators +{ + public class TragedyCalculator : IGenreCalculator + { + public decimal CalculateAmount(Performance perf, Play play) + { + decimal thisAmount = 40000; + if (perf.Audience > 30) + { + thisAmount += 1000 * (perf.Audience - 30); + } + return thisAmount; + } + + public int CalculateVolumeCredits(Performance perf) + { + return Math.Max(perf.Audience - 30, 0); + } + } +} diff --git a/TheatricalPlayersRefactoringKata.Application/Services/Factories/GenreCalculatorFactory.cs b/TheatricalPlayersRefactoringKata.Application/Services/Factories/GenreCalculatorFactory.cs new file mode 100644 index 00000000..f7b72143 --- /dev/null +++ b/TheatricalPlayersRefactoringKata.Application/Services/Factories/GenreCalculatorFactory.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TheatricalPlayersRefactoringKata.Application.Interfaces; +using TheatricalPlayersRefactoringKata.Application.Services.Calculators; + +namespace TheatricalPlayersRefactoringKata.Application.Services.Factories +{ + public class GenreCalculatorFactory + { + public static IGenreCalculator GetCalculator(string genreType) + { + return genreType switch + { + "tragedy" => new TragedyCalculator(), + "comedy" => new ComedyCalculator(), + "history" => new HistoryCalculator(), + _ => throw new Exception("unknown genre: " + genreType), + }; + } + } +} diff --git a/TheatricalPlayersRefactoringKata.Application/Services/Factories/StatementPrinterFactory.cs b/TheatricalPlayersRefactoringKata.Application/Services/Factories/StatementPrinterFactory.cs new file mode 100644 index 00000000..545be067 --- /dev/null +++ b/TheatricalPlayersRefactoringKata.Application/Services/Factories/StatementPrinterFactory.cs @@ -0,0 +1,23 @@ +using System; +using TheatricalPlayersRefactoringKata.Application.Interfaces; +using TheatricalPlayersRefactoringKata.Application.Services.Printers; + +namespace TheatricalPlayersRefactoringKata.Application.Services.Factories +{ + public static class StatementPrinterFactory + { + public static IStatementPrinter GetPrinter(string format) + { + switch (format.ToLower()) + { + case "text": + return new TextStatementPrinter(); + case "xml": + return new XmlStatementPrinter(); + // Adicione mais casos conforme necessário para novos formatos + default: + throw new ArgumentException("Invalid format", nameof(format)); + } + } + } +} diff --git a/TheatricalPlayersRefactoringKata.Application/Services/Printers/TextStatementPrinter.cs b/TheatricalPlayersRefactoringKata.Application/Services/Printers/TextStatementPrinter.cs new file mode 100644 index 00000000..ff6b6930 --- /dev/null +++ b/TheatricalPlayersRefactoringKata.Application/Services/Printers/TextStatementPrinter.cs @@ -0,0 +1,38 @@ + +using System.Globalization; +using TheatricalPlayersRefactoringKata.Application.Interfaces; +using TheatricalPlayersRefactoringKata.Application.Services.Factories; + +namespace TheatricalPlayersRefactoringKata.Application.Services.Printers +{ + public class TextStatementPrinter : IStatementPrinter + { + public string Print(Invoice invoice, Dictionary plays) + { + var totalAmount = 0m; + var volumeCredits = 0; + var result = string.Format("Statement for {0}\n", invoice.Customer); + CultureInfo cultureInfo = new CultureInfo("en-US"); + + foreach (var perf in invoice.Performances) + { + var play = plays[perf.PlayId]; + var calculator = GenreCalculatorFactory.GetCalculator(play.Type); + + var thisAmount = calculator.CalculateAmount(perf, play); + totalAmount += thisAmount; + + // Add volume credits + volumeCredits += calculator.CalculateVolumeCredits(perf); + + // Print line for this order + result += string.Format(cultureInfo, " {0}: {1:C} ({2} seats)\n", play.Name, Convert.ToDecimal(thisAmount / 100), perf.Audience); + } + + result += string.Format(cultureInfo, "Amount owed is {0:C}\n", Convert.ToDecimal(totalAmount / 100)); + result += string.Format("You earned {0} credits\n", volumeCredits); + + return result; + } + } +} diff --git a/TheatricalPlayersRefactoringKata.Application/Services/Printers/XmlStatementPrinter.cs b/TheatricalPlayersRefactoringKata.Application/Services/Printers/XmlStatementPrinter.cs new file mode 100644 index 00000000..96246d83 --- /dev/null +++ b/TheatricalPlayersRefactoringKata.Application/Services/Printers/XmlStatementPrinter.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TheatricalPlayersRefactoringKata.Application.Interfaces; +using TheatricalPlayersRefactoringKata.Application.Services.Factories; +using TheatricalPlayersRefactoringKata.Domain.Entities; + +namespace TheatricalPlayersRefactoringKata.Application.Services.Printers +{ + public class XmlStatementPrinter : IStatementPrinter + { + public string Print(Invoice invoice, Dictionary plays) + { + var totalAmount = 0m; + var totalVolumeCredits = 0; + var xml = new StringBuilder(); + + xml.AppendLine(""); + xml.AppendLine(""); + xml.AppendLine($" {invoice.Customer}"); + xml.AppendLine(" "); + + foreach (var perf in invoice.Performances) + { + var play = plays[perf.PlayId]; + var calculator = GenreCalculatorFactory.GetCalculator(play.Type); + + // Calcula o valor devido e os créditos ganhados para cada performance + var thisAmount = calculator.CalculateAmount(perf, play); + var volumeCredits = calculator.CalculateVolumeCredits(perf); + + totalAmount += thisAmount; + totalVolumeCredits += volumeCredits; + + // Gera o XML para cada item + xml.AppendLine(" "); + xml.AppendLine($" {thisAmount / 100}"); + xml.AppendLine($" {volumeCredits}"); + xml.AppendLine($" {perf.Audience}"); + xml.AppendLine(" "); + } + + xml.AppendLine(" "); + xml.AppendLine($" {totalAmount / 100}"); + xml.AppendLine($" {totalVolumeCredits}"); + xml.Append(""); + + return xml.ToString(); + } + } +} diff --git a/TheatricalPlayersRefactoringKata.Application/TheatricalPlayersRefactoringKata.Application.csproj b/TheatricalPlayersRefactoringKata.Application/TheatricalPlayersRefactoringKata.Application.csproj new file mode 100644 index 00000000..c96970a9 --- /dev/null +++ b/TheatricalPlayersRefactoringKata.Application/TheatricalPlayersRefactoringKata.Application.csproj @@ -0,0 +1,13 @@ + + + + net8.0 + enable + enable + + + + + + + diff --git a/TheatricalPlayersRefactoringKata/Invoice.cs b/TheatricalPlayersRefactoringKata.Domain/Entities/Invoice.cs similarity index 75% rename from TheatricalPlayersRefactoringKata/Invoice.cs rename to TheatricalPlayersRefactoringKata.Domain/Entities/Invoice.cs index 5d3e5484..d6c57867 100644 --- a/TheatricalPlayersRefactoringKata/Invoice.cs +++ b/TheatricalPlayersRefactoringKata.Domain/Entities/Invoice.cs @@ -1,6 +1,6 @@ using System.Collections.Generic; -namespace TheatricalPlayersRefactoringKata; +namespace TheatricalPlayersRefactoringKata.Domain.Entities; public class Invoice { @@ -12,8 +12,8 @@ public class Invoice public Invoice(string customer, List performance) { - this._customer = customer; - this._performances = performance; + _customer = customer; + _performances = performance; } } diff --git a/TheatricalPlayersRefactoringKata/Performance.cs b/TheatricalPlayersRefactoringKata.Domain/Entities/Performance.cs similarity index 71% rename from TheatricalPlayersRefactoringKata/Performance.cs rename to TheatricalPlayersRefactoringKata.Domain/Entities/Performance.cs index 99cb84eb..6a0eb08e 100644 --- a/TheatricalPlayersRefactoringKata/Performance.cs +++ b/TheatricalPlayersRefactoringKata.Domain/Entities/Performance.cs @@ -1,4 +1,4 @@ -namespace TheatricalPlayersRefactoringKata; +namespace TheatricalPlayersRefactoringKata.Domain.Entities; public class Performance { @@ -10,8 +10,8 @@ public class Performance public Performance(string playID, int audience) { - this._playId = playID; - this._audience = audience; + _playId = playID; + _audience = audience; } } diff --git a/TheatricalPlayersRefactoringKata/Play.cs b/TheatricalPlayersRefactoringKata.Domain/Entities/Play.cs similarity index 61% rename from TheatricalPlayersRefactoringKata/Play.cs rename to TheatricalPlayersRefactoringKata.Domain/Entities/Play.cs index 3b722a4f..4c3cae77 100644 --- a/TheatricalPlayersRefactoringKata/Play.cs +++ b/TheatricalPlayersRefactoringKata.Domain/Entities/Play.cs @@ -1,4 +1,4 @@ -namespace TheatricalPlayersRefactoringKata; +namespace TheatricalPlayersRefactoringKata.Domain.Entities; public class Play { @@ -10,9 +10,10 @@ public class Play public int Lines { get => _lines; set => _lines = value; } public string Type { get => _type; set => _type = value; } - public Play(string name, int lines, string type) { - this._name = name; - this._lines = lines; - this._type = type; + public Play(string name, int lines, string type) + { + _name = name; + _lines = lines; + _type = type; } } diff --git a/TheatricalPlayersRefactoringKata.Domain/TheatricalPlayersRefactoringKata.Domain.csproj b/TheatricalPlayersRefactoringKata.Domain/TheatricalPlayersRefactoringKata.Domain.csproj new file mode 100644 index 00000000..fa71b7ae --- /dev/null +++ b/TheatricalPlayersRefactoringKata.Domain/TheatricalPlayersRefactoringKata.Domain.csproj @@ -0,0 +1,9 @@ + + + + net8.0 + enable + enable + + + diff --git a/TheatricalPlayersRefactoringKata.Infrastructure/TheatricalPlayersRefactoringKata.Infrastructure.csproj b/TheatricalPlayersRefactoringKata.Infrastructure/TheatricalPlayersRefactoringKata.Infrastructure.csproj new file mode 100644 index 00000000..fbb2999c --- /dev/null +++ b/TheatricalPlayersRefactoringKata.Infrastructure/TheatricalPlayersRefactoringKata.Infrastructure.csproj @@ -0,0 +1,13 @@ + + + + net8.0 + enable + enable + + + + + + + diff --git a/TheatricalPlayersRefactoringKata.Presentation/Controllers/WeatherForecastController.cs b/TheatricalPlayersRefactoringKata.Presentation/Controllers/WeatherForecastController.cs new file mode 100644 index 00000000..748102f0 --- /dev/null +++ b/TheatricalPlayersRefactoringKata.Presentation/Controllers/WeatherForecastController.cs @@ -0,0 +1,33 @@ +using Microsoft.AspNetCore.Mvc; + +namespace TheatricalPlayersRefactoringKata.Presentation.Controllers +{ + [ApiController] + [Route("[controller]")] + public class WeatherForecastController : ControllerBase + { + private static readonly string[] Summaries = new[] + { + "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" + }; + + private readonly ILogger _logger; + + public WeatherForecastController(ILogger logger) + { + _logger = logger; + } + + [HttpGet(Name = "GetWeatherForecast")] + public IEnumerable Get() + { + return Enumerable.Range(1, 5).Select(index => new WeatherForecast + { + Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)), + TemperatureC = Random.Shared.Next(-20, 55), + Summary = Summaries[Random.Shared.Next(Summaries.Length)] + }) + .ToArray(); + } + } +} diff --git a/TheatricalPlayersRefactoringKata.Presentation/Program.cs b/TheatricalPlayersRefactoringKata.Presentation/Program.cs new file mode 100644 index 00000000..48863a6d --- /dev/null +++ b/TheatricalPlayersRefactoringKata.Presentation/Program.cs @@ -0,0 +1,25 @@ +var builder = WebApplication.CreateBuilder(args); + +// Add services to the container. + +builder.Services.AddControllers(); +// 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(); + +app.UseAuthorization(); + +app.MapControllers(); + +app.Run(); diff --git a/TheatricalPlayersRefactoringKata.Presentation/Properties/launchSettings.json b/TheatricalPlayersRefactoringKata.Presentation/Properties/launchSettings.json new file mode 100644 index 00000000..f9f42692 --- /dev/null +++ b/TheatricalPlayersRefactoringKata.Presentation/Properties/launchSettings.json @@ -0,0 +1,41 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "iisSettings": { + "windowsAuthentication": false, + "anonymousAuthentication": true, + "iisExpress": { + "applicationUrl": "http://localhost:16965", + "sslPort": 44309 + } + }, + "profiles": { + "http": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "http://localhost:5235", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "https": { + "commandName": "Project", + "dotnetRunMessages": true, + "launchBrowser": true, + "launchUrl": "swagger", + "applicationUrl": "https://localhost:7205;http://localhost:5235", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + }, + "IIS Express": { + "commandName": "IISExpress", + "launchBrowser": true, + "launchUrl": "swagger", + "environmentVariables": { + "ASPNETCORE_ENVIRONMENT": "Development" + } + } + } +} diff --git a/TheatricalPlayersRefactoringKata.Presentation/TheatricalPlayersRefactoringKata.Presentation.csproj b/TheatricalPlayersRefactoringKata.Presentation/TheatricalPlayersRefactoringKata.Presentation.csproj new file mode 100644 index 00000000..9daa1808 --- /dev/null +++ b/TheatricalPlayersRefactoringKata.Presentation/TheatricalPlayersRefactoringKata.Presentation.csproj @@ -0,0 +1,13 @@ + + + + net8.0 + enable + enable + + + + + + + diff --git a/TheatricalPlayersRefactoringKata.Presentation/TheatricalPlayersRefactoringKata.Presentation.csproj.user b/TheatricalPlayersRefactoringKata.Presentation/TheatricalPlayersRefactoringKata.Presentation.csproj.user new file mode 100644 index 00000000..9ff5820a --- /dev/null +++ b/TheatricalPlayersRefactoringKata.Presentation/TheatricalPlayersRefactoringKata.Presentation.csproj.user @@ -0,0 +1,6 @@ + + + + https + + \ No newline at end of file diff --git a/TheatricalPlayersRefactoringKata.Presentation/TheatricalPlayersRefactoringKata.Presentation.http b/TheatricalPlayersRefactoringKata.Presentation/TheatricalPlayersRefactoringKata.Presentation.http new file mode 100644 index 00000000..cd97bb3e --- /dev/null +++ b/TheatricalPlayersRefactoringKata.Presentation/TheatricalPlayersRefactoringKata.Presentation.http @@ -0,0 +1,6 @@ +@TheatricalPlayersRefactoringKata.Presentation_HostAddress = http://localhost:5235 + +GET {{TheatricalPlayersRefactoringKata.Presentation_HostAddress}}/weatherforecast/ +Accept: application/json + +### diff --git a/TheatricalPlayersRefactoringKata.Presentation/WeatherForecast.cs b/TheatricalPlayersRefactoringKata.Presentation/WeatherForecast.cs new file mode 100644 index 00000000..7e5bdafa --- /dev/null +++ b/TheatricalPlayersRefactoringKata.Presentation/WeatherForecast.cs @@ -0,0 +1,13 @@ +namespace TheatricalPlayersRefactoringKata.Presentation +{ + public class WeatherForecast + { + public DateOnly Date { get; set; } + + public int TemperatureC { get; set; } + + public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); + + public string? Summary { get; set; } + } +} diff --git a/TheatricalPlayersRefactoringKata.Presentation/appsettings.Development.json b/TheatricalPlayersRefactoringKata.Presentation/appsettings.Development.json new file mode 100644 index 00000000..0c208ae9 --- /dev/null +++ b/TheatricalPlayersRefactoringKata.Presentation/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + } +} diff --git a/TheatricalPlayersRefactoringKata.Presentation/appsettings.json b/TheatricalPlayersRefactoringKata.Presentation/appsettings.json new file mode 100644 index 00000000..10f68b8c --- /dev/null +++ b/TheatricalPlayersRefactoringKata.Presentation/appsettings.json @@ -0,0 +1,9 @@ +{ + "Logging": { + "LogLevel": { + "Default": "Information", + "Microsoft.AspNetCore": "Warning" + } + }, + "AllowedHosts": "*" +} diff --git a/TheatricalPlayersRefactoringKata.Tests/StatementPrinterTests.cs b/TheatricalPlayersRefactoringKata.Tests/StatementPrinterTests.cs index f2b7e742..0f751558 100644 --- a/TheatricalPlayersRefactoringKata.Tests/StatementPrinterTests.cs +++ b/TheatricalPlayersRefactoringKata.Tests/StatementPrinterTests.cs @@ -2,12 +2,15 @@ using System.Collections.Generic; using ApprovalTests; using ApprovalTests.Reporters; +using TheatricalPlayersRefactoringKata.Application.Services.Printers; +using TheatricalPlayersRefactoringKata.Domain.Entities; using Xunit; namespace TheatricalPlayersRefactoringKata.Tests; public class StatementPrinterTests { + /* #deprecated [Fact] [UseReporter(typeof(DiffReporter))] public void TestStatementExampleLegacy() @@ -27,11 +30,45 @@ public void TestStatementExampleLegacy() } ); - StatementPrinter statementPrinter = new StatementPrinter(); + //StatementPrinter statementPrinter = new StatementPrinter(); + //var result = statementPrinter.Print(invoice, plays); + + //Approvals.Verify(result); + } + */ + + /* #deprecated + [Fact] + [UseReporter(typeof(DiffReporter))] + public void TestTextStatementExample() + { + var plays = new Dictionary(); + 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")); + + Invoice invoice = new Invoice( + "BigCo", + new List + { + 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) + } + ); + + StatementPrinter statementPrinter = new StatementPrinter(); var result = statementPrinter.Print(invoice, plays); Approvals.Verify(result); } + */ [Fact] [UseReporter(typeof(DiffReporter))] @@ -58,9 +95,42 @@ public void TestTextStatementExample() } ); - StatementPrinter statementPrinter = new StatementPrinter(); + TextStatementPrinter statementPrinter = new TextStatementPrinter(); + var result = statementPrinter.Print(invoice, plays); + + Approvals.Verify(result); + } + + [Fact] + [UseReporter(typeof(DiffReporter))] // Define qual reporter usar para mostrar as diferenças + public void TestXmlStatementExample() + { + var plays = new Dictionary(); + 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")); + + Invoice invoice = new Invoice( + "BigCo", + new List + { + 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) + } + ); + + // Gerando o resultado da impressão XML + XmlStatementPrinter statementPrinter = new XmlStatementPrinter(); var result = statementPrinter.Print(invoice, plays); + // Verificando o resultado gerado usando Approvals Approvals.Verify(result); } } diff --git a/TheatricalPlayersRefactoringKata.Tests/TheatricalPlayersRefactoringKata.Tests.csproj b/TheatricalPlayersRefactoringKata.Tests/TheatricalPlayersRefactoringKata.Tests.csproj index bb04c740..543df513 100644 --- a/TheatricalPlayersRefactoringKata.Tests/TheatricalPlayersRefactoringKata.Tests.csproj +++ b/TheatricalPlayersRefactoringKata.Tests/TheatricalPlayersRefactoringKata.Tests.csproj @@ -1,7 +1,7 @@ - net6.0 + net8.0 false @@ -17,7 +17,7 @@ - + diff --git a/TheatricalPlayersRefactoringKata.sln b/TheatricalPlayersRefactoringKata.sln index 6fbf02f4..4af7c751 100644 --- a/TheatricalPlayersRefactoringKata.sln +++ b/TheatricalPlayersRefactoringKata.sln @@ -1,28 +1,49 @@  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.35303.130 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.Tests", "TheatricalPlayersRefactoringKata.Tests\TheatricalPlayersRefactoringKata.Tests.csproj", "{2857C714-9035-4532-BD37-B54160CEACFA}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TheatricalPlayersRefactoringKata.Tests", "TheatricalPlayersRefactoringKata.Tests\TheatricalPlayersRefactoringKata.Tests.csproj", "{2AFBF20D-3D97-46EC-B506-8B7C82598951}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TheatricalPlayersRefactoringKata.Application", "TheatricalPlayersRefactoringKata.Application\TheatricalPlayersRefactoringKata.Application.csproj", "{C7D0DF27-6D90-4175-86F8-43A69731D4C1}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TheatricalPlayersRefactoringKata.Domain", "TheatricalPlayersRefactoringKata.Domain\TheatricalPlayersRefactoringKata.Domain.csproj", "{D80EE90D-E93E-48E2-98C5-21FC28CF1282}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TheatricalPlayersRefactoringKata.Infrastructure", "TheatricalPlayersRefactoringKata.Infrastructure\TheatricalPlayersRefactoringKata.Infrastructure.csproj", "{C52440B0-1F52-4FC3-8D39-F536784AA10D}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TheatricalPlayersRefactoringKata.Presentation", "TheatricalPlayersRefactoringKata.Presentation\TheatricalPlayersRefactoringKata.Presentation.csproj", "{2EDDF718-9889-4C01-8565-6A527DEDD8E0}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU Release|Any CPU = Release|Any CPU EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {2857C714-9035-4532-BD37-B54160CEACFA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2857C714-9035-4532-BD37-B54160CEACFA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2857C714-9035-4532-BD37-B54160CEACFA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2857C714-9035-4532-BD37-B54160CEACFA}.Release|Any CPU.Build.0 = Release|Any CPU + {C7D0DF27-6D90-4175-86F8-43A69731D4C1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C7D0DF27-6D90-4175-86F8-43A69731D4C1}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C7D0DF27-6D90-4175-86F8-43A69731D4C1}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C7D0DF27-6D90-4175-86F8-43A69731D4C1}.Release|Any CPU.Build.0 = Release|Any CPU + {D80EE90D-E93E-48E2-98C5-21FC28CF1282}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D80EE90D-E93E-48E2-98C5-21FC28CF1282}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D80EE90D-E93E-48E2-98C5-21FC28CF1282}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D80EE90D-E93E-48E2-98C5-21FC28CF1282}.Release|Any CPU.Build.0 = Release|Any CPU + {C52440B0-1F52-4FC3-8D39-F536784AA10D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C52440B0-1F52-4FC3-8D39-F536784AA10D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C52440B0-1F52-4FC3-8D39-F536784AA10D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C52440B0-1F52-4FC3-8D39-F536784AA10D}.Release|Any CPU.Build.0 = Release|Any CPU + {2EDDF718-9889-4C01-8565-6A527DEDD8E0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2EDDF718-9889-4C01-8565-6A527DEDD8E0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2EDDF718-9889-4C01-8565-6A527DEDD8E0}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2EDDF718-9889-4C01-8565-6A527DEDD8E0}.Release|Any CPU.Build.0 = 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 - {6D24051E-3E14-4B98-A8B3-DFDA7E4ACD7B}.Release|Any CPU.ActiveCfg = Release|Any CPU - {6D24051E-3E14-4B98-A8B3-DFDA7E4ACD7B}.Release|Any CPU.Build.0 = Release|Any CPU - {2AFBF20D-3D97-46EC-B506-8B7C82598951}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {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 + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {194835C3-095C-48D3-85A7-DCD337D24005} EndGlobalSection EndGlobal diff --git a/TheatricalPlayersRefactoringKata/Application/Interfaces/IGenreCalculator.cs b/TheatricalPlayersRefactoringKata/Application/Interfaces/IGenreCalculator.cs new file mode 100644 index 00000000..c5289099 --- /dev/null +++ b/TheatricalPlayersRefactoringKata/Application/Interfaces/IGenreCalculator.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TheatricalPlayersRefactoringKata.Domain.Entities; + +namespace TheatricalPlayersRefactoringKata.Application.Interfaces +{ + public interface IGenreCalculator + { + decimal CalculateAmount(Performance perf, Play play); + int CalculateVolumeCredits(Performance perf); + } +} diff --git a/TheatricalPlayersRefactoringKata/Application/Interfaces/IStatementPrinter.cs b/TheatricalPlayersRefactoringKata/Application/Interfaces/IStatementPrinter.cs new file mode 100644 index 00000000..4b542f2e --- /dev/null +++ b/TheatricalPlayersRefactoringKata/Application/Interfaces/IStatementPrinter.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TheatricalPlayersRefactoringKata.Domain.Entities; + +namespace TheatricalPlayersRefactoringKata.Application.Interfaces +{ + public interface IStatementPrinter + { + string Print(Invoice invoice, Dictionary plays); + } +} diff --git a/TheatricalPlayersRefactoringKata/Application/Services/Calculators/ComedyCalculator.cs b/TheatricalPlayersRefactoringKata/Application/Services/Calculators/ComedyCalculator.cs new file mode 100644 index 00000000..8bbe22d1 --- /dev/null +++ b/TheatricalPlayersRefactoringKata/Application/Services/Calculators/ComedyCalculator.cs @@ -0,0 +1,31 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TheatricalPlayersRefactoringKata.Application.Interfaces; +using TheatricalPlayersRefactoringKata.Domain.Entities; + +namespace TheatricalPlayersRefactoringKata.Application.Services.Calculators +{ + public class ComedyCalculator : IGenreCalculator + { + public decimal CalculateAmount(Performance perf, Play play) + { + decimal thisAmount = 30000; + if (perf.Audience > 20) + { + thisAmount += 10000 + 500 * (perf.Audience - 20); + } + thisAmount += 300 * perf.Audience; + return thisAmount; + } + + public int CalculateVolumeCredits(Performance perf) + { + int volumeCredits = Math.Max(perf.Audience - 30, 0); + volumeCredits += (int)Math.Floor((decimal)perf.Audience / 5); // Extra credits for comedy + return volumeCredits; + } + } +} diff --git a/TheatricalPlayersRefactoringKata/Application/Services/Calculators/HistoryCalculator.cs b/TheatricalPlayersRefactoringKata/Application/Services/Calculators/HistoryCalculator.cs new file mode 100644 index 00000000..ead0c77f --- /dev/null +++ b/TheatricalPlayersRefactoringKata/Application/Services/Calculators/HistoryCalculator.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TheatricalPlayersRefactoringKata.Application.Interfaces; +using TheatricalPlayersRefactoringKata.Domain.Entities; + +namespace TheatricalPlayersRefactoringKata.Application.Services.Calculators +{ + public class HistoryCalculator : IGenreCalculator + { + public decimal CalculateAmount(Performance perf, Play play) + { + decimal thisAmount = 50000; // Base amount for history plays + if (perf.Audience > 25) + { + thisAmount += 1500 * (perf.Audience - 25); + } + return thisAmount; + } + + public int CalculateVolumeCredits(Performance perf) + { + return Math.Max(perf.Audience - 25, 0); // Different credit rule for history + } + } +} diff --git a/TheatricalPlayersRefactoringKata/Application/Services/Calculators/TragedyCalculator.cs b/TheatricalPlayersRefactoringKata/Application/Services/Calculators/TragedyCalculator.cs new file mode 100644 index 00000000..174e48c3 --- /dev/null +++ b/TheatricalPlayersRefactoringKata/Application/Services/Calculators/TragedyCalculator.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TheatricalPlayersRefactoringKata.Application.Interfaces; +using TheatricalPlayersRefactoringKata.Domain.Entities; + +namespace TheatricalPlayersRefactoringKata.Application.Services.Calculators +{ + public class TragedyCalculator : IGenreCalculator + { + public decimal CalculateAmount(Performance perf, Play play) + { + decimal thisAmount = 40000; + if (perf.Audience > 30) + { + thisAmount += 1000 * (perf.Audience - 30); + } + return thisAmount; + } + + public int CalculateVolumeCredits(Performance perf) + { + return Math.Max(perf.Audience - 30, 0); + } + } +} diff --git a/TheatricalPlayersRefactoringKata/Application/Services/Factories/GenreCalculatorFactory.cs b/TheatricalPlayersRefactoringKata/Application/Services/Factories/GenreCalculatorFactory.cs new file mode 100644 index 00000000..f7b72143 --- /dev/null +++ b/TheatricalPlayersRefactoringKata/Application/Services/Factories/GenreCalculatorFactory.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TheatricalPlayersRefactoringKata.Application.Interfaces; +using TheatricalPlayersRefactoringKata.Application.Services.Calculators; + +namespace TheatricalPlayersRefactoringKata.Application.Services.Factories +{ + public class GenreCalculatorFactory + { + public static IGenreCalculator GetCalculator(string genreType) + { + return genreType switch + { + "tragedy" => new TragedyCalculator(), + "comedy" => new ComedyCalculator(), + "history" => new HistoryCalculator(), + _ => throw new Exception("unknown genre: " + genreType), + }; + } + } +} diff --git a/TheatricalPlayersRefactoringKata/Application/Services/Factories/StatementPrinterFactory.cs b/TheatricalPlayersRefactoringKata/Application/Services/Factories/StatementPrinterFactory.cs new file mode 100644 index 00000000..545be067 --- /dev/null +++ b/TheatricalPlayersRefactoringKata/Application/Services/Factories/StatementPrinterFactory.cs @@ -0,0 +1,23 @@ +using System; +using TheatricalPlayersRefactoringKata.Application.Interfaces; +using TheatricalPlayersRefactoringKata.Application.Services.Printers; + +namespace TheatricalPlayersRefactoringKata.Application.Services.Factories +{ + public static class StatementPrinterFactory + { + public static IStatementPrinter GetPrinter(string format) + { + switch (format.ToLower()) + { + case "text": + return new TextStatementPrinter(); + case "xml": + return new XmlStatementPrinter(); + // Adicione mais casos conforme necessário para novos formatos + default: + throw new ArgumentException("Invalid format", nameof(format)); + } + } + } +} diff --git a/TheatricalPlayersRefactoringKata/Application/Services/Printers/TextStatementPrinter.cs b/TheatricalPlayersRefactoringKata/Application/Services/Printers/TextStatementPrinter.cs new file mode 100644 index 00000000..562f5301 --- /dev/null +++ b/TheatricalPlayersRefactoringKata/Application/Services/Printers/TextStatementPrinter.cs @@ -0,0 +1,43 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TheatricalPlayersRefactoringKata.Application.Interfaces; +using TheatricalPlayersRefactoringKata.Application.Services.Factories; +using TheatricalPlayersRefactoringKata.Domain.Entities; + +namespace TheatricalPlayersRefactoringKata.Application.Services.Printers +{ + public class TextStatementPrinter : IStatementPrinter + { + public string Print(Invoice invoice, Dictionary plays) + { + var totalAmount = 0m; + var volumeCredits = 0; + var result = string.Format("Statement for {0}\n", invoice.Customer); + CultureInfo cultureInfo = new CultureInfo("en-US"); + + foreach (var perf in invoice.Performances) + { + var play = plays[perf.PlayId]; + var calculator = GenreCalculatorFactory.GetCalculator(play.Type); + + var thisAmount = calculator.CalculateAmount(perf, play); + totalAmount += thisAmount; + + // Add volume credits + volumeCredits += calculator.CalculateVolumeCredits(perf); + + // Print line for this order + result += string.Format(cultureInfo, " {0}: {1:C} ({2} seats)\n", play.Name, Convert.ToDecimal(thisAmount / 100), perf.Audience); + } + + result += string.Format(cultureInfo, "Amount owed is {0:C}\n", Convert.ToDecimal(totalAmount / 100)); + result += string.Format("You earned {0} credits\n", volumeCredits); + + return result; + } + } +} diff --git a/TheatricalPlayersRefactoringKata/Application/Services/Printers/XmlStatementPrinter.cs b/TheatricalPlayersRefactoringKata/Application/Services/Printers/XmlStatementPrinter.cs new file mode 100644 index 00000000..96246d83 --- /dev/null +++ b/TheatricalPlayersRefactoringKata/Application/Services/Printers/XmlStatementPrinter.cs @@ -0,0 +1,53 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using TheatricalPlayersRefactoringKata.Application.Interfaces; +using TheatricalPlayersRefactoringKata.Application.Services.Factories; +using TheatricalPlayersRefactoringKata.Domain.Entities; + +namespace TheatricalPlayersRefactoringKata.Application.Services.Printers +{ + public class XmlStatementPrinter : IStatementPrinter + { + public string Print(Invoice invoice, Dictionary plays) + { + var totalAmount = 0m; + var totalVolumeCredits = 0; + var xml = new StringBuilder(); + + xml.AppendLine(""); + xml.AppendLine(""); + xml.AppendLine($" {invoice.Customer}"); + xml.AppendLine(" "); + + foreach (var perf in invoice.Performances) + { + var play = plays[perf.PlayId]; + var calculator = GenreCalculatorFactory.GetCalculator(play.Type); + + // Calcula o valor devido e os créditos ganhados para cada performance + var thisAmount = calculator.CalculateAmount(perf, play); + var volumeCredits = calculator.CalculateVolumeCredits(perf); + + totalAmount += thisAmount; + totalVolumeCredits += volumeCredits; + + // Gera o XML para cada item + xml.AppendLine(" "); + xml.AppendLine($" {thisAmount / 100}"); + xml.AppendLine($" {volumeCredits}"); + xml.AppendLine($" {perf.Audience}"); + xml.AppendLine(" "); + } + + xml.AppendLine(" "); + xml.AppendLine($" {totalAmount / 100}"); + xml.AppendLine($" {totalVolumeCredits}"); + xml.Append(""); + + return xml.ToString(); + } + } +} diff --git a/TheatricalPlayersRefactoringKata/Domain/Entities/Invoice.cs b/TheatricalPlayersRefactoringKata/Domain/Entities/Invoice.cs new file mode 100644 index 00000000..d6c57867 --- /dev/null +++ b/TheatricalPlayersRefactoringKata/Domain/Entities/Invoice.cs @@ -0,0 +1,19 @@ +using System.Collections.Generic; + +namespace TheatricalPlayersRefactoringKata.Domain.Entities; + +public class Invoice +{ + private string _customer; + private List _performances; + + public string Customer { get => _customer; set => _customer = value; } + public List Performances { get => _performances; set => _performances = value; } + + public Invoice(string customer, List performance) + { + _customer = customer; + _performances = performance; + } + +} diff --git a/TheatricalPlayersRefactoringKata/Domain/Entities/Performance.cs b/TheatricalPlayersRefactoringKata/Domain/Entities/Performance.cs new file mode 100644 index 00000000..6a0eb08e --- /dev/null +++ b/TheatricalPlayersRefactoringKata/Domain/Entities/Performance.cs @@ -0,0 +1,17 @@ +namespace TheatricalPlayersRefactoringKata.Domain.Entities; + +public class Performance +{ + private string _playId; + private int _audience; + + public string PlayId { get => _playId; set => _playId = value; } + public int Audience { get => _audience; set => _audience = value; } + + public Performance(string playID, int audience) + { + _playId = playID; + _audience = audience; + } + +} diff --git a/TheatricalPlayersRefactoringKata/Domain/Entities/Play.cs b/TheatricalPlayersRefactoringKata/Domain/Entities/Play.cs new file mode 100644 index 00000000..4c3cae77 --- /dev/null +++ b/TheatricalPlayersRefactoringKata/Domain/Entities/Play.cs @@ -0,0 +1,19 @@ +namespace TheatricalPlayersRefactoringKata.Domain.Entities; + +public class Play +{ + private string _name; + private int _lines; + private string _type; + + public string Name { get => _name; set => _name = value; } + public int Lines { get => _lines; set => _lines = value; } + public string Type { get => _type; set => _type = value; } + + public Play(string name, int lines, string type) + { + _name = name; + _lines = lines; + _type = type; + } +} diff --git a/TheatricalPlayersRefactoringKata/StatementPrinter.cs b/TheatricalPlayersRefactoringKata/StatementPrinter.cs deleted file mode 100644 index 70309bee..00000000 --- a/TheatricalPlayersRefactoringKata/StatementPrinter.cs +++ /dev/null @@ -1,52 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Globalization; - -namespace TheatricalPlayersRefactoringKata; - -public class StatementPrinter -{ - public string Print(Invoice invoice, Dictionary plays) - { - var totalAmount = 0; - var volumeCredits = 0; - var result = string.Format("Statement for {0}\n", invoice.Customer); - CultureInfo cultureInfo = new CultureInfo("en-US"); - - foreach(var perf in invoice.Performances) - { - var play = plays[perf.PlayId]; - var lines = play.Lines; - if (lines < 1000) lines = 1000; - if (lines > 4000) lines = 4000; - var thisAmount = lines * 10; - switch (play.Type) - { - case "tragedy": - if (perf.Audience > 30) { - thisAmount += 1000 * (perf.Audience - 30); - } - break; - case "comedy": - if (perf.Audience > 20) { - thisAmount += 10000 + 500 * (perf.Audience - 20); - } - thisAmount += 300 * perf.Audience; - break; - default: - throw new Exception("unknown type: " + play.Type); - } - // add volume credits - volumeCredits += Math.Max(perf.Audience - 30, 0); - // add extra credit for every ten comedy attendees - if ("comedy" == play.Type) volumeCredits += (int)Math.Floor((decimal)perf.Audience / 5); - - // print line for this order - result += String.Format(cultureInfo, " {0}: {1:C} ({2} seats)\n", play.Name, Convert.ToDecimal(thisAmount / 100), perf.Audience); - totalAmount += thisAmount; - } - result += String.Format(cultureInfo, "Amount owed is {0:C}\n", Convert.ToDecimal(totalAmount / 100)); - result += String.Format("You earned {0} credits\n", volumeCredits); - return result; - } -} diff --git a/TheatricalPlayersRefactoringKata/TheatricalPlayersRefactoringKata.csproj b/TheatricalPlayersRefactoringKata/TheatricalPlayersRefactoringKata.csproj index 3d59dcc5..9716c365 100644 --- a/TheatricalPlayersRefactoringKata/TheatricalPlayersRefactoringKata.csproj +++ b/TheatricalPlayersRefactoringKata/TheatricalPlayersRefactoringKata.csproj @@ -1,7 +1,14 @@ - + net6.0 + + + + + + + From 93b87f636c108ab1c703f60dd7372cec427d16a9 Mon Sep 17 00:00:00 2001 From: lucasvil4r Date: Mon, 16 Sep 2024 18:14:11 -0300 Subject: [PATCH 02/12] bugfix: corrigindo as dependencias das camadas Application e Tests --- .../Services/Calculators/HistoryCalculator.cs | 3 +- .../Services/Calculators/TragedyCalculator.cs | 7 +-- .../Factories/GenreCalculatorFactory.cs | 7 +-- .../Factories/StatementPrinterFactory.cs | 3 +- .../Services/Printers/TextStatementPrinter.cs | 1 + .../Services/Printers/XmlStatementPrinter.cs | 6 +-- .../StatementPrinterTests.cs | 1 - ...atricalPlayersRefactoringKata.Tests.csproj | 1 + .../Interfaces/IGenreCalculator.cs | 15 ------ .../Interfaces/IStatementPrinter.cs | 14 ----- .../Services/Calculators/ComedyCalculator.cs | 31 ----------- .../Services/Calculators/HistoryCalculator.cs | 28 ---------- .../Services/Calculators/TragedyCalculator.cs | 28 ---------- .../Factories/GenreCalculatorFactory.cs | 24 --------- .../Factories/StatementPrinterFactory.cs | 23 -------- .../Services/Printers/TextStatementPrinter.cs | 43 --------------- .../Services/Printers/XmlStatementPrinter.cs | 53 ------------------- .../Domain/Entities/Invoice.cs | 19 ------- .../Domain/Entities/Performance.cs | 17 ------ .../Domain/Entities/Play.cs | 19 ------- .../TheatricalPlayersRefactoringKata.csproj | 14 ----- 21 files changed, 8 insertions(+), 349 deletions(-) delete mode 100644 TheatricalPlayersRefactoringKata/Application/Interfaces/IGenreCalculator.cs delete mode 100644 TheatricalPlayersRefactoringKata/Application/Interfaces/IStatementPrinter.cs delete mode 100644 TheatricalPlayersRefactoringKata/Application/Services/Calculators/ComedyCalculator.cs delete mode 100644 TheatricalPlayersRefactoringKata/Application/Services/Calculators/HistoryCalculator.cs delete mode 100644 TheatricalPlayersRefactoringKata/Application/Services/Calculators/TragedyCalculator.cs delete mode 100644 TheatricalPlayersRefactoringKata/Application/Services/Factories/GenreCalculatorFactory.cs delete mode 100644 TheatricalPlayersRefactoringKata/Application/Services/Factories/StatementPrinterFactory.cs delete mode 100644 TheatricalPlayersRefactoringKata/Application/Services/Printers/TextStatementPrinter.cs delete mode 100644 TheatricalPlayersRefactoringKata/Application/Services/Printers/XmlStatementPrinter.cs delete mode 100644 TheatricalPlayersRefactoringKata/Domain/Entities/Invoice.cs delete mode 100644 TheatricalPlayersRefactoringKata/Domain/Entities/Performance.cs delete mode 100644 TheatricalPlayersRefactoringKata/Domain/Entities/Play.cs delete mode 100644 TheatricalPlayersRefactoringKata/TheatricalPlayersRefactoringKata.csproj diff --git a/TheatricalPlayersRefactoringKata.Application/Services/Calculators/HistoryCalculator.cs b/TheatricalPlayersRefactoringKata.Application/Services/Calculators/HistoryCalculator.cs index 8b023dd9..222d3f6a 100644 --- a/TheatricalPlayersRefactoringKata.Application/Services/Calculators/HistoryCalculator.cs +++ b/TheatricalPlayersRefactoringKata.Application/Services/Calculators/HistoryCalculator.cs @@ -1,4 +1,5 @@ -using System; +using TheatricalPlayersRefactoringKata.Application.Interfaces; +using TheatricalPlayersRefactoringKata.Domain.Entities; namespace TheatricalPlayersRefactoringKata.Application.Services.Calculators { diff --git a/TheatricalPlayersRefactoringKata.Application/Services/Calculators/TragedyCalculator.cs b/TheatricalPlayersRefactoringKata.Application/Services/Calculators/TragedyCalculator.cs index 174e48c3..b7067fc6 100644 --- a/TheatricalPlayersRefactoringKata.Application/Services/Calculators/TragedyCalculator.cs +++ b/TheatricalPlayersRefactoringKata.Application/Services/Calculators/TragedyCalculator.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using TheatricalPlayersRefactoringKata.Application.Interfaces; +using TheatricalPlayersRefactoringKata.Application.Interfaces; using TheatricalPlayersRefactoringKata.Domain.Entities; namespace TheatricalPlayersRefactoringKata.Application.Services.Calculators diff --git a/TheatricalPlayersRefactoringKata.Application/Services/Factories/GenreCalculatorFactory.cs b/TheatricalPlayersRefactoringKata.Application/Services/Factories/GenreCalculatorFactory.cs index f7b72143..013d667d 100644 --- a/TheatricalPlayersRefactoringKata.Application/Services/Factories/GenreCalculatorFactory.cs +++ b/TheatricalPlayersRefactoringKata.Application/Services/Factories/GenreCalculatorFactory.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using TheatricalPlayersRefactoringKata.Application.Interfaces; +using TheatricalPlayersRefactoringKata.Application.Interfaces; using TheatricalPlayersRefactoringKata.Application.Services.Calculators; namespace TheatricalPlayersRefactoringKata.Application.Services.Factories diff --git a/TheatricalPlayersRefactoringKata.Application/Services/Factories/StatementPrinterFactory.cs b/TheatricalPlayersRefactoringKata.Application/Services/Factories/StatementPrinterFactory.cs index 545be067..ace3feea 100644 --- a/TheatricalPlayersRefactoringKata.Application/Services/Factories/StatementPrinterFactory.cs +++ b/TheatricalPlayersRefactoringKata.Application/Services/Factories/StatementPrinterFactory.cs @@ -1,5 +1,4 @@ -using System; -using TheatricalPlayersRefactoringKata.Application.Interfaces; +using TheatricalPlayersRefactoringKata.Application.Interfaces; using TheatricalPlayersRefactoringKata.Application.Services.Printers; namespace TheatricalPlayersRefactoringKata.Application.Services.Factories diff --git a/TheatricalPlayersRefactoringKata.Application/Services/Printers/TextStatementPrinter.cs b/TheatricalPlayersRefactoringKata.Application/Services/Printers/TextStatementPrinter.cs index ff6b6930..c4a25c37 100644 --- a/TheatricalPlayersRefactoringKata.Application/Services/Printers/TextStatementPrinter.cs +++ b/TheatricalPlayersRefactoringKata.Application/Services/Printers/TextStatementPrinter.cs @@ -2,6 +2,7 @@ using System.Globalization; using TheatricalPlayersRefactoringKata.Application.Interfaces; using TheatricalPlayersRefactoringKata.Application.Services.Factories; +using TheatricalPlayersRefactoringKata.Domain.Entities; namespace TheatricalPlayersRefactoringKata.Application.Services.Printers { diff --git a/TheatricalPlayersRefactoringKata.Application/Services/Printers/XmlStatementPrinter.cs b/TheatricalPlayersRefactoringKata.Application/Services/Printers/XmlStatementPrinter.cs index 96246d83..b25b8647 100644 --- a/TheatricalPlayersRefactoringKata.Application/Services/Printers/XmlStatementPrinter.cs +++ b/TheatricalPlayersRefactoringKata.Application/Services/Printers/XmlStatementPrinter.cs @@ -1,8 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using System.Text; using TheatricalPlayersRefactoringKata.Application.Interfaces; using TheatricalPlayersRefactoringKata.Application.Services.Factories; using TheatricalPlayersRefactoringKata.Domain.Entities; diff --git a/TheatricalPlayersRefactoringKata.Tests/StatementPrinterTests.cs b/TheatricalPlayersRefactoringKata.Tests/StatementPrinterTests.cs index 0f751558..11f054b3 100644 --- a/TheatricalPlayersRefactoringKata.Tests/StatementPrinterTests.cs +++ b/TheatricalPlayersRefactoringKata.Tests/StatementPrinterTests.cs @@ -1,4 +1,3 @@ -using System; using System.Collections.Generic; using ApprovalTests; using ApprovalTests.Reporters; diff --git a/TheatricalPlayersRefactoringKata.Tests/TheatricalPlayersRefactoringKata.Tests.csproj b/TheatricalPlayersRefactoringKata.Tests/TheatricalPlayersRefactoringKata.Tests.csproj index 543df513..15279642 100644 --- a/TheatricalPlayersRefactoringKata.Tests/TheatricalPlayersRefactoringKata.Tests.csproj +++ b/TheatricalPlayersRefactoringKata.Tests/TheatricalPlayersRefactoringKata.Tests.csproj @@ -18,6 +18,7 @@ + diff --git a/TheatricalPlayersRefactoringKata/Application/Interfaces/IGenreCalculator.cs b/TheatricalPlayersRefactoringKata/Application/Interfaces/IGenreCalculator.cs deleted file mode 100644 index c5289099..00000000 --- a/TheatricalPlayersRefactoringKata/Application/Interfaces/IGenreCalculator.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using TheatricalPlayersRefactoringKata.Domain.Entities; - -namespace TheatricalPlayersRefactoringKata.Application.Interfaces -{ - public interface IGenreCalculator - { - decimal CalculateAmount(Performance perf, Play play); - int CalculateVolumeCredits(Performance perf); - } -} diff --git a/TheatricalPlayersRefactoringKata/Application/Interfaces/IStatementPrinter.cs b/TheatricalPlayersRefactoringKata/Application/Interfaces/IStatementPrinter.cs deleted file mode 100644 index 4b542f2e..00000000 --- a/TheatricalPlayersRefactoringKata/Application/Interfaces/IStatementPrinter.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using TheatricalPlayersRefactoringKata.Domain.Entities; - -namespace TheatricalPlayersRefactoringKata.Application.Interfaces -{ - public interface IStatementPrinter - { - string Print(Invoice invoice, Dictionary plays); - } -} diff --git a/TheatricalPlayersRefactoringKata/Application/Services/Calculators/ComedyCalculator.cs b/TheatricalPlayersRefactoringKata/Application/Services/Calculators/ComedyCalculator.cs deleted file mode 100644 index 8bbe22d1..00000000 --- a/TheatricalPlayersRefactoringKata/Application/Services/Calculators/ComedyCalculator.cs +++ /dev/null @@ -1,31 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using TheatricalPlayersRefactoringKata.Application.Interfaces; -using TheatricalPlayersRefactoringKata.Domain.Entities; - -namespace TheatricalPlayersRefactoringKata.Application.Services.Calculators -{ - public class ComedyCalculator : IGenreCalculator - { - public decimal CalculateAmount(Performance perf, Play play) - { - decimal thisAmount = 30000; - if (perf.Audience > 20) - { - thisAmount += 10000 + 500 * (perf.Audience - 20); - } - thisAmount += 300 * perf.Audience; - return thisAmount; - } - - public int CalculateVolumeCredits(Performance perf) - { - int volumeCredits = Math.Max(perf.Audience - 30, 0); - volumeCredits += (int)Math.Floor((decimal)perf.Audience / 5); // Extra credits for comedy - return volumeCredits; - } - } -} diff --git a/TheatricalPlayersRefactoringKata/Application/Services/Calculators/HistoryCalculator.cs b/TheatricalPlayersRefactoringKata/Application/Services/Calculators/HistoryCalculator.cs deleted file mode 100644 index ead0c77f..00000000 --- a/TheatricalPlayersRefactoringKata/Application/Services/Calculators/HistoryCalculator.cs +++ /dev/null @@ -1,28 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using TheatricalPlayersRefactoringKata.Application.Interfaces; -using TheatricalPlayersRefactoringKata.Domain.Entities; - -namespace TheatricalPlayersRefactoringKata.Application.Services.Calculators -{ - public class HistoryCalculator : IGenreCalculator - { - public decimal CalculateAmount(Performance perf, Play play) - { - decimal thisAmount = 50000; // Base amount for history plays - if (perf.Audience > 25) - { - thisAmount += 1500 * (perf.Audience - 25); - } - return thisAmount; - } - - public int CalculateVolumeCredits(Performance perf) - { - return Math.Max(perf.Audience - 25, 0); // Different credit rule for history - } - } -} diff --git a/TheatricalPlayersRefactoringKata/Application/Services/Calculators/TragedyCalculator.cs b/TheatricalPlayersRefactoringKata/Application/Services/Calculators/TragedyCalculator.cs deleted file mode 100644 index 174e48c3..00000000 --- a/TheatricalPlayersRefactoringKata/Application/Services/Calculators/TragedyCalculator.cs +++ /dev/null @@ -1,28 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using TheatricalPlayersRefactoringKata.Application.Interfaces; -using TheatricalPlayersRefactoringKata.Domain.Entities; - -namespace TheatricalPlayersRefactoringKata.Application.Services.Calculators -{ - public class TragedyCalculator : IGenreCalculator - { - public decimal CalculateAmount(Performance perf, Play play) - { - decimal thisAmount = 40000; - if (perf.Audience > 30) - { - thisAmount += 1000 * (perf.Audience - 30); - } - return thisAmount; - } - - public int CalculateVolumeCredits(Performance perf) - { - return Math.Max(perf.Audience - 30, 0); - } - } -} diff --git a/TheatricalPlayersRefactoringKata/Application/Services/Factories/GenreCalculatorFactory.cs b/TheatricalPlayersRefactoringKata/Application/Services/Factories/GenreCalculatorFactory.cs deleted file mode 100644 index f7b72143..00000000 --- a/TheatricalPlayersRefactoringKata/Application/Services/Factories/GenreCalculatorFactory.cs +++ /dev/null @@ -1,24 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using TheatricalPlayersRefactoringKata.Application.Interfaces; -using TheatricalPlayersRefactoringKata.Application.Services.Calculators; - -namespace TheatricalPlayersRefactoringKata.Application.Services.Factories -{ - public class GenreCalculatorFactory - { - public static IGenreCalculator GetCalculator(string genreType) - { - return genreType switch - { - "tragedy" => new TragedyCalculator(), - "comedy" => new ComedyCalculator(), - "history" => new HistoryCalculator(), - _ => throw new Exception("unknown genre: " + genreType), - }; - } - } -} diff --git a/TheatricalPlayersRefactoringKata/Application/Services/Factories/StatementPrinterFactory.cs b/TheatricalPlayersRefactoringKata/Application/Services/Factories/StatementPrinterFactory.cs deleted file mode 100644 index 545be067..00000000 --- a/TheatricalPlayersRefactoringKata/Application/Services/Factories/StatementPrinterFactory.cs +++ /dev/null @@ -1,23 +0,0 @@ -using System; -using TheatricalPlayersRefactoringKata.Application.Interfaces; -using TheatricalPlayersRefactoringKata.Application.Services.Printers; - -namespace TheatricalPlayersRefactoringKata.Application.Services.Factories -{ - public static class StatementPrinterFactory - { - public static IStatementPrinter GetPrinter(string format) - { - switch (format.ToLower()) - { - case "text": - return new TextStatementPrinter(); - case "xml": - return new XmlStatementPrinter(); - // Adicione mais casos conforme necessário para novos formatos - default: - throw new ArgumentException("Invalid format", nameof(format)); - } - } - } -} diff --git a/TheatricalPlayersRefactoringKata/Application/Services/Printers/TextStatementPrinter.cs b/TheatricalPlayersRefactoringKata/Application/Services/Printers/TextStatementPrinter.cs deleted file mode 100644 index 562f5301..00000000 --- a/TheatricalPlayersRefactoringKata/Application/Services/Printers/TextStatementPrinter.cs +++ /dev/null @@ -1,43 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Globalization; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using TheatricalPlayersRefactoringKata.Application.Interfaces; -using TheatricalPlayersRefactoringKata.Application.Services.Factories; -using TheatricalPlayersRefactoringKata.Domain.Entities; - -namespace TheatricalPlayersRefactoringKata.Application.Services.Printers -{ - public class TextStatementPrinter : IStatementPrinter - { - public string Print(Invoice invoice, Dictionary plays) - { - var totalAmount = 0m; - var volumeCredits = 0; - var result = string.Format("Statement for {0}\n", invoice.Customer); - CultureInfo cultureInfo = new CultureInfo("en-US"); - - foreach (var perf in invoice.Performances) - { - var play = plays[perf.PlayId]; - var calculator = GenreCalculatorFactory.GetCalculator(play.Type); - - var thisAmount = calculator.CalculateAmount(perf, play); - totalAmount += thisAmount; - - // Add volume credits - volumeCredits += calculator.CalculateVolumeCredits(perf); - - // Print line for this order - result += string.Format(cultureInfo, " {0}: {1:C} ({2} seats)\n", play.Name, Convert.ToDecimal(thisAmount / 100), perf.Audience); - } - - result += string.Format(cultureInfo, "Amount owed is {0:C}\n", Convert.ToDecimal(totalAmount / 100)); - result += string.Format("You earned {0} credits\n", volumeCredits); - - return result; - } - } -} diff --git a/TheatricalPlayersRefactoringKata/Application/Services/Printers/XmlStatementPrinter.cs b/TheatricalPlayersRefactoringKata/Application/Services/Printers/XmlStatementPrinter.cs deleted file mode 100644 index 96246d83..00000000 --- a/TheatricalPlayersRefactoringKata/Application/Services/Printers/XmlStatementPrinter.cs +++ /dev/null @@ -1,53 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using TheatricalPlayersRefactoringKata.Application.Interfaces; -using TheatricalPlayersRefactoringKata.Application.Services.Factories; -using TheatricalPlayersRefactoringKata.Domain.Entities; - -namespace TheatricalPlayersRefactoringKata.Application.Services.Printers -{ - public class XmlStatementPrinter : IStatementPrinter - { - public string Print(Invoice invoice, Dictionary plays) - { - var totalAmount = 0m; - var totalVolumeCredits = 0; - var xml = new StringBuilder(); - - xml.AppendLine(""); - xml.AppendLine(""); - xml.AppendLine($" {invoice.Customer}"); - xml.AppendLine(" "); - - foreach (var perf in invoice.Performances) - { - var play = plays[perf.PlayId]; - var calculator = GenreCalculatorFactory.GetCalculator(play.Type); - - // Calcula o valor devido e os créditos ganhados para cada performance - var thisAmount = calculator.CalculateAmount(perf, play); - var volumeCredits = calculator.CalculateVolumeCredits(perf); - - totalAmount += thisAmount; - totalVolumeCredits += volumeCredits; - - // Gera o XML para cada item - xml.AppendLine(" "); - xml.AppendLine($" {thisAmount / 100}"); - xml.AppendLine($" {volumeCredits}"); - xml.AppendLine($" {perf.Audience}"); - xml.AppendLine(" "); - } - - xml.AppendLine(" "); - xml.AppendLine($" {totalAmount / 100}"); - xml.AppendLine($" {totalVolumeCredits}"); - xml.Append(""); - - return xml.ToString(); - } - } -} diff --git a/TheatricalPlayersRefactoringKata/Domain/Entities/Invoice.cs b/TheatricalPlayersRefactoringKata/Domain/Entities/Invoice.cs deleted file mode 100644 index d6c57867..00000000 --- a/TheatricalPlayersRefactoringKata/Domain/Entities/Invoice.cs +++ /dev/null @@ -1,19 +0,0 @@ -using System.Collections.Generic; - -namespace TheatricalPlayersRefactoringKata.Domain.Entities; - -public class Invoice -{ - private string _customer; - private List _performances; - - public string Customer { get => _customer; set => _customer = value; } - public List Performances { get => _performances; set => _performances = value; } - - public Invoice(string customer, List performance) - { - _customer = customer; - _performances = performance; - } - -} diff --git a/TheatricalPlayersRefactoringKata/Domain/Entities/Performance.cs b/TheatricalPlayersRefactoringKata/Domain/Entities/Performance.cs deleted file mode 100644 index 6a0eb08e..00000000 --- a/TheatricalPlayersRefactoringKata/Domain/Entities/Performance.cs +++ /dev/null @@ -1,17 +0,0 @@ -namespace TheatricalPlayersRefactoringKata.Domain.Entities; - -public class Performance -{ - private string _playId; - private int _audience; - - public string PlayId { get => _playId; set => _playId = value; } - public int Audience { get => _audience; set => _audience = value; } - - public Performance(string playID, int audience) - { - _playId = playID; - _audience = audience; - } - -} diff --git a/TheatricalPlayersRefactoringKata/Domain/Entities/Play.cs b/TheatricalPlayersRefactoringKata/Domain/Entities/Play.cs deleted file mode 100644 index 4c3cae77..00000000 --- a/TheatricalPlayersRefactoringKata/Domain/Entities/Play.cs +++ /dev/null @@ -1,19 +0,0 @@ -namespace TheatricalPlayersRefactoringKata.Domain.Entities; - -public class Play -{ - private string _name; - private int _lines; - private string _type; - - public string Name { get => _name; set => _name = value; } - public int Lines { get => _lines; set => _lines = value; } - public string Type { get => _type; set => _type = value; } - - public Play(string name, int lines, string type) - { - _name = name; - _lines = lines; - _type = type; - } -} diff --git a/TheatricalPlayersRefactoringKata/TheatricalPlayersRefactoringKata.csproj b/TheatricalPlayersRefactoringKata/TheatricalPlayersRefactoringKata.csproj deleted file mode 100644 index 9716c365..00000000 --- a/TheatricalPlayersRefactoringKata/TheatricalPlayersRefactoringKata.csproj +++ /dev/null @@ -1,14 +0,0 @@ - - - - net6.0 - - - - - - - - - - From 87ef09c7af10f9d256252b3f6a3fd333a045fc02 Mon Sep 17 00:00:00 2001 From: lucasvil4r Date: Mon, 16 Sep 2024 21:57:36 -0300 Subject: [PATCH 03/12] =?UTF-8?q?bugfix:=20criando=20logica=20correta=20pa?= =?UTF-8?q?ra=20calcular=20as=20pe=C3=A7as=20hist=C3=B3ricas?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Interfaces/IGenreCalculator.cs | 7 +------ .../Services/Calculators/ComedyCalculator.cs | 6 +++++- .../Services/Calculators/HistoryCalculator.cs | 19 +++++++++++-------- .../Services/Calculators/TragedyCalculator.cs | 6 +++++- .../Services/Printers/TextStatementPrinter.cs | 2 -- .../Services/Printers/XmlStatementPrinter.cs | 9 ++++----- 6 files changed, 26 insertions(+), 23 deletions(-) diff --git a/TheatricalPlayersRefactoringKata.Application/Interfaces/IGenreCalculator.cs b/TheatricalPlayersRefactoringKata.Application/Interfaces/IGenreCalculator.cs index c5289099..3268f530 100644 --- a/TheatricalPlayersRefactoringKata.Application/Interfaces/IGenreCalculator.cs +++ b/TheatricalPlayersRefactoringKata.Application/Interfaces/IGenreCalculator.cs @@ -1,9 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using TheatricalPlayersRefactoringKata.Domain.Entities; +using TheatricalPlayersRefactoringKata.Domain.Entities; namespace TheatricalPlayersRefactoringKata.Application.Interfaces { diff --git a/TheatricalPlayersRefactoringKata.Application/Services/Calculators/ComedyCalculator.cs b/TheatricalPlayersRefactoringKata.Application/Services/Calculators/ComedyCalculator.cs index 536b98cc..df2c987c 100644 --- a/TheatricalPlayersRefactoringKata.Application/Services/Calculators/ComedyCalculator.cs +++ b/TheatricalPlayersRefactoringKata.Application/Services/Calculators/ComedyCalculator.cs @@ -7,7 +7,11 @@ public class ComedyCalculator : IGenreCalculator { public decimal CalculateAmount(Performance perf, Play play) { - decimal thisAmount = 30000; + var lines = play.Lines; + if (lines < 1000) lines = 1000; + if (lines > 4000) lines = 4000; + var thisAmount = lines * 10; + if (perf.Audience > 20) { thisAmount += 10000 + 500 * (perf.Audience - 20); diff --git a/TheatricalPlayersRefactoringKata.Application/Services/Calculators/HistoryCalculator.cs b/TheatricalPlayersRefactoringKata.Application/Services/Calculators/HistoryCalculator.cs index 222d3f6a..7f5d80ed 100644 --- a/TheatricalPlayersRefactoringKata.Application/Services/Calculators/HistoryCalculator.cs +++ b/TheatricalPlayersRefactoringKata.Application/Services/Calculators/HistoryCalculator.cs @@ -5,19 +5,22 @@ namespace TheatricalPlayersRefactoringKata.Application.Services.Calculators { public class HistoryCalculator : IGenreCalculator { - public decimal CalculateAmount(Performance perf, Play play) + private readonly TragedyCalculator _tragedyCalculator; + private readonly ComedyCalculator _comedyCalculator; + + public HistoryCalculator() { - decimal thisAmount = 50000; // Base amount for history plays - if (perf.Audience > 25) - { - thisAmount += 1500 * (perf.Audience - 25); - } - return thisAmount; + _tragedyCalculator = new TragedyCalculator(); + _comedyCalculator = new ComedyCalculator(); } + public decimal CalculateAmount(Performance perf, Play play) + { + return _tragedyCalculator.CalculateAmount(perf, play) + _comedyCalculator.CalculateAmount(perf, play); + } public int CalculateVolumeCredits(Performance perf) { - return Math.Max(perf.Audience - 25, 0); // Different credit rule for history + return Math.Max(perf.Audience - 30, 0); } } } diff --git a/TheatricalPlayersRefactoringKata.Application/Services/Calculators/TragedyCalculator.cs b/TheatricalPlayersRefactoringKata.Application/Services/Calculators/TragedyCalculator.cs index b7067fc6..817fe61c 100644 --- a/TheatricalPlayersRefactoringKata.Application/Services/Calculators/TragedyCalculator.cs +++ b/TheatricalPlayersRefactoringKata.Application/Services/Calculators/TragedyCalculator.cs @@ -7,7 +7,11 @@ public class TragedyCalculator : IGenreCalculator { public decimal CalculateAmount(Performance perf, Play play) { - decimal thisAmount = 40000; + var lines = play.Lines; + if (lines < 1000) lines = 1000; + if (lines > 4000) lines = 4000; + var thisAmount = lines * 10; + if (perf.Audience > 30) { thisAmount += 1000 * (perf.Audience - 30); diff --git a/TheatricalPlayersRefactoringKata.Application/Services/Printers/TextStatementPrinter.cs b/TheatricalPlayersRefactoringKata.Application/Services/Printers/TextStatementPrinter.cs index c4a25c37..89192f9c 100644 --- a/TheatricalPlayersRefactoringKata.Application/Services/Printers/TextStatementPrinter.cs +++ b/TheatricalPlayersRefactoringKata.Application/Services/Printers/TextStatementPrinter.cs @@ -23,10 +23,8 @@ public string Print(Invoice invoice, Dictionary plays) var thisAmount = calculator.CalculateAmount(perf, play); totalAmount += thisAmount; - // Add volume credits volumeCredits += calculator.CalculateVolumeCredits(perf); - // Print line for this order result += string.Format(cultureInfo, " {0}: {1:C} ({2} seats)\n", play.Name, Convert.ToDecimal(thisAmount / 100), perf.Audience); } diff --git a/TheatricalPlayersRefactoringKata.Application/Services/Printers/XmlStatementPrinter.cs b/TheatricalPlayersRefactoringKata.Application/Services/Printers/XmlStatementPrinter.cs index b25b8647..9e429f8e 100644 --- a/TheatricalPlayersRefactoringKata.Application/Services/Printers/XmlStatementPrinter.cs +++ b/TheatricalPlayersRefactoringKata.Application/Services/Printers/XmlStatementPrinter.cs @@ -1,4 +1,5 @@ -using System.Text; +using System.Globalization; +using System.Text; using TheatricalPlayersRefactoringKata.Application.Interfaces; using TheatricalPlayersRefactoringKata.Application.Services.Factories; using TheatricalPlayersRefactoringKata.Domain.Entities; @@ -23,23 +24,21 @@ public string Print(Invoice invoice, Dictionary plays) var play = plays[perf.PlayId]; var calculator = GenreCalculatorFactory.GetCalculator(play.Type); - // Calcula o valor devido e os créditos ganhados para cada performance var thisAmount = calculator.CalculateAmount(perf, play); var volumeCredits = calculator.CalculateVolumeCredits(perf); totalAmount += thisAmount; totalVolumeCredits += volumeCredits; - // Gera o XML para cada item xml.AppendLine(" "); - xml.AppendLine($" {thisAmount / 100}"); + xml.AppendLine($" {(thisAmount / 100).ToString(new CultureInfo("en-US"))}"); xml.AppendLine($" {volumeCredits}"); xml.AppendLine($" {perf.Audience}"); xml.AppendLine(" "); } xml.AppendLine(" "); - xml.AppendLine($" {totalAmount / 100}"); + xml.AppendLine($" {(totalAmount / 100).ToString(new CultureInfo("en-US"))}"); xml.AppendLine($" {totalVolumeCredits}"); xml.Append(""); From 5117ffae337f98858319efe5d0b0c7660549ed18 Mon Sep 17 00:00:00 2001 From: lucasvil4r Date: Mon, 16 Sep 2024 21:58:59 -0300 Subject: [PATCH 04/12] =?UTF-8?q?refactor:=20limpando=20c=C3=B3digo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TheatricalPlayersRefactoringKata.Domain/Entities/Invoice.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/TheatricalPlayersRefactoringKata.Domain/Entities/Invoice.cs b/TheatricalPlayersRefactoringKata.Domain/Entities/Invoice.cs index d6c57867..40156ffb 100644 --- a/TheatricalPlayersRefactoringKata.Domain/Entities/Invoice.cs +++ b/TheatricalPlayersRefactoringKata.Domain/Entities/Invoice.cs @@ -1,5 +1,3 @@ -using System.Collections.Generic; - namespace TheatricalPlayersRefactoringKata.Domain.Entities; public class Invoice From 6df723ec006c1bfa52e3e029764acd5f23c21618 Mon Sep 17 00:00:00 2001 From: lucasvil4r Date: Mon, 16 Sep 2024 22:00:03 -0300 Subject: [PATCH 05/12] =?UTF-8?q?test:=20reecriando=20os=20testes=20com=20?= =?UTF-8?q?a=20mesma=20l=C3=B3gica=20inicial,=20por=C3=A9m=20com=20os=20no?= =?UTF-8?q?vos=20m=C3=A9todos=20adicionados?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../StatementPrinterTests.cs | 43 ++----------------- 1 file changed, 4 insertions(+), 39 deletions(-) diff --git a/TheatricalPlayersRefactoringKata.Tests/StatementPrinterTests.cs b/TheatricalPlayersRefactoringKata.Tests/StatementPrinterTests.cs index 11f054b3..0d35fd41 100644 --- a/TheatricalPlayersRefactoringKata.Tests/StatementPrinterTests.cs +++ b/TheatricalPlayersRefactoringKata.Tests/StatementPrinterTests.cs @@ -9,7 +9,6 @@ namespace TheatricalPlayersRefactoringKata.Tests; public class StatementPrinterTests { - /* #deprecated [Fact] [UseReporter(typeof(DiffReporter))] public void TestStatementExampleLegacy() @@ -29,45 +28,12 @@ public void TestStatementExampleLegacy() } ); - //StatementPrinter statementPrinter = new StatementPrinter(); - //var result = statementPrinter.Print(invoice, plays); - - //Approvals.Verify(result); - } - */ - - /* #deprecated - [Fact] - [UseReporter(typeof(DiffReporter))] - public void TestTextStatementExample() - { - var plays = new Dictionary(); - 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")); - - Invoice invoice = new Invoice( - "BigCo", - new List - { - 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) - } - ); - - StatementPrinter statementPrinter = new StatementPrinter(); + //StatementPrinter statementPrinter = new StatementPrinter(); deprecated + TextStatementPrinter statementPrinter = new TextStatementPrinter(); var result = statementPrinter.Print(invoice, plays); Approvals.Verify(result); } - */ [Fact] [UseReporter(typeof(DiffReporter))] @@ -94,6 +60,7 @@ public void TestTextStatementExample() } ); + //StatementPrinter statementPrinter = new StatementPrinter(); deprecated TextStatementPrinter statementPrinter = new TextStatementPrinter(); var result = statementPrinter.Print(invoice, plays); @@ -101,7 +68,7 @@ public void TestTextStatementExample() } [Fact] - [UseReporter(typeof(DiffReporter))] // Define qual reporter usar para mostrar as diferenças + [UseReporter(typeof(DiffReporter))] public void TestXmlStatementExample() { var plays = new Dictionary(); @@ -125,11 +92,9 @@ public void TestXmlStatementExample() } ); - // Gerando o resultado da impressão XML XmlStatementPrinter statementPrinter = new XmlStatementPrinter(); var result = statementPrinter.Print(invoice, plays); - // Verificando o resultado gerado usando Approvals Approvals.Verify(result); } } From e4efd295e33d628880d57dae86bfbd7444c68893 Mon Sep 17 00:00:00 2001 From: lucasvil4r Date: Mon, 16 Sep 2024 22:11:22 -0300 Subject: [PATCH 06/12] =?UTF-8?q?bugfix:=20corre=C3=A7=C3=A3o=20no=20cabe?= =?UTF-8?q?=C3=A7alho=20do=20xml?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/Printers/XmlStatementPrinter.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TheatricalPlayersRefactoringKata.Application/Services/Printers/XmlStatementPrinter.cs b/TheatricalPlayersRefactoringKata.Application/Services/Printers/XmlStatementPrinter.cs index 9e429f8e..7b616820 100644 --- a/TheatricalPlayersRefactoringKata.Application/Services/Printers/XmlStatementPrinter.cs +++ b/TheatricalPlayersRefactoringKata.Application/Services/Printers/XmlStatementPrinter.cs @@ -14,7 +14,7 @@ public string Print(Invoice invoice, Dictionary plays) var totalVolumeCredits = 0; var xml = new StringBuilder(); - xml.AppendLine(""); + xml.AppendLine(""); xml.AppendLine(""); xml.AppendLine($" {invoice.Customer}"); xml.AppendLine(" "); From 05879fa10af685258ca45cfba4cd32c2ff9bd2ac Mon Sep 17 00:00:00 2001 From: lucasvil4r Date: Tue, 17 Sep 2024 15:24:40 -0300 Subject: [PATCH 07/12] =?UTF-8?q?feature:=20create=20API=20rest=20e=20docu?= =?UTF-8?q?menta=C3=A7=C3=A3o=20da=20API=20por=20Swagger?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ApiTheatrical.cs | 50 ++++++++++ .../Controllers/ApiTheatricalController.cs | 76 ++++++++++++++++ .../Controllers/WeatherForecastController.cs | 33 ------- .../Program.cs | 15 ++- ...PlayersRefactoringKata.Presentation.csproj | 12 ++- ...rsRefactoringKata.Presentation.csproj.user | 7 +- .../TheatricalSwaggerAnnotation.xml | 91 +++++++++++++++++++ .../WeatherForecast.cs | 13 --- 8 files changed, 248 insertions(+), 49 deletions(-) create mode 100644 TheatricalPlayersRefactoringKata.Presentation/ApiTheatrical.cs create mode 100644 TheatricalPlayersRefactoringKata.Presentation/Controllers/ApiTheatricalController.cs delete mode 100644 TheatricalPlayersRefactoringKata.Presentation/Controllers/WeatherForecastController.cs create mode 100644 TheatricalPlayersRefactoringKata.Presentation/TheatricalSwaggerAnnotation.xml delete mode 100644 TheatricalPlayersRefactoringKata.Presentation/WeatherForecast.cs diff --git a/TheatricalPlayersRefactoringKata.Presentation/ApiTheatrical.cs b/TheatricalPlayersRefactoringKata.Presentation/ApiTheatrical.cs new file mode 100644 index 00000000..16ec497d --- /dev/null +++ b/TheatricalPlayersRefactoringKata.Presentation/ApiTheatrical.cs @@ -0,0 +1,50 @@ +using System.ComponentModel.DataAnnotations; + +/// +/// Representa um modelo de domínio para performances teatrais. +/// +public class ApiTheatrical +{ + /// + /// ID chave primária (PK) + /// + [Required] + public string Id { get; set; } + + /// + /// O nome da peça teatral. + /// + [Required] + [StringLength(100)] + public string Title { get; set; } + + /// + /// Tipo de peça, pode ser Comédia, Tragédia, Histórica e etc. + /// + [Required] + public string Genre { get; set; } + + /// + /// Duração da peça em minutos. + /// + [Range(30, 240)] + public int DurationInMinutes { get; set; } + + /// + /// Preço base da peça. + /// + [Range(0, double.MaxValue, ErrorMessage = "O valor deve ser maior ou igual a zero")] + public decimal BasePrice { get; set; } + + /// + /// Data da apresentação. + /// + [Required] + public DateTime PerformanceDate { get; set; } + + /// + /// Número de espectadores. + /// + [Range(1, int.MaxValue)] + public int Audience { get; set; } +} diff --git a/TheatricalPlayersRefactoringKata.Presentation/Controllers/ApiTheatricalController.cs b/TheatricalPlayersRefactoringKata.Presentation/Controllers/ApiTheatricalController.cs new file mode 100644 index 00000000..58d681f0 --- /dev/null +++ b/TheatricalPlayersRefactoringKata.Presentation/Controllers/ApiTheatricalController.cs @@ -0,0 +1,76 @@ +using Microsoft.AspNetCore.Mvc; + +namespace TheatricalPlayersRefactoringKata.Presentation.Controllers +{ + [Route("api/[controller]")] + [ApiController] + public class ApiTheatricalController : ControllerBase + { + /// + /// Retorna todas as performances teatrais. + /// + /// Lista de performances. + [HttpGet] + public IActionResult GetAllPerformances() + { + return Ok("Este método deve retornar uma lista de todas as performances."); + } + + /// + /// Retorna uma performance por ID. + /// + /// ID da performance. + /// Detalhes da performance. + [HttpGet("{id}")] + public IActionResult GetPerformanceById(int id) + { + return Ok($"Este método deve retornar os detalhes da performance com o ID {id}."); + } + + /// + /// Cria uma nova performance. + /// + /// Modelo da performance. + /// Confirmação da criação. + [HttpPost] + public IActionResult CreatePerformance([FromBody] ApiTheatrical model) + { + return Ok("Este método deve criar uma nova performance com base nos dados fornecidos."); + } + + /// + /// Atualiza completamente uma performance existente. + /// + /// ID da performance. + /// Modelo da performance atualizada. + /// Confirmação da atualização. + [HttpPut("{id}")] + public IActionResult UpdatePerformance(int id, [FromBody] ApiTheatrical model) + { + return Ok($"Este método deve atualizar completamente a performance com o ID {id} com os dados fornecidos."); + } + + /// + /// Atualiza parcialmente uma performance existente. + /// + /// ID da performance. + /// Modelo com as propriedades a serem atualizadas. + /// Confirmação da atualização parcial. + [HttpPatch("{id}")] + public IActionResult PatchPerformance(int id, [FromBody] ApiTheatrical patchModel) + { + return Ok($"Este método deve atualizar parcialmente a performance com o ID {id} com os dados fornecidos."); + } + + /// + /// Remove uma performance existente. + /// + /// ID da performance. + /// Confirmação da remoção. + [HttpDelete("{id}")] + public IActionResult DeletePerformance(int id) + { + return Ok($"Este método deve excluir a performance com o ID {id}."); + } + } +} diff --git a/TheatricalPlayersRefactoringKata.Presentation/Controllers/WeatherForecastController.cs b/TheatricalPlayersRefactoringKata.Presentation/Controllers/WeatherForecastController.cs deleted file mode 100644 index 748102f0..00000000 --- a/TheatricalPlayersRefactoringKata.Presentation/Controllers/WeatherForecastController.cs +++ /dev/null @@ -1,33 +0,0 @@ -using Microsoft.AspNetCore.Mvc; - -namespace TheatricalPlayersRefactoringKata.Presentation.Controllers -{ - [ApiController] - [Route("[controller]")] - public class WeatherForecastController : ControllerBase - { - private static readonly string[] Summaries = new[] - { - "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" - }; - - private readonly ILogger _logger; - - public WeatherForecastController(ILogger logger) - { - _logger = logger; - } - - [HttpGet(Name = "GetWeatherForecast")] - public IEnumerable Get() - { - return Enumerable.Range(1, 5).Select(index => new WeatherForecast - { - Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)), - TemperatureC = Random.Shared.Next(-20, 55), - Summary = Summaries[Random.Shared.Next(Summaries.Length)] - }) - .ToArray(); - } - } -} diff --git a/TheatricalPlayersRefactoringKata.Presentation/Program.cs b/TheatricalPlayersRefactoringKata.Presentation/Program.cs index 48863a6d..fd50a83a 100644 --- a/TheatricalPlayersRefactoringKata.Presentation/Program.cs +++ b/TheatricalPlayersRefactoringKata.Presentation/Program.cs @@ -1,3 +1,5 @@ +using Microsoft.OpenApi.Models; + var builder = WebApplication.CreateBuilder(args); // Add services to the container. @@ -5,7 +7,18 @@ builder.Services.AddControllers(); // Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle builder.Services.AddEndpointsApiExplorer(); -builder.Services.AddSwaggerGen(); +builder.Services.AddSwaggerGen( + c => + { + c.SwaggerDoc("v1", new OpenApiInfo + { + Title = "Theatrical Players API", + Description = "API para calcular valores e créditos para performances teatrais.", + Contact = new OpenApiContact() { Name = "Lucas Vilar", Email = "lucasvilar-celestino@hotmail.com" } + }); + c.IncludeXmlComments(Path.Combine(System.AppContext.BaseDirectory, "TheatricalSwaggerAnnotation.xml")); + } +); var app = builder.Build(); diff --git a/TheatricalPlayersRefactoringKata.Presentation/TheatricalPlayersRefactoringKata.Presentation.csproj b/TheatricalPlayersRefactoringKata.Presentation/TheatricalPlayersRefactoringKata.Presentation.csproj index 9daa1808..075c684c 100644 --- a/TheatricalPlayersRefactoringKata.Presentation/TheatricalPlayersRefactoringKata.Presentation.csproj +++ b/TheatricalPlayersRefactoringKata.Presentation/TheatricalPlayersRefactoringKata.Presentation.csproj @@ -4,10 +4,20 @@ net8.0 enable enable + True + TheatricalSwaggerAnnotation.xml + + + + 1701;1702;1591 + + + + 1701;1702;1591 - + diff --git a/TheatricalPlayersRefactoringKata.Presentation/TheatricalPlayersRefactoringKata.Presentation.csproj.user b/TheatricalPlayersRefactoringKata.Presentation/TheatricalPlayersRefactoringKata.Presentation.csproj.user index 9ff5820a..9d70f603 100644 --- a/TheatricalPlayersRefactoringKata.Presentation/TheatricalPlayersRefactoringKata.Presentation.csproj.user +++ b/TheatricalPlayersRefactoringKata.Presentation/TheatricalPlayersRefactoringKata.Presentation.csproj.user @@ -1,6 +1,11 @@  - https + http + ApiControllerWithActionsScaffolder + root/Common/Api + + + ProjectDebugger \ No newline at end of file diff --git a/TheatricalPlayersRefactoringKata.Presentation/TheatricalSwaggerAnnotation.xml b/TheatricalPlayersRefactoringKata.Presentation/TheatricalSwaggerAnnotation.xml new file mode 100644 index 00000000..5e8d0949 --- /dev/null +++ b/TheatricalPlayersRefactoringKata.Presentation/TheatricalSwaggerAnnotation.xml @@ -0,0 +1,91 @@ + + + + TheatricalPlayersRefactoringKata.Presentation + + + + + Representa um modelo de domínio para performances teatrais. + + + + + ID chave primária (PK) + + + + + O nome da peça teatral. + + + + + Tipo de peça, pode ser Comédia, Tragédia, Histórica e etc. + + + + + Duração da peça em minutos. + + + + + Preço base da peça. + + + + + Data da apresentação. + + + + + Número de espectadores. + + + + + Retorna todas as performances teatrais. + + Lista de performances. + + + + Retorna uma performance por ID. + + ID da performance. + Detalhes da performance. + + + + Cria uma nova performance. + + Modelo da performance. + Confirmação da criação. + + + + Atualiza completamente uma performance existente. + + ID da performance. + Modelo da performance atualizada. + Confirmação da atualização. + + + + Atualiza parcialmente uma performance existente. + + ID da performance. + Modelo com as propriedades a serem atualizadas. + Confirmação da atualização parcial. + + + + Remove uma performance existente. + + ID da performance. + Confirmação da remoção. + + + diff --git a/TheatricalPlayersRefactoringKata.Presentation/WeatherForecast.cs b/TheatricalPlayersRefactoringKata.Presentation/WeatherForecast.cs deleted file mode 100644 index 7e5bdafa..00000000 --- a/TheatricalPlayersRefactoringKata.Presentation/WeatherForecast.cs +++ /dev/null @@ -1,13 +0,0 @@ -namespace TheatricalPlayersRefactoringKata.Presentation -{ - public class WeatherForecast - { - public DateOnly Date { get; set; } - - public int TemperatureC { get; set; } - - public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); - - public string? Summary { get; set; } - } -} From 715a709e48e7c65e9af5f417b30b1c455d2a4bb2 Mon Sep 17 00:00:00 2001 From: lucasvil4r Date: Tue, 17 Sep 2024 19:26:54 -0300 Subject: [PATCH 08/12] =?UTF-8?q?refactor:=20modelando=20diretorio=20da=20?= =?UTF-8?q?aplica=C3=A7=C3=A3o=20para=20Clean=20Architecture?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/Printers/TextStatementPrinter.cs | 6 +++--- .../Services/Printers/XmlStatementPrinter.cs | 6 +++--- .../Calculators/ComedyCalculator.cs | 8 ++++---- .../Calculators/HistoryCalculator.cs | 8 ++++---- .../Calculators/TragedyCalculator.cs | 8 ++++---- .../Factories/StatementPrinterFactory.cs | 6 +++--- .../Factories/TheatricalCalculatorFactory.cs} | 10 +++++----- .../Interfaces/IStatementPrinter.cs | 2 +- .../Interfaces/ITheatricalCalculator.cs | 4 ++-- .../ApiTheatrical.cs | 6 +++--- 10 files changed, 32 insertions(+), 32 deletions(-) rename TheatricalPlayersRefactoringKata.Application/{Services => UseCases}/Calculators/ComedyCalculator.cs (76%) rename TheatricalPlayersRefactoringKata.Application/{Services => UseCases}/Calculators/HistoryCalculator.cs (73%) rename TheatricalPlayersRefactoringKata.Application/{Services => UseCases}/Calculators/TragedyCalculator.cs (70%) rename TheatricalPlayersRefactoringKata.Application/{Services => UseCases}/Factories/StatementPrinterFactory.cs (74%) rename TheatricalPlayersRefactoringKata.Application/{Services/Factories/GenreCalculatorFactory.cs => UseCases/Factories/TheatricalCalculatorFactory.cs} (51%) rename {TheatricalPlayersRefactoringKata.Application => TheatricalPlayersRefactoringKata.Domain}/Interfaces/IStatementPrinter.cs (73%) rename TheatricalPlayersRefactoringKata.Application/Interfaces/IGenreCalculator.cs => TheatricalPlayersRefactoringKata.Domain/Interfaces/ITheatricalCalculator.cs (64%) diff --git a/TheatricalPlayersRefactoringKata.Application/Services/Printers/TextStatementPrinter.cs b/TheatricalPlayersRefactoringKata.Application/Services/Printers/TextStatementPrinter.cs index 89192f9c..a514e70a 100644 --- a/TheatricalPlayersRefactoringKata.Application/Services/Printers/TextStatementPrinter.cs +++ b/TheatricalPlayersRefactoringKata.Application/Services/Printers/TextStatementPrinter.cs @@ -1,8 +1,8 @@  using System.Globalization; -using TheatricalPlayersRefactoringKata.Application.Interfaces; -using TheatricalPlayersRefactoringKata.Application.Services.Factories; +using TheatricalPlayersRefactoringKata.Application.UseCases.Factories; using TheatricalPlayersRefactoringKata.Domain.Entities; +using TheatricalPlayersRefactoringKata.Domain.Interfaces; namespace TheatricalPlayersRefactoringKata.Application.Services.Printers { @@ -18,7 +18,7 @@ public string Print(Invoice invoice, Dictionary plays) foreach (var perf in invoice.Performances) { var play = plays[perf.PlayId]; - var calculator = GenreCalculatorFactory.GetCalculator(play.Type); + var calculator = TheatricalCalculatorFactory.GetCalculator(play.Type); var thisAmount = calculator.CalculateAmount(perf, play); totalAmount += thisAmount; diff --git a/TheatricalPlayersRefactoringKata.Application/Services/Printers/XmlStatementPrinter.cs b/TheatricalPlayersRefactoringKata.Application/Services/Printers/XmlStatementPrinter.cs index 7b616820..54b9ebbc 100644 --- a/TheatricalPlayersRefactoringKata.Application/Services/Printers/XmlStatementPrinter.cs +++ b/TheatricalPlayersRefactoringKata.Application/Services/Printers/XmlStatementPrinter.cs @@ -1,8 +1,8 @@ using System.Globalization; using System.Text; -using TheatricalPlayersRefactoringKata.Application.Interfaces; -using TheatricalPlayersRefactoringKata.Application.Services.Factories; +using TheatricalPlayersRefactoringKata.Application.UseCases.Factories; using TheatricalPlayersRefactoringKata.Domain.Entities; +using TheatricalPlayersRefactoringKata.Domain.Interfaces; namespace TheatricalPlayersRefactoringKata.Application.Services.Printers { @@ -22,7 +22,7 @@ public string Print(Invoice invoice, Dictionary plays) foreach (var perf in invoice.Performances) { var play = plays[perf.PlayId]; - var calculator = GenreCalculatorFactory.GetCalculator(play.Type); + var calculator = TheatricalCalculatorFactory.GetCalculator(play.Type); var thisAmount = calculator.CalculateAmount(perf, play); var volumeCredits = calculator.CalculateVolumeCredits(perf); diff --git a/TheatricalPlayersRefactoringKata.Application/Services/Calculators/ComedyCalculator.cs b/TheatricalPlayersRefactoringKata.Application/UseCases/Calculators/ComedyCalculator.cs similarity index 76% rename from TheatricalPlayersRefactoringKata.Application/Services/Calculators/ComedyCalculator.cs rename to TheatricalPlayersRefactoringKata.Application/UseCases/Calculators/ComedyCalculator.cs index df2c987c..8f4d6beb 100644 --- a/TheatricalPlayersRefactoringKata.Application/Services/Calculators/ComedyCalculator.cs +++ b/TheatricalPlayersRefactoringKata.Application/UseCases/Calculators/ComedyCalculator.cs @@ -1,9 +1,9 @@ -using TheatricalPlayersRefactoringKata.Application.Interfaces; -using TheatricalPlayersRefactoringKata.Domain.Entities; +using TheatricalPlayersRefactoringKata.Domain.Entities; +using TheatricalPlayersRefactoringKata.Domain.Interfaces; -namespace TheatricalPlayersRefactoringKata.Application.Services.Calculators +namespace TheatricalPlayersRefactoringKata.Application.UseCases.Calculators { - public class ComedyCalculator : IGenreCalculator + public class ComedyCalculator : ITheatricalCalculator { public decimal CalculateAmount(Performance perf, Play play) { diff --git a/TheatricalPlayersRefactoringKata.Application/Services/Calculators/HistoryCalculator.cs b/TheatricalPlayersRefactoringKata.Application/UseCases/Calculators/HistoryCalculator.cs similarity index 73% rename from TheatricalPlayersRefactoringKata.Application/Services/Calculators/HistoryCalculator.cs rename to TheatricalPlayersRefactoringKata.Application/UseCases/Calculators/HistoryCalculator.cs index 7f5d80ed..4abe1789 100644 --- a/TheatricalPlayersRefactoringKata.Application/Services/Calculators/HistoryCalculator.cs +++ b/TheatricalPlayersRefactoringKata.Application/UseCases/Calculators/HistoryCalculator.cs @@ -1,9 +1,9 @@ -using TheatricalPlayersRefactoringKata.Application.Interfaces; -using TheatricalPlayersRefactoringKata.Domain.Entities; +using TheatricalPlayersRefactoringKata.Domain.Entities; +using TheatricalPlayersRefactoringKata.Domain.Interfaces; -namespace TheatricalPlayersRefactoringKata.Application.Services.Calculators +namespace TheatricalPlayersRefactoringKata.Application.UseCases.Calculators { - public class HistoryCalculator : IGenreCalculator + public class HistoryCalculator : ITheatricalCalculator { private readonly TragedyCalculator _tragedyCalculator; private readonly ComedyCalculator _comedyCalculator; diff --git a/TheatricalPlayersRefactoringKata.Application/Services/Calculators/TragedyCalculator.cs b/TheatricalPlayersRefactoringKata.Application/UseCases/Calculators/TragedyCalculator.cs similarity index 70% rename from TheatricalPlayersRefactoringKata.Application/Services/Calculators/TragedyCalculator.cs rename to TheatricalPlayersRefactoringKata.Application/UseCases/Calculators/TragedyCalculator.cs index 817fe61c..7300ec06 100644 --- a/TheatricalPlayersRefactoringKata.Application/Services/Calculators/TragedyCalculator.cs +++ b/TheatricalPlayersRefactoringKata.Application/UseCases/Calculators/TragedyCalculator.cs @@ -1,9 +1,9 @@ -using TheatricalPlayersRefactoringKata.Application.Interfaces; -using TheatricalPlayersRefactoringKata.Domain.Entities; +using TheatricalPlayersRefactoringKata.Domain.Entities; +using TheatricalPlayersRefactoringKata.Domain.Interfaces; -namespace TheatricalPlayersRefactoringKata.Application.Services.Calculators +namespace TheatricalPlayersRefactoringKata.Application.UseCases.Calculators { - public class TragedyCalculator : IGenreCalculator + public class TragedyCalculator : ITheatricalCalculator { public decimal CalculateAmount(Performance perf, Play play) { diff --git a/TheatricalPlayersRefactoringKata.Application/Services/Factories/StatementPrinterFactory.cs b/TheatricalPlayersRefactoringKata.Application/UseCases/Factories/StatementPrinterFactory.cs similarity index 74% rename from TheatricalPlayersRefactoringKata.Application/Services/Factories/StatementPrinterFactory.cs rename to TheatricalPlayersRefactoringKata.Application/UseCases/Factories/StatementPrinterFactory.cs index ace3feea..4fbd6d79 100644 --- a/TheatricalPlayersRefactoringKata.Application/Services/Factories/StatementPrinterFactory.cs +++ b/TheatricalPlayersRefactoringKata.Application/UseCases/Factories/StatementPrinterFactory.cs @@ -1,7 +1,7 @@ -using TheatricalPlayersRefactoringKata.Application.Interfaces; -using TheatricalPlayersRefactoringKata.Application.Services.Printers; +using TheatricalPlayersRefactoringKata.Application.Services.Printers; +using TheatricalPlayersRefactoringKata.Domain.Interfaces; -namespace TheatricalPlayersRefactoringKata.Application.Services.Factories +namespace TheatricalPlayersRefactoringKata.Application.UseCases.Factories { public static class StatementPrinterFactory { diff --git a/TheatricalPlayersRefactoringKata.Application/Services/Factories/GenreCalculatorFactory.cs b/TheatricalPlayersRefactoringKata.Application/UseCases/Factories/TheatricalCalculatorFactory.cs similarity index 51% rename from TheatricalPlayersRefactoringKata.Application/Services/Factories/GenreCalculatorFactory.cs rename to TheatricalPlayersRefactoringKata.Application/UseCases/Factories/TheatricalCalculatorFactory.cs index 013d667d..bd1955e5 100644 --- a/TheatricalPlayersRefactoringKata.Application/Services/Factories/GenreCalculatorFactory.cs +++ b/TheatricalPlayersRefactoringKata.Application/UseCases/Factories/TheatricalCalculatorFactory.cs @@ -1,11 +1,11 @@ -using TheatricalPlayersRefactoringKata.Application.Interfaces; -using TheatricalPlayersRefactoringKata.Application.Services.Calculators; +using TheatricalPlayersRefactoringKata.Application.UseCases.Calculators; +using TheatricalPlayersRefactoringKata.Domain.Interfaces; -namespace TheatricalPlayersRefactoringKata.Application.Services.Factories +namespace TheatricalPlayersRefactoringKata.Application.UseCases.Factories { - public class GenreCalculatorFactory + public class TheatricalCalculatorFactory { - public static IGenreCalculator GetCalculator(string genreType) + public static ITheatricalCalculator GetCalculator(string genreType) { return genreType switch { diff --git a/TheatricalPlayersRefactoringKata.Application/Interfaces/IStatementPrinter.cs b/TheatricalPlayersRefactoringKata.Domain/Interfaces/IStatementPrinter.cs similarity index 73% rename from TheatricalPlayersRefactoringKata.Application/Interfaces/IStatementPrinter.cs rename to TheatricalPlayersRefactoringKata.Domain/Interfaces/IStatementPrinter.cs index fa3b8635..17732e13 100644 --- a/TheatricalPlayersRefactoringKata.Application/Interfaces/IStatementPrinter.cs +++ b/TheatricalPlayersRefactoringKata.Domain/Interfaces/IStatementPrinter.cs @@ -1,6 +1,6 @@ using TheatricalPlayersRefactoringKata.Domain.Entities; -namespace TheatricalPlayersRefactoringKata.Application.Interfaces +namespace TheatricalPlayersRefactoringKata.Domain.Interfaces { public interface IStatementPrinter { diff --git a/TheatricalPlayersRefactoringKata.Application/Interfaces/IGenreCalculator.cs b/TheatricalPlayersRefactoringKata.Domain/Interfaces/ITheatricalCalculator.cs similarity index 64% rename from TheatricalPlayersRefactoringKata.Application/Interfaces/IGenreCalculator.cs rename to TheatricalPlayersRefactoringKata.Domain/Interfaces/ITheatricalCalculator.cs index 3268f530..e87bd77b 100644 --- a/TheatricalPlayersRefactoringKata.Application/Interfaces/IGenreCalculator.cs +++ b/TheatricalPlayersRefactoringKata.Domain/Interfaces/ITheatricalCalculator.cs @@ -1,8 +1,8 @@ using TheatricalPlayersRefactoringKata.Domain.Entities; -namespace TheatricalPlayersRefactoringKata.Application.Interfaces +namespace TheatricalPlayersRefactoringKata.Domain.Interfaces { - public interface IGenreCalculator + public interface ITheatricalCalculator { decimal CalculateAmount(Performance perf, Play play); int CalculateVolumeCredits(Performance perf); diff --git a/TheatricalPlayersRefactoringKata.Presentation/ApiTheatrical.cs b/TheatricalPlayersRefactoringKata.Presentation/ApiTheatrical.cs index 16ec497d..afc40f20 100644 --- a/TheatricalPlayersRefactoringKata.Presentation/ApiTheatrical.cs +++ b/TheatricalPlayersRefactoringKata.Presentation/ApiTheatrical.cs @@ -9,20 +9,20 @@ public class ApiTheatrical /// ID chave primária (PK) /// [Required] - public string Id { get; set; } + public required string Id { get; set; } /// /// O nome da peça teatral. /// [Required] [StringLength(100)] - public string Title { get; set; } + public required string Title { get; set; } /// /// Tipo de peça, pode ser Comédia, Tragédia, Histórica e etc. /// [Required] - public string Genre { get; set; } + public required string Genre { get; set; } /// /// Duração da peça em minutos. From 43738e8a21297bca93fba189a212d84c736fac67 Mon Sep 17 00:00:00 2001 From: lucasvil4r Date: Tue, 17 Sep 2024 20:47:21 -0300 Subject: [PATCH 09/12] =?UTF-8?q?feature:=20Persistencia=20dos=20dados=20e?= =?UTF-8?q?m=20um=20banco=20de=20dados=20para=20salvar=20o=20extrato=20com?= =?UTF-8?q?=20suas=20respectivas=20pe=C3=A7as=20e=20refatorando=20estrutur?= =?UTF-8?q?a=20de=20pasta=20clear=20arch?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Entities/Invoice.cs | 1 - .../Entities/Item.cs | 13 ++++ .../Entities/Statement.cs | 11 ++++ .../Context/AppDbContext.cs | 24 +++++++ ...ayersRefactoringKata.Infrastructure.csproj | 15 +++++ .../theatrical.db | 1 + .../AppDbContextTests.cs | 62 +++++++++++++++++++ ...atricalPlayersRefactoringKata.Tests.csproj | 2 + 8 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 TheatricalPlayersRefactoringKata.Domain/Entities/Item.cs create mode 100644 TheatricalPlayersRefactoringKata.Domain/Entities/Statement.cs create mode 100644 TheatricalPlayersRefactoringKata.Infrastructure/Context/AppDbContext.cs create mode 100644 TheatricalPlayersRefactoringKata.Infrastructure/theatrical.db create mode 100644 TheatricalPlayersRefactoringKata.Tests/AppDbContextTests.cs diff --git a/TheatricalPlayersRefactoringKata.Domain/Entities/Invoice.cs b/TheatricalPlayersRefactoringKata.Domain/Entities/Invoice.cs index 40156ffb..6009f44f 100644 --- a/TheatricalPlayersRefactoringKata.Domain/Entities/Invoice.cs +++ b/TheatricalPlayersRefactoringKata.Domain/Entities/Invoice.cs @@ -1,5 +1,4 @@ namespace TheatricalPlayersRefactoringKata.Domain.Entities; - public class Invoice { private string _customer; diff --git a/TheatricalPlayersRefactoringKata.Domain/Entities/Item.cs b/TheatricalPlayersRefactoringKata.Domain/Entities/Item.cs new file mode 100644 index 00000000..687a8471 --- /dev/null +++ b/TheatricalPlayersRefactoringKata.Domain/Entities/Item.cs @@ -0,0 +1,13 @@ +namespace TheatricalPlayersRefactoringKata.Domain.Entities + +{ + public class Item + { + public int Id { get; set; } + public decimal AmountOwed { get; set; } + public int EarnedCredits { get; set; } + public int Seats { get; set; } + public int? StatementId { get; set; } + public Statement? Statement { get; set; } + } +} diff --git a/TheatricalPlayersRefactoringKata.Domain/Entities/Statement.cs b/TheatricalPlayersRefactoringKata.Domain/Entities/Statement.cs new file mode 100644 index 00000000..0630f205 --- /dev/null +++ b/TheatricalPlayersRefactoringKata.Domain/Entities/Statement.cs @@ -0,0 +1,11 @@ +namespace TheatricalPlayersRefactoringKata.Domain.Entities +{ + public class Statement + { + public int Id { get; set; } + public required string Customer { get; set; } + public required List Items { get; set; } + public decimal TotalAmountOwed { get; set; } + public int TotalEarnedCredits { get; set; } + } +} diff --git a/TheatricalPlayersRefactoringKata.Infrastructure/Context/AppDbContext.cs b/TheatricalPlayersRefactoringKata.Infrastructure/Context/AppDbContext.cs new file mode 100644 index 00000000..46eccbb9 --- /dev/null +++ b/TheatricalPlayersRefactoringKata.Infrastructure/Context/AppDbContext.cs @@ -0,0 +1,24 @@ +using Microsoft.EntityFrameworkCore; +using TheatricalPlayersRefactoringKata.Domain.Entities; + +namespace TheatricalPlayersRefactoringKata.Infrastructure.Data +{ + public class AppDbContext : DbContext + { + public AppDbContext(DbContextOptions options) : base(options) + { } + + public DbSet Statements { get; set; } + public DbSet Items { get; set; } + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + optionsBuilder.UseSqlite("Data Source=theatrical.db"); + } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + base.OnModelCreating(modelBuilder); + } + } +} diff --git a/TheatricalPlayersRefactoringKata.Infrastructure/TheatricalPlayersRefactoringKata.Infrastructure.csproj b/TheatricalPlayersRefactoringKata.Infrastructure/TheatricalPlayersRefactoringKata.Infrastructure.csproj index fbb2999c..9cfaa7e3 100644 --- a/TheatricalPlayersRefactoringKata.Infrastructure/TheatricalPlayersRefactoringKata.Infrastructure.csproj +++ b/TheatricalPlayersRefactoringKata.Infrastructure/TheatricalPlayersRefactoringKata.Infrastructure.csproj @@ -8,6 +8,21 @@ + + + + + + + + + + TextTemplatingFileGenerator + + + + + diff --git a/TheatricalPlayersRefactoringKata.Infrastructure/theatrical.db b/TheatricalPlayersRefactoringKata.Infrastructure/theatrical.db new file mode 100644 index 00000000..5f282702 --- /dev/null +++ b/TheatricalPlayersRefactoringKata.Infrastructure/theatrical.db @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/TheatricalPlayersRefactoringKata.Tests/AppDbContextTests.cs b/TheatricalPlayersRefactoringKata.Tests/AppDbContextTests.cs new file mode 100644 index 00000000..9cb58971 --- /dev/null +++ b/TheatricalPlayersRefactoringKata.Tests/AppDbContextTests.cs @@ -0,0 +1,62 @@ +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using TheatricalPlayersRefactoringKata.Domain.Entities; +using TheatricalPlayersRefactoringKata.Infrastructure.Data; +using Xunit; +using SQLitePCL; + +namespace TheatricalPlayersRefactoringKata.Tests; + +public class AppDbContextTests : IDisposable +{ + private readonly AppDbContext _context; + + public AppDbContextTests() + { + // Inicializa o SQLitePCL se necessário + raw.SetProvider(new SQLitePCL.SQLite3Provider_e_sqlite3()); + + var options = new DbContextOptionsBuilder() + .UseSqlite("DataSource=:memory:") + .Options; + + _context = new AppDbContext(options); + _context.Database.OpenConnection(); + _context.Database.EnsureCreated(); + } + + [Fact] + public void CanAddStatementWithItems() + { + // Arrange + var statement = new Statement + { + Customer = "BigCo", + TotalAmountOwed = 3995.4m, + TotalEarnedCredits = 56, + Items = new List + { + new Item { AmountOwed = 650, EarnedCredits = 25, Seats = 55 }, + new Item { AmountOwed = 547, EarnedCredits = 12, Seats = 35 } + } + }; + + _context.Statements.Add(statement); + _context.SaveChanges(); + + var statements = _context.Statements.Include(s => s.Items).ToList(); + var addedStatement = statements.First(); + + Assert.Equal("BigCo", addedStatement.Customer); + Assert.Equal(2, addedStatement.Items.Count); + Assert.Equal(3995.4m, addedStatement.TotalAmountOwed); + } + + public void Dispose() + { + _context.Database.CloseConnection(); + _context.Dispose(); + } +} diff --git a/TheatricalPlayersRefactoringKata.Tests/TheatricalPlayersRefactoringKata.Tests.csproj b/TheatricalPlayersRefactoringKata.Tests/TheatricalPlayersRefactoringKata.Tests.csproj index 15279642..7d5b6948 100644 --- a/TheatricalPlayersRefactoringKata.Tests/TheatricalPlayersRefactoringKata.Tests.csproj +++ b/TheatricalPlayersRefactoringKata.Tests/TheatricalPlayersRefactoringKata.Tests.csproj @@ -9,6 +9,7 @@ + runtime; build; native; contentfiles; analyzers; buildtransitive @@ -19,6 +20,7 @@ + From 2607854549ab44e7ee017c61cf38f21dfdd600c5 Mon Sep 17 00:00:00 2001 From: lucasvil4r Date: Tue, 17 Sep 2024 23:47:45 -0300 Subject: [PATCH 10/12] =?UTF-8?q?refactor:=20criando=20pasta=20para=20docu?= =?UTF-8?q?menta=C3=A7=C3=A3o=20swagger=20API=20Rest?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../{ => Swagger}/ApiTheatrical.cs | 0 .../{ => Swagger}/TheatricalSwaggerAnnotation.xml | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename TheatricalPlayersRefactoringKata.Presentation/{ => Swagger}/ApiTheatrical.cs (100%) rename TheatricalPlayersRefactoringKata.Presentation/{ => Swagger}/TheatricalSwaggerAnnotation.xml (100%) diff --git a/TheatricalPlayersRefactoringKata.Presentation/ApiTheatrical.cs b/TheatricalPlayersRefactoringKata.Presentation/Swagger/ApiTheatrical.cs similarity index 100% rename from TheatricalPlayersRefactoringKata.Presentation/ApiTheatrical.cs rename to TheatricalPlayersRefactoringKata.Presentation/Swagger/ApiTheatrical.cs diff --git a/TheatricalPlayersRefactoringKata.Presentation/TheatricalSwaggerAnnotation.xml b/TheatricalPlayersRefactoringKata.Presentation/Swagger/TheatricalSwaggerAnnotation.xml similarity index 100% rename from TheatricalPlayersRefactoringKata.Presentation/TheatricalSwaggerAnnotation.xml rename to TheatricalPlayersRefactoringKata.Presentation/Swagger/TheatricalSwaggerAnnotation.xml From c37e17af987d19435775ddc0c40e4f29cf11338b Mon Sep 17 00:00:00 2001 From: lucasvil4r Date: Wed, 18 Sep 2024 00:00:01 -0300 Subject: [PATCH 11/12] refactor: corrigindo nomeclatura dos arquivos --- ...nter.cs => TextStatementPrinterService.cs} | 2 +- ...inter.cs => XmlStatementPrinterService.cs} | 2 +- .../Factories/StatementPrinterFactory.cs | 4 +- .../TheatricalSwaggerAnnotation.xml | 91 +++++++++++++++++++ .../StatementPrinterTests.cs | 6 +- 5 files changed, 98 insertions(+), 7 deletions(-) rename TheatricalPlayersRefactoringKata.Application/Services/Printers/{TextStatementPrinter.cs => TextStatementPrinterService.cs} (95%) rename TheatricalPlayersRefactoringKata.Application/Services/Printers/{XmlStatementPrinter.cs => XmlStatementPrinterService.cs} (96%) create mode 100644 TheatricalPlayersRefactoringKata.Presentation/TheatricalSwaggerAnnotation.xml diff --git a/TheatricalPlayersRefactoringKata.Application/Services/Printers/TextStatementPrinter.cs b/TheatricalPlayersRefactoringKata.Application/Services/Printers/TextStatementPrinterService.cs similarity index 95% rename from TheatricalPlayersRefactoringKata.Application/Services/Printers/TextStatementPrinter.cs rename to TheatricalPlayersRefactoringKata.Application/Services/Printers/TextStatementPrinterService.cs index a514e70a..acb33b95 100644 --- a/TheatricalPlayersRefactoringKata.Application/Services/Printers/TextStatementPrinter.cs +++ b/TheatricalPlayersRefactoringKata.Application/Services/Printers/TextStatementPrinterService.cs @@ -6,7 +6,7 @@ namespace TheatricalPlayersRefactoringKata.Application.Services.Printers { - public class TextStatementPrinter : IStatementPrinter + public class TextStatementPrinterService : IStatementPrinter { public string Print(Invoice invoice, Dictionary plays) { diff --git a/TheatricalPlayersRefactoringKata.Application/Services/Printers/XmlStatementPrinter.cs b/TheatricalPlayersRefactoringKata.Application/Services/Printers/XmlStatementPrinterService.cs similarity index 96% rename from TheatricalPlayersRefactoringKata.Application/Services/Printers/XmlStatementPrinter.cs rename to TheatricalPlayersRefactoringKata.Application/Services/Printers/XmlStatementPrinterService.cs index 54b9ebbc..3517173f 100644 --- a/TheatricalPlayersRefactoringKata.Application/Services/Printers/XmlStatementPrinter.cs +++ b/TheatricalPlayersRefactoringKata.Application/Services/Printers/XmlStatementPrinterService.cs @@ -6,7 +6,7 @@ namespace TheatricalPlayersRefactoringKata.Application.Services.Printers { - public class XmlStatementPrinter : IStatementPrinter + public class XmlStatementPrinterService : IStatementPrinter { public string Print(Invoice invoice, Dictionary plays) { diff --git a/TheatricalPlayersRefactoringKata.Application/UseCases/Factories/StatementPrinterFactory.cs b/TheatricalPlayersRefactoringKata.Application/UseCases/Factories/StatementPrinterFactory.cs index 4fbd6d79..73f1ab86 100644 --- a/TheatricalPlayersRefactoringKata.Application/UseCases/Factories/StatementPrinterFactory.cs +++ b/TheatricalPlayersRefactoringKata.Application/UseCases/Factories/StatementPrinterFactory.cs @@ -10,9 +10,9 @@ public static IStatementPrinter GetPrinter(string format) switch (format.ToLower()) { case "text": - return new TextStatementPrinter(); + return new TextStatementPrinterService(); case "xml": - return new XmlStatementPrinter(); + return new XmlStatementPrinterService(); // Adicione mais casos conforme necessário para novos formatos default: throw new ArgumentException("Invalid format", nameof(format)); diff --git a/TheatricalPlayersRefactoringKata.Presentation/TheatricalSwaggerAnnotation.xml b/TheatricalPlayersRefactoringKata.Presentation/TheatricalSwaggerAnnotation.xml new file mode 100644 index 00000000..4ff4d178 --- /dev/null +++ b/TheatricalPlayersRefactoringKata.Presentation/TheatricalSwaggerAnnotation.xml @@ -0,0 +1,91 @@ + + + + TheatricalPlayersRefactoringKata.Presentation + + + + + Retorna todas as performances teatrais. + + Lista de performances. + + + + Retorna uma performance por ID. + + ID da performance. + Detalhes da performance. + + + + Cria uma nova performance. + + Modelo da performance. + Confirmação da criação. + + + + Atualiza completamente uma performance existente. + + ID da performance. + Modelo da performance atualizada. + Confirmação da atualização. + + + + Atualiza parcialmente uma performance existente. + + ID da performance. + Modelo com as propriedades a serem atualizadas. + Confirmação da atualização parcial. + + + + Remove uma performance existente. + + ID da performance. + Confirmação da remoção. + + + + Representa um modelo de domínio para performances teatrais. + + + + + ID chave primária (PK) + + + + + O nome da peça teatral. + + + + + Tipo de peça, pode ser Comédia, Tragédia, Histórica e etc. + + + + + Duração da peça em minutos. + + + + + Preço base da peça. + + + + + Data da apresentação. + + + + + Número de espectadores. + + + + diff --git a/TheatricalPlayersRefactoringKata.Tests/StatementPrinterTests.cs b/TheatricalPlayersRefactoringKata.Tests/StatementPrinterTests.cs index 0d35fd41..2a8cebd2 100644 --- a/TheatricalPlayersRefactoringKata.Tests/StatementPrinterTests.cs +++ b/TheatricalPlayersRefactoringKata.Tests/StatementPrinterTests.cs @@ -29,7 +29,7 @@ public void TestStatementExampleLegacy() ); //StatementPrinter statementPrinter = new StatementPrinter(); deprecated - TextStatementPrinter statementPrinter = new TextStatementPrinter(); + TextStatementPrinterService statementPrinter = new TextStatementPrinterService(); var result = statementPrinter.Print(invoice, plays); Approvals.Verify(result); @@ -61,7 +61,7 @@ public void TestTextStatementExample() ); //StatementPrinter statementPrinter = new StatementPrinter(); deprecated - TextStatementPrinter statementPrinter = new TextStatementPrinter(); + TextStatementPrinterService statementPrinter = new TextStatementPrinterService(); var result = statementPrinter.Print(invoice, plays); Approvals.Verify(result); @@ -92,7 +92,7 @@ public void TestXmlStatementExample() } ); - XmlStatementPrinter statementPrinter = new XmlStatementPrinter(); + XmlStatementPrinterService statementPrinter = new XmlStatementPrinterService(); var result = statementPrinter.Print(invoice, plays); Approvals.Verify(result); From 765a8ae1d465e06819279f4e3931ca5e12177b26 Mon Sep 17 00:00:00 2001 From: lucasvil4r Date: Wed, 18 Sep 2024 10:13:07 -0300 Subject: [PATCH 12/12] feature: criando alguns testes para os calculators --- .../AppDbContextTests.cs | 4 +- ...ts.TestStatementExampleLegacy.approved.txt | 0 ...ests.TestTextStatementExample.approved.txt | 0 ...Tests.TestXmlStatementExample.approved.txt | 0 .../StatementPrinterTests.cs | 200 +++++++++--------- ...atricalPlayersRefactoringKata.Tests.csproj | 2 +- .../UnitTests/ComedyCalculatorTests.cs | 57 +++++ .../UnitTests/HistoryCalculatorTests.cs | 56 +++++ .../UnitTests/TragedyCalculatorTests.cs | 57 +++++ 9 files changed, 273 insertions(+), 103 deletions(-) rename TheatricalPlayersRefactoringKata.Tests/{ => IntegrationTests}/AppDbContextTests.cs (92%) rename TheatricalPlayersRefactoringKata.Tests/{ => IntegrationTests}/StatementPrinterTests.TestStatementExampleLegacy.approved.txt (100%) rename TheatricalPlayersRefactoringKata.Tests/{ => IntegrationTests}/StatementPrinterTests.TestTextStatementExample.approved.txt (100%) rename TheatricalPlayersRefactoringKata.Tests/{ => IntegrationTests}/StatementPrinterTests.TestXmlStatementExample.approved.txt (100%) rename TheatricalPlayersRefactoringKata.Tests/{ => IntegrationTests}/StatementPrinterTests.cs (96%) create mode 100644 TheatricalPlayersRefactoringKata.Tests/UnitTests/ComedyCalculatorTests.cs create mode 100644 TheatricalPlayersRefactoringKata.Tests/UnitTests/HistoryCalculatorTests.cs create mode 100644 TheatricalPlayersRefactoringKata.Tests/UnitTests/TragedyCalculatorTests.cs diff --git a/TheatricalPlayersRefactoringKata.Tests/AppDbContextTests.cs b/TheatricalPlayersRefactoringKata.Tests/IntegrationTests/AppDbContextTests.cs similarity index 92% rename from TheatricalPlayersRefactoringKata.Tests/AppDbContextTests.cs rename to TheatricalPlayersRefactoringKata.Tests/IntegrationTests/AppDbContextTests.cs index 9cb58971..1eb6c0a0 100644 --- a/TheatricalPlayersRefactoringKata.Tests/AppDbContextTests.cs +++ b/TheatricalPlayersRefactoringKata.Tests/IntegrationTests/AppDbContextTests.cs @@ -7,7 +7,7 @@ using Xunit; using SQLitePCL; -namespace TheatricalPlayersRefactoringKata.Tests; +namespace TheatricalPlayersRefactoringKata.Tests.IntegrationTests; public class AppDbContextTests : IDisposable { @@ -16,7 +16,7 @@ public class AppDbContextTests : IDisposable public AppDbContextTests() { // Inicializa o SQLitePCL se necessário - raw.SetProvider(new SQLitePCL.SQLite3Provider_e_sqlite3()); + raw.SetProvider(new SQLite3Provider_e_sqlite3()); var options = new DbContextOptionsBuilder() .UseSqlite("DataSource=:memory:") diff --git a/TheatricalPlayersRefactoringKata.Tests/StatementPrinterTests.TestStatementExampleLegacy.approved.txt b/TheatricalPlayersRefactoringKata.Tests/IntegrationTests/StatementPrinterTests.TestStatementExampleLegacy.approved.txt similarity index 100% rename from TheatricalPlayersRefactoringKata.Tests/StatementPrinterTests.TestStatementExampleLegacy.approved.txt rename to TheatricalPlayersRefactoringKata.Tests/IntegrationTests/StatementPrinterTests.TestStatementExampleLegacy.approved.txt diff --git a/TheatricalPlayersRefactoringKata.Tests/StatementPrinterTests.TestTextStatementExample.approved.txt b/TheatricalPlayersRefactoringKata.Tests/IntegrationTests/StatementPrinterTests.TestTextStatementExample.approved.txt similarity index 100% rename from TheatricalPlayersRefactoringKata.Tests/StatementPrinterTests.TestTextStatementExample.approved.txt rename to TheatricalPlayersRefactoringKata.Tests/IntegrationTests/StatementPrinterTests.TestTextStatementExample.approved.txt diff --git a/TheatricalPlayersRefactoringKata.Tests/StatementPrinterTests.TestXmlStatementExample.approved.txt b/TheatricalPlayersRefactoringKata.Tests/IntegrationTests/StatementPrinterTests.TestXmlStatementExample.approved.txt similarity index 100% rename from TheatricalPlayersRefactoringKata.Tests/StatementPrinterTests.TestXmlStatementExample.approved.txt rename to TheatricalPlayersRefactoringKata.Tests/IntegrationTests/StatementPrinterTests.TestXmlStatementExample.approved.txt diff --git a/TheatricalPlayersRefactoringKata.Tests/StatementPrinterTests.cs b/TheatricalPlayersRefactoringKata.Tests/IntegrationTests/StatementPrinterTests.cs similarity index 96% rename from TheatricalPlayersRefactoringKata.Tests/StatementPrinterTests.cs rename to TheatricalPlayersRefactoringKata.Tests/IntegrationTests/StatementPrinterTests.cs index 2a8cebd2..a126fa8b 100644 --- a/TheatricalPlayersRefactoringKata.Tests/StatementPrinterTests.cs +++ b/TheatricalPlayersRefactoringKata.Tests/IntegrationTests/StatementPrinterTests.cs @@ -1,100 +1,100 @@ -using System.Collections.Generic; -using ApprovalTests; -using ApprovalTests.Reporters; -using TheatricalPlayersRefactoringKata.Application.Services.Printers; -using TheatricalPlayersRefactoringKata.Domain.Entities; -using Xunit; - -namespace TheatricalPlayersRefactoringKata.Tests; - -public class StatementPrinterTests -{ - [Fact] - [UseReporter(typeof(DiffReporter))] - public void TestStatementExampleLegacy() - { - var plays = new Dictionary(); - 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")); - - Invoice invoice = new Invoice( - "BigCo", - new List - { - new Performance("hamlet", 55), - new Performance("as-like", 35), - new Performance("othello", 40), - } - ); - - //StatementPrinter statementPrinter = new StatementPrinter(); deprecated - TextStatementPrinterService statementPrinter = new TextStatementPrinterService(); - var result = statementPrinter.Print(invoice, plays); - - Approvals.Verify(result); - } - - [Fact] - [UseReporter(typeof(DiffReporter))] - public void TestTextStatementExample() - { - var plays = new Dictionary(); - 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")); - - Invoice invoice = new Invoice( - "BigCo", - new List - { - 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) - } - ); - - //StatementPrinter statementPrinter = new StatementPrinter(); deprecated - TextStatementPrinterService statementPrinter = new TextStatementPrinterService(); - var result = statementPrinter.Print(invoice, plays); - - Approvals.Verify(result); - } - - [Fact] - [UseReporter(typeof(DiffReporter))] - public void TestXmlStatementExample() - { - var plays = new Dictionary(); - 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")); - - Invoice invoice = new Invoice( - "BigCo", - new List - { - 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) - } - ); - - XmlStatementPrinterService statementPrinter = new XmlStatementPrinterService(); - var result = statementPrinter.Print(invoice, plays); - - Approvals.Verify(result); - } -} +using System.Collections.Generic; +using ApprovalTests; +using ApprovalTests.Reporters; +using TheatricalPlayersRefactoringKata.Application.Services.Printers; +using TheatricalPlayersRefactoringKata.Domain.Entities; +using Xunit; + +namespace TheatricalPlayersRefactoringKata.Tests.IntegrationTests; + +public class StatementPrinterTests +{ + [Fact] + [UseReporter(typeof(DiffReporter))] + public void TestStatementExampleLegacy() + { + var plays = new Dictionary(); + 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")); + + Invoice invoice = new Invoice( + "BigCo", + new List + { + new Performance("hamlet", 55), + new Performance("as-like", 35), + new Performance("othello", 40), + } + ); + + //StatementPrinter statementPrinter = new StatementPrinter(); deprecated + TextStatementPrinterService statementPrinter = new TextStatementPrinterService(); + var result = statementPrinter.Print(invoice, plays); + + Approvals.Verify(result); + } + + [Fact] + [UseReporter(typeof(DiffReporter))] + public void TestTextStatementExample() + { + var plays = new Dictionary(); + 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")); + + Invoice invoice = new Invoice( + "BigCo", + new List + { + 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) + } + ); + + //StatementPrinter statementPrinter = new StatementPrinter(); deprecated + TextStatementPrinterService statementPrinter = new TextStatementPrinterService(); + var result = statementPrinter.Print(invoice, plays); + + Approvals.Verify(result); + } + + [Fact] + [UseReporter(typeof(DiffReporter))] + public void TestXmlStatementExample() + { + var plays = new Dictionary(); + 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")); + + Invoice invoice = new Invoice( + "BigCo", + new List + { + 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) + } + ); + + XmlStatementPrinterService statementPrinter = new XmlStatementPrinterService(); + var result = statementPrinter.Print(invoice, plays); + + Approvals.Verify(result); + } +} diff --git a/TheatricalPlayersRefactoringKata.Tests/TheatricalPlayersRefactoringKata.Tests.csproj b/TheatricalPlayersRefactoringKata.Tests/TheatricalPlayersRefactoringKata.Tests.csproj index 7d5b6948..c9c3bced 100644 --- a/TheatricalPlayersRefactoringKata.Tests/TheatricalPlayersRefactoringKata.Tests.csproj +++ b/TheatricalPlayersRefactoringKata.Tests/TheatricalPlayersRefactoringKata.Tests.csproj @@ -1,4 +1,4 @@ - + net8.0 diff --git a/TheatricalPlayersRefactoringKata.Tests/UnitTests/ComedyCalculatorTests.cs b/TheatricalPlayersRefactoringKata.Tests/UnitTests/ComedyCalculatorTests.cs new file mode 100644 index 00000000..2772da89 --- /dev/null +++ b/TheatricalPlayersRefactoringKata.Tests/UnitTests/ComedyCalculatorTests.cs @@ -0,0 +1,57 @@ +using System.Collections.Generic; +using TheatricalPlayersRefactoringKata.Application.UseCases.Factories; +using TheatricalPlayersRefactoringKata.Domain.Entities; +using Xunit; + +namespace TheatricalPlayersRefactoringKata.Tests.UnitTests +{ + public class ComedyCalculatorTests + { + [Fact] + public void TestCalculateAmount() + { + var plays = new Dictionary(); + plays.Add("as-like", new Play("As You Like It", 2670, "comedy")); + + Invoice invoice = new Invoice( + "BigCo", + new List + { + new Performance("as-like", 35), + } + ); + + Performance perf = invoice.Performances[0]; + var play = plays[perf.PlayId]; + var calculator = TheatricalCalculatorFactory.GetCalculator(play.Type); + + var thisAmount = calculator.CalculateAmount(perf, play); + var volumeCredits = calculator.CalculateVolumeCredits(perf); + + Assert.Equal(54700m, thisAmount); + } + + [Fact] + public void TestCalculateVolumeCredits() + { + var plays = new Dictionary(); + plays.Add("as-like", new Play("As You Like It", 2670, "comedy")); + + Invoice invoice = new Invoice( + "BigCo", + new List + { + new Performance("as-like", 35), + } + ); + + Performance perf = invoice.Performances[0]; + var play = plays[perf.PlayId]; + var calculator = TheatricalCalculatorFactory.GetCalculator(play.Type); + + var volumeCredits = calculator.CalculateVolumeCredits(perf); + + Assert.Equal(12, volumeCredits); + } + } +} diff --git a/TheatricalPlayersRefactoringKata.Tests/UnitTests/HistoryCalculatorTests.cs b/TheatricalPlayersRefactoringKata.Tests/UnitTests/HistoryCalculatorTests.cs new file mode 100644 index 00000000..74f5d41a --- /dev/null +++ b/TheatricalPlayersRefactoringKata.Tests/UnitTests/HistoryCalculatorTests.cs @@ -0,0 +1,56 @@ +using System.Collections.Generic; +using TheatricalPlayersRefactoringKata.Application.UseCases.Factories; +using TheatricalPlayersRefactoringKata.Domain.Entities; +using Xunit; + +namespace TheatricalPlayersRefactoringKata.Tests.UnitTests +{ + public class HistoryCalculatorTests + { + [Fact] + public void TestCalculateAmount() + { + var plays = new Dictionary(); + plays.Add("henry-v", new Play("Henry V", 3227, "history")); + + Invoice invoice = new Invoice( + "BigCo", + new List + { + new Performance("henry-v", 20), + } + ); + + Performance perf = invoice.Performances[0]; + var play = plays[perf.PlayId]; + var calculator = TheatricalCalculatorFactory.GetCalculator(play.Type); + + var thisAmount = calculator.CalculateAmount(perf, play); + + Assert.Equal(70540m, thisAmount); + } + + [Fact] + public void TestCalculateVolumeCredits() + { + var plays = new Dictionary(); + plays.Add("henry-v", new Play("Henry V", 3227, "history")); + + Invoice invoice = new Invoice( + "BigCo", + new List + { + new Performance("henry-v", 20), + } + ); + + Performance perf = invoice.Performances[0]; + var play = plays[perf.PlayId]; + var calculator = TheatricalCalculatorFactory.GetCalculator(play.Type); + + var volumeCredits = calculator.CalculateVolumeCredits(perf); + + Assert.Equal(0, volumeCredits); + } + } +} diff --git a/TheatricalPlayersRefactoringKata.Tests/UnitTests/TragedyCalculatorTests.cs b/TheatricalPlayersRefactoringKata.Tests/UnitTests/TragedyCalculatorTests.cs new file mode 100644 index 00000000..2afc3055 --- /dev/null +++ b/TheatricalPlayersRefactoringKata.Tests/UnitTests/TragedyCalculatorTests.cs @@ -0,0 +1,57 @@ +using System.Collections.Generic; +using TheatricalPlayersRefactoringKata.Application.UseCases.Factories; +using TheatricalPlayersRefactoringKata.Domain.Entities; +using Xunit; + +namespace TheatricalPlayersRefactoringKata.Tests.UnitTests +{ + public class TragedyCalculatorTests + { + [Fact] + public void TestCalculateAmount() + { + var plays = new Dictionary(); + plays.Add("hamlet", new Play("Hamlet", 4024, "tragedy")); + + Invoice invoice = new Invoice( + "BigCo", + new List + { + new Performance("hamlet", 55), + } + ); + + Performance perf = invoice.Performances[0]; + var play = plays[perf.PlayId]; + var calculator = TheatricalCalculatorFactory.GetCalculator(play.Type); + + var thisAmount = calculator.CalculateAmount(perf, play); + var volumeCredits = calculator.CalculateVolumeCredits(perf); + + Assert.Equal(65000m, thisAmount); + } + + [Fact] + public void TestCalculateVolumeCredits() + { + var plays = new Dictionary(); + plays.Add("hamlet", new Play("Hamlet", 4024, "tragedy")); + + Invoice invoice = new Invoice( + "BigCo", + new List + { + new Performance("hamlet", 55), + } + ); + + Performance perf = invoice.Performances[0]; + var play = plays[perf.PlayId]; + var calculator = TheatricalCalculatorFactory.GetCalculator(play.Type); + + var volumeCredits = calculator.CalculateVolumeCredits(perf); + + Assert.Equal(25, volumeCredits); + } + } +}