Skip to content

Commit

Permalink
Closes #60
Browse files Browse the repository at this point in the history
Add Create List
  • Loading branch information
mtracewicz committed Sep 10, 2024
1 parent 8d13eca commit 7a5f829
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
19 changes: 17 additions & 2 deletions backend/src/api/Controllers/TodoController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,30 @@ public TodoController(ITodoService service, ILogger<TodoController> 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(),
var user => Ok(_service.GetLists(user)),
};
}

[HttpPost("lists")]
public async Task<IActionResult> 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<IActionResult> Create(string user, string name){
var list = await _service.CreateList(user, name);
return Created(HttpContext.Request.Path.Add(new PathString($"/{list.Id}")), list);
}
}
}
16 changes: 9 additions & 7 deletions backend/src/api/Services/Todo/ITodoService.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using api.Data.DTO;

namespace api.Services;

public interface ITodoService
{
public IEnumerable<TodoListDTO> GetLists(string userId);
using api.Data.DAO;
using api.Data.DTO;

namespace api.Services;

public interface ITodoService
{
Task<TodoListModel> CreateList(string user, string name);
public IEnumerable<TodoListDTO> GetLists(string userId);
}
10 changes: 9 additions & 1 deletion backend/src/api/Services/Todo/TodoService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using api.Data;
using api.Data.DAO;
using api.Data.DTO;
using Microsoft.EntityFrameworkCore;

Expand All @@ -21,4 +22,11 @@ public IEnumerable<TodoListDTO> GetLists(string userId)
.Select(list => new TodoListDTO(list.Id, list.Name))
.AsEnumerable();
}
}

public async Task<TodoListModel> CreateList(string user, string name){
var newList = new TodoListModel(){Name = name, Owner = Guid.Parse(user)};
_context.TodoLists.Add(newList);
await _context.SaveChangesAsync();
return newList;
}
}

0 comments on commit 7a5f829

Please sign in to comment.