コード例 #1
0
        public static IActionResult Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");

            var invoiceBuilder = new InvoiceBuilder
            {
                Init = () =>
                {
                },
                GetRequestStream  = () => new MemoryStream(Properties.Resources.Request),
                GetTemplateStream = (template) => new MemoryStream(Properties.Resources.Invoice),
                Output            = (pdf) =>
                {
                    using (var stream = new MemoryStream())
                    {
                        stream.Write(pdf, 0, pdf.Length);
                    }
                }
            };

            invoiceBuilder.Build();

            return(new OkObjectResult(invoiceBuilder.ToString()));
        }
コード例 #2
0
        public virtual ActionResult Download()
        {
            var rentalResult = Session["LastOrderResult"] as RentalResult;

            if (rentalResult == null)
            {
                return(RedirectToAction(MVC.Home.Index()));
            }

            var invoice = InvoiceBuilder.Build(CurrentUser, rentalResult);
            var content = Encoding.UTF8.GetBytes(invoice);

            return(File(content, "text/plain", "invoice.txt"));
        }
コード例 #3
0
        private static InvoiceBuilder Build(int number)
        {
            System.Console.WriteLine($"Build No.{number}");
            var invoiceBuilder = new InvoiceBuilder
            {
                Init              = () => { },
                GetRequestStream  = () => new MemoryStream(Properties.Resources.Request),
                GetTemplateStream = (template) => new MemoryStream(Properties.Resources.Invoice),
                Output            = (pdf) =>
                {
                    using (var stream = new MemoryStream())
                    {
                        stream.Write(pdf, 0, pdf.Length);
                    }
                }
            };

            invoiceBuilder.Build();
            return(invoiceBuilder);
        }
コード例 #4
0
        public static async Task Run(
            [EventGridTrigger] EventGridEvent eventGridEvent,
            [Blob("{data.url}", FileAccess.Read, Connection = "AzureWebJobsStorage")] Stream input,
            [Queue("diodocsqueue"), StorageAccount("AzureWebJobsStorage")] ICollector <string> msg,
            ILogger log)
        {
            var             createdEvent   = ((JObject)eventGridEvent.Data).ToObject <StorageBlobCreatedEventData>();
            CloudBlobClient blobClient     = null;
            var             invoiceBuilder = new InvoiceBuilder
            {
                Init = () =>
                {
                    var storageAccount = CloudStorageAccount.Parse(BlobStorageConnectionString);
                    blobClient = storageAccount.CreateCloudBlobClient();
                },
                GetRequestStream  = () => input,
                GetTemplateStream = (template) =>
                {
                    var container = blobClient.GetContainerReference("excel");
                    var blockBlob = container.GetBlockBlobReference(template);
                    return(blockBlob.OpenReadAsync().GetAwaiter().GetResult());
                },
                Output = (pdf) =>
                {
                    var container = blobClient.GetContainerReference("pdf");

                    var uri       = new Uri(createdEvent.Url);
                    var cloudBlob = new CloudBlob(uri);

                    var blobName  = cloudBlob.Name;
                    var blockBlob = container.GetBlockBlobReference(blobName + ".pdf");
                    blockBlob.UploadFromByteArrayAsync(pdf, 0, pdf.Length).GetAwaiter().GetResult();
                }
            };

            invoiceBuilder.Build();

            msg.Add($"Completed. {invoiceBuilder}");
        }
コード例 #5
0
        public ActionResult <IEnumerable <string> > Get()
        {
            var invoiceBuilder = new InvoiceBuilder
            {
                Init = () =>
                {
                },
                GetRequestStream  = () => new MemoryStream(Properties.Resources.Request),
                GetTemplateStream = (template) => new MemoryStream(Properties.Resources.Invoice),
                Output            = (pdf) =>
                {
                    using (var stream = new MemoryStream())
                    {
                        stream.Write(pdf, 0, pdf.Length);
                    }
                }
            };

            invoiceBuilder.Build();

            return(invoiceBuilder.MeasurementResults.Select(x => $"{x.Item1}:{x.Item2}").ToArray());
        }
