Beispiel #1
0
        private void AddTask(string title, string tags = null)
        {
            if (null != tags)
            {
                tags = Sanitizer.SanitizeTags(tags);
                tags = Sanitizer.DeduplicateTags(tags);
            }

            Log.Debug($"Adding a new task : {title}, Status = {(_name == "now" ? "Doing" : "Later")}, Tags = {tags}");
            var newTask = new TaskItem
            {
                TaskDescription = title,
                AddedOn         = DateTime.Now,
                Status          = _name == "now" ? "Doing" : "Later",
                Tags            = tags
            };

            _repository.AddTask(newTask);
            var success = _repository.SaveChanges();

            if (success)
            {
                Log.Information($"Added a new task : {title}, Status = {(_name == "now" ? "Doing" : "Later")}, Tags = {tags}");
                Output.WriteText($"[green]Added a new task[/] : {title}, [aqua]Status[/] = {(_name == "now" ? "Doing" : "Later")}, [aqua]Tags[/] = {tags}");
            }
            else
            {
                Log.Error($"Failed to add task.");
                Output.WriteError($"Failed to add task. Please try again.");
            }
        }
Beispiel #2
0
        private void UpdateTaskDetails(int id, string title = null, string addTags = null, string removeTags = null, bool resetTags = false)
        {
            Log.Debug($"Modifying information of task [{id}]. Params -> Title: {title}, Tags: {addTags}");

            var task = _repository.GetTaskById(id);

            if (null == task)
            {
                Log.Error($"Task with ID {id} does not exist.");
                Output.WriteError($"Task with ID {id} does not exist. View all tasks with -> dewit list");
                return;
            }

            // Modify the title of the task
            if (!string.IsNullOrEmpty(title))
            {
                task.TaskDescription = title;
            }

            // Add tag(s) to the task
            if (!string.IsNullOrEmpty(addTags))
            {
                addTags = Sanitizer.SanitizeTags(addTags);
                var updatedTags = string.Join(',', task.Tags, addTags);
                updatedTags = Sanitizer.DeduplicateTags(updatedTags);
                task.Tags   = updatedTags[0] == ',' ? updatedTags.Remove(0, 1) : updatedTags;
            }

            // Remove tag(s) from a task
            if (!string.IsNullOrEmpty(removeTags))
            {
                var tagsToRemove = Sanitizer.SanitizeTags(removeTags).Split(',');
                var oldTags      = task.Tags.Split(',');
                task.Tags = string.Join(',', oldTags.Except(tagsToRemove));
            }

            // Remove all tags from a task
            if (resetTags)
            {
                task.Tags = string.Empty;
            }

            _repository.UpdateTask(task);
            var success = _repository.SaveChanges();

            if (success)
            {
                Log.Information($"Successfully updated task : {task.Id} | {task.TaskDescription} | {task.Tags}");
                Output.WriteText($"[green]Successfully updated task[/] : {task.Id} | {task.TaskDescription} | {task.Tags}");
            }
            else
            {
                Log.Error($"Failed to update task [{id}].");
                Output.WriteError($"Failed to update task details. Please try again.");
            }
        }
Beispiel #3
0
        private void GetTasks(string sort = "date", string duration = "today", string status = null, string tags = null, string search = null)
        {
            Log.Debug($"Showing all tasks with arguments -> sort: {sort}, duration : {duration}, status: {status}, tags: {tags}, seach string : {search}");
            var             tasks    = _repository.GetTasks();
            List <TaskItem> tempList = new List <TaskItem>();

            switch (duration)
            {
            case "yesterday":
                tasks = tasks.Where(p => p.AddedOn.Date > DateTime.Today.AddDays(-1));
                break;

            case "today":
                tasks = tasks.Where(p => p.AddedOn.Date == DateTime.Today.Date);
                break;

            case "week":
                tasks = tasks.Where(p => p.AddedOn.Date > DateTime.Today.AddDays(-7));
                break;

            case "month":
                tasks = tasks.Where(p => p.AddedOn.Date > DateTime.Today.AddDays(-30));
                break;

            case "all": break;
            }

            switch (status)
            {
            case "doing":
                tasks = tasks.Where(p => p.Status == "Doing");
                break;

            case "done":
                tasks = tasks.Where(p => p.Status == "Done");
                break;

            case "later":
                tasks = tasks.Where(p => p.Status == "Later");
                break;
            }

            if (null != search)
            {
                tasks = tasks.Where(p => p.TaskDescription.IndexOf(search, StringComparison.OrdinalIgnoreCase) >= 0);
            }

            // Filter tasks by tags
            if (null != tags)
            {
                tags = Sanitizer.SanitizeTags(tags);
                string[] allTags = tags.Contains(',') ? tags.Split(',') : new string[] { tags };

                foreach (string tag in allTags)
                {
                    var test = tasks.Where(p => (!string.IsNullOrEmpty(p.Tags) && p.Tags.Split(',').Contains <string>(tag)));
                    tempList.AddRange(test);
                }

                // Assign final output
                tasks = tempList.Distinct();
            }

            if (sort == "status")
            {
                tasks = tasks.OrderBy(p => p.Status);
            }
            else
            {
                tasks = tasks.OrderBy(p => p.AddedOn);
            }

            Output.WriteText($"Displaying tasks using parameters -> [aqua]sort[/]: {sort}, [aqua]duration[/] : {duration}, [aqua]status[/]: {(status == null ? "n/a" : status)}, [aqua]tags[/]:{tags}");
            Output.WriteTable(new string[] { "ID", "Task", "Status", "Tags", "Added On", "Completed On" }, tasks);
        }