public void ModifyInvoice_ReplacesInvoiceLines()
        {
            // arrange
            var context = new CBAContext(dboptions);
            var seeder  = new CBASeeder(context, cryptography);

            seeder.Seed();

            var service         = new InvoiceService(context, options, mapper, pdf, logger);
            var originalInvoice = context.Invoice.Include("InvoiceLine")
                                  .SingleOrDefault(t => t.InvoiceNumber == "ABNZ000420");

            var modifiedInvoice = new InvoiceForUpdateDto()
            {
                // all fields should be the same except InvoiceLine
                ClientName          = originalInvoice.ClientName,
                ClientContact       = originalInvoice.ClientContact,
                ClientContactPerson = originalInvoice.ClientContactPerson,
                DateDue             = originalInvoice.DateDue,
                Email = originalInvoice.Email,
                PurchaseOrderNumber = originalInvoice.PurchaseOrderNumber,

                InvoiceLine = new List <InvoiceLineDto>()
                {
                    new InvoiceLineDto()
                    {
                        Description = "New Dinner",
                        Amount      = 50
                    },
                    new InvoiceLineDto()
                    {
                        Description = "New Cookie",
                        Amount      = 10
                    }
                }
            };

            var expectedLineCount = context.InvoiceLine.Count()
                                    - originalInvoice.InvoiceLine.Count()
                                    + modifiedInvoice.InvoiceLine.Count();

            // act
            service.ModifyInvoice("ABNZ000420", modifiedInvoice);
            var lineCount = context.InvoiceLine.Count();

            // assert
            Assert.AreEqual(expectedLineCount, lineCount);
        }
Ejemplo n.º 2
0
        public void ModifyInvoice(string invoiceNumber, InvoiceForUpdateDto invoice)
        {
            var invoiceToUpdate = GetInvoice(invoiceNumber);

            if (invoiceToUpdate.Status != InvoiceStatus.Draft)
            {
                throw new InvalidOperationException($"Invoice {invoiceNumber} is not a draft and may not be modified");
            }

            // apply invoice to invoiceToUpdate
            mapper.Map(invoice, invoiceToUpdate);

            Validate(invoiceToUpdate);

            context.SaveChanges();
        }
        public void ModifyInvoice_UpdatesInvoiceLines_WhenOnlyInvoiceLinesModified()
        {
            // arrange
            var context = new CBAContext(dboptions);
            var seeder  = new CBASeeder(context, cryptography);

            seeder.Seed();

            var     service         = new InvoiceService(context, options, mapper, pdf, logger);
            Invoice invoiceToUpdate = context.Invoice.Include("InvoiceLine")
                                      .SingleOrDefault(t => t.InvoiceNumber == "ABNZ000420");

            InvoiceForUpdateDto invoice = new InvoiceForUpdateDto()
            {
                // all fields should be the same except InvoiceLine
                ClientName          = invoiceToUpdate.ClientName,
                ClientContact       = invoiceToUpdate.ClientContact,
                ClientContactPerson = invoiceToUpdate.ClientContactPerson,
                DateDue             = invoiceToUpdate.DateDue,
                Email = invoiceToUpdate.Email,
                PurchaseOrderNumber = invoiceToUpdate.PurchaseOrderNumber
            };

            invoice.InvoiceLine = new List <InvoiceLineDto>()
            {
                new InvoiceLineDto()
                {
                    Description = "New Dinner",
                    Amount      = 50
                },
                new InvoiceLineDto()
                {
                    Description = "New Cookie",
                    Amount      = 10
                }
            };

            // act
            service.ModifyInvoice("ABNZ000420", invoice);

            // assert
            var cleancontext = new CBAContext(dboptions);

            Assert.AreEqual(2, cleancontext.Invoice.Include("InvoiceLine")
                            .SingleOrDefault(t => t.InvoiceNumber == "ABNZ000420").InvoiceLine.Count());
        }
Ejemplo n.º 4
0
        public IActionResult ModifyInvoice([FromRoute] string invoiceNumber,
                                           [FromBody] InvoiceForUpdateDto invoice)
        {
            try
            {
                if (!service.InvoiceExists(invoiceNumber))
                {
                    return(NotFound());
                }

                service.ModifyInvoice(invoiceNumber, invoice);

                return(Ok(mapper.Map <InvoiceDto>(service.GetInvoice(invoiceNumber))));
            }
            catch (InvalidOperationException ex)
            {
                return(BadRequest(ex.Message));
            }
            catch (ValidationException ex)
            {
                return(BadRequest(ex.Message));
            }
        }
        public async Task <IActionResult> UpdateInvoice(int id, [FromBody] InvoiceForUpdateDto invoice)
        {
            try
            {
                if (invoice == null)
                {
                    _logger.LogError("Invoice received is a Null Object.");
                    return(BadRequest("Invoice object is null. Please send full request."));
                }
                else if (!ModelState.IsValid)
                {
                    _logger.LogError("Invalid Invoice object sent from client.");
                    return(BadRequest("Invoice object is not Valid"));
                }

                Boolean rentExist = await _repositoryWrapper.Rent.CheckIfRentExistByRentId(invoice.RentId);

                if (!rentExist)
                {
                    _logger.LogError($"Invoice cannot be updated, because Rent with id:{invoice.RentId} does not exist in DB.");
                    return(BadRequest($"Not updated. Rent wiht id: {invoice.RentId} does not exist."));
                }

                Boolean stateHasInvoice = await _repositoryWrapper.Invoice.CheckIfInvoiceExistByStateId(invoice.StateId);

                if (!stateHasInvoice)
                {
                    _logger.LogError($"Invoice with id: {id}, hasn't been found in db.");
                    return(NotFound($"Not updated.Invoice with id: {id} not found in DB."));
                }

                var invoiceEntity = await _repositoryWrapper.Invoice.GetInvoiceById(id);

                if (invoiceEntity is null)
                {
                    return(NotFound($"Not updated.Invoice with id: {id} not found in DB."));
                }

                _mapper.Map(invoice, invoiceEntity);
                _repositoryWrapper.Invoice.UpdateInvoice(invoiceEntity);

                await _repositoryWrapper.Save();

                /* Invoice document update in updated invoice */
                var pdfFileToInclude = await GetGeneratedPDF(invoiceEntity.Id);

                invoiceEntity.InvoiceDocument = pdfFileToInclude.getInvoiceBytes();
                invoiceEntity.FileName        = pdfFileToInclude.getInvoiceFileName();
                _repositoryWrapper.Invoice.UpdateInvoice(invoiceEntity);
                await _repositoryWrapper.Save();

                /* modification end 03.06.2020 */

                return(NoContent());
            }
            catch (Exception e)
            {
                _logger.LogError($"Something went wrong inside UpdateInvoice(id, invoiceForUpdateDto) action: {e.Message}");
                return(StatusCode(500, e.Message));
            }
        }