Esempio n. 1
0
        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);
        }
        public async Task Can_Save_PayingItem_With_Same_Category()
        {
            PayingItemEditModel.OldCategoryId = 2;
            var pItemEditModel = new PayingItemEditModel()
            {
                PricesAndIdsInItem = new List <PriceAndIdForEdit>(),
                PayingItem         = new PayingItem()
                {
                    CategoryID = 2
                }
            };
            var target = new PayingItemController(_pItemProductHelper.Object, _payingItemHelper.Object,
                                                  _payingItemService.Object, _categoryService.Object, _accountService.Object);

            var result = await target.Edit(new WebUser()
            {
                Id = "1"
            }, pItemEditModel);

            var routeResult = (RedirectToRouteResult)result;

            Assert.IsInstanceOfType(result, typeof(RedirectToRouteResult));
            Assert.AreEqual(routeResult.RouteValues["action"], "List");
            _payingItemService.Verify(m => m.UpdateAsync(pItemEditModel.PayingItem));
            _pItemProductHelper.Verify(m => m.UpdatePayingItemProduct(pItemEditModel));
        }
Esempio n. 3
0
        public async Task Edit_GetPayingItemEditViewModel_ReturnsPartialViewResult()
        {
            var viewModel = new PayingItemEditModel();

            _payingItemEditViewModelCreatorMock.Setup(x => x.CreateViewModel(It.IsAny <int>())).ReturnsAsync(viewModel);
            _accountServiceMock.Setup(x => x.GetList()).Returns(new List <Account>());
            _categoryServiceMock.Setup(x => x.GetList()).Returns(new List <Category>());
            _categoryServiceMock.Setup(x => x.GetItemAsync(It.IsAny <int>())).ReturnsAsync(new Category()
            {
                TypeOfFlowID = 1
            });
            var target = new PayingItemController(
                null,
                _categoryServiceMock.Object,
                _accountServiceMock.Object,
                null,
                _payingItemEditViewModelCreatorMock.Object,
                null);

            var result = await target.Edit(new WebUser()
            {
                Id = "1"
            }, 1, 5);

            Assert.IsInstanceOfType(result, typeof(PartialViewResult));
        }
