public ICommandResult Handle(UpdateGroceriesPositionCommand command)
        {
            var groceries = _groceriesRepository.GetAll();
            var grocery   = groceries.FirstOrDefault(x => x.Id == command.Id);

            if (grocery == null)
            {
                AddNotification(new Notification("Id", "not found!"));
                return(null);
            }

            var oldIndex = groceries.IndexOf(grocery);
            var newIndex = command.Position;

            groceries.RemoveAt(oldIndex);

            groceries.Insert(newIndex, grocery);

            AddNotifications(grocery.Notifications);

            if (IsValid())
            {
                _groceriesRepository.Update(groceries);
            }

            return(new UpdateGroceriesPositionCommandResult(grocery.Id));
        }
예제 #2
0
        public async Task Adding_an_item_saves_to_repository()
        {
            var itemsBefore = await _repository.GetAll();

            _viewModel.CurrentGroceryListItem.Name     = "Fish";
            _viewModel.CurrentGroceryListItem.Quantity = 1;

            _viewModel.AddItemCommand.Execute(null);

            var itemsAfter = await _repository.GetAll();

            Assert.True(itemsBefore.Count == itemsAfter.Count - 1);
            var added = itemsAfter.Last();

            Assert.Equal("Fish", added.Name);
            Assert.Equal(1, added.Quantity);
        }
예제 #3
0
        public ICommandResult Handle(SearchGroceriesCommand command)
        {
            var groceries = _groceriesRepository.GetAll();

            //pagination removed
            //if (!string.IsNullOrEmpty(command.Term))
            //    groceries = groceries.Where(x => x.Name.ToUpper().Contains(command.Term.ToUpper())).ToList();
            //groceries = groceries.Skip((command.Page - 1) * command.PageSize).Take(command.PageSize).ToList();

            if (groceries == null)
            {
                AddNotification(new Notification("Groceries", "not found!"));
                return(null);
            }

            int totalItems = groceries.Count;

            return(new SearchGroceriesCommandResult(groceries, totalItems));
        }