コード例 #1
0
    public async Task <Result <List <ToDoItem> > > GetAllIncompleteItemsAsync(int projectId, string searchString)
    {
        if (string.IsNullOrEmpty(searchString))
        {
            var errors = new List <ValidationError>
            {
                new() { Identifier = nameof(searchString), ErrorMessage = $"{nameof(searchString)} is required." }
            };

            return(Result <List <ToDoItem> > .Invalid(errors));
        }

        var projectSpec = new ProjectByIdWithItemsSpec(projectId);
        var project     = await _repository.GetBySpecAsync(projectSpec);

        // TODO: Optionally use Ardalis.GuardClauses Guard.Against.NotFound and catch
        if (project == null)
        {
            return(Result <List <ToDoItem> > .NotFound());
        }

        var incompleteSpec = new IncompleteItemsSearchSpec(searchString);

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

            return(new Result <List <ToDoItem> >(items));
        }
        catch (Exception ex)
        {
            // TODO: Log details here
            return(Result <List <ToDoItem> > .Error(new[] { ex.Message }));
        }
    }
コード例 #2
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 }));
            }
        }