Esempio n. 4
0
        public async Task <PayingItem> UpdatePayingItemFromViewModel(PayingItemEditModel model)
        {
            if (model is null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            var sum        = GetSumOfTheProducts(model);
            var payingItem = await _payingItemService.GetItemAsync(model.PayingItem.ItemID).ConfigureAwait(false);

            payingItem.Summ = sum;
            var comment = CreateCommentForPayingItem(model);

            payingItem.Comment    = comment;
            payingItem.CategoryID = model.PayingItem.CategoryID;
            payingItem.AccountID  = model.PayingItem.AccountID;
            payingItem.Date       = model.PayingItem.Date;

            payingItem.PayingItemProducts.Clear();

            if (model.ProductsInItem == null && model.ProductsNotInItem == null)
            {
                await _payingItemService.UpdateAsync(payingItem);

                return(payingItem);
            }

            return(await CreatePayingItemProduct(model, payingItem).ConfigureAwait(false));
        }
Esempio n. 5
0
        private decimal GetSumOfTheProducts(PayingItemEditModel model)
        {
            var products = new List <Product>();

            if (model.ProductsInItem == null && model.ProductsNotInItem == null)
            {
                return(model.PayingItem.Summ);
            }

            if (model.ProductsInItem != null && model.ProductsNotInItem == null)
            {
                if (model.ProductsInItem.Any(x => x.ProductID != 0))
                {
                    products.AddRange(model.ProductsInItem.Where(x => x.ProductID != 0).ToList());

                    return(products.Sum(x => x.Price));
                }

                return(model.PayingItem.Summ);
            }

            if (model.ProductsInItem != null && model.ProductsNotInItem != null)
            {
                products.AddRange(model.ProductsInItem.Where(x => x.ProductID != 0).ToList());
                products.AddRange(model.ProductsNotInItem.Where(x => x.ProductID != 0).ToList());
            }

            return(products.Sum(x => x.Price));
        }
Esempio n. 6
0
        public async Task Edit_Post_InvalidModel_ReturnsPartialView()
        {
            _accountServiceMock.Setup(x => x.GetList()).Returns(new List <Account>());
            _categoryServiceMock.Setup(x => x.GetList()).Returns(new List <Category>());
            _categoryServiceMock.Setup(x => x.GetItemAsync(It.IsAny <int>())).ReturnsAsync(new Category()
            {
                TypeOfFlowID = 1
            });
            var target         = new PayingItemController(null, _categoryServiceMock.Object, _accountServiceMock.Object, null, null, null);
            var pItemEditModel = new PayingItemEditModel()
            {
                PayingItem = new PayingItem()
                {
                }
            };

            target.ModelState.AddModelError("error", "error");

            var result = await target.Edit(new WebUser()
            {
                Id = "1"
            }, pItemEditModel);

            Assert.IsInstanceOfType(result, typeof(PartialViewResult));
        }
Esempio n. 7
0
        public async Task CreatePayingItemProduct(PayingItemEditModel pItem)
        {
            try
            {
                var paiyngItemProducts = _pItemProductService.GetList()
                                         .Where(x => x.PayingItemID == pItem.PayingItem.ItemID);

                foreach (var item in paiyngItemProducts)
                {
                    await _pItemProductService.DeleteAsync(item.ItemID);
                }
                await _pItemProductService.SaveAsync();

                foreach (var item in pItem.PricesAndIdsInItem)
                {
                    if (item.Id != 0)
                    {
                        var pItemProd = new PaiyngItemProduct()
                        {
                            PayingItemID = pItem.PayingItem.ItemID,
                            Summ         = item.Price,
                            ProductID    = item.Id
                        };
                        await _pItemProductService.CreateAsync(pItemProd);
                    }
                }
                await _pItemProductService.SaveAsync();
            }
            catch (ServiceException e)
            {
                throw new WebUiHelperException(
                          $"Ошибка в типе {nameof(PayingItemProductHelper)} в методе {nameof(CreatePayingItemProduct)}", e);
            }
        }
Esempio n. 8
0
        public async Task SetCorrectSumIfProductsInItemAreUncheckedAndProductsNotInItemAreNull()
        {
            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
                    }
                }
            };

            var result = await target.UpdatePayingItemFromViewModel(payingItemViewModel);

            Assert.AreEqual(500, result.Summ);
        }
Esempio n. 9
0
        public async Task <ActionResult> Edit(WebUser user, PayingItemEditModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    await _payingItemUpdater.UpdatePayingItemFromViewModel(model);

                    return(RedirectToAction("List"));
                }
                catch (ServiceException e)
                {
                    throw new WebUiException(
                              $"Ошибка в контроллере {nameof(PayingItemController)} в методе {nameof(Edit)}", e);
                }
                catch (Exception e)
                {
                    throw new WebUiException(
                              $"Ошибка в контроллере {nameof(PayingItemController)} в методе {nameof(Edit)}", e);
                }
            }

            await FillViewBag(user, await GetTypeOfFlowId(model.PayingItem));

            return(PartialView("_Edit", model));
        }
 private decimal GetSummForPayingItem(PayingItemEditModel pItem)
 {
     if (pItem.PricesAndIdsNotInItem == null)
     {
         return(pItem.PricesAndIdsInItem.Where(x => x.Id != 0).Sum(x => x.Price));
     }
     return(pItem.PricesAndIdsInItem.Where(x => x.Id != 0).Sum(x => x.Price) +
            pItem.PricesAndIdsNotInItem.Where(x => x.Id != 0).Sum(x => x.Price));
 }
        public async Task <PayingItemEditModel> CreateViewModel(int payingItemId)
        {
            try
            {
                var payingItem = await _payingItemService.GetItemAsync(payingItemId);

                if (payingItem == null)
                {
                    return(null);
                }

                var model = new PayingItemEditModel()
                {
                    PayingItem        = payingItem,
                    ProductsInItem    = new List <Product>(),
                    ProductsNotInItem = new List <Product>()
                };

                var productsInItem = payingItem.PayingItemProducts
                                     .Select(x => new Product()
                {
                    Price       = x.Price,
                    ProductID   = x.Product.ProductID,
                    Description = x.Product.Description,
                    ProductName = x.Product.ProductName,
                    UserID      = x.Product.UserID
                })
                                     .ToList();
                var productsByCategory = payingItem.Category.Products.ToList();

                if (productsInItem.Count != 0)
                {
                    model.ProductsInItem    = productsInItem;
                    model.ProductsNotInItem = productsByCategory.Except(productsInItem, new ProductEqualityComparer()).ToList();
                }
                else
                {
                    model.ProductsNotInItem = productsByCategory;
                }

                return(model);
            }
            catch (ServiceException e)
            {
                throw new WebUiException(
                          $"Ошибка в типе {nameof(PayingItemEditViewModelCreator)} в методе {nameof(CreateViewModel)}", e);
            }

            catch (Exception e)
            {
                throw new WebUiException(
                          $"Ошибка в типе {nameof(PayingItemEditViewModelCreator)} в методе {nameof(CreateViewModel)}", e);
            }
        }
