Example #1
0
        public async Task <Result <List <Item> > > GetAllIncompleteItemsAsync(int listId, string searchString)
        {
            if (string.IsNullOrEmpty(searchString))
            {
                var errors = new List <ValidationError>();
                errors.Add(new ValidationError()
                {
                    Identifier   = nameof(searchString),
                    ErrorMessage = $"{nameof(searchString)} is required."
                });
                return(Result <List <Item> > .Invalid(errors));
            }

            var shoppingListSpec = new ShoppingListByIdWithItemsSpec(listId);
            var shoppingList     = await _repository.GetBySpecAsync(shoppingListSpec);

            if (shoppingList == null)
            {
                return(Result <List <Item> > .NotFound());
            }

            var incompleteSpec = new IncompleteItemsSearchSpec(searchString);

            try
            {
                var items = incompleteSpec.Evaluate(shoppingList.Items).ToList();

                return(new Result <List <Item> >(items));
            }
            catch (Exception ex)
            {
                // TODO: Log details here
                return(Result <List <Item> > .Error(new[] { ex.Message }));
            }
        }
Example #2
0
        public async Task <IActionResult> Index(int projectId = 1)
        {
            var spec         = new ShoppingListByIdWithItemsSpec(projectId);
            var shoppingList = await _shoppingListRepository.GetBySpecAsync(spec);

            var dto = new ShoppingListViewModel
            {
                Id    = shoppingList.Id,
                Name  = shoppingList.Name,
                Items = shoppingList.Items
                        .Select(item => ItemViewModel.FromItem(item))
                        .ToList()
            };

            return(View(dto));
        }
Example #3
0
        public async Task <Result <Item> > GetNextIncompleteItemAsync(int projectId)
        {
            var shoppingListSpec = new ShoppingListByIdWithItemsSpec(projectId);
            var shoppingList     = await _repository.GetBySpecAsync(shoppingListSpec);

            var incompleteSpec = new IncompleteItemsSpec();

            var items = incompleteSpec.Evaluate(shoppingList.Items).ToList();

            if (!items.Any())
            {
                return(Result <Item> .NotFound());
            }

            return(new Result <Item>(items.First()));
        }
Example #4
0
        public async Task <IActionResult> GetById(int id)
        {
            var shoppingListSpec = new ShoppingListByIdWithItemsSpec(id);
            var shoppingList     = await _repository.GetBySpecAsync(shoppingListSpec);

            var result = new ShoppingListDTO
            {
                Id    = shoppingList.Id,
                Name  = shoppingList.Name,
                Items = new List <ItemDTO>
                        (
                    shoppingList.Items.Select(i => ItemDTO.FromToDoItem(i)).ToList()
                        )
            };

            return(Ok(result));
        }
Example #5
0
        public async Task <IActionResult> Complete(int projectId, int itemId)
        {
            var shoppingListSpec = new ShoppingListByIdWithItemsSpec(projectId);
            var shoppingList     = await _repository.GetBySpecAsync(shoppingListSpec);

            if (shoppingList == null)
            {
                return(NotFound("No such shoppingList"));
            }

            var toDoItem = shoppingList.Items.FirstOrDefault(item => item.Id == itemId);

            if (toDoItem == null)
            {
                return(NotFound("No such item."));
            }

            toDoItem.MarkComplete();
            await _repository.UpdateAsync(shoppingList);

            return(Ok());
        }