コード例 #6
0
        public void CalculatePriceOnNewInvoice()
        {
            var invoice = _invoiceBuilder.Build();

            Assert.AreEqual(0, invoice.InvoiceTotal);
        }
コード例 #7
0
        public void CalculatePriceOnNewInvoice()
        {
            var invoice = _invoiceBuilder.Build();

            invoice.InvoiceTotal.Should().Be(0);
        }
コード例 #8
0
        static void Main(string[] args)
        {
            #region Strategy

            Orcamento orcamento = new Orcamento();
            orcamento.Add(new Item("CANETA", 250.0));
            orcamento.Add(new Item("LAPIS", 250.0));

            IImposto novoImposto = new ICCC();
            Console.WriteLine(novoImposto.Calcula(orcamento));

            Console.ReadKey();

            #endregion

            #region Chain of Responsibility

            CalculadorDeDescontos calculador = new CalculadorDeDescontos();

            double desconto = calculador.Calcula(orcamento);

            Console.WriteLine(desconto);

            Console.ReadKey();

            List <Request> requests = new List <Request>();

            var xmlRequest     = new Request(Format.XML);
            var csvRequest     = new Request(Format.CSV);
            var percentRequest = new Request(Format.PERCENT);

            requests.Add(xmlRequest);
            requests.Add(csvRequest);
            requests.Add(percentRequest);

            var account     = new Account("Raphael", "3252-2", 17508, 6000.99);
            var bankRequest = new BankRequester();

            requests.ForEach(x => bankRequest.Response(x, account));
            Console.ReadKey();

            #endregion

            #region Template Method

            orcamento = new Orcamento();
            orcamento.Add(new Item("CANETA", 250.0));
            orcamento.Add(new Item("LAPIS", 250.0));
            orcamento.Add(new Item("APAGADOR", 50.0));
            orcamento.Add(new Item("BORRACHA", 10.0));
            orcamento.Add(new Item("BORRACHA", 10.0));
            orcamento.Add(new Item("BORRACHA", 10.0));

            ICPP   iCPP = new ICPP();
            double tax;
            tax = iCPP.Calcula(orcamento);
            Console.WriteLine($"Valor do ICPP: {tax} ");

            IKCV iKCV = new IKCV();
            tax = iKCV.Calcula(orcamento);
            Console.WriteLine($"Valor do IKCV: {tax} ");

            var iHIT = new IHIT();
            tax = iHIT.Calcula(orcamento);
            Console.WriteLine($"Valor do IHIT: {tax} ");

            Console.ReadKey();

            Bank bank = new Bank()
            {
                CorporateName = "Banco do Brasil S/A",
                Phone         = "6666-12345",
                Address       = "Av. Borges de Medeiros, Nº123",
                Email         = "*****@*****.**",
                Date          = DateTime.Now
            };

            account = new Account("Pintado", "3252-2", 17508, 6000.99);

            IList <Account> accounts = new List <Account>();
            accounts.Add(account);

            account = new Account("Raphael", "1404-5", 52223, 8500);
            accounts.Add(account);

            Console.WriteLine("Imprimindo relatório simplificado... ");
            IReport report = new SimpleReport();
            report.Print(accounts, bank);
            Console.WriteLine();
            Console.WriteLine();

            Console.WriteLine("Imprimindo relatório completo... ");
            report = new ComplexReport();
            report.Print(accounts, bank);

            Console.ReadKey();

            #endregion

            #region Decorator e Comportamentos Compostos

            var iss     = new ISS(new ICMS(new IKCV()));
            var imposto = new ImpostoAlto(new ICPP(new IHIT(new IKCV())));

            orcamento = new Orcamento();
            orcamento.Add(new Item("CANETA", 250.0));
            orcamento.Add(new Item("LAPIS", 250.0));

            double valor = imposto.Calcula(orcamento);
            Console.WriteLine($"Imposto Alto: R${valor}");
            valor = iss.Calcula(orcamento);
            Console.WriteLine($"Valor do ISS: R${valor}");
            Console.ReadKey();

            account = new Account("Pintado", "3252-2", 17508, new DateTime(2015, 05, 07));
            account.Deposit(50);

            accounts = new List <Account>();
            accounts.Add(account);

            account = new Account("Raphael", "1404-5", 52223, new DateTime(2015, 05, 07));
            account.Deposit(850000);

            accounts.Add(account);

            account = new Account("Teste", "3252-2", 666, 6500);

            accounts.Add(account);

            var filter = new CurrentMonthAccounts(new LowBallanceAccounts());

            var filteredAccounts = filter.Search(accounts);

            bank = new Bank()
            {
                CorporateName = "Banco do Brasil S/A",
                Phone         = "6666-12345",
                Address       = "Av. Borges de Medeiros, Nº123",
                Email         = "*****@*****.**",
                Date          = DateTime.Now
            };

            report = new ComplexReport();
            report.Print(filteredAccounts, bank);
            Console.ReadKey();

            #endregion

            #region State

            orcamento = new Orcamento();
            orcamento.Add(new Item("Estados do Orçamento", 500));

            Console.WriteLine($"Valor do Orçamento: R${orcamento.Valor}");

            orcamento.ApplyExtraDiscount();
            Console.WriteLine($"Orçamento em aprovação... Desconto extra de R${orcamento.Desconto}");

            //orcamento.ApplyExtraDiscount(); teste para não aplicar desconto novamente no estado "Em aprovação"

            orcamento.Approves();
            Console.WriteLine("Orçamento aprovado!");

            orcamento.ApplyExtraDiscount();
            Console.WriteLine($"Desconto extra de R${orcamento.Desconto}");

            orcamento.Finalized();
            Console.WriteLine("Orçamento finalizado!");
            Console.WriteLine($"Orçamento final de R${orcamento.Valor}");

            Console.ReadKey();

            account = new Account("Pintado", "3252-2", 17508, 5000);
            account.CashWithDrawal(6000);
            Console.WriteLine("Saque Realizado!");
            Console.WriteLine($"Nome:{account.Owner}, Saldo R${account.Balance}");

            account.CashWithDrawal(2000);
            account.Deposit(2000);
            Console.WriteLine("Depósito Realizado!");
            Console.WriteLine($"Nome:{account.Owner}, Saldo R${account.Balance}");

            account.Deposit(2000);
            Console.WriteLine("Depósito Realizado!");
            Console.WriteLine($"Nome:{account.Owner}, Saldo R${account.Balance}");

            Console.ReadKey();

            #endregion

            #region Builder

            var builder = new InvoiceBuilder();
            builder.ToCompany("Alura Design Patterns LTDA.")
            .AddCnpj("45.429.459/0001-43")
            .Add(new InvoiceItem("Item 1", 100.0))
            .Add(new InvoiceItem("Item 2", 200.0))
            .AddObservation("Add some observation");

            #region Observer Pattern
            Console.WriteLine();
            Console.WriteLine("Executando ações de Observer pattern...");
            builder.AddAction(new EmailSender())
            .AddAction(new InvoiceDAO())
            .AddAction(new SmsSender())
            .AddAction(new Multiplier(5));
            Console.WriteLine();
            #endregion

            var nf = builder.Build();

            Console.WriteLine();
            Console.WriteLine($"Empresa: {nf.CorporateName}");
            Console.WriteLine($"CNPJ {nf.Cnpj}");
            Console.WriteLine($"Valor da Nota: R${nf.TotalValue}");
            Console.WriteLine($"Taxas: R${nf.Taxes}");
            Console.WriteLine($"Observações: {nf.Observations}");

            Console.ReadKey();

            #endregion
        }