Esempio n. 12
0
        public void FillPayingItemEditModel(PayingItemEditModel model, int payingItemId)
        {
            var payingItemProducts = new List <PaiyngItemProduct>();
            var products           = new List <Product>();

            try
            {
                payingItemProducts = _pItemProductService.GetList() //Находим платежки, связанные с этой транзакцией
                                     .Where(x => x.PayingItemID == payingItemId)
                                     .ToList();
                products =
                    _productService.GetList().Where(x => x.CategoryID == model.PayingItem.CategoryID).ToList(); // Находим продукты, которые привязаны к данной категории
            }
            catch (ServiceException e)
            {
                throw new WebUiHelperException(
                          $"Ошибка в типе {nameof(PayingItemProductHelper)} в методе {nameof(FillPayingItemEditModel)}", e);
            }

            model.PayingItemProducts = payingItemProducts;

            if (payingItemProducts.Count != 0)
            {
                var productsInItem = payingItemProducts.Join(products,
                                                             x => x.ProductID,
                                                             y => y.ProductID,
                                                             (x, y) => new IdNamePrice()
                {
                    PayingItemProductId = x.ItemID,
                    ProductId           = x.ProductID,
                    ProductName         = y.ProductName,
                    ProductDescription  = y.Description,
                    Price = x.Summ
                })
                                     .ToList();
                var productsNotInItem = payingItemProducts.Join(products,
                                                                x => x.ProductID,
                                                                y => y.ProductID,
                                                                (x, y) => y)
                                        .ToList();

                model.ProductsInItem    = productsInItem;
                model.ProductsNotInItem = products.Except(productsNotInItem).ToList();
            }
            else
            {
                model.ProductsNotInItem = products;
            }
        }
        public async Task <ActionResult> Edit(WebUser user, int typeOfFlowId, int id)
        {
            await FillViewBag(user, typeOfFlowId);

            try
            {
                var pItem = await _payingItemService.GetItemAsync(id);

                if (pItem == null)
                {
                    return(RedirectToAction("ListAjax", 1));
                }

                var pItemEditModel = new PayingItemEditModel()
                {
                    PayingItem         = pItem,
                    PayingItemProducts = new List <PaiyngItemProduct>()
                };
                PayingItemEditModel.OldCategoryId = pItem.CategoryID;

                if (!CheckForSubCategories(pItem))
                {
                    return(PartialView(pItemEditModel));
                }
                _pItemProductHelper.FillPayingItemEditModel(pItemEditModel, id);
                return(PartialView(pItemEditModel));
            }
            catch (ServiceException e)
            {
                throw new WebUiException(
                          $"Ошибка в контроллере {nameof(PayingItemController)} в методе {nameof(Edit)}", e);
            }
            catch (WebUiException e)
            {
                throw new WebUiException(
                          $"Ошибка в контроллере {nameof(PayingItemController)} в методе {nameof(Edit)}", e);
            }
            catch (WebUiHelperException e)
            {
                throw new WebUiException(
                          $"Ошибка в контроллере {nameof(PayingItemController)} в методе {nameof(Edit)}", e);
            }
            catch (Exception e)
            {
                throw new WebUiException(
                          $"Ошибка в контроллере {nameof(PayingItemController)} в методе {nameof(Edit)}", e);
            }
        }
