Skip to content

Commit

Permalink
Closes #60
Browse files Browse the repository at this point in the history
Add renamming of lists
  • Loading branch information
mtracewicz committed Sep 10, 2024
1 parent 52034da commit 75c5880
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 7 deletions.
8 changes: 5 additions & 3 deletions backend/bruno/KSummarized/ToDo/List/Edit.bru
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -15,5 +15,7 @@ auth:bearer {
}

body:json {
"ToDo2"
{
"name":"Vax"
}
}
34 changes: 31 additions & 3 deletions backend/src/api/Controllers/TodoController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ public IActionResult DeleteList([FromRoute] int id)
}

[HttpPost("lists")]
public async Task<IActionResult> CreateLists([FromQuery] string name)
public async Task<IActionResult> 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<IActionResult> Create(string user, string name)
Expand All @@ -76,4 +76,32 @@ async Task<IActionResult> Create(string user, string name)
return Created(HttpContext.Request.Path.Add(new PathString($"/{list.Id}")), list);
}
}

[HttpPut("lists/{id}")]
public async Task<IActionResult> 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<IActionResult> 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; }
}
1 change: 1 addition & 0 deletions backend/src/api/Services/Todo/ITodoService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ public interface ITodoService
IEnumerable<TodoListDTO> GetLists(string userId);
TodoListDTO? GetList(string userId, int id);
bool DeleteList(string userId, int id);
Task<bool> RenameList(string user, int id, string name);
}
13 changes: 12 additions & 1 deletion backend/src/api/Services/Todo/TodoService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,22 @@ public async Task<TodoListModel> 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<bool> 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;
}
}

0 comments on commit 75c5880

Please sign in to comment.