From 7a5f829af23c0899e3f89846cd3a16dfe62d2814 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Tracewicz?= Date: Tue, 10 Sep 2024 12:45:36 +0200 Subject: [PATCH] Closes #60 Add Create List --- backend/src/api/Controllers/TodoController.cs | 19 +++++++++++++++++-- backend/src/api/Services/Todo/ITodoService.cs | 16 +++++++++------- backend/src/api/Services/Todo/TodoService.cs | 10 +++++++++- 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/backend/src/api/Controllers/TodoController.cs b/backend/src/api/Controllers/TodoController.cs index 35f9bad..e650003 100644 --- a/backend/src/api/Controllers/TodoController.cs +++ b/backend/src/api/Controllers/TodoController.cs @@ -18,10 +18,10 @@ public TodoController(ITodoService service, ILogger logger){ } [HttpGet("lists")] - public IActionResult List() + public IActionResult GetLists() { var userId = Request.UserId(); - _logger.LogInformation("User: {user} requested his lists", userId); + _logger.LogDebug("User: {user} requested his lists", userId); return userId switch { null => Unauthorized(), @@ -29,4 +29,19 @@ public IActionResult List() }; } + [HttpPost("lists")] + public async Task CreateLists([FromQuery] string name) + { + var userId = Request.UserId(); + _logger.LogDebug("User: {user} created: {list}", userId, name); + return userId switch + { + null => Unauthorized(), + var user => await Create(user, name), + }; + async Task Create(string user, string name){ + var list = await _service.CreateList(user, name); + return Created(HttpContext.Request.Path.Add(new PathString($"/{list.Id}")), list); + } + } } \ No newline at end of file diff --git a/backend/src/api/Services/Todo/ITodoService.cs b/backend/src/api/Services/Todo/ITodoService.cs index ff5f22a..91bd29e 100644 --- a/backend/src/api/Services/Todo/ITodoService.cs +++ b/backend/src/api/Services/Todo/ITodoService.cs @@ -1,8 +1,10 @@ -using api.Data.DTO; - -namespace api.Services; - -public interface ITodoService -{ - public IEnumerable GetLists(string userId); +using api.Data.DAO; +using api.Data.DTO; + +namespace api.Services; + +public interface ITodoService +{ + Task CreateList(string user, string name); + public IEnumerable GetLists(string userId); } \ No newline at end of file diff --git a/backend/src/api/Services/Todo/TodoService.cs b/backend/src/api/Services/Todo/TodoService.cs index f616843..58fb12c 100644 --- a/backend/src/api/Services/Todo/TodoService.cs +++ b/backend/src/api/Services/Todo/TodoService.cs @@ -1,4 +1,5 @@ using api.Data; +using api.Data.DAO; using api.Data.DTO; using Microsoft.EntityFrameworkCore; @@ -21,4 +22,11 @@ public IEnumerable GetLists(string userId) .Select(list => new TodoListDTO(list.Id, list.Name)) .AsEnumerable(); } -} + + public async Task CreateList(string user, string name){ + var newList = new TodoListModel(){Name = name, Owner = Guid.Parse(user)}; + _context.TodoLists.Add(newList); + await _context.SaveChangesAsync(); + return newList; + } +} \ No newline at end of file