public async Task Add_ValidModel_DateGreaterThanNow_ProductsNull_ReturnsRedirect()
        {
            //Arrange
            var month = DateTime.Today.Month + 1;
            var year  = DateTime.Today.Year;

            if (month > 12)
            {
                year += 1;
                month = 1;
            }
            PayingItemModel pItemModel = new PayingItemModel()
            {
                PayingItem = new PayingItem()
                {
                    AccountID = 1, CategoryID = 1, Date = new DateTime(year, month, 1), UserId = "1", ItemID = 1
                },
                Products = null,
            };
            var target = new PayingItemController(null, null, _payingItemService.Object, null, null);

            //Act
            var tmpResult = await target.Add(new WebUser()
            {
                Id = "1"
            }, pItemModel, 1);

            //Assert
            _payingItemService.Verify(m => m.CreateAsync(It.IsAny <PayingItem>()), Times.Exactly(1));
            Assert.IsInstanceOfType(tmpResult, typeof(RedirectToRouteResult));
        }
Beispiel #2
0
 public void CreateCommentWhileAdd(PayingItemModel model)
 {
     if (string.IsNullOrEmpty(model.PayingItem.Comment))
     {
         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(CreateCommentWhileAdd)}", e);
         }
         var comment = string.Empty;
         foreach (var item in model.Products)
         {
             if (item.ProductID != 0)
             {
                 comment += products.Single(x => x.ProductID == item.ProductID).ProductName + ", ";
             }
         }
         model.PayingItem.Comment = string.IsNullOrEmpty(comment)?comment : comment.Remove(comment.LastIndexOf(","));
     }
 }
        public async Task Add_ValidModel_ProductsHaveSumm_ReturnsRedirect()
        {
            PayingItemModel pItemModel = new PayingItemModel()
            {
                PayingItem = new PayingItem()
                {
                    AccountID = 1, CategoryID = 1, Date = DateTime.Today, UserId = "1", ItemID = 1
                },
                Products = new List <Product>()
                {
                    new Product()
                    {
                        Price = 100
                    },
                    new Product()
                    {
                        Price = 200
                    }
                }
            };
            var target = new PayingItemController(_pItemProductHelper.Object, _payingItemHelper.Object, _payingItemService.Object, null, null);

            var result = await target.Add(new WebUser()
            {
                Id = "1"
            }, pItemModel, 2);

            _payingItemHelper.Verify(m => m.CreateCommentWhileAdd(pItemModel), Times.Exactly(1));
            _payingItemService.Verify(m => m.CreateAsync(It.IsAny <PayingItem>()), Times.Exactly(1));
            _pItemProductHelper.Verify(m => m.CreatePayingItemProduct(pItemModel), Times.Exactly(1));
            Assert.IsInstanceOfType(result, typeof(RedirectToRouteResult));
        }
Beispiel #4
0
        public async Task <ActionResult> Add(WebUser user, PayingItemModel model, int typeOfFlowId)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    await _payingItemCreator.CreatePayingItemFromViewModel(model);

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

            return(PartialView("_Add", model));
        }
Beispiel #5
0
        public async Task CreatePayingItemProduct(PayingItemModel pItem)
        {
            try
            {
                foreach (var item in pItem.Products)
                {
                    if (item.ProductID != 0)
                    {
                        var pItemProd = new PaiyngItemProduct()
                        {
                            PayingItemID = pItem.PayingItem.ItemID,
                            Summ         = item.Price,
                            ProductID    = item.ProductID
                        };
                        await _pItemProductService.CreateAsync(pItemProd);

                        await _pItemProductService.SaveAsync();
                    }
                }
            }
            catch (ServiceException e)
            {
                throw new WebUiHelperException(
                          $"Ошибка в типе {nameof(PayingItemProductHelper)} в методе {nameof(CreatePayingItemProduct)}", e);
            }
        }
