From 87de18eebcbda34431fdd3b53533fe98a9ea9b95 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Tracewicz?= Date: Tue, 10 Sep 2024 13:14:43 +0200 Subject: [PATCH] Closes #60 Implement get for a single list --- backend/bruno/KSummarized/ToDo/List/Get.bru | 15 ++++++++++++ backend/src/api/Controllers/TodoController.cs | 24 +++++++++++++++---- backend/src/api/Services/Todo/ITodoService.cs | 3 ++- backend/src/api/Services/Todo/TodoService.cs | 17 +++++++++---- 4 files changed, 50 insertions(+), 9 deletions(-) create mode 100644 backend/bruno/KSummarized/ToDo/List/Get.bru diff --git a/backend/bruno/KSummarized/ToDo/List/Get.bru b/backend/bruno/KSummarized/ToDo/List/Get.bru new file mode 100644 index 0000000..6fd7964 --- /dev/null +++ b/backend/bruno/KSummarized/ToDo/List/Get.bru @@ -0,0 +1,15 @@ +meta { + name: Get + type: http + seq: 5 +} + +get { + url: https://localhost:5000/api/todo/lists/9 + body: none + auth: bearer +} + +auth:bearer { + token: {{token}} +} diff --git a/backend/src/api/Controllers/TodoController.cs b/backend/src/api/Controllers/TodoController.cs index e650003..0b8428f 100644 --- a/backend/src/api/Controllers/TodoController.cs +++ b/backend/src/api/Controllers/TodoController.cs @@ -1,4 +1,4 @@ -using api.Services; +using api.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; @@ -12,7 +12,8 @@ public class TodoController : ControllerBase private readonly ITodoService _service; private readonly ILogger _logger; - public TodoController(ITodoService service, ILogger logger){ + public TodoController(ITodoService service, ILogger logger) + { _service = service; _logger = logger; } @@ -29,6 +30,20 @@ public IActionResult GetLists() }; } + [HttpGet("lists/{id}")] + public IActionResult GetList([FromRoute] int id) + { + var userId = Request.UserId(); + _logger.LogDebug("User: {user} requested his lists", userId); + if (userId is null) { return Unauthorized(); } + var list = _service.GetList(userId, id); + return list switch + { + null => NotFound(), + var user => Ok(list), + }; + } + [HttpPost("lists")] public async Task CreateLists([FromQuery] string name) { @@ -39,9 +54,10 @@ public async Task CreateLists([FromQuery] string name) null => Unauthorized(), var user => await Create(user, name), }; - async Task Create(string user, string 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 91bd29e..b6b7889 100644 --- a/backend/src/api/Services/Todo/ITodoService.cs +++ b/backend/src/api/Services/Todo/ITodoService.cs @@ -7,4 +7,5 @@ public interface ITodoService { Task CreateList(string user, string name); public IEnumerable GetLists(string userId); -} \ No newline at end of file + public TodoListDTO? GetList(string userId, int id); +} diff --git a/backend/src/api/Services/Todo/TodoService.cs b/backend/src/api/Services/Todo/TodoService.cs index 58fb12c..0c37fff 100644 --- a/backend/src/api/Services/Todo/TodoService.cs +++ b/backend/src/api/Services/Todo/TodoService.cs @@ -1,4 +1,4 @@ -using api.Data; +using api.Data; using api.Data.DAO; using api.Data.DTO; using Microsoft.EntityFrameworkCore; @@ -23,10 +23,19 @@ public IEnumerable GetLists(string userId) .AsEnumerable(); } - public async Task CreateList(string user, string name){ - var newList = new TodoListModel(){Name = name, Owner = Guid.Parse(user)}; + public TodoListDTO? GetList(string userId, int id) + { + var list = _context.TodoLists.AsNoTracking() + .SingleOrDefault(l => l.Owner.Equals(Guid.Parse(userId)) && l.Id == id); + if (list is not null) { return new(list.Id, list.Name); } + return null; + } + + 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 +}