Skip to content

Commit

Permalink
delete a task by your id
Browse files Browse the repository at this point in the history
  • Loading branch information
marcos-venicius committed Jun 6, 2023
1 parent c98b2c7 commit 6f0f6cc
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 1 deletion.
Binary file added .docker-compose.yml.swp
Binary file not shown.
7 changes: 6 additions & 1 deletion GrpcTodo.CLI/UseCases/TaskDelete/TaskDeleteUseCase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public override async Task ExecuteAsync()

var taskIdToDelete = taskDeletePrompt.PromptTask(tasks);

ConsoleWritter.Write($"the task [{taskIdToDelete}] will be deleted");
await client.DeleteAsync(new TaskDeleteRequest {
TaskId = taskIdToDelete,
AccessToken = accessToken
});

ConsoleWritter.WriteSuccess($"task [{taskIdToDelete}] deleted successfully");
}
}
17 changes: 17 additions & 0 deletions GrpcTodo.Server/Application/Repositories/TaskRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,23 @@ public async Task CreateAsync(Guid id, string name, Guid userId)
});
}

public async Task DeleteAsync(Guid id)
{
using var connection = _context.CreateConnection();

await connection.ExecuteAsync(TaskQueries.Delete, new
{
id
});
}

public async Task<TaskItem?> FindByShortIdAsync(string id)
{
using var connection = _context.CreateConnection();

return await connection.QueryFirstOrDefaultAsync<TaskItem>(TaskQueries.FindByShortId, new { id = id + '%' });
}

public async Task<TaskItem?> FindByNameAsync(string name)
{
using var connection = _context.CreateConnection();
Expand Down
2 changes: 2 additions & 0 deletions GrpcTodo.Server/Domain/Repositories/ITaskRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ public interface ITaskRepository
{
public Task CreateAsync(Guid id, string name, Guid userId);
public Task<TaskItem?> FindByNameAsync(string name);
public Task<TaskItem?> FindByShortIdAsync(string id);
public Task UpdateTaskNameAsync(Guid id, string name);
public Task CompleteAsync(Guid id);
public Task UncompleteAsync(Guid id);
public Task<List<TaskItem>> ListAsync(Guid userId);
public Task DeleteAsync(Guid id);
}
28 changes: 28 additions & 0 deletions GrpcTodo.Server/Domain/UseCases/Tasks/DeleteTaskUseCase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using GrpcTodo.Server.Domain.Repositories;

namespace GrpcTodo.Server.Domain.UseCases.Tasks;

public sealed class DeleteTaskUseCase
{
private readonly ITaskRepository _taskRepository;

public DeleteTaskUseCase(ITaskRepository taskRepository)
{
_taskRepository = taskRepository;
}

public async Task ExecuteAsync(Guid userId, DeleteTaskUseCaseInput request)
{
var task = await _taskRepository.FindByShortIdAsync(request.Id);

if (task is null)
throw new NotFoundException("this task does not exists");

if (task.UserId != userId)
throw new DeniedException("this task does not belongs to you");

await _taskRepository.DeleteAsync(task.Id);
}
}

public sealed record DeleteTaskUseCaseInput(string Id);
14 changes: 14 additions & 0 deletions GrpcTodo.Server/GrpcServices/TaskGrpcService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@ public sealed class TaskGrpcService : TaskItem.TaskItemBase
private readonly IAuthMiddleware _authMiddleware;
private readonly CreateTaskUseCase _createTaskUseCase;
private readonly ListAllTasksUseCase _listAllTasksUseCase;
private readonly DeleteTaskUseCase _deleteTaskUseCase;

public TaskGrpcService(
CreateTaskUseCase createTaskUseCase,
ListAllTasksUseCase listAllTasksUseCase,
DeleteTaskUseCase deleteTaskUseCase,
IAuthMiddleware authMiddleware)
{
_createTaskUseCase = createTaskUseCase;
_listAllTasksUseCase = listAllTasksUseCase;
_authMiddleware = authMiddleware;
_deleteTaskUseCase = deleteTaskUseCase;
}

public override async Task<TaskCreateResponse> Create(TaskCreateRequest request, ServerCallContext context)
Expand Down Expand Up @@ -60,4 +63,15 @@ public override async Task<TaskListResponse> ListAll(TaskListRequest request, Se

return tasks;
}

public override async Task<TaskDeleteResponse> Delete(TaskDeleteRequest request, ServerCallContext context)
{
var credentials = new Credentials(request.AccessToken);

var input = new DeleteTaskUseCaseInput(request.TaskId);

await _authMiddleware.Authenticate(credentials, input, _deleteTaskUseCase.ExecuteAsync);

return new TaskDeleteResponse();
}
}
2 changes: 2 additions & 0 deletions GrpcTodo.Server/Infra/Queries/TaskQueries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ namespace GrpcTodo.Server.Infra.Queries;

public static class TaskQueries
{
public const string Delete = @"DELETE FROM ""task"" WHERE id = @id";
public const string Complete = "";
public const string Create = """INSERT INTO "task" (id, user_id, name, created_at) VALUES (@id, @userId, @name, @createdAt)""";
public const string FindByName = @"SELECT id, user_id as userId, name, completed, created_at as CreatedAt FROM ""task"" WHERE LOWER(name) = @name;";
public const string FindByShortId = @"SELECT id, user_id as userId, name, completed, created_at as CreatedAt FROM ""task"" WHERE id::text LIKE @id;";
public const string List = """SELECT id, user_id as UserId, name, completed, created_at as CreatedAt FROM "task" WHERE user_id = @userId;""";
public const string Uncomplete = "";
public const string UpdateTaskName = "";
Expand Down
1 change: 1 addition & 0 deletions GrpcTodo.Server/IoC/DependencyInjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public static IServiceCollection AddIoC(this IServiceCollection services)
services.AddSingleton<IGuidGenerator, GuidGenerator>();

// use cases
services.AddScoped<DeleteTaskUseCase>();
services.AddScoped<CreateUserUseCase>();
services.AddScoped<UserLoginUseCase>();
services.AddScoped<UpdateTokenUseCase>();
Expand Down
12 changes: 12 additions & 0 deletions GrpcTodo.SharedKernel/Exceptions/DeniedException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Grpc.Core;

namespace GrpcTodo.SharedKernel.Exceptions;

public sealed class DeniedException : RpcException
{
public DeniedException(string message) : base(new Status(
StatusCode.PermissionDenied,
message
), message)
{ }
}
5 changes: 5 additions & 0 deletions GrpcTodo.SharedKernel/Protos/task/requests.proto
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@ message TaskCreateRequest {
message TaskListRequest {
string accessToken = 1;
}

message TaskDeleteRequest {
string accessToken = 1;
string taskId = 2;
}
3 changes: 3 additions & 0 deletions GrpcTodo.SharedKernel/Protos/task/responses.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ message TaskCreateResponse {
string id = 1;
}

message TaskDeleteResponse {
}

message TaskListResponseItem {
string id = 1;
string name = 2;
Expand Down
1 change: 1 addition & 0 deletions GrpcTodo.SharedKernel/Protos/task/service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ package grpc_todo.shared_kernel.protos.tasks;

service TaskItem {
rpc Create(requests.TaskCreateRequest) returns (responses.TaskCreateResponse);
rpc Delete(requests.TaskDeleteRequest) returns (responses.TaskDeleteResponse);
rpc ListAll(requests.TaskListRequest) returns (responses.TaskListResponse);
}

0 comments on commit 6f0f6cc

Please sign in to comment.