From 0c91d2a1394851c1c58dfa3b8cc4ba8d01af7227 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Tracewicz?= Date: Wed, 11 Sep 2024 21:56:21 +0200 Subject: [PATCH] Closes #60 Add maping to a repsonse --- backend/src/api/Controllers/TodoController.cs | 7 ++++--- backend/src/api/Data/DTO/TodoListDTO.cs | 3 --- backend/src/api/Responses/TodoList.cs | 8 ++++++++ 3 files changed, 12 insertions(+), 6 deletions(-) delete mode 100644 backend/src/api/Data/DTO/TodoListDTO.cs create mode 100644 backend/src/api/Responses/TodoList.cs diff --git a/backend/src/api/Controllers/TodoController.cs b/backend/src/api/Controllers/TodoController.cs index bc83dc8..dbc0ddb 100644 --- a/backend/src/api/Controllers/TodoController.cs +++ b/backend/src/api/Controllers/TodoController.cs @@ -1,6 +1,7 @@ using core.Ports; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; +using api.Resonses; namespace api.Controllers; @@ -26,7 +27,7 @@ public IActionResult GetLists() return userId switch { null => Unauthorized(), - var user => Ok(_service.GetLists(user)), + var user => Ok(_service.GetLists(user).Select(l => l.ToResponse())), }; } @@ -36,7 +37,7 @@ public IActionResult GetList([FromRoute] int id) var userId = Request.UserId(); _logger.LogDebug("User: {user} requested his list: {id}", userId, id); if (userId is null) { return Unauthorized(); } - var list = _service.GetList(userId, id); + var list = _service.GetList(userId, id)?.ToResponse(); return list switch { null => NotFound(), @@ -73,7 +74,7 @@ async Task Create(string user, string name) { //TODO: return DTO instead of DAO var list = await _service.CreateList(user, name); - return Created(HttpContext.Request.Path.Add(new PathString($"/{list.Id}")), list); + return Created(HttpContext.Request.Path.Add(new PathString($"/{list.Id}")), list.ToResponse()); } } diff --git a/backend/src/api/Data/DTO/TodoListDTO.cs b/backend/src/api/Data/DTO/TodoListDTO.cs deleted file mode 100644 index 275e8d4..0000000 --- a/backend/src/api/Data/DTO/TodoListDTO.cs +++ /dev/null @@ -1,3 +0,0 @@ -namespace api.Data.DTO; - -public record TodoListDTO(int Id, string Name); diff --git a/backend/src/api/Responses/TodoList.cs b/backend/src/api/Responses/TodoList.cs new file mode 100644 index 0000000..9207e35 --- /dev/null +++ b/backend/src/api/Responses/TodoList.cs @@ -0,0 +1,8 @@ +namespace api.Resonses; + +public record TodoList(int Id, string Name); + +public static class MapExtensions +{ + public static TodoList ToResponse(this core.TodoList list) => new TodoList(list.Id, list.Name); +}