Beispiel #6
0
        public async Task <PayingItem> CreatePayingItemFromViewModel(PayingItemModel model)
        {
            if (model is null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            try
            {
                if (model.PayingItem.Date.Month > DateTime.Today.Date.Month ||
                    model.PayingItem.Date.Year > DateTime.Today.Year)
                {
                    model.PayingItem.Date = DateTime.Today.Date;
                }

                if (model.Products != null)
                {
                    var sum = model.Products.Sum(x => x.Price);
                    if (sum != 0)
                    {
                        model.PayingItem.Summ = sum;
                    }

                    CreateCommentWhileAdd(model);

                    var itemsToAdd = model.Products.Where(x => x.ProductID != 0)
                                     .Select(product => new PayingItemProduct()
                    {
                        PayingItemId = model.PayingItem.ItemID,
                        ProductId    = product.ProductID,
                        Price        = product.Price
                    });

                    foreach (var item in itemsToAdd)
                    {
                        model.PayingItem.PayingItemProducts.Add(item);
                    }
                }

                return(await _payingItemService.CreateAsync(model.PayingItem));
            }
            catch (ServiceException e)
            {
                throw new WebUiException($"Ошибка в типе {nameof(PayingItemCreator)} в методе {nameof(CreatePayingItemFromViewModel)}", e);
            }
            catch (Exception e)
            {
                throw new WebUiException($"Ошибка в типе {nameof(PayingItemCreator)} в методе {nameof(CreatePayingItemFromViewModel)}", e);
            }
        }
        public async Task <ActionResult> Add(WebUser user, PayingItemModel pItem, int typeOfFlow)
        {
            if (ModelState.IsValid)
            {
                if (pItem.PayingItem.Date.Month > DateTime.Today.Date.Month ||
                    pItem.PayingItem.Date.Year > DateTime.Today.Year)
                {
                    pItem.PayingItem.Date = DateTime.Today.Date;
                }
                try
                {
                    if (pItem.Products == null)
                    {
                        await _payingItemService.CreateAsync(pItem.PayingItem);
                    }
                    else
                    {
                        var summ = pItem.Products.Sum(x => x.Price);
                        if (summ != 0)
                        {
                            pItem.PayingItem.Summ = summ;
                        }
                        _payingItemHelper.CreateCommentWhileAdd(pItem);
                        await _payingItemService.CreateAsync(pItem.PayingItem);

                        await _pItemProductHelper.CreatePayingItemProduct(pItem);
                    }
                }
                catch (ServiceException e)
                {
                    throw new WebUiException(
                              $"Ошибка в контроллере {nameof(PayingItemController)} в методе {nameof(Add)}", e);
                }
                catch (WebUiHelperException e)
                {
                    throw new WebUiException(
                              $"Ошибка в контроллере {nameof(PayingItemController)} в методе {nameof(Add)}", e);
                }
                catch (Exception e)
                {
                    throw new WebUiException(
                              $"Ошибка в контроллере {nameof(PayingItemController)} в методе {nameof(Add)}", e);
                }
                return(RedirectToAction("List"));
            }
            await FillViewBag(user, typeOfFlow);

            return(PartialView(pItem));
        }
        public async Task <ActionResult> Add(WebUser user, int typeOfFlow)
        {
            await FillViewBag(user, typeOfFlow);

            var piModel = new PayingItemModel()
            {
                PayingItem = new PayingItem()
                {
                    UserId = user.Id
                },
                Products = new List <Product>()
            };

            return(PartialView(piModel));
        }
Beispiel #9
0
        public async Task CheckForCorrectPayingItemDate()
        {
            var payingItemModel = new PayingItemModel()
            {
                PayingItem = new PayingItem()
                {
                    Date = DateTime.Today.AddMonths(1)
                },
                Products = null
            };
            var target = new PayingItemCreator(_payingItemService.Object);

            await target.CreatePayingItemFromViewModel(payingItemModel);

            Assert.AreEqual(DateTime.Today.Date, payingItemModel.PayingItem.Date);
        }
Beispiel #10
0
        private void CreateCommentWhileAdd(PayingItemModel model)
        {
            if (string.IsNullOrEmpty(model.PayingItem.Comment))
            {
                var comment = string.Empty;

                foreach (var item in model.Products.Where(p => p.ProductID != 0))
                {
                    comment += item.ProductName + ", ";
                }

                if (!string.IsNullOrEmpty(comment))
                {
                    model.PayingItem.Comment = comment.Remove(comment.LastIndexOf(",", StringComparison.Ordinal));
                }
            }
        }
Beispiel #11
0
        public async Task Add_ValidModel_ReturnsRedirect_ToAction_List()
        {
            var payingItemModel = new PayingItemModel()
            {
                PayingItem = new PayingItem()
                {
                    AccountID = 1, CategoryID = 1, Date = DateTime.Today, UserId = "1", ItemID = 1
                },
                Products = new List <Product>()
            };
            var target = new PayingItemController(null, null, null, _payingItemCreator.Object, null, null);

            var result = await target.Add(new WebUser()
            {
                Id = "1"
            }, payingItemModel, It.IsAny <int>());

            Assert.IsInstanceOfType(result, typeof(RedirectToRouteResult));
            Assert.AreEqual("List", ((RedirectToRouteResult)result).RouteValues["action"]);
        }
Beispiel #12
0
        public async Task Add_ValidModel_Throws_WebUiExceptionWithInnerWebUiException()
        {
            _payingItemCreator.Setup(m => m.CreatePayingItemFromViewModel(It.IsAny <PayingItemModel>())).ThrowsAsync(new WebUiException());
            var target          = new PayingItemController(null, null, null, _payingItemCreator.Object, null, null);
            var user            = new WebUser();
            var payingItemModel = new PayingItemModel()
            {
                PayingItem = new PayingItem(),
                Products   = new List <Product>()
            };

            try
            {
                await target.Add(user, payingItemModel, It.IsAny <int>());
            }
            catch (WebUiException e)
            {
                Assert.IsInstanceOfType(e.InnerException, typeof(WebUiException));
            }
        }
Beispiel #13
0
        public async Task CheckIfPayingItemProductsAreAddedCorrectly()
        {
            var payingItemModel = new PayingItemModel()
            {
                PayingItem = new PayingItem()
                {
                    Date = DateTime.Today
                },
                Products = new List <Product>()
                {
                    new Product()
                    {
                        CategoryID  = 1,
                        ProductName = "Product_1",
                        ProductID   = 1,
                        Price       = 100
                    },
                    new Product()
                    {
                        CategoryID  = 1,
                        ProductID   = 2,
                        ProductName = "Product_2",
                        Price       = 100
                    },
                    new Product()
                    {
                        CategoryID  = 1,
                        ProductID   = 0,
                        ProductName = "Product_3",
                        Price       = 100
                    }
                }
            };
            var target = new PayingItemCreator(_payingItemService.Object);

            await target.CreatePayingItemFromViewModel(payingItemModel);

            // PayingItem.PayingItemProducts  must include only those PayingTemModel.Products where ProductId != 0
            Assert.AreEqual(2, payingItemModel.PayingItem.PayingItemProducts.Count);
        }
Beispiel #14
0
        public async Task CheckCommentCorrectnessIfPayingItemCommentIsNotEmpty()
        {
            var payingItemModel = new PayingItemModel()
            {
                PayingItem = new PayingItem()
                {
                    Date    = DateTime.Today,
                    Comment = "PayingItemComment"
                },
                Products = new List <Product>()
                {
                    new Product()
                    {
                        CategoryID  = 1,
                        ProductName = "Product_1",
                        ProductID   = 1,
                        Price       = 100
                    },
                    new Product()
                    {
                        CategoryID  = 1,
                        ProductID   = 2,
                        ProductName = "Product_2",
                        Price       = 100
                    },
                    new Product()
                    {
                        CategoryID  = 1,
                        ProductID   = 3,
                        ProductName = "Product_3",
                        Price       = 100
                    }
                }
            };
            var target = new PayingItemCreator(_payingItemService.Object);

            await target.CreatePayingItemFromViewModel(payingItemModel);

            Assert.AreEqual("PayingItemComment", payingItemModel.PayingItem.Comment);
        }