public void Handle_UsersIsEmpty_ShouldThrowValidationException() { var updateCommand = new UpdateReceiptItemCommand { Count = 1235, ItemGroup = (int)ItemGroup.Essentials, Price = 231.32321, Id = "asdasdasd", UserDtos = new List <UserDto> { }, FinancialProjectId = "asd" }; FluentActions.Awaiting(() => SendAsync(updateCommand)).Should().Throw <ValidationException>(); }
public void Handle_InvalidId_ShouldThrowNotFoundException() { var updateCommand = new UpdateReceiptItemCommand { Count = 1235, ItemGroup = (int)ItemGroup.Essentials, Price = 231.32321, Id = "asdasdas", UserDtos = new List <UserDto> { new UserDto { Id = User.Id } }, FinancialProjectId = "asd" }; FluentActions.Awaiting(() => SendAsync(updateCommand)).Should().Throw <NotFoundException>(); }
public void Handle_ItemGroupLessThanZero_ShouldThrowValidationException() { var updateCommand = new UpdateReceiptItemCommand { Count = 1235, Price = 231.32321, Id = "dont even matter lmao", ItemGroup = -1, UserDtos = new List <UserDto> { new UserDto { Id = User.Id } }, FinancialProjectId = "asd" }; FluentActions.Awaiting(() => SendAsync(updateCommand)).Should().Throw <ValidationException>(); }
public async Task Handle_ValidId_ShouldUpdateEntity(int price, int updatedPrice) { var newUser = await CreateNewUser("kekw", "kekw"); var project = await CreateFinancialProject(newUser); var receiptId = await CreateReceipt(project); var createCommand = new CreateReceiptItemCommand { ReceiptId = receiptId, Price = price, ItemGroup = (int)ItemGroup.Essentials, Count = 2, Name = "das", UserIds = new List <string> { User.Id, newUser.Id } }; var itemId = await SendAsync(createCommand); var updateCommand = new UpdateReceiptItemCommand { ItemGroup = (int)ItemGroup.Miscellaneous, Price = updatedPrice, Count = 2, Id = itemId, UserDtos = new List <UserDto> { new UserDto { Id = User.Id }, new UserDto { Id = SecondUser.Id } }, FinancialProjectId = project }; await SendAsync(updateCommand); var context = CreateContext(); var entity = await context.ReceiptItems .Include(x => x.Users) .FirstOrDefaultAsync(x => x.Id == itemId); entity.Should().NotBeNull(); entity.Count.Should().Be(updateCommand.Count); entity.ItemGroup.Should().Be(updateCommand.ItemGroup); entity.Price.Should().Be(updateCommand.Price); entity.Users.Count.Should().Be(2); entity.Users.Any(x => x.ApplicationUserId == User.Id).Should().Be(true); entity.Users.Any(x => x.ApplicationUserId == SecondUser.Id).Should().Be(true); entity.LastModified.Should().BeCloseTo(DateTime.Now, 1000); entity.LastModifiedBy.Should().Be(User.Id); //owerecord var newOweRecord = context.OweRecords .FirstOrDefault(x => x.UserId == SecondUser.Id && x.OwedUserId == User.Id); newOweRecord.Amount.Should().Be( updateCommand.Price * updateCommand.Count / updateCommand.UserDtos.Count); var oldOweRecord = context.OweRecords .FirstOrDefault(x => x.UserId == newUser.Id && x.OwedUserId == User.Id); oldOweRecord.Amount.Should().Be(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 }) })); }