public void SaveCommand_Does_Not_Allow_Duplicate_Names()
        {
            var categoryList = new List<CategoryViewModel>();

            var categoryRepositorySetup = new Mock<ICategoryRepository>();
            categoryRepositorySetup.Setup(c => c.GetList(It.IsAny<Expression<Func<CategoryViewModel, bool>>>()))
                .Returns(categoryList);
            categoryRepositorySetup.Setup(c => c.Save(It.IsAny<CategoryViewModel>()))
                .Callback((CategoryViewModel cat) => { categoryList.Add(cat); });

            var settingsManagerMock = new Mock<ISettingsManager>();

            var categoryPrimary = new CategoryViewModel
            {
                Id = 1,
                Name = "Test CategoryViewModel"
            };
            var categorySecondary = new CategoryViewModel
            {
                Name = "Test CategoryViewModel"
            };
            categoryList.Add(categoryPrimary);

            var viewmodel = new ModifyCategoryViewModel(categoryRepositorySetup.Object,
                new Mock<IDialogService>().Object, 
                settingsManagerMock.Object,
                new Mock<IBackupManager>().Object)
            {
                IsEdit = false,
                SelectedCategory = categorySecondary
            };

            viewmodel.SaveCommand.Execute();
            categoryList.Count.ShouldBe(1);
        }
 /// <summary>
 ///     Deletes the passed CategoryViewModel and removes it from cache
 /// </summary>
 public bool Delete(CategoryViewModel accountToDelete)
 {
     using (var db = dbManager.GetConnection())
     {
         var itemToDelete = db.Table<Category>().Single(x => x.Id == accountToDelete.Id);
         return db.Delete(itemToDelete) == 1;
     }
 }
        public void GetValues_InitializedData_IgnoreTransfers()
        {
            //Setup
            var testCat = new CategoryViewModel {Id = 2, Name = "Ausgehen"};

            var paymentList = new List<PaymentViewModel>
            {
                new PaymentViewModel
                {
                    Id = 1,
                    Type = PaymentType.Income,
                    Date = DateTime.Today,
                    Amount = 60,
                    Category = testCat,
                    CategoryId = 1
                },
                new PaymentViewModel
                {
                    Id = 2,
                    Type = PaymentType.Expense,
                    Date = DateTime.Today,
                    Amount = 90,
                    Category = testCat,
                    CategoryId = 1
                },
                new PaymentViewModel
                {
                    Id = 3,
                    Type = PaymentType.Transfer,
                    Date = DateTime.Today,
                    Amount = 40,
                    Category = testCat,
                    CategoryId = 1
                }
            };

            var paymentRepoSetup = new Mock<IPaymentRepository>();
            paymentRepoSetup.Setup(x => x.GetList(It.IsAny<Expression<Func<PaymentViewModel, bool>>>()))
                .Returns((Expression<Func<PaymentViewModel, bool>> filter) => paymentList.Where(filter.Compile()).ToList());

            //Excution
            var result =
                new CategorySpreadingDataProvider(paymentRepoSetup.Object).GetValues(DateTime.Today.AddDays(-3),
                    DateTime.Today.AddDays(3)).ToList();

            //Assertion
            result.Count.ShouldBe(1);
            result.First().Value.ShouldBe(30);
        }
        /// <summary>
        ///     Save a new CategoryViewModel or update an existing one.
        /// </summary>
        public bool Save(CategoryViewModel categoryVmToSave)
        {
            using (var db = dbManager.GetConnection())
            {
                if (string.IsNullOrWhiteSpace(categoryVmToSave.Name))
                {
                    categoryVmToSave.Name = Strings.NoNamePlaceholderLabel;
                }

                var category = Mapper.Map<Category>(categoryVmToSave);

                if (category.Id == 0)
                {
                    var rows = db.Insert(category);
                    categoryVmToSave.Id = db.Table<Category>().OrderByDescending(x => x.Id).First().Id;
                    return rows == 1;
                }
                return db.Update(category) == 1;
            }
        }
        public void Cancel_SelectedCategoryReseted()
        {
            string name = "Cateory";
            var baseCategory = new CategoryViewModel { Id = 5, Name = name };
            var category = new CategoryViewModel { Id = 5, Name = name };

            var categoryRepositorySetup = new Mock<ICategoryRepository>();
            categoryRepositorySetup.Setup(x => x.FindById(It.IsAny<int>())).Returns(baseCategory);

            var settingsManagerMock = new Mock<ISettingsManager>();

            var viewmodel = new ModifyCategoryViewModel(categoryRepositorySetup.Object,
                new Mock<IDialogService>().Object, 
                settingsManagerMock.Object,
                new Mock<IBackupManager>().Object)
            {
                IsEdit = true,
                SelectedCategory = category
            };

            viewmodel.SelectedCategory.Name = "foooo";
            viewmodel.CancelCommand.Execute();

            viewmodel.SelectedCategory.Name.ShouldBe(name);
        }
        public void DeleteCategory_DeletesCategory()
        {
            var categoryList = new List<CategoryViewModel>();
            var categoryRepositorySetup = new Mock<ICategoryRepository>();

            categoryRepositorySetup.Setup(c => c.Save(It.IsAny<CategoryViewModel>()))
                .Callback((CategoryViewModel cat) => { categoryList.Add(cat); });
            categoryRepositorySetup.Setup(c => c.Delete(It.IsAny<CategoryViewModel>()))
                .Callback((CategoryViewModel cat) => { categoryList.Remove(cat); });

            var settingsManagerMock = new Mock<ISettingsManager>();

            var categoryPrimary = new CategoryViewModel
            {
                Id = 1,
                Name = "Test CategoryViewModel",
                Notes = "Notes about the test CategoryViewModel"
            };

            categoryList.Add(categoryPrimary);

            var viewmodel = new ModifyCategoryViewModel(categoryRepositorySetup.Object, 
                new Mock<IDialogService>().Object, 
                settingsManagerMock.Object,
                new Mock<IBackupManager>().Object)
            {
                IsEdit = true,
                SelectedCategory = categoryPrimary
            };

            viewmodel.DeleteCommand.Execute();
            categoryList.Any().ShouldBeFalse();
        }
        public void SaveCategory_UpdateTimeStamp()
        {
            var category = new CategoryViewModel { Id = 0, Name = "CategoryViewModel", Notes = "" };

            var categoryRepositorySetup = new Mock<ICategoryRepository>();

            categoryRepositorySetup.SetupAllProperties();
            categoryRepositorySetup.Setup(x => x.Save(category)).Returns(true);
            categoryRepositorySetup.Setup(x => x.GetList(null)).Returns(() => new ObservableCollection<CategoryViewModel>());

            var localDateSetting = DateTime.MinValue;

            var settingsManagerMock = new Mock<ISettingsManager>();
            settingsManagerMock.SetupSet(x => x.LastDatabaseUpdate = It.IsAny<DateTime>()).Callback((DateTime x) => localDateSetting = x);
            
            var viewmodel = new ModifyCategoryViewModel(categoryRepositorySetup.Object, 
                new Mock<IDialogService>().Object, 
                settingsManagerMock.Object,
                new Mock<IBackupManager>().Object)
            {
                IsEdit = false,
                SelectedCategory = category
            };

            viewmodel.SaveCommand.Execute();

            localDateSetting.ShouldBeGreaterThan(DateTime.Now.AddSeconds(-1));
            localDateSetting.ShouldBeLessThan(DateTime.Now.AddSeconds(1));
        }
 /// <summary>
 ///     Handle the selection of a CategoryViewModel in the list
 /// </summary>
 protected abstract void ItemClick(CategoryViewModel category);
        private async void DeleteCategory(CategoryViewModel categoryToDelete)
        {
            if (await DialogService.ShowConfirmMessage(Strings.DeleteTitle, Strings.DeleteCategoryConfirmationMessage))
            {
                if (Categories.Contains(categoryToDelete))
                {
                    Categories.Remove(categoryToDelete);
                }

                CategoryRepository.Delete(categoryToDelete);
                Search();
            }
        }
        private async void OpenContextMenu(CategoryViewModel category)
        {
            var result = await ModifyDialogService.ShowEditSelectionDialog();

            switch (result)
            {
                case ModifyOperation.Edit:
                    EditCategoryCommand.Execute(category);
                    break;

                case ModifyOperation.Delete:
                    DeleteCategoryCommand.Execute(category);
                    break;
            }
        }
 private void CreateNewCategory(CategoryViewModel category)
 {
     ShowViewModel<ModifyCategoryViewModel>(new {isEdit = false, SelectedCategory = 0});
 }
 private void EditCategory(CategoryViewModel category)
 {
     ShowViewModel<ModifyCategoryViewModel>(new {isEdit = true, selectedCategoryId = category.Id});
 }
 /// <summary>
 ///     Post selected CategoryViewModel to message hub
 /// </summary>
 protected override void ItemClick(CategoryViewModel category)
 {
     messenger.Publish(new CategorySelectedMessage(this, category));
     Close(this);
 }
 /// <summary>
 ///     Initialize the ViewModel
 /// </summary>
 /// <param name="categoryId">Pass the ID of the category to edit. If this is 0 the VM changes to Creation mode</param>
 public void Init(int categoryId)
 {
     if (categoryId == 0)
     {
         IsEdit = false;
         SelectedCategory = new CategoryViewModel();
     }
     else
     {
         IsEdit = true;
         SelectedCategory = categoryRepository.FindById(categoryId);
     }
 }
 /// <summary>
 ///     Message to notify about a selected CategoryViewModel after choosing.
 /// </summary>
 /// <param name="sender">sender</param>
 /// <param name="selectedCategory">Selected CategoryViewModel</param>
 public CategorySelectedMessage(object sender, CategoryViewModel selectedCategory) : base(sender)
 {
     SelectedCategory = selectedCategory;
 }