Can now update title
Some checks failed
continuous-integration/drone/pr Build is passing
continuous-integration/drone/push Build is failing

This commit is contained in:
2021-11-15 22:31:52 +01:00
parent 6ed71eb8f8
commit 1865425d38
10 changed files with 251 additions and 8 deletions

View File

@@ -0,0 +1,18 @@
using System.Text.Json.Serialization;
namespace Todo.Api.Hubs.Models;
public record UpdateTodoRequest
{
[JsonPropertyName("id")]
public string Id { get; init; }
[JsonPropertyName("title")]
public string Title { get; init; }
[JsonPropertyName("status")]
public bool Status { get; init; }
[JsonPropertyName("project")]
public string Project { get; set; }
}

View File

@@ -51,7 +51,6 @@ namespace Todo.Api.Hubs
await Clients.Caller.SendAsync("todos", serializedTodos);
}
public async Task GetInboxTodos()
{
var todos = await _todoRepository.GetNotDoneTodos();
@@ -61,5 +60,44 @@ namespace Todo.Api.Hubs
await Clients.Caller.SendAsync("getInboxTodos", serializedTodos);
}
public async Task GetTodo(string todoId)
{
var todo = await _todoRepository.GetTodoByIdAsync(todoId);
var serializedTodo = JsonSerializer.Serialize(new TodoResponse()
{
Id = todo.Id,
Project = todo.Project,
Status = todo.Status,
Title = todo.Title,
});
await Clients.Caller.SendAsync("getTodo", serializedTodo);
}
public async Task ReplaceTodo(string updateTodoRequest)
{
var updateTodo = JsonSerializer.Deserialize<UpdateTodoRequest>(updateTodoRequest);
if (updateTodo is null)
throw new InvalidOperationException("Could not parse invalid updateTodo");
var updatedTodo = await _todoRepository.UpdateTodoAsync(new Core.Entities.Todo()
{
Id = updateTodo.Id,
Project = updateTodo.Project,
Status = updateTodo.Status,
Title = updateTodo.Title
});
var serializedTodo = JsonSerializer.Serialize(new TodoResponse()
{
Id = updatedTodo.Id,
Project = updatedTodo.Project,
Status = updatedTodo.Status,
Title = updatedTodo.Title,
});
await Clients.Caller.SendAsync("getTodo", serializedTodo);
}
}
}

View File

@@ -1,3 +1,4 @@
namespace Todo.Core.Interfaces.Persistence;
public interface ITodoRepository
@@ -6,4 +7,6 @@ public interface ITodoRepository
Task<IEnumerable<Entities.Todo>> GetTodosAsync();
Task UpdateTodoStatus(string todoId, bool todoStatus);
Task<IEnumerable<Entities.Todo>> GetNotDoneTodos();
Task<Entities.Todo> GetTodoByIdAsync(string todoId);
Task<Entities.Todo> UpdateTodoAsync(Entities.Todo todo);
}

View File

@@ -44,4 +44,37 @@ public class TodoRepository : ITodoRepository
var todos = await GetTodosAsync();
return todos.Where(t => t.Status == false);
}
public async Task<Core.Entities.Todo> GetTodoByIdAsync(string todoId)
{
var todoCursor = await _todosCollection.FindAsync(f => f.Id == todoId);
var todo = await todoCursor.FirstOrDefaultAsync();
return new Core.Entities.Todo()
{
Id = todo.Id,
Project = todo.ProjectName,
Status = todo.Status,
Title = todo.Title
};
}
public async Task<Core.Entities.Todo> UpdateTodoAsync(Core.Entities.Todo todo)
{
var updatedTodo = await _todosCollection.FindOneAndReplaceAsync<MongoTodo>(f => f.Id == todo.Id, new MongoTodo()
{
Id = todo.Id,
Status = todo.Status,
Title = todo.Title,
ProjectName = todo.Project
});
return new Core.Entities.Todo()
{
Id = updatedTodo.Id,
Project = updatedTodo.ProjectName,
Status = updatedTodo.Status,
Title = updatedTodo.Title
};
}
}