-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from marcos-venicius/feat/delete-task
Feat: delete task
- Loading branch information
Showing
23 changed files
with
265 additions
and
60 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using System.Text; | ||
|
||
using GrpcTodo.CLI.UseCases.Common; | ||
using GrpcTodo.CLI.Utils; | ||
using GrpcTodo.SharedKernel.Protos.Tasks.Responses; | ||
|
||
namespace GrpcTodo.CLI.UseCases.TaskDelete; | ||
|
||
public sealed class TaskDeletePrompt : Prompt | ||
{ | ||
public string PromptTask(IOrderedEnumerable<TaskListResponseItem> tasks) | ||
{ | ||
Dictionary<string, bool> tasksId = new(); | ||
|
||
foreach (var task in tasks) | ||
tasksId.Add(task.Id[0..4], true); | ||
|
||
ConsoleWritter.WriteInfo("Choose a task id to delete\n"); | ||
|
||
ConsoleWritter.WriteWithColor("+ completed", ConsoleColor.Green); | ||
ConsoleWritter.WriteWithColor("- uncompleted", ConsoleColor.White); | ||
Console.WriteLine(); | ||
|
||
StringBuilder sb = new(); | ||
|
||
foreach (var task in tasks) | ||
{ | ||
var id = task.Id[0..4]; | ||
|
||
sb.Append(task.Completed ? "+" : "- "); | ||
|
||
var createdAt = new DateTime(task.CreatedAt); | ||
|
||
sb.Append($"[{id}] [{createdAt:MM/dd HH:mm}] {task.Name}"); | ||
|
||
if (task.Completed) | ||
ConsoleWritter.WriteWithColor(sb.ToString(), ConsoleColor.Green); | ||
else | ||
ConsoleWritter.WriteWithColor(sb.ToString(), ConsoleColor.White); | ||
|
||
sb.Clear(); | ||
} | ||
|
||
Console.WriteLine(); | ||
|
||
var taskId = Read("task id: ", new PromptOptions | ||
{ | ||
RemoveWhitespaces = true, | ||
ShouldBeSingleWord = true, | ||
CustomMessage = "This task id does not exists", | ||
Custom = id => !tasksId.ContainsKey(id) | ||
}); | ||
|
||
return taskId; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
using GrpcTodo.CLI.Lib; | ||
using GrpcTodo.CLI.UseCases.Common; | ||
using GrpcTodo.CLI.Utils; | ||
using GrpcTodo.SharedKernel.Protos.Tasks; | ||
using GrpcTodo.SharedKernel.Protos.Tasks.Requests; | ||
|
||
namespace GrpcTodo.CLI.UseCases.TaskDelete; | ||
public class TaskDeleteUseCase : UseCase | ||
{ | ||
public TaskDeleteUseCase(ConfigsManager configsManager) : base(configsManager) | ||
{ | ||
} | ||
|
||
public override async Task ExecuteAsync() | ||
{ | ||
var accessToken = _configsManager.GetItem(ConfigKey.Item, Settings.AuthTokenKey); | ||
|
||
if (accessToken is null) | ||
throw new ArgumentNullException(nameof(accessToken), "you are not authenticated"); | ||
|
||
using var channel = GrpcConnection.CreateChannel(); | ||
|
||
var client = new TaskItem.TaskItemClient(channel); | ||
|
||
var request = new TaskListRequest | ||
{ | ||
AccessToken = accessToken | ||
}; | ||
|
||
var response = await client.ListAllAsync(request); | ||
|
||
var tasks = response.Items.OrderByDescending(x => x.CreatedAt); | ||
|
||
var taskDeletePrompt = new TaskDeletePrompt(); | ||
|
||
var taskIdToDelete = taskDeletePrompt.PromptTask(tasks); | ||
|
||
await client.DeleteAsync(new TaskDeleteRequest { | ||
TaskId = taskIdToDelete, | ||
AccessToken = accessToken | ||
}); | ||
|
||
ConsoleWritter.WriteSuccess($"task [{taskIdToDelete}] deleted successfully"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
using Grpc.Net.Client; | ||
|
||
namespace GrpcTodo.CLI.Utils; | ||
|
||
public static class GrpcConnection | ||
{ | ||
public static GrpcChannel CreateChannel() | ||
{ | ||
var httpHandler = new HttpClientHandler | ||
{ | ||
ServerCertificateCustomValidationCallback = HttpClientHandler.DangerousAcceptAnyServerCertificateValidator | ||
}; | ||
|
||
var configurations = new GrpcChannelOptions | ||
{ | ||
HttpHandler = httpHandler | ||
}; | ||
|
||
return GrpcChannel.ForAddress(Settings.ServerAddress, configurations); | ||
} | ||
} |
Oops, something went wrong.