public async Task Handle_ValidFields_ShouldUpdate()
        {
            var newProject = await CreateFinancialProject();

            var projectId = await CreateFinancialProject();

            var id = await CreateReceipt(projectId);

            var command = new UpdateReceiptCommand
            {
                Id                 = id,
                Location           = "Netto",
                DateVisited        = DateTime.Now,
                Note               = "meh",
                FinancialProjectId = newProject
            };


            await SendAsync(command);

            var context = CreateContext();
            var entity  = context.Receipts.FirstOrDefault(x => x.Id == command.Id);

            entity.Should().NotBeNull();
            entity.Note.Should().Be(command.Note);
            entity.FinancialProjectId.Should().Be(newProject);
            entity.Location.Should().Be(command.Location);
            entity.DateVisited.Should().BeCloseTo(DateTime.Now, 1000);
            entity.LastModified.Should().BeCloseTo(DateTime.Now, 1000);
            entity.LastModifiedBy.Should().Be(User.Id);
        }
        public void Handle_BoughtAtEmpty_ShouldThrowValidationException()
        {
            var command = new UpdateReceiptCommand
            {
                Id       = "asdadas",
                Location = "Title",
            };

            FluentActions.Invoking(async() => await SendAsync(command)).Should().Throw <ValidationException>();
        }
        public void Handle_IdEmpty_ShouldThrowValidationException()
        {
            var command = new UpdateReceiptCommand
            {
                Id          = "",
                Location    = "Title",
                DateVisited = DateTime.Now
            };

            FluentActions.Invoking(async() => await SendAsync(command)).Should().Throw <ValidationException>();
        }
        public void Handle_InvalidId_ShouldThrowNotFoundException()
        {
            var command = new UpdateReceiptCommand
            {
                Id          = "nah",
                Location    = "Title",
                DateVisited = DateTime.Now,
            };

            FluentActions.Invoking(async() => await SendAsync(command)).Should().Throw <NotFoundException>();
        }
        public void Handle_TitleAboveMaxLength_ShouldThrowValidationException()
        {
            var command = new UpdateReceiptCommand
            {
                Id       = "asdadas",
                Location = "dafhdsugjhsfdosjdfjiodsfoijdsfjiosdfsdfdfsdfsdfsdfsfsdfsdfsdf" +
                           "dfgdfgdfgojifdsjoifsdjoisdfojisdfjoisdfjoifsojdijoisdfjoifsdoij",
                DateVisited = DateTime.Now
            };

            FluentActions.Invoking(async() => await SendAsync(command)).Should().Throw <ValidationException>();
        }
        public async Task Handle_WithoutFinancialProjectId_ShouldUpdate()
        {
            var projectId = await CreateFinancialProject();

            var id = await CreateReceipt(projectId);

            var command = new UpdateReceiptCommand
            {
                Id          = id,
                Location    = "Title",
                DateVisited = DateTime.Now,
            };

            await SendAsync(command);

            var entity = await FindAsync <Receipt>(command.Id);

            entity.Should().NotBeNull();
            entity.FinancialProjectId.Should().Be(projectId);
            entity.LastModified.Should().BeCloseTo(DateTime.Now, 1000);
            entity.LastModifiedBy.Should().Be(User.Id);
            entity.DateVisited.Should().BeCloseTo(DateTime.Now, 1000);
            entity.Location.Should().Be(command.Location);
        }
Example #7
0
        public async Task <IActionResult> EditReceiptPost(CreateReceiptModel model)
        {
            //update receipt
            var command = new UpdateReceiptCommand
            {
                Id          = model.ReceiptDto.Id,
                Location    = model.ReceiptDto.Location,
                DateVisited = model.ReceiptDto.DateVisited,
                Note        = model.ReceiptDto.Note
            };

            await Mediator.Send(command);

            //get already existing receipt items
            var itemQuery = new GetReceiptItemByReceiptIdQuery
            {
                ReceiptId = model.ReceiptDto.Id
            };

            var alreadyExistingItems = await Mediator.Send(itemQuery);

            #region Delete

            //delete receipt item - own request

            var receiptItemsToDelete = alreadyExistingItems
                                       .Where(x => model.ReceiptDto.Items
                                              .All(y => y.Id != x.Id))
                                       .ToList();

            foreach (var itemDto in receiptItemsToDelete)
            {
                var deleteCommand = new DeleteReceiptItemCommand
                {
                    Id = itemDto.Id !,
                    FinancialProjectId = model.FinancialProjectId
                };

                await Mediator.Send(deleteCommand);
            }

            #endregion

            #region Create

            //Create receipt item
            var receiptItemToBeCreated = model.ReceiptDto.Items
                                         .Where(x => alreadyExistingItems
                                                .All(y => y.Id != x.Id))
                                         .ToList();

            foreach (var receiptItemDto in receiptItemToBeCreated)
            {
                var createCommand = new CreateReceiptItemCommand
                {
                    Name      = "",
                    Count     = receiptItemDto.Count,
                    Price     = receiptItemDto.Price,
                    ItemGroup = receiptItemDto.ItemGroup.Value,
                    ReceiptId = model.ReceiptDto.Id,
                    UserIds   = receiptItemDto.Users.Select(x => x.Id).ToList() !
                };

                await Mediator.Send(createCommand);
            }

            #endregion

            #region Update

            //update receipt item - own request
            var receiptItemsToUpdate = model.ReceiptDto.Items
                                       .Where(x => receiptItemsToDelete
                                              .All(y => y.Id != x.Id) &&
                                              receiptItemToBeCreated.All(q => q.Id != x.Id))
                                       .ToList();

            foreach (var receiptItemDto in receiptItemsToUpdate)
            {
                var updateCommand = new UpdateReceiptItemCommand
                {
                    Id                 = receiptItemDto.Id ?? "",
                    Count              = receiptItemDto.Count,
                    ItemGroup          = receiptItemDto.ItemGroup.Value,
                    Price              = receiptItemDto.Price,
                    UserDtos           = receiptItemDto.Users.ToList(),
                    FinancialProjectId = model.FinancialProjectId
                };
                await Mediator.Send(updateCommand);
            }

            #endregion

            return(Json(new
            {
                result = "Redirect",
                url = Url.Action("Index", "Project", new { id = model.FinancialProjectId })
            }));
        }