From c33562ff90cb948cae970603ccee594701f209e1 Mon Sep 17 00:00:00 2001 From: Mehmet Seckin Date: Tue, 15 Oct 2019 20:13:52 +0000 Subject: [PATCH] Implement add command --- src/Todo.CLI/Commands/AddCommand.cs | 29 +++++++++++++++++++ src/Todo.CLI/Commands/TodoCommand.cs | 1 + src/Todo.CLI/Handlers/AddCommandHandler.cs | 21 ++++++++++++++ src/Todo.CLI/appsettings.json | 2 +- .../Repository/ITodoItemRepository.cs | 1 + .../Repository/TodoItemRepository.cs | 9 ++++++ 6 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 src/Todo.CLI/Commands/AddCommand.cs create mode 100644 src/Todo.CLI/Handlers/AddCommandHandler.cs diff --git a/src/Todo.CLI/Commands/AddCommand.cs b/src/Todo.CLI/Commands/AddCommand.cs new file mode 100644 index 0000000..8386362 --- /dev/null +++ b/src/Todo.CLI/Commands/AddCommand.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using System.CommandLine; +using System.Text; +using Todo.CLI.Handlers; + +namespace Todo.CLI.Commands +{ + public class AddCommand : Command + { + public AddCommand(IServiceProvider serviceProvider) : base("add") + { + Description = "Adds a to do item."; + + AddArgument(GetSubjectArgument()); + + Handler = AddCommandHandler.Create(serviceProvider); + } + + private Argument GetSubjectArgument() + { + return new Argument("subject") + { + Description = "The subject of the new to do item.", + ArgumentType = typeof(string) + }; + } + } +} diff --git a/src/Todo.CLI/Commands/TodoCommand.cs b/src/Todo.CLI/Commands/TodoCommand.cs index 3f08cf6..096c39d 100644 --- a/src/Todo.CLI/Commands/TodoCommand.cs +++ b/src/Todo.CLI/Commands/TodoCommand.cs @@ -21,6 +21,7 @@ public TodoCommand(IServiceProvider serviceProvider) Handler = TodoCommandHandler.Create(); // Add subcommands + AddCommand(new AddCommand(serviceProvider)); AddCommand(new ListCommand(serviceProvider)); } diff --git a/src/Todo.CLI/Handlers/AddCommandHandler.cs b/src/Todo.CLI/Handlers/AddCommandHandler.cs new file mode 100644 index 0000000..0b99c59 --- /dev/null +++ b/src/Todo.CLI/Handlers/AddCommandHandler.cs @@ -0,0 +1,21 @@ +using System; +using System.CommandLine; +using System.CommandLine.Invocation; +using System.IO; +using System.Reflection; +using Todo.Core; + +namespace Todo.CLI.Handlers +{ + public class AddCommandHandler + { + public static ICommandHandler Create(IServiceProvider serviceProvider) + { + return CommandHandler.Create(async (subject) => + { + var todoItemRepository = (ITodoItemRepository)serviceProvider.GetService(typeof(ITodoItemRepository)); + await todoItemRepository.AddAsync(subject); + }); + } + } +} diff --git a/src/Todo.CLI/appsettings.json b/src/Todo.CLI/appsettings.json index 7b5fee9..dbb72ad 100644 --- a/src/Todo.CLI/appsettings.json +++ b/src/Todo.CLI/appsettings.json @@ -3,7 +3,7 @@ "ClientId": "5c594999-30b1-4a68-afcf-56ce11715b4d", "Scopes": [ "user.read", - "tasks.read" + "tasks.readwrite" ] } } \ No newline at end of file diff --git a/src/Todo.Core/Repository/ITodoItemRepository.cs b/src/Todo.Core/Repository/ITodoItemRepository.cs index d7397cc..03051c7 100644 --- a/src/Todo.Core/Repository/ITodoItemRepository.cs +++ b/src/Todo.Core/Repository/ITodoItemRepository.cs @@ -9,6 +9,7 @@ namespace Todo.Core { public interface ITodoItemRepository { + Task AddAsync(string subject); Task> ListAsync(); } } diff --git a/src/Todo.Core/Repository/TodoItemRepository.cs b/src/Todo.Core/Repository/TodoItemRepository.cs index 2a19f21..a5e149d 100644 --- a/src/Todo.Core/Repository/TodoItemRepository.cs +++ b/src/Todo.Core/Repository/TodoItemRepository.cs @@ -15,6 +15,15 @@ public TodoItemRepository(IAuthenticationProvider authenticationProvider) { } + public async Task AddAsync(string subject) + { + var graphServiceClient = new GraphServiceClient(AuthenticationProvider); + await graphServiceClient.Me.Outlook.Tasks.Request().AddAsync(new OutlookTask() + { + Subject = subject + }); + } + public async Task> ListAsync() { var graphServiceClient = new GraphServiceClient(AuthenticationProvider);