Esempio n. 14
0
        public async Task UpdatePayingItemProduct(PayingItemEditModel pItem)
        {
            try
            {
                foreach (var item in pItem.PricesAndIdsInItem)
                {
                    if (item.Id != 0)
                    {
                        var itemToUpdate = await _pItemProductService.GetItemAsync(item.PayingItemProductId);

                        if (itemToUpdate != null)
                        {
                            itemToUpdate.Summ = item.Price;
                            await _pItemProductService.UpdateAsync(itemToUpdate);
                        }
                    }
                    if (item.Id == 0 && item.Price != 0)
                    {
                        await _pItemProductService.DeleteAsync(item.PayingItemProductId);
                    }
                }
                await _pItemProductService.SaveAsync();

                if (pItem.PricesAndIdsNotInItem != null)
                {
                    foreach (var item in pItem.PricesAndIdsNotInItem)
                    {
                        if (item.Id != 0)
                        {
                            var payingItemProduct = new PaiyngItemProduct()
                            {
                                PayingItemID = pItem.PayingItem.ItemID,
                                Summ         = item.Price,
                                ProductID    = item.Id
                            };
                            await _pItemProductService.CreateAsync(payingItemProduct);
                        }
                    }
                    await _pItemProductService.SaveAsync();
                }
            }
            catch (ServiceException e)
            {
                throw new WebUiHelperException(
                          $"Ошибка в типе {nameof(PayingItemProductHelper)} в методе {nameof(UpdatePayingItemProduct)}", e);
            }
        }
        public async Task <ActionResult> Edit(WebUser user, PayingItemEditModel pItem)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    if (pItem.PricesAndIdsInItem == null)
                    {
                        await _payingItemService.UpdateAsync(pItem.PayingItem);
                    }
                    else
                    {
                        pItem.PayingItem.Summ = GetSummForPayingItem(pItem);
                        _payingItemHelper.CreateCommentWhileEdit(pItem);
                        await _payingItemService.UpdateAsync(pItem.PayingItem);

                        if (PayingItemEditModel.OldCategoryId != pItem.PayingItem.CategoryID)
                        {
                            await _pItemProductHelper.CreatePayingItemProduct(pItem);
                        }
                        else
                        {
                            await _pItemProductHelper.UpdatePayingItemProduct(pItem);
                        }
                    }
                }
                catch (ServiceException e)
                {
                    throw new WebUiException(
                              $"Ошибка в контроллере {nameof(PayingItemController)} в методе {nameof(Edit)}", e);
                }
                catch (WebUiHelperException e)
                {
                    throw new WebUiException(
                              $"Ошибка в контроллере {nameof(PayingItemController)} в методе {nameof(Edit)}", e);
                }
                catch (Exception e)
                {
                    throw new WebUiException(
                              $"Ошибка в контроллере {nameof(PayingItemController)} в методе {nameof(Edit)}", e);
                }
                return(RedirectToAction("List"));
            }
            await FillViewBag(user, await GetTypeOfFlowId(pItem.PayingItem));

            return(PartialView(pItem));
        }
Esempio n. 16
0
        public async Task Edit_CannotGetPayingItemEditViewModel_ReturnsRedirectToListAjax()
        {
            PayingItemEditModel viewModel = null;

            _payingItemEditViewModelCreatorMock.Setup(x => x.CreateViewModel(It.IsAny <int>())).ReturnsAsync(viewModel);
            var target = new PayingItemController(_payingItemServiceMock.Object, _categoryServiceMock.Object, _accountServiceMock.Object, null, _payingItemEditViewModelCreatorMock.Object, null);

            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");
        }
Esempio n. 17
0
        private string CreateCommentForPayingItem(PayingItemEditModel model)
        {
            var productsNames = new List <string>();
            var comment       = string.Empty;

            if (model.ProductsInItem == null && model.ProductsNotInItem == null)
            {
                return(model.PayingItem.Comment);
            }

            if (model.ProductsInItem != null && model.ProductsNotInItem == null)
            {
                if (model.ProductsInItem.Any(x => x.ProductID != 0))
                {
                    productsNames.AddRange(model.ProductsInItem.Where(x => x.ProductID != 0).Select(p => p.ProductName));

                    foreach (var productName in productsNames)
                    {
                        comment += productName + ", ";
                    }

                    return(comment.Remove(comment.LastIndexOf(",", StringComparison.Ordinal)));
                }

                return(model.PayingItem.Comment);
            }

            if (model.ProductsInItem != null && model.ProductsNotInItem != null)
            {
                productsNames.AddRange(model.ProductsInItem.Where(x => x.ProductID != 0).Select(x => x.ProductName).ToList());
                productsNames.AddRange(model.ProductsNotInItem.Where(x => x.ProductID != 0).Select(x => x.ProductName).ToList());

                foreach (var productName in productsNames)
                {
                    comment += productName + ", ";
                }
            }

            return(string.IsNullOrEmpty(comment) ? comment : comment.Remove(comment.LastIndexOf(",", StringComparison.Ordinal)));
        }
Esempio n. 18
0
        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);
        }
Esempio n. 19
0
        public void CreateCommentWhileEdit(PayingItemEditModel model)
        {
            var products = new List <Product>();

            try
            {
                products = _productService.GetList().Where(x => x.CategoryID == model.PayingItem.CategoryID).ToList();
            }
            catch (ServiceException e)
            {
                throw new WebUiHelperException(
                          $"Ошибка в типе {nameof(PayingItemHelper)} в методе {nameof(CreateCommentWhileEdit)}", e);
            }

            model.PayingItem.Comment = "";
            var comment = string.Empty;

            foreach (var item in model.PricesAndIdsInItem)
            {
                if (item.Id != 0)
                {
                    comment += products.Single(x => x.ProductID == item.Id).ProductName + ", ";
                }
            }
            if (model.PricesAndIdsNotInItem != null)
            {
                foreach (var item in model.PricesAndIdsNotInItem)
                {
                    if (item.Id != 0)
                    {
                        comment += products.Single(x => x.ProductID == item.Id).ProductName + ", ";
                    }
                }
            }
            if (!string.IsNullOrEmpty(comment))
            {
                model.PayingItem.Comment = comment.Remove(comment.LastIndexOf(","));
            }
        }
Esempio n. 20
0
        public async Task Edit_Post_ModelIsValid_CanUpdate_RedirectsToList()
        {
            var pItemEditModel = new PayingItemEditModel()
            {
                PayingItem = new PayingItem()
                {
                    CategoryID = 2
                }
            };

            _payingItemUpdaterMock.Setup(m => m.UpdatePayingItemFromViewModel(It.IsAny <PayingItemEditModel>())).ReturnsAsync(pItemEditModel.PayingItem);
            var target = new PayingItemController(null, null, null, null, null, _payingItemUpdaterMock.Object);

            var result = await target.Edit(new WebUser()
            {
                Id = "1"
            }, pItemEditModel);

            var routeResult = (RedirectToRouteResult)result;

            Assert.IsInstanceOfType(result, typeof(RedirectToRouteResult));
            Assert.AreEqual(routeResult.RouteValues["action"], "List");
        }
        public async Task Edit_Post_PricesAndIdsInItemAreNull_ReturnsRedirectToList()
        {
            var pItemEditModel = new PayingItemEditModel()
            {
                PayingItem = new PayingItem()
                {
                    CategoryID = 1,
                    AccountID  = 1,
                    ItemID     = 1
                },
                PricesAndIdsInItem = null
            };
            var target = new PayingItemController(null, null, _payingItemService.Object, null, null);

            var result = await target.Edit(new WebUser()
            {
                Id = "1"
            }, pItemEditModel);

            var redirectResult = (RedirectToRouteResult)result;

            Assert.IsInstanceOfType(result, typeof(RedirectToRouteResult));
            Assert.AreEqual(redirectResult.RouteValues["action"], "List");
        }
Esempio n. 22
0
        public async Task SetCorrectCommentFromViewModelIfProductsInItemAndNotInItemAreNotNull()
        {
            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("P1, P2, P3, P4, P5", result.Comment);
        }