From ce3d3517699099ecbe213e94777cd19812482ee5 Mon Sep 17 00:00:00 2001 From: kjuulh Date: Sun, 14 Nov 2021 01:50:18 +0100 Subject: [PATCH] Refined the design a bit --- .../Todo.Api/Controllers/TodosController.cs | 18 ++-- .../server/Todo/Todo.Api/Hubs/TodoHub.cs | 23 +++-- .../Interfaces/Persistence/ITodoRepository.cs | 1 + .../Mongo/Repositories/TodoRepository.cs | 8 +- src/client/.idea/runConfigurations/dev.xml | 13 +++ src/client/src/components/todos/addTodo.tsx | 8 +- .../todos/collapsed/addTodoForm.tsx | 61 ++++++++----- .../todos/collapsed/collapsedAddTodo.tsx | 9 +- .../src/components/todos/todoCheckmark.tsx | 2 +- src/client/src/components/todos/todoItem.tsx | 2 +- src/client/src/components/todos/todoList.tsx | 44 +++++----- src/client/src/core/entities/todo.tsx | 18 +++- src/client/src/pages/_app.tsx | 6 +- src/client/src/pages/index.tsx | 27 +----- .../presentation/contexts/SocketContext.tsx | 85 +++++++++++++++++++ .../src/presentation/hooks/socketHooks.tsx | 36 ++++++++ src/client/src/styles/tailwind.css | 22 ++++- src/client/tailwind.config.js | 29 ++++++- 18 files changed, 315 insertions(+), 97 deletions(-) create mode 100644 src/client/.idea/runConfigurations/dev.xml create mode 100644 src/client/src/presentation/contexts/SocketContext.tsx create mode 100644 src/client/src/presentation/hooks/socketHooks.tsx diff --git a/src/backend/server/Todo/Todo.Api/Controllers/TodosController.cs b/src/backend/server/Todo/Todo.Api/Controllers/TodosController.cs index 9259c98..2cfa88b 100644 --- a/src/backend/server/Todo/Todo.Api/Controllers/TodosController.cs +++ b/src/backend/server/Todo/Todo.Api/Controllers/TodosController.cs @@ -16,20 +16,16 @@ public class TodosController : ControllerBase } [HttpPost] - public async Task> CreateTodo([FromBody] CreateTodoRequest request) - { - var todo = await _todoRepository.CreateTodoAsync(request.Title); - - return Ok(todo); - } + public async Task> CreateTodo([FromBody] CreateTodoRequest request) => + Ok(await _todoRepository.CreateTodoAsync(request.Title)); [HttpGet] - public async Task>> GetTodos() - { - var todos = await _todoRepository.GetTodosAsync(); + public async Task>> GetTodos() => + Ok(await _todoRepository.GetTodosAsync()); - return Ok(todos); - } + [HttpGet] + public async Task>> GetNotDoneTodos() => + Ok(await _todoRepository.GetNotDoneTodos()); public record CreateTodoRequest { diff --git a/src/backend/server/Todo/Todo.Api/Hubs/TodoHub.cs b/src/backend/server/Todo/Todo.Api/Hubs/TodoHub.cs index fcc4d53..101f049 100644 --- a/src/backend/server/Todo/Todo.Api/Hubs/TodoHub.cs +++ b/src/backend/server/Todo/Todo.Api/Hubs/TodoHub.cs @@ -18,36 +18,47 @@ namespace Todo.Api.Hubs { var _ = await _todoRepository.CreateTodoAsync(todoTitle); - var todos = await _todoRepository.GetTodosAsync(); + var todos = await _todoRepository.GetNotDoneTodos(); var serializedTodos = JsonSerializer.Serialize(todos .Select(t => new TodoResponse { Id = t.Id, Title = t.Title }) .ToList()); - await Clients.Caller.SendAsync("todos", serializedTodos); + await Clients.Caller.SendAsync("getInboxTodos", serializedTodos); } - + public async Task UpdateTodo(string todoId, bool todoStatus) { await _todoRepository.UpdateTodoStatus(todoId, todoStatus); - var todos = await _todoRepository.GetTodosAsync(); + var todos = await _todoRepository.GetNotDoneTodos(); var serializedTodos = JsonSerializer.Serialize(todos .Select(t => new TodoResponse { Id = t.Id, Title = t.Title, Status = t.Status }) .ToList()); - await Clients.Caller.SendAsync("todos", serializedTodos); + await Clients.Caller.SendAsync("getInboxTodos", serializedTodos); } public async Task GetTodos() { var todos = await _todoRepository.GetTodosAsync(); var serializedTodos = JsonSerializer.Serialize(todos - .Select(t => new TodoResponse { Id = t.Id, Title = t.Title, Status = t.Status}) + .Select(t => new TodoResponse { Id = t.Id, Title = t.Title, Status = t.Status }) .ToList()); await Clients.Caller.SendAsync("todos", serializedTodos); } + + + public async Task GetInboxTodos() + { + var todos = await _todoRepository.GetNotDoneTodos(); + var serializedTodos = JsonSerializer.Serialize(todos + .Select(t => new TodoResponse { Id = t.Id, Title = t.Title, Status = t.Status }) + .ToList()); + + await Clients.Caller.SendAsync("getInboxTodos", serializedTodos); + } } } \ No newline at end of file diff --git a/src/backend/server/Todo/Todo.Core/Interfaces/Persistence/ITodoRepository.cs b/src/backend/server/Todo/Todo.Core/Interfaces/Persistence/ITodoRepository.cs index 3f5fcb9..3a722dc 100644 --- a/src/backend/server/Todo/Todo.Core/Interfaces/Persistence/ITodoRepository.cs +++ b/src/backend/server/Todo/Todo.Core/Interfaces/Persistence/ITodoRepository.cs @@ -5,4 +5,5 @@ public interface ITodoRepository Task CreateTodoAsync(string title); Task> GetTodosAsync(); Task UpdateTodoStatus(string todoId, bool todoStatus); + Task> GetNotDoneTodos(); } \ No newline at end of file diff --git a/src/backend/server/Todo/Todo.Persistence/Mongo/Repositories/TodoRepository.cs b/src/backend/server/Todo/Todo.Persistence/Mongo/Repositories/TodoRepository.cs index fed9133..daaba12 100644 --- a/src/backend/server/Todo/Todo.Persistence/Mongo/Repositories/TodoRepository.cs +++ b/src/backend/server/Todo/Todo.Persistence/Mongo/Repositories/TodoRepository.cs @@ -20,7 +20,7 @@ public class TodoRepository : ITodoRepository { var todo = new MongoTodo() { Title = title }; await _todosCollection.InsertOneAsync(todo); - return new Core.Entities.Todo() { Id = todo.Id, Title = todo.Title }; + return new Core.Entities.Todo() { Id = todo.Id, Title = todo.Title, Status = false}; } public async Task> GetTodosAsync() @@ -38,4 +38,10 @@ public class TodoRepository : ITodoRepository .UpdateOneAsync(t => t.Id == todoId, Builders.Update.Set(t => t.Status, todoStatus)); } + + public async Task> GetNotDoneTodos() + { + var todos = await GetTodosAsync(); + return todos.Where(t => t.Status == false); + } } \ No newline at end of file diff --git a/src/client/.idea/runConfigurations/dev.xml b/src/client/.idea/runConfigurations/dev.xml new file mode 100644 index 0000000..c19e5b3 --- /dev/null +++ b/src/client/.idea/runConfigurations/dev.xml @@ -0,0 +1,13 @@ + + + + + +