public async Task <PayingItem> CreateAsync(PayingItem item) { var insertedItem = await _payingItemService.CreateAsync(item); await _serviceTrigger.Insert(insertedItem); return(insertedItem); }
public void Can_Paginate() { DateTime date = DateTime.Today.AddDays(-2); var itemList = new PayingItem[] { new PayingItem() { AccountID = 4, CategoryID = 4, Comment = "PayingItem 4", Date = date, UserId = "1", Category = new Category() { Name = "Cat1" } }, new PayingItem() { AccountID = 1, CategoryID = 1, Comment = "PayingItem 1", Date = date, UserId = "1", Category = new Category() { Name = "Cat2" } }, new PayingItem() { AccountID = 2, CategoryID = 2, Comment = "PayingItem 2", Date = date, UserId = "1", Category = new Category() { Name = "Cat3" } }, new PayingItem() { AccountID = 3, CategoryID = 2, Comment = "PayingItem 3", Date = DateTime.Now - TimeSpan.FromDays(1), UserId = "1", Category = new Category() { Name = "Cat4" } } }; _payingItemServiceMock.Setup(m => m.GetList(It.IsAny <Expression <Func <PayingItem, bool> > >())).Returns(itemList.Where(i => i.Date >= DateTime.Today.AddDays(-2) && i.UserId == "1")); var target = new PayingItemController(_payingItemServiceMock.Object, null, null, null, null, null) { ItemsPerPage = 2 }; var pItemToView = ((PartialViewResult)target.List(new WebUser() { Id = "1" }, 2)).Model as PayingItemsCollectionModel; var result = pItemToView?.PayingItems.ToArray(); Assert.AreEqual(2, result.Count()); Assert.AreEqual(1, result[0].AccountID); Assert.AreEqual(2, result[1].AccountID); }
public async Task PayingItemIsNull_ReturnsNullViewModel() { PayingItem payingItem = null; _payingItemServiceMock.Setup(m => m.GetItemAsync(It.IsAny <int>())).ReturnsAsync(payingItem); var target = new PayingItemEditViewModelCreator(_payingItemServiceMock.Object); var result = await target.CreateViewModel(It.IsAny <int>()); Assert.AreEqual(null, result); }
private async Task <int> GetTypeOfFlowId(PayingItem pItem) { try { return((await _categoryService.GetItemAsync(pItem.CategoryID)).TypeOfFlowID); } catch (ServiceException e) { throw new WebUiException( $"Ошибка в контроллере {nameof(PayingItemController)} в методе {nameof(GetTypeOfFlowId)}", e); } }
private bool CheckForSubCategories(PayingItem item) { try { return(_payingItemService.GetList() .Any(x => x.CategoryID == item.CategoryID)); } catch (ServiceException e) { throw new WebUiException( $"Ошибка в контроллере {nameof(PayingItemController)} в методе {nameof(CheckForSubCategories)}", e); } }
public async Task UpdateAsync(PayingItem item) { try { await _repository.UpdateAsync(item); await _repository.SaveAsync(); } catch (DomainModelsException e) { throw new ServiceException($"Ошибка в сервисе {nameof(PayingItemService)} в методе {nameof(UpdateAsync)} при обращении к БД", e); } }
public async Task PartialCloseAsync_DebtTypeIncome_CategoryDoesNotExist_InputSum300_CreatesPayingItemWithSumm300() { PayingItem payingItem = null; _debtRepositoryMock.Setup(x => x.GetItemAsync(It.IsAny <int>())).ReturnsAsync(_listOfDebts[0]); _payingItemRepositoryMock.Setup(x => x.Create(It.IsAny <PayingItem>())) .Returns(new PayingItem()) .Callback <PayingItem>(x => payingItem = x); await _target.PartialCloseAsync(It.IsAny <int>(), 300, It.IsAny <int>()); Assert.AreEqual(300, payingItem.Summ); }
public async Task <PayingItem> CreateAsync(PayingItem item) { try { var createdItem = await _repository.CreateAsync(item); await _repository.SaveAsync(); return(createdItem); } catch (DomainModelsException e) { throw new ServiceException($"Ошибка в сервисе {nameof(PayingItemService)} в методе {nameof(CreateAsync)} при обращении к БД", e); } }
public async Task SetCorrectCommentFromViewModelIfProductsInItemAreNotNull() { var payingItem = new PayingItem() { ItemID = 1 }; _payinItemServiceMock.Setup(x => x.GetItemAsync(It.IsAny <int>())).ReturnsAsync(payingItem); var target = new PayingItemUpdater(_payinItemServiceMock.Object); var payingItemViewModel = CreatePayingItemViewModelWithCheckedProductsInItem(); var result = await target.UpdatePayingItemFromViewModel(payingItemViewModel); Assert.AreEqual("P1, P2, P3", result.Comment); }
public async Task SetCorrectSumIfProductsInItemAreUncheckedAndProductsNotInItemAreUnchecked() { var payingItem = new PayingItem() { ItemID = 1 }; _payinItemServiceMock.Setup(x => x.GetItemAsync(It.IsAny <int>())).ReturnsAsync(payingItem); var target = new PayingItemUpdater(_payinItemServiceMock.Object); var payingItemViewModel = new PayingItemEditModel() { PayingItem = new PayingItem() { ItemID = 1, Summ = 500M }, ProductsInItem = new List <Product>() { new Product() { ProductID = 0, Price = 200 }, new Product() { ProductID = 0, Price = 100 } }, ProductsNotInItem = new List <Product>() { new Product() { ProductID = 0, Price = 100 }, new Product() { ProductID = 0, Price = 200 } } }; var result = await target.UpdatePayingItemFromViewModel(payingItemViewModel); Assert.AreEqual(0, result.Summ); }
public async Task UpdateAsync(PayingItem item) { try { (var oldPayingItem, var newPayingItem) = await GetNewAndOldItems(item); _repository.Update(item); await _repository.SaveAsync(); await _serviceTrigger.Update(oldPayingItem, newPayingItem); } catch (DomainModelsException e) { throw new ServiceException($"Ошибка в сервисе {nameof(PayingItemService)} в методе {nameof(UpdateAsync)} при обращении к БД", e); } }
private async Task CreateOpenedDebtPayingItem(Debt debt) { var debtCategoryId = await GetDebtCategoryId(debt.UserId, debt.TypeOfFlowId); var payingItem = new PayingItem() { Date = debt.DateEnd ?? DateTime.Now, AccountID = debt.AccountId, CategoryID = debtCategoryId, Comment = debt.TypeOfFlowId == 1 ? "Взял деньги в долг" : "Дал деньги в долг", Summ = debt.Summ, UserId = debt.UserId }; _payingItemRepository.Create(payingItem); await _payingItemRepository.SaveAsync(); }
public async Task Edit_Cannot_Get_PayingItem_Returns_RedirectToRouteResult() { PayingItem pItem = null; _payingItemService.Setup(m => m.GetItemAsync(It.IsAny <int>())).ReturnsAsync(pItem); var target = new PayingItemController(null, null, _payingItemService.Object, _categoryService.Object, _accountService.Object); var result = await target.Edit(new WebUser() { Id = "1" }, 1, 5); var routes = (result as RedirectToRouteResult).RouteValues; Assert.IsInstanceOfType(result, typeof(RedirectToRouteResult)); Assert.AreEqual(routes["action"], "ListAjax"); }
private async Task <(PayingItem OldItem, PayingItem NewItem)> GetNewAndOldItems(PayingItem item) { var typeOfFlowId = (await _categoryService.GetItemAsync(item.CategoryID)).TypeOfFlowID; var categories = (await _typeOfFlowService.GetCategoriesAsync(typeOfFlowId)).Where(x => x.UserId == item.UserId); var oldCategoryId = await GetCategoryIdAsync(categories, item.ItemID); var oldCategory = await _categoryService.GetItemAsync(oldCategoryId); var oldPayingItem = oldCategory.PayingItems.FirstOrDefault(x => x.ItemID == item.ItemID); var newPayingItem = new PayingItem() { Category = oldCategory, AccountID = item.AccountID, Summ = item.Summ }; return(oldPayingItem, newPayingItem); }
private async Task CreateClosedDebtPayingItem(Debt debt, decimal sum = 0M) { var typeOfFlowId = debt.TypeOfFlowId == 1 ? 2 : 1; var categoryId = await GetDebtCategoryId(debt.UserId, typeOfFlowId); var payingItem = new PayingItem() { AccountID = debt.AccountId, Date = debt.DateEnd ?? DateTime.Now, CategoryID = categoryId, Comment = debt.TypeOfFlowId == 1 ? "Закрыл свой долг" : "Мне вернули долг", Summ = sum == 0M ? debt.Summ : sum, UserId = debt.UserId }; _payingItemRepository.Create(payingItem); await _payingItemRepository.SaveAsync(); }
public async Task UpdateAsync(PayingItem item) { var typeOfFlowId = (await _categoryService.GetItemAsync(item.CategoryID)).TypeOfFlowID; var categories = (await _typeOfFlowService.GetCategoriesAsync(typeOfFlowId)).Where(x => x.UserId == item.UserId); var oldCategoryId = await GetCategoryIdAsync(categories, item.ItemID); var oldCategory = await _categoryService.GetItemAsync(oldCategoryId); var oldPayingItem = oldCategory.PayingItem.FirstOrDefault(x => x.ItemID == item.ItemID); var newItem = new PayingItem() { Category = oldCategory, AccountID = item.AccountID, Summ = item.Summ }; await _payingItemService.UpdateAsync(item); await _serviceTrigger.Update(oldPayingItem, newItem); }
public async Task PayingItemContainsNoProducts_CategoryContainsNoProducts() { var payingItem = new PayingItem() { Category = new Category() { Products = new List <Product>() }, PayingItemProducts = new List <PayingItemProduct>() }; _payingItemServiceMock.Setup(m => m.GetItemAsync(It.IsAny <int>())).ReturnsAsync(payingItem); var target = new PayingItemEditViewModelCreator(_payingItemServiceMock.Object); var result = await target.CreateViewModel(1); Assert.AreEqual(0, result.ProductsInItem.Count); Assert.AreEqual(0, result.ProductsNotInItem.Count); }
public async Task CreateAsync_CategoryExists_CreatesOutgoPayingItem() { var payingItem = new PayingItem(); var debt = CreateOutgoingDebt(); _categoryRepositoryMock.Setup(m => m.GetListAsync()).ReturnsAsync(new List <Category>() { new Category() { Name = "долг", CategoryID = 1 } }); _payingItemRepositoryMock.Setup(x => x.Create(It.IsAny <PayingItem>())).Returns(new PayingItem()) .Callback <PayingItem>(pi => payingItem = pi); await _target.CreateAsync(debt); Assert.AreEqual("Дал деньги в долг", payingItem.Comment); }
public async Task CreateAsync_CategoryNotExists_CreatesIncomePayingItem() { var payingItem = new PayingItem(); var debt = CreateIncomeDebt(); _categoryRepositoryMock.Setup(m => m.GetListAsync()).ReturnsAsync(new List <Category>() { new Category() { CategoryID = 1, Name = "Test" } }); _categoryRepositoryMock.Setup(m => m.Create(It.IsAny <Category>())).Returns(CreateCategory(debt)); _payingItemRepositoryMock.Setup(x => x.Create(It.IsAny <PayingItem>())).Returns(new PayingItem()) .Callback <PayingItem>(pi => payingItem = pi); await _target.CreateAsync(debt); Assert.AreEqual("Взял деньги в долг", payingItem.Comment); }
public async Task PartialCloseAsync_CreatesPayingItem() { PayingItem payingItem = null; _debtRepositoryMock.Setup(x => x.GetItemAsync(It.IsAny <int>())).ReturnsAsync(_listOfDebts[0]); _payingItemRepositoryMock.Setup(x => x.Create(It.IsAny <PayingItem>())).Returns(new PayingItem()) .Callback <PayingItem>(pi => payingItem = pi); _categoryRepositoryMock.Setup(m => m.GetListAsync()).ReturnsAsync(new List <Category>() { new Category() { UserId = "1", TypeOfFlowID = 1, Name = "Долг", } }); await _target.PartialCloseAsync(It.IsAny <int>(), 300, It.IsAny <int>()); Assert.IsNotNull(payingItem); }
public async Task Insert_TypeOfFlowId2_AccountCashDecrease() { var insertedItem = new PayingItem() { Summ = 200 }; var account = new Account() { Cash = 500 }; _categoryService.Setup(m => m.GetItemAsync(It.IsAny <int>())) .ReturnsAsync(new Category { TypeOfFlowID = 2 }); _accountService.Setup(m => m.GetItemAsync(It.IsAny <int>())).ReturnsAsync(account); var target = new PayingItemServiceTrigger(_categoryService.Object, _accountService.Object); await target.Insert(insertedItem); Assert.AreEqual(account.Cash, 300); }
public async Task CloseAsync_CategoryExists_CreatesOutgoPayingItemWithTypeOfFlow2() { var payingItem = new PayingItem(); var debt = CreateOutgoingDebt(); _debtRepositoryMock.Setup(m => m.GetItemAsync(It.IsAny <int>())).ReturnsAsync(debt); _categoryRepositoryMock.Setup(m => m.GetListAsync()).ReturnsAsync(new List <Category>() { new Category() { UserId = "1", TypeOfFlowID = 1, Name = "Долг", } }); _payingItemRepositoryMock.Setup(x => x.Create(It.IsAny <PayingItem>())).Returns(new PayingItem()) .Callback <PayingItem>(pi => payingItem = pi); await _target.CloseAsync(debt.DebtID, It.IsAny <int>()); Assert.AreEqual("Мне вернули долг", payingItem.Comment); }
public async Task SetCorrectSumFromViewModelIfAllProductsAreNull() { var payingItem = new PayingItem() { ItemID = 1 }; _payinItemServiceMock.Setup(x => x.GetItemAsync(It.IsAny <int>())).ReturnsAsync(payingItem); var target = new PayingItemUpdater(_payinItemServiceMock.Object); var payingItemViewModel = new PayingItemEditModel() { PayingItem = new PayingItem() { ItemID = 1, Summ = 500M } }; var result = await target.UpdatePayingItemFromViewModel(payingItemViewModel); Assert.AreEqual(500, result.Summ); }
public async Task SetCorrectCommentIfProductsInItemAreCheckedAndProductsNotInItemAreNull() { var payingItem = new PayingItem() { ItemID = 1 }; _payinItemServiceMock.Setup(x => x.GetItemAsync(It.IsAny <int>())).ReturnsAsync(payingItem); var target = new PayingItemUpdater(_payinItemServiceMock.Object); var payingItemViewModel = new PayingItemEditModel() { PayingItem = new PayingItem() { ItemID = 1, Summ = 500M, Comment = "CommentFromViewModel" }, ProductsInItem = new List <Product>() { new Product() { ProductID = 1, Price = 200, ProductName = "P1" }, new Product() { ProductID = 2, Price = 100, ProductName = "P2" } } }; var result = await target.UpdatePayingItemFromViewModel(payingItemViewModel); Assert.AreEqual("P1, P2", result.Comment); }
public async Task PartialCloseAsync_InputSum300_CreatesPayingItemWithSumm300() { PayingItem payingItem = null; _debtRepositoryMock.Setup(x => x.GetItemAsync(It.IsAny <int>())).ReturnsAsync(_listOfDebts[0]); _payingItemRepositoryMock.Setup(x => x.CreateAsync(It.IsAny <PayingItem>())) .ReturnsAsync(new PayingItem()) .Callback <PayingItem>(x => payingItem = x); _categoryRepositoryMock.Setup(m => m.GetListAsync()).ReturnsAsync(new List <Category>() { new Category() { UserId = "1", TypeOfFlowID = 1, Name = "Долг", } }); await _target.PartialCloseAsync(It.IsAny <int>(), 300); Assert.AreEqual(300, payingItem.Summ); }
public async Task SetCorrectSumFromViewModelIfProductsInItemAndNotInItemAreNotNull() { var payingItem = new PayingItem() { ItemID = 1 }; _payinItemServiceMock.Setup(x => x.GetItemAsync(It.IsAny <int>())).ReturnsAsync(payingItem); var target = new PayingItemUpdater(_payinItemServiceMock.Object); var payingItemViewModel = new PayingItemEditModel() { PayingItem = new PayingItem() { ItemID = 1, Summ = 500M, Comment = "CommentFromViewModel" }, ProductsInItem = new List <Product>() { new Product() { ProductID = 1, ProductName = "P1", Price = 100 }, new Product() { ProductID = 2, ProductName = "P2", Price = 100 }, new Product() { ProductID = 3, ProductName = "P3", Price = 100 } }, ProductsNotInItem = new List <Product>() { new Product() { ProductID = 4, ProductName = "P4", Price = 100 }, new Product() { ProductID = 5, ProductName = "P5", Price = 100 }, new Product() { ProductID = 0, ProductName = "P0", Price = 100 } } }; var result = await target.UpdatePayingItemFromViewModel(payingItemViewModel); Assert.AreEqual(5, result.PayingItemProducts.Count); Assert.AreEqual(500, result.Summ); }
private async Task <PayingItem> CreatePayingItemProduct(PayingItemEditModel model, PayingItem payingItem) { var payingItemProducts = new List <PayingItemProduct>(); if (model.ProductsInItem != null) { payingItemProducts.AddRange(model.ProductsInItem.Where(x => x.ProductID != 0).Select(p => new PayingItemProduct() { PayingItemId = payingItem.ItemID, ProductId = p.ProductID, Price = p.Price }) .ToList()); } if (model.ProductsNotInItem != null) { payingItemProducts.AddRange(model.ProductsNotInItem.Where(x => x.ProductID != 0).Select(p => new PayingItemProduct() { PayingItemId = payingItem.ItemID, ProductId = p.ProductID, Price = p.Price }).ToList()); } foreach (var payingItemProduct in payingItemProducts) { payingItem.PayingItemProducts.Add(payingItemProduct); } await _payingItemService.UpdateAsync(payingItem); return(payingItem); }