From 75c58800c87d065a6cda62ddab76254532694910 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Tracewicz?= Date: Tue, 10 Sep 2024 20:08:09 +0200 Subject: [PATCH] Closes #60 Add renamming of lists --- backend/bruno/KSummarized/ToDo/List/Edit.bru | 8 +++-- backend/src/api/Controllers/TodoController.cs | 34 +++++++++++++++++-- backend/src/api/Services/Todo/ITodoService.cs | 1 + backend/src/api/Services/Todo/TodoService.cs | 13 ++++++- 4 files changed, 49 insertions(+), 7 deletions(-) diff --git a/backend/bruno/KSummarized/ToDo/List/Edit.bru b/backend/bruno/KSummarized/ToDo/List/Edit.bru index 635062f..c1e0e45 100644 --- a/backend/bruno/KSummarized/ToDo/List/Edit.bru +++ b/backend/bruno/KSummarized/ToDo/List/Edit.bru @@ -4,8 +4,8 @@ meta { seq: 3 } -post { - url: https://localhost:5000/api/todo/lists/{{listId}} +put { + url: https://localhost:5000/api/todo/lists/5 body: json auth: bearer } @@ -15,5 +15,7 @@ auth:bearer { } body:json { - "ToDo2" + { + "name":"Vax" + } } diff --git a/backend/src/api/Controllers/TodoController.cs b/backend/src/api/Controllers/TodoController.cs index 4a69380..5436e8c 100644 --- a/backend/src/api/Controllers/TodoController.cs +++ b/backend/src/api/Controllers/TodoController.cs @@ -59,14 +59,14 @@ public IActionResult DeleteList([FromRoute] int id) } [HttpPost("lists")] - public async Task CreateLists([FromQuery] string name) + public async Task CreateLists([FromQuery] Request request) { var userId = Request.UserId(); - _logger.LogDebug("User: {user} created: {list}", userId, name); + _logger.LogDebug("User: {user} created: {list}", userId, request.Name); return userId switch { null => Unauthorized(), - var user => await Create(user, name), + var user => await Create(user, request.Name), }; async Task Create(string user, string name) @@ -76,4 +76,32 @@ async Task Create(string user, string name) return Created(HttpContext.Request.Path.Add(new PathString($"/{list.Id}")), list); } } + + [HttpPut("lists/{id}")] + public async Task RenameList([FromRoute] int id, [FromBody] Request request) + { + var userId = Request.UserId(); + _logger.LogDebug("User: {user} created: {list}", userId, request.Name); + return userId switch + { + null => Unauthorized(), + var user => await Rename(user, id, request.Name), + }; + + async Task Rename(string user, int id, string name) + { + //TODO: return DTO instead of DAO + var list = await _service.RenameList(user, id, name); + if (list) + { + return Ok(); + } + return BadRequest(); + } + } +} + +public class Request +{ + public required string Name { get; set; } } diff --git a/backend/src/api/Services/Todo/ITodoService.cs b/backend/src/api/Services/Todo/ITodoService.cs index ab35faa..fe5788e 100644 --- a/backend/src/api/Services/Todo/ITodoService.cs +++ b/backend/src/api/Services/Todo/ITodoService.cs @@ -9,4 +9,5 @@ public interface ITodoService IEnumerable GetLists(string userId); TodoListDTO? GetList(string userId, int id); bool DeleteList(string userId, int id); + Task RenameList(string user, int id, string name); } diff --git a/backend/src/api/Services/Todo/TodoService.cs b/backend/src/api/Services/Todo/TodoService.cs index 88ffea6..8b24c70 100644 --- a/backend/src/api/Services/Todo/TodoService.cs +++ b/backend/src/api/Services/Todo/TodoService.cs @@ -42,11 +42,22 @@ public async Task CreateList(string user, string name) public bool DeleteList(string userId, int id) { - var list = _context.TodoLists.AsNoTracking() + var list = _context.TodoLists .SingleOrDefault(l => l.Owner.Equals(Guid.Parse(userId)) && l.Id == id); if (list is null) { return false; } _context.TodoLists.Remove(list); _context.SaveChanges(); return true; } + + public async Task RenameList(string userId, int id, string name) + { + var list = _context.TodoLists + .SingleOrDefault(l => l.Owner.Equals(Guid.Parse(userId)) && l.Id == id); + + if (list is null) { return false; } + list.Name = name; + await _context.SaveChangesAsync(); + return